Set the native library path with the RIDs value set in TizenFX
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / main.cc
index 6c1ac12..f09705b 100644 (file)
 #include <sys/types.h>
 #include <unistd.h>
 
-#define __XSTR(x) #x
-#define __STR(x) __XSTR(x)
+// By the specification, application id must be shorter than 50 characters.
+// Current length of argv[0] is 25 with a space. ("/usr/bin/dotnet-launcher ")
+// To be able to change argv[0] when standalone mode padding for executable path is added.
+#define APPID_MAX_LENGTH       (25 + 105)
 
-#ifndef VERSION
-#define LAUNCHER_VERSION_STR "-Unknown-"
-#else
-#define LAUNCHER_VERSION_STR __STR(VERSION)
-#endif
-
-#define CMD_LINE_SIZE  24      // sizeof("/usr/bin/dotnet-launcher")
-
-static std::string VersionOption("--version");
 static std::string StandaloneOption("--standalone");
+static std::string PaddingOption("--PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE");
 
-int main(int argc, char *argv[])
+extern "C" int realMain(int argc, char *argv[], const char* mode)
 {
        int i;
-       bool standalone = false;
+       bool standaloneMode = false;
        char* standalonePath = nullptr;
+       bool corerunMode = false;
+       bool paddingExist = false;
 
        std::vector<char*> vargs;
 
        // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
        for (i = 1; i < argc; i++) {
-               if (VersionOption.compare(argv[i]) == 0) {
-                       printf("Dotnet launcher Version %s\n", LAUNCHER_VERSION_STR);
-                       return 0;
-               } else if (StandaloneOption.compare(argv[i]) == 0) {
-                       standalone = true;
+               if (StandaloneOption.compare(argv[i]) == 0) {
+                       standaloneMode = true;
 
-                       if (i > argc-1) {
+                       if (i > argc - 1) {
                                fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
                                return 1;
                        }
                        i++;
                        standalonePath = argv[i];
+               } else if (PaddingOption.compare(argv[i]) == 0) {
+                       paddingExist = true;
                } else {
                        vargs.push_back(argv[i]);
                }
        }
 
-       using tizen::runtime::LauncherInterface;
+       if (isManagedAssembly(argv[1]) || isNativeImage(argv[1])) {
+               corerunMode = true;
+       }
+
        using tizen::runtime::Launchpad;
        using tizen::runtime::AppInfo;
-       std::unique_ptr<LauncherInterface> runtime;
-
        using tizen::runtime::dotnetcore::CoreRuntime;
-       std::unique_ptr<LauncherInterface> coreRuntime(new CoreRuntime());
-       runtime = std::move(coreRuntime);
 
-       if (standalone) {
-               _DBG("##### Run it standalone #########");
-               char appId[1024] = {0,};
+       std::unique_ptr<CoreRuntime> runtime(new CoreRuntime(mode));
+
+       // Intiailize ecore first (signal handlers, etc.) before runtime init.
+       ecore_init();
+
+       if (corerunMode) {
+               _INFO("##### Run in corerun mode #####");
+               char appId[APPID_MAX_LENGTH] = {0,};
+               std::string appRoot;
+               snprintf(appId, 16, "%s", "dotnet-launcher");
+               appRoot = baseName(argv[1]);
+
+               if (runtime->initialize(true) != 0) {
+                       _ERR("Failed to initialize");
+                       return 1;
+               }
+
+               int argsLen = vargs.size() - 1;
+               char** args = &vargs[1];
+               if (runtime->launch(appId, appRoot.c_str(), argv[1], argsLen, args)) {
+                       _ERR("Failed to launch");
+                       return 1;
+               }
+       } else if (standaloneMode) {
+               _INFO("##### Run in standalone mode #####");
+               char appId[APPID_MAX_LENGTH] = {0,};
                std::string appRoot;
                if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
                        const char* appRootPath = aul_get_app_root_path();
@@ -92,7 +109,7 @@ int main(int argc, char *argv[])
                        snprintf(appId, 16, "%s", "dotnet-launcher");
                        appRoot = baseName(baseName(standalonePath));
                }
-               _DBG("AUL_APPID : %s", appId);
+               _INFO("AUL_APPID : %s", appId);
 
                if (runtime->initialize(true) != 0) {
                        _ERR("Failed to initialize");
@@ -100,8 +117,9 @@ int main(int argc, char *argv[])
                }
 
                // change cmdline from dotnet-launcher to executable path
-               memset(argv[0], '\0', CMD_LINE_SIZE);
-               snprintf(argv[0], CMD_LINE_SIZE - 1, "%s", appId);
+               int cmdlineSize = paddingExist ? APPID_MAX_LENGTH : APPID_MAX_LENGTH - PaddingOption.length();
+               memset(argv[0], '\0', cmdlineSize);
+               snprintf(argv[0], cmdlineSize, "%s", standalonePath);
 
                int argsLen = vargs.size();
                char** args = &vargs[0];
@@ -110,18 +128,24 @@ int main(int argc, char *argv[])
                        return 1;
                }
        } else {
+               // change cmdline from dotnet-hydra-launcher to dotnet-launcher
+               if (strcmp(argv[0], "/usr/bin/dotnet-hydra-launcher") == 0) {
+                       memset(argv[0], '\0', strlen("/usr/bin/dotnet-hydra-launcher"));
+                       snprintf(argv[0], strlen("/usr/bin/dotnet-launcher") + 1, "/usr/bin/dotnet-launcher");
+               }
+
                Launchpad.onCreate = [&runtime]() {
-                       if (runtime->initialize(false) != 0)
+                       if (runtime->initialize(false) != 0) {
                                _ERR("Failed to initialized");
-                       else
-                               _DBG("Success to initialized");
+                       } else {
+                               _INFO("Success to initialized");
+                       }
                };
 
                Launchpad.onTerminate = [&runtime](const AppInfo& appInfo, int argc, char** argv) {
-                       _DBG("launch request with app path : %s", appInfo.path.c_str());
-                       _DBG("appId : %s", appInfo.id.c_str());
-                       _DBG("pkg : %s", appInfo.pkg.c_str());
-                       _DBG("type : %s", appInfo.type.c_str());
+                       _INFO("launch request with app path : %s", appInfo.path.c_str());
+                       _INFO("appId : %s", appInfo.id.c_str());
+                       _INFO("pkg : %s", appInfo.pkg.c_str());
 
                        // aul_get_app_root_path() can return NULL for error case.
                        if (appInfo.root.empty()) {
@@ -135,11 +159,15 @@ int main(int argc, char *argv[])
                };
                int ret = Launchpad.loaderMain(argc, argv);
                if (ret < 0) {
-                       _DBG("fail to start loaderMain. candidate process is not created.");
+                       _ERR("fail to start loaderMain. candidate process is not created.");
                        return 1;
                }
        }
 
-       runtime->dispose();
        return 0;
 }
+
+int main(int argc, char *argv[])
+{
+       return realMain(argc, argv, "default");
+}