Pass the first argument that name of exe file
[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   // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
53   for (i=1; i<argc; i++)
54   {
55     if (VersionOption.compare(argv[i]) == 0)
56     {
57       printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
58       return 0;
59     }
60     else if (StandaloneOption.compare(argv[i]) == 0)
61     {
62       standalone = true;
63
64       if (i > argc-1)
65       {
66         fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
67         return 1;
68       }
69       i++;
70       standalonePath = argv[i];
71     }
72     else if (NativeOption.compare(argv[i]) == 0)
73     {
74       nativeOnly = true;
75     }
76     else
77     {
78       vargs.push_back(argv[i]);
79     }
80   }
81
82   if (!standalone && nativeOnly)
83   {
84     fprintf(stderr, "\"--native\" option must be use with \"--standalone\"\n");
85     return 1;
86   }
87
88   using tizen::runtime::LauncherInterface;
89   using tizen::runtime::Launchpad;
90   using tizen::runtime::AppInfo;
91   std::unique_ptr<LauncherInterface> runtime;
92
93   bool useMono = !FileNotExist("/etc/.use_mono");
94
95   if (!useMono)
96   {
97     using tizen::runtime::dotnetcore::CoreRuntime;
98     std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
99     runtime = std::move(coreRuntime);
100
101     _DBG("##### CoreCLR Launcher ######");
102   }
103   else
104   {
105     using tizen::runtime::mono::MonoRuntime;
106     std::unique_ptr<LauncherInterface> monoRuntime(new MonoRuntime());
107     runtime = std::move(monoRuntime);
108
109     _DBG("##### Mono Launcher ######");
110   }
111
112   if (standalone)
113   {
114     _DBG("##### Run it standalone #########");
115     const char* appid = getenv("AUL_APPID");
116     _DBG("AUL_APPID : %s", appid);
117     std::string approot;
118     if (appid != nullptr)
119     {
120       const char* approot_path = aul_get_app_root_path();
121       if (approot_path != nullptr)
122       {
123         approot = std::string(approot_path);
124       }
125     }
126     if (approot.empty())
127     {
128       approot = Basename(standalonePath);
129     }
130     if (runtime->Initialize(true) != 0)
131     {
132       _ERR("Failed to initialize");
133       return 1;
134     }
135
136     if (!nativeOnly && runtime->RunManagedLauncher() != 0)
137     {
138       _ERR("Failed to run managed launcher");
139       return 1;
140     }
141
142     int args_len = vargs.size();
143     char** args = &vargs[0];
144     if (runtime->Launch(approot.c_str(), standalonePath, args_len, args))
145     {
146         _ERR("Failed to launch");
147         return 0;
148     }
149   }
150   else
151   {
152     Launchpad.OnCreate = [&runtime]()
153     {
154       if (runtime->Initialize(false) != 0)
155       {
156         _ERR("Failed to initialized");
157       }
158       else
159       {
160         auto idle_task = [](void *data) -> Eina_Bool
161         {
162           LauncherInterface* runtime = static_cast<LauncherInterface*>(data);
163           if (runtime->RunManagedLauncher() != 0)
164           {
165             _ERR("Failed to run managed launcher");
166           }
167           return ECORE_CALLBACK_CANCEL;
168         };
169         ecore_idler_add(idle_task, runtime.get());
170       }
171     };
172
173     Launchpad.OnTerminate = [&runtime](const AppInfo& info, int argc, char** argv)
174     {
175       _DBG("terminated with app path : %s", info.path.c_str());
176       _DBG("appid : %s", info.id.c_str());
177       _DBG("pkg : %s", info.pkg.c_str());
178       _DBG("type : %s", info.type.c_str());
179
180       // The launchpad pass the name of exe file to the first argument.
181       // For the C# spec, we have to skip this first argument.
182
183       if (runtime->Launch(info.root.c_str(), info.path.c_str(), argc-1, argv+1))
184       {
185         _ERR("Failed to launch");
186       }
187     };
188     Launchpad.LoaderMain(argc, argv);
189   }
190
191   return 0;
192 }