Add "--version" option for dotnet-launcher
[platform/core/dotnet/launcher.git] / NativeLauncher / src / main.cc
1 #include "dotnet/dotnet_launcher.h"
2 #include "mono/mono_launcher.h"
3 #include "utils.h"
4 #include "log.h"
5
6 #include <cstdio>
7 #include <vector>
8 #include <memory>
9
10 #include <Ecore.h>
11 #include <Eina.h>
12
13 #define __XSTR(x) #x
14 #define __STR(x) __XSTR(x)
15
16 #ifndef VERSION
17 #define LAUNCHER_VERSION_STR "-Unknown-"
18 #else
19 #define LAUNCHER_VERSION_STR __STR(VERSION)
20 #endif
21
22 static std::string VersionOption("--version");
23 static std::string StandaloneOption("--standalone");
24
25 int main(int argc, char *argv[])
26 {
27   int i;
28   bool standalone = false;
29   const char* standalonePath = nullptr;
30
31   std::vector<char*> vargs;
32
33   for (i=1; i<argc; i++)
34   {
35     if (VersionOption.compare(argv[i]) == 0)
36     {
37       printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
38       return 0;
39     }
40     else if (StandaloneOption.compare(argv[i]) == 0)
41     {
42       standalone = true;
43
44       if (i > argc-1)
45       {
46         fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
47         return 1;
48       }
49       i++;
50       standalonePath = argv[i];
51     }
52     else
53     {
54       vargs.push_back(argv[i]);
55     }
56   }
57
58   using tizen::runtime::LauncherInterface;
59   using tizen::runtime::Launchpad;
60   using tizen::runtime::AppInfo;
61   std::unique_ptr<LauncherInterface> runtime;
62
63   bool useMono = !FileNotExist("/etc/.use_mono");
64
65   if (!useMono)
66   {
67     using tizen::runtime::dotnetcore::CoreRuntime;
68     std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
69     runtime = std::move(coreRuntime);
70   }
71   else
72   {
73     using tizen::runtime::mono::MonoRuntime;
74     std::unique_ptr<LauncherInterface> monoRuntime(new MonoRuntime());
75     runtime = std::move(monoRuntime);
76   }
77
78   if (standalone)
79   {
80     std::string base = Basename(standalonePath);
81     if (runtime->Initialize(true) != 0)
82     {
83       _ERR("Failed to initialize");
84       return 1;
85     }
86     if (runtime->RunManagedLauncher() != 0)
87     {
88       _ERR("Failed to run managed launcher");
89       return 1;
90     }
91
92     int args_len = vargs.size();
93     char** args = &vargs[0];
94     if (!runtime->Launch(base.c_str(), standalonePath, args_len, args))
95     {
96         _ERR("Failed to launch");
97         return 1;
98     }
99   }
100   else
101   {
102     Launchpad.OnCreate = [&runtime]
103     {
104       auto idle_task = [](void *data) -> Eina_Bool 
105       {
106         LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
107         if (runtime->RunManagedLauncher() != 0)
108         {
109           _ERR("Failed to run managed launcher");
110         }
111         return ECORE_CALLBACK_CANCEL;
112       };
113       if (runtime->Initialize(false) != 0)
114       {
115         _ERR("Failed to initialized");
116         return 1;
117       }
118       ecore_idler_add(idle_task, runtime.get());
119     };
120
121     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
122     {
123       _DBG("terminated with app path : %s", info.path.c_str());
124       _DBG("appid : %s", info.id.c_str());
125       _DBG("pkg : %s", info.pkg.c_str());
126       _DBG("type : %s", info.type.c_str());
127
128       if (!runtime->Launch(info.root.c_str(), info.path.c_str(), argc, argv))
129       {
130         _ERR("Failed to launch");
131       }
132     };
133     Launchpad.LoaderMain(argc, argv);
134   }
135
136   return 0;
137 }