Fix app's native image existence checking code. (#376)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index ed24af3..7c146b7 100644 (file)
@@ -67,9 +67,9 @@ static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
 #undef __STR
 #undef __XSTR
 
-static const char* CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
-static const char* CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
-static const char* CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
+static std::string CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
+static std::string CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
+static std::string CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
 static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
 static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
 static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
@@ -102,38 +102,6 @@ NIOption* getNIOption()
        return __ni_option;
 }
 
-static bool isCoreLibPrepared()
-{
-       std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
-       if (isFile(coreLibBackup)) {
-               return true;
-       } else {
-               _SERR("The native image of System.Private.CoreLib does not exist\n"
-                                       "Run the command to create the native image\n"
-                                       "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n");
-               return false;
-       }
-}
-
-static bool hasCoreLibNI()
-{
-       std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
-       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
-       std::string cmd = ildasm + " " + coreLib + " -noil -stats | grep '.rsrc'";
-
-       FILE *fp;
-       fp = popen(cmd.c_str(), "r");
-       if (fp != NULL) {
-               char buff[1024];
-               if (fgets(buff, sizeof(buff), fp) != NULL) {
-                       pclose(fp);
-                       return false;
-               }
-               pclose(fp);
-       }
-       return true;
-}
-
 static void waitInterval()
 {
        // by the recommand, ignore small value for performance.
@@ -143,23 +111,6 @@ static void waitInterval()
        }
 }
 
-static std::string getNIFilePath(const std::string& dllPath)
-{
-       size_t index = dllPath.find_last_of(".");
-       if (index == std::string::npos) {
-               _SERR("File doesnot contain extension. fail to get NI file name");
-               return "";
-       }
-       std::string fName = dllPath.substr(0, index);
-       std::string fExt = dllPath.substr(index, dllPath.length());
-
-       // crossgen generate file with lower case extension only
-       std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
-       std::string niPath = fName + ".ni" + fExt;
-
-       return niPath;
-}
-
 /**
  * @brief create the directory including parents directory, and
  *        copy ownership and smack labels to the created directory.
@@ -201,6 +152,23 @@ static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::stri
        return true;
 }
 
+static std::string getNIFilePath(const std::string& dllPath)
+{
+       size_t index = dllPath.find_last_of(".");
+       if (index == std::string::npos) {
+               _SERR("File doesnot contain extension. fail to get NI file name");
+               return "";
+       }
+       std::string fName = dllPath.substr(0, index);
+       std::string fExt = dllPath.substr(index, dllPath.length());
+
+       // crossgen generate file with lower case extension only
+       std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
+       std::string niPath = fName + ".ni" + fExt;
+
+       return niPath;
+}
+
 static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt)
 {
        std::string niDirPath;
@@ -239,9 +207,21 @@ static bool checkNIExistence(const std::string& absDllPath)
        // native image of System.Private.CoreLib.dll should have to overwrite
        // original file to support new coreclr
        if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
-               if (hasCoreLibNI()) {
-                       return true;
-               }
+               return isR2RImage(absDllPath);
+       }
+
+       return false;
+}
+
+static bool checkAppNIExistence(const std::string& absDllPath, NIOption* opt)
+{
+       std::string absNIPath = getAppNIFilePath(absDllPath, opt);
+       if (absNIPath.empty()) {
+               return false;
+       }
+
+       if (isFile(absNIPath)) {
+               return true;
        }
 
        return false;
@@ -265,8 +245,6 @@ static bool checkDllExistInDir(const std::string& path)
  * Get the list of managed files in the specific directory
  * Absolute paths of managed files are stored at the result list.
  * If native image already exist in the same directory, managed file is ignored.
- *
- * Note: System.Private.CoreLib is excluded from the result list.
  */
 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
 {
@@ -276,9 +254,30 @@ static ni_error_e getTargetDllList(const std::string& path, std::vector<std::str
 
        auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
                if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
-                       if (f_path.find("System.Private.CoreLib") == std::string::npos) {
-                               fileList.push_back(getAbsolutePath(f_path));
-                       }
+                       fileList.push_back(getAbsolutePath(f_path));
+               }
+       };
+
+       scanFilesInDirectory(path, func, 0);
+
+       return NI_ERROR_NONE;
+}
+
+/*
+ * Get the list of managed files in the specific directory of Application
+ * Absolute paths of managed files are stored at the result list.
+ * If native image already exist in the .native_image directory, managed file is ignored.
+ *
+ */
+static ni_error_e getAppTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
+{
+       if (!isDirectory(path)) {
+               return NI_ERROR_INVALID_PARAMETER;
+       }
+
+       auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
+               if (isManagedAssembly(f_path) && !checkAppNIExistence(f_path, opt)) {
+                       fileList.push_back(getAbsolutePath(f_path));
                }
        };
 
@@ -289,10 +288,12 @@ static ni_error_e getTargetDllList(const std::string& path, std::vector<std::str
 
 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
 {
-       args.push_back(CORERUN_CMD);
-       args.push_back(CROSSGEN2_PATH);
+       args.push_back(CORERUN_CMD.c_str());
+       if (CROSSGEN2_PATH != "") {
+               args.push_back(CROSSGEN2_PATH.c_str());
+       }
        args.push_back(CROSSGEN_OPT_JITPATH);
-       args.push_back(CLRJIT_PATH);
+       args.push_back(CLRJIT_PATH.c_str());
        args.push_back(CROSSGEN_OPT_TARGET_ARCH);
        args.push_back(ARCHITECTURE_IDENTIFIER);
        if (!(opt->flags & NI_FLAGS_NO_PIPELINE)) {
@@ -425,10 +426,11 @@ static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, con
 
                                _SOUT("Native image %s generated successfully.", niPath.c_str());
                        }
+               } else {
+                       _SERR("Failed. Forked process terminated abnormally");
                }
        } else {
                std::vector<const char*> argv;
-
                makeArgs(argv, refPaths, opt);
 
                // add input files at the end of parameter
@@ -437,6 +439,7 @@ static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, con
                        _SOUT("+ %s", input.c_str());
                }
 
+               // end param
                argv.push_back(nullptr);
 
                // print cmd
@@ -445,7 +448,7 @@ static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, con
                        for (auto &arg: argv) _SOUT("+ %s", arg);
                }
 
-               execv(CORERUN_CMD, const_cast<char* const*>(argv.data()));
+               execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
 
                clearArgs(argv);
                exit(0);
@@ -484,6 +487,8 @@ static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, c
                                }
 
                                _SOUT("Native image %s generated successfully.", niPath.c_str());
+                       } else {
+                               _SERR("Failed. Forked process terminated abnormally");
                        }
                } else {
                        std::vector<const char*> argv;
@@ -504,7 +509,7 @@ static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, c
                                for (auto &arg: argv) _SOUT("+ %s", arg);
                        }
 
-                       execv(CORERUN_CMD, const_cast<char* const*>(argv.data()));
+                       execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
 
                        clearArgs(argv);
                        exit(0);
@@ -516,14 +521,47 @@ static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, c
        return NI_ERROR_NONE;
 }
 
+static ni_error_e createCoreLibNI(NIOption* opt)
+{
+       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
+       std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
+       std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
+
+       std::vector<std::string> dllList;
+       std::vector<std::string> refPaths;
+       dllList.push_back(getAbsolutePath(coreLib));
+
+       if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
+               if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE && exist(niCoreLib)) {
+                       if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
+                               _SERR("Failed to rename System.Private.CoreLib.dll");
+                               return NI_ERROR_CORE_NI_FILE;
+                       }
+                       if (rename(niCoreLib.c_str(), coreLib.c_str())) {
+                               _SERR("Failed to rename System.Private.CoreLib.ni.dll");
+                               return NI_ERROR_CORE_NI_FILE;
+                       }
+               } else {
+                       _SERR("Failed to create native image for %s", coreLib.c_str());
+                       return NI_ERROR_CORE_NI_FILE;
+               }
+       }
+       return NI_ERROR_NONE;
+}
+
 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
 {
+       ni_error_e ret = NI_ERROR_NONE;
+
        if (dllList.empty()) {
                return NI_ERROR_INVALID_PARAMETER;
        }
        // When performing AOT for one Dll, an error is returned when an error occurs.
        // However, when processing multiple dlls at once, only the log for errors is output and skipped.
 
+       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
+       bool hasSPC = false;
+
        for (auto it = dllList.begin(); it != dllList.end(); it++) {
                std::string f = *it;
                if (!isFile(f)) {
@@ -534,18 +572,43 @@ static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string
                        _SERR("Input file is not a dll file : %s", f.c_str());
                        dllList.erase(it--);
                }
+               // handle System.Private.CoreLib.dll separately.
+               // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
+               if (f == coreLib) {
+                       hasSPC = true;
+                       dllList.erase(it--);
+               }
+       }
+
+       // In the case of SPC, post-processing is required to change the name of the native image.
+       // In order to avoid repeatedly checking whether the generated native image is an SPC,
+       // the SPC native image generation is performed separately.
+       if (hasSPC) {
+               ret = createCoreLibNI(opt);
+               if (ret != NI_ERROR_NONE) {
+                       return ret;
+               }
+       }
+
+       // if there is no proper input after processing dll list
+       if (dllList.empty()) {
+               if (hasSPC) {
+                       return ret;
+               } else {
+                       return NI_ERROR_INVALID_PARAMETER;
+               }
        }
 
        std::vector<std::string> paths;
        splitPath(refPaths, paths);
 
        if (opt->flags & NI_FLAGS_NO_PIPELINE) {
-               crossgen2NoPipeLine(dllList, paths, opt);
+               ret = crossgen2NoPipeLine(dllList, paths, opt);
        } else {
-               crossgen2PipeLine(dllList, paths, opt);
+               ret = crossgen2PipeLine(dllList, paths, opt);
        }
 
-       return NI_ERROR_NONE;
+       return ret;
 }
 
 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
@@ -560,6 +623,11 @@ static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPa
                return NI_ERROR_INVALID_PARAMETER;
        }
 
+       if (checkNIExistence(dllFile)) {
+               _SERR("Native image file is already exist : %s", dllFile.c_str());
+               return NI_ERROR_ALREADY_EXIST;
+       }
+
        std::vector<std::string> dllList;
        dllList.push_back(getAbsolutePath(dllFile));
        return doAOTList(dllList, refPaths, opt);
@@ -593,33 +661,18 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
        return 0;
 }
 
-static ni_error_e createCoreLibNI(NIOption* opt)
+ni_error_e initNICommon()
 {
-       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
-       std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
-       std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
+#if defined(__arm__) || defined(__aarch64__)
 
-       if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
-               if (doAOTFile(coreLib, std::string(), opt) == NI_ERROR_NONE && exist(niCoreLib)) {
-                       if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
-                               _SERR("Failed to rename System.Private.CoreLib.dll");
-                               return NI_ERROR_CORE_NI_FILE;
-                       }
-                       if (rename(niCoreLib.c_str(), coreLib.c_str())) {
-                               _SERR("Failed to rename System.Private.CoreLib.ni.dll");
-                               return NI_ERROR_CORE_NI_FILE;
-                       }
-               } else {
-                       _SERR("Failed to create native image for %s", coreLib.c_str());
-                       return NI_ERROR_CORE_NI_FILE;
-               }
+       char *env = nullptr;
+       env = getenv("MIC_CROSSGEN2_ENABLED");
+       if (env != nullptr && !strcmp(env, "1")) {
+               CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
+               CROSSGEN2_PATH = "";
+               CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
        }
-       return NI_ERROR_NONE;
-}
 
-ni_error_e initNICommon()
-{
-#if defined(__arm__) || defined(__aarch64__)
        // get interval value
        const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
        std::ifstream inFile(intervalFile);
@@ -674,24 +727,16 @@ void finalizeNICommon()
 
 ni_error_e createNIPlatform(NIOption* opt)
 {
-       ni_error_e ret = createCoreLibNI(opt);
+       ni_error_e ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
        if (ret != NI_ERROR_NONE) {
                return ret;
        }
 
-       return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), opt);
+       return createNIUnderDirs(__pm->getTizenFXPath(), opt);
 }
 
 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
 {
-       if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
-               return createCoreLibNI(opt);
-       }
-
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
-
        return doAOTFile(dllPath, std::string(), opt);
 }
 
@@ -746,9 +791,6 @@ ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& re
 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
 {
        ni_error_e ret = NI_ERROR_NONE;
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
 
        std::vector<std::string> fileList;
        std::vector<std::string> paths;
@@ -764,6 +806,11 @@ ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
                        if (ret != NI_ERROR_NONE) {
                                return ret;
                        }
+               } else if (opt->flags & NI_FLAGS_APPNI) {
+                       ret = getAppTargetDllList(path, fileList, opt);
+                       if (ret != NI_ERROR_NONE) {
+                               return ret;
+                       }
                } else {
                        ret = getTargetDllList(path, fileList);
                        if (ret != NI_ERROR_NONE) {
@@ -789,6 +836,11 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
 
        __pm->setAppRootPath(rootPath);
 
+       char* extraDllPaths = pluginGetExtraDllPath();
+       if (extraDllPaths && extraDllPaths[0] != '\0') {
+               __pm->setExtraDllPaths(extraDllPaths);
+       }
+
        opt->flags |= NI_FLAGS_APPNI;
 
        if (isReadOnlyArea(rootPath)) {
@@ -809,7 +861,7 @@ void removeNIPlatform()
        std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
        std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
 
-       if (hasCoreLibNI()) {
+       if (isR2RImage(coreLib)) {
                if (!isFile(coreLibBackup)) {
                        return;
                }
@@ -894,10 +946,6 @@ ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
 
 ni_error_e regenerateAppNI(NIOption* opt)
 {
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
-
        int ret = 0;
        pkgmgrinfo_appinfo_metadata_filter_h handle;
 
@@ -966,10 +1014,6 @@ static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
 
 ni_error_e regenerateTACNI(NIOption* opt)
 {
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
-
        removeNIUnderDirs(__DOTNET_DIR);
 
        pkgmgrinfo_appinfo_metadata_filter_h handle;