314e7198bf3ddff302c27cc8a8076811e7111777
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / main.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "dotnet/dotnet_launcher.h"
18 #include "mono/mono_launcher.h"
19 #include "utils.h"
20 #include "log.h"
21
22 #include <cstdio>
23 #include <vector>
24 #include <memory>
25
26 #include <Ecore.h>
27 #include <Eina.h>
28 #include <aul.h>
29
30 #define __XSTR(x) #x
31 #define __STR(x) __XSTR(x)
32
33 #ifndef VERSION
34 #define LAUNCHER_VERSION_STR "-Unknown-"
35 #else
36 #define LAUNCHER_VERSION_STR __STR(VERSION)
37 #endif
38
39 static std::string VersionOption("--version");
40 static std::string StandaloneOption("--standalone");
41 static std::string NativeOption("--native");
42
43 int main(int argc, char *argv[])
44 {
45   int i;
46   bool standalone = false;
47   const char* standalonePath = nullptr;
48   bool nativeOnly = false;
49
50   std::vector<char*> vargs;
51
52   for (i=0; i<argc; i++)
53   {
54     if (VersionOption.compare(argv[i]) == 0)
55     {
56       printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
57       return 0;
58     }
59     else if (StandaloneOption.compare(argv[i]) == 0)
60     {
61       standalone = true;
62
63       if (i > argc-1)
64       {
65         fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
66         return 1;
67       }
68       i++;
69       standalonePath = argv[i];
70     }
71     else if (NativeOption.compare(argv[i]) == 0)
72     {
73       nativeOnly = true;
74     }
75     else
76     {
77       vargs.push_back(argv[i]);
78     }
79   }
80
81   if (!standalone && nativeOnly)
82   {
83     fprintf(stderr, "\"--native\" option must be use with \"--standalone\"\n");
84     return 1;
85   }
86
87   using tizen::runtime::LauncherInterface;
88   using tizen::runtime::Launchpad;
89   using tizen::runtime::AppInfo;
90   std::unique_ptr<LauncherInterface> runtime;
91
92   bool useMono = !FileNotExist("/etc/.use_mono");
93
94   if (!useMono)
95   {
96     using tizen::runtime::dotnetcore::CoreRuntime;
97     std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
98     runtime = std::move(coreRuntime);
99
100     _DBG("##### CoreCLR Launcher ######");
101   }
102   else
103   {
104     using tizen::runtime::mono::MonoRuntime;
105     std::unique_ptr<LauncherInterface> monoRuntime(new MonoRuntime());
106     runtime = std::move(monoRuntime);
107
108     _DBG("##### Mono Launcher ######");
109   }
110
111   if (standalone)
112   {
113     _DBG("##### Run it standalone #########");
114     const char* appid = getenv("AUL_APPID");
115     _DBG("AUL_APPID : %s", appid);
116     std::string approot;
117     if (appid != nullptr)
118     {
119       const char* approot_path = aul_get_app_root_path();
120       if (approot_path != nullptr)
121       {
122         approot = std::string(approot_path);
123       }
124     }
125     if (approot.empty())
126     {
127       approot = Basename(standalonePath);
128     }
129     if (runtime->Initialize(true) != 0)
130     {
131       _ERR("Failed to initialize");
132       return 1;
133     }
134
135     if (!nativeOnly && runtime->RunManagedLauncher() != 0)
136     {
137       _ERR("Failed to run managed launcher");
138       return 1;
139     }
140
141     int args_len = vargs.size();
142     char** args = &vargs[0];
143     if (runtime->Launch(approot.c_str(), standalonePath, args_len, args))
144     {
145         _ERR("Failed to launch");
146         return 0;
147     }
148   }
149   else
150   {
151     Launchpad.OnCreate = [&runtime]()
152     {
153       if (runtime->Initialize(false) != 0)
154       {
155         _ERR("Failed to initialized");
156       }
157       else
158       {
159         auto idle_task = [](void *data) -> Eina_Bool
160         {
161           LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
162           if (runtime->RunManagedLauncher() != 0)
163           {
164             _ERR("Failed to run managed launcher");
165           }
166           return ECORE_CALLBACK_CANCEL;
167         };
168         ecore_idler_add(idle_task, runtime.get());
169       }
170     };
171
172     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
173     {
174       _DBG("terminated with app path : %s", info.path.c_str());
175       _DBG("appid : %s", info.id.c_str());
176       _DBG("pkg : %s", info.pkg.c_str());
177       _DBG("type : %s", info.type.c_str());
178
179       if (runtime->Launch(info.root.c_str(), info.path.c_str(), argc, argv))
180       {
181         _ERR("Failed to launch");
182       }
183     };
184     Launchpad.LoaderMain(argc, argv);
185   }
186
187   return 0;
188 }