add nitool option to regenerate all app ni files
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / common.cc
index 9cd2109..3ab426c 100644 (file)
@@ -33,6 +33,9 @@
 #include <grp.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <string.h>
+
+#include <fstream>
 
 #include "common.h"
 
@@ -62,10 +65,8 @@ static const char* __JIT_PATH = __STR(RUNTIME_DIR)"/libclrjit.so";
 #undef __STR
 #undef __XSTR
 
-static void crossgen(const char* dllPath, const char* appPath);
-static void smack_(const char* dllPath, const char* label);
-
-std::string replace(std::string &str, const std::string& from, const std::string& to)
+#if 0
+static std::string replace(std::string &str, const std::string& from, const std::string& to)
 {
        size_t startPos = 0;
        while ((startPos = str.find(from, startPos)) != std::string::npos) {
@@ -74,45 +75,7 @@ std::string replace(std::string &str, const std::string& from, const std::string
        }
        return str;
 }
-
-void createNiPlatform()
-{
-       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
-       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
-
-       if (fileNotExist(niCoreLib)) {
-               crossgen(coreLib.c_str(), nullptr);
-               smack_(niCoreLib.c_str(), "_");
-       }
-
-       const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
-       const char* ignores[] = {coreLib.c_str()};
-
-       createNiUnderDirs(platformDirs, 3, ignores, 1, [](const char* ni) {
-               smack_(ni, "_");
-       });
-}
-
-void createNiSelect(const char* dllPath)
-{
-       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
-       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
-
-       if (fileNotExist(niCoreLib)) {
-               crossgen(coreLib.c_str(), nullptr);
-               smack_(niCoreLib.c_str(), "_");
-       }
-
-       if (!fileNotExist(dllPath)) {
-               std::string strPath = dllPath;
-               std::string niPath = replace(strPath, std::string(".dll"), std::string(".ni.dll"));
-               if (fileNotExist(niPath))
-                       crossgen(dllPath, nullptr);
-               else
-                       printf("Already [%s] file is exist\n", niPath.c_str());
-               smack_(niPath.c_str(), "_");
-       }
-}
+#endif
 
 static void smack_(const char* dllPath, const char* label)
 {
@@ -169,6 +132,16 @@ static void crossgen(const char* dllPath, const char* appPath)
                tpaDir.push_back(__RUNTIME_DIR);
                tpaDir.push_back(__DEVICE_API_DIR);
 
+               // get reference API directory ([DEVICE_API_DIR]/ref)
+               int len = strlen(__DEVICE_API_DIR);
+               char* refAPIDir = (char*)calloc(len + 5, 1);
+               if (!refAPIDir) {
+                       printf("fail to allocate memory for reference API directory\n");
+                       return;
+               }
+               snprintf(refAPIDir, len + 5, "%s%s", __DEVICE_API_DIR, "/ref");
+               tpaDir.push_back(refAPIDir);
+
                std::string tpa;
                assembliesInDirectory(tpaDir, tpa);
 
@@ -230,6 +203,17 @@ static int getRootPath(const char *pkgId, std::string& rootPath)
 
 static bool niExist(const std::string& path, std::string& ni)
 {
+       // native image of System.Private.CoreLib.dll should have to overwrite
+       // original file to support new coreclr
+       if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
+               std::string coreLibBackup = path + ".Backup";
+               if (!fileNotExist(coreLibBackup)) {
+                       ni = path;
+                       return true;
+               }
+               return false;
+       }
+
        static const char* possibleExts[] = {
                ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL",
                ".ni.exe", ".NI.exe", ".NI.EXE", ".ni.EXE"
@@ -249,7 +233,149 @@ static bool niExist(const std::string& path, std::string& ni)
        return false;
 }
 
-void createNiUnderDirs(const char* rootPaths[], int count, const char* ignores[], int igcount, afterCreate cb)
+static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
+{
+       char *pkgId = NULL;
+       int ret = 0;
+
+       ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+       if (ret != PMINFO_R_OK) {
+               _DBG("Failed to get pkgid");
+               return -1;
+       }
+
+       // When you create native image with pkgid, ni file is generated even though already ni file exist.
+       if (createNiUnderPkgRoot(pkgId) != 0) {
+               _ERR("Failed to get root path from [%s]", pkgId);
+               return -1;
+       } else {
+               _DBG("Complete make application to native image");
+       }
+
+       return 0;
+}
+
+int regenerateAppNI()
+{
+       int ret = 0;
+       pkgmgrinfo_appinfo_metadata_filter_h handle;
+
+       ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
+       if (ret != PMINFO_R_OK)
+               return -1;
+
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, "http://tizen.org/metadata/prefer_dotnet_aot", "true");
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
+               return -1;
+       }
+
+       ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, NULL);
+       if (ret != PMINFO_R_OK) {
+               _DBG("Failed pkgmgrinfo_appinfo_metadata_filter_foreach");
+               pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
+               return -1;
+       }
+
+       _DBG("Success pkgmgrinfo_appinfo_metadata_filter_foreach");
+
+       pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
+       return 0;
+}
+
+static void createCoreLibNI()
+{
+       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
+       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
+       std::string coreLibBackup = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll.Backup");
+
+       if (!niExist(coreLib, niCoreLib)) {
+               crossgen(coreLib.c_str(), nullptr);
+               if (!fileNotExist(niCoreLib)) {
+                       // change owner and groups for generated ni file.
+                       struct stat info;
+                       if (!stat(coreLib.c_str(), &info)) {
+                               if (chown(niCoreLib.c_str(), info.st_uid, info.st_gid) == -1)
+                                       _ERR("Failed to change owner and group name");
+                       }
+                       smack_(niCoreLib.c_str(), "_");
+                       rename(coreLib.c_str(), coreLibBackup.c_str());
+                       rename(niCoreLib.c_str(), coreLib.c_str());
+               }
+       }
+}
+
+void removeNiUnderDirs(const char* rootPaths[], int count)
+{
+       auto convert = [](const char* path, const char* name) {
+               std::string ni;
+               if (isNativeImage(path)) {
+                       remove(path);
+               }
+       };
+
+       for (int i = 0; i < count; i++)
+               scanFilesInDir(rootPaths[i], convert, -1);
+}
+
+void removeNiPlatform()
+{
+       std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
+       std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
+       std::string coreLibBackup = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll.Backup");
+
+       if (fileNotExist(coreLibBackup)) {
+               return;
+       }
+
+       if (remove(coreLib.c_str())) {
+               _ERR("Failed to remove System.Private.CoreLib native image file");
+       }
+
+       rename(coreLibBackup.c_str(), coreLib.c_str());
+
+       const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
+
+       removeNiUnderDirs(platformDirs, 3);
+}
+
+void createNiPlatform()
+{
+       createCoreLibNI();
+
+       const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
+
+       createNiUnderDirs(platformDirs, 3, [](const char* ni) {
+               smack_(ni, "_");
+       }, false);
+}
+
+void createNiSelect(const char* dllPath)
+{
+       createCoreLibNI();
+
+       std::string niPath;
+       if (!fileNotExist(dllPath)) {
+               if (!niExist(dllPath, niPath)) {
+                       crossgen(dllPath, nullptr);
+                       if (niExist(dllPath, niPath)) {
+                               // change owner and groups for generated ni file.
+                               struct stat info;
+                               if (!stat(dllPath, &info)) {
+                                       if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
+                                               _ERR("Failed to change owner and group name");
+                               }
+                               smack_(niPath.c_str(), "_");
+                       }
+               } else {
+                       printf("Already [%s] file is exist\n", niPath.c_str());
+               }
+       } else {
+               printf("Failed to find dll : %s", dllPath);
+       }
+}
+
+void createNiUnderDirs(const char* rootPaths[], int count, const char* ignores[], int igcount, afterCreate cb, bool update)
 {
        std::string appPaths;
        for (int i = 0; i < count; i++) {
@@ -260,46 +386,89 @@ void createNiUnderDirs(const char* rootPaths[], int count, const char* ignores[]
        if (appPaths.back() == ':')
                appPaths.pop_back();
 
-       auto convert = [&appPaths, ignores, igcount, &cb](const char* path, const char* name) {
+       auto convert = [&appPaths, ignores, igcount, &cb, update](const char* path, const char* name) {
                for (int i = 0; i < igcount; i++) {
                        if (strcmp(path, ignores[i]) == 0)
                                return;
                }
-               std::string ni;
-               if (isManagedAssembly(path) && !isNativeImage(path) && !niExist(path, ni)) {
+               std::string niPath;
+               if (isManagedAssembly(path) && !isNativeImage(path)) {
+                       if (niExist(path, niPath)) {
+                               if (update && !niPath.empty()) {
+                                       _INFO("override [%s] file", niPath.c_str());
+                               } else {
+                                       _INFO("Already [%s] file is exist", niPath.c_str());
+                                       return;
+                               }
+                       }
                        crossgen(path, appPaths.c_str());
-                       if (niExist(path, ni)) {
+                       if (niExist(path, niPath)) {
                                // change owner and groups for generated ni file.
                                struct stat info;
                                if (!stat(path, &info)) {
-                                       if (chown(ni.c_str(), info.st_uid, info.st_gid) == -1)
+                                       if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
                                                _ERR("Failed to change owner and group name");
                                }
 
                                if (cb != nullptr)
-                                       cb(ni.c_str());
+                                       cb(niPath.c_str());
+                       } else {
+                               _INFO("Failed to create native image for %s", path);
                        }
                }
        };
 
        for (int i = 0; i < count; i++)
-               scanFilesInDir(rootPaths[i], convert, -1);
+               scanFilesInDir(rootPaths[i], convert, 1);
 }
-void createNiUnderDirs(const char* rootPaths[], int count, afterCreate cb)
+void createNiUnderDirs(const char* rootPaths[], int count, afterCreate cb, bool update)
 {
-       createNiUnderDirs(rootPaths, count, nullptr, 0, cb);
+       createNiUnderDirs(rootPaths, count, nullptr, 0, cb, update);
 }
-void createNiUnderDirs(const char* rootPaths[], int count)
+void createNiUnderDirs(const char* rootPaths[], int count, bool update)
 {
-       createNiUnderDirs(rootPaths, count, nullptr);
+       createNiUnderDirs(rootPaths, count, nullptr, update);
+}
+
+int removeNiUnderPkgRoot(const char* pkgName)
+{
+       std::string pkgRoot;
+       if (getRootPath(pkgName, pkgRoot) < 0)
+               return 1;
+
+       std::string binDir = concatPath(pkgRoot, "bin");
+       std::string libDir = concatPath(pkgRoot, "lib");
+       _INFO("bindir : %s", binDir.c_str());
+       _INFO("libdir : %s", libDir.c_str());
+
+       const char* paths[] = {
+               binDir.c_str(),
+               libDir.c_str()
+       };
+
+       removeNiUnderDirs(paths, 2);
+
+       return 0;
 }
 
+
 int createNiUnderPkgRoot(const char* pkgName)
 {
        std::string pkgRoot;
        if (getRootPath(pkgName, pkgRoot) < 0)
                return 1;
 
+       // get interval value
+       const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
+       int interval = 0;
+       std::ifstream inFile(intervalFile);
+       if (inFile) {
+               _INFO("crossgen_interval.txt is found");
+               inFile >> interval;
+       } else {
+               _INFO("fail to read crossgen_interval.txt file");
+       }
+
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string libDir = concatPath(pkgRoot, "lib");
        _INFO("bindir : %s", binDir.c_str());
@@ -312,9 +481,13 @@ int createNiUnderPkgRoot(const char* pkgName)
 
        // change smack label for generated ni file.
        std::string label = "User::Pkg::" + std::string(pkgName) + "::RO";
-       createNiUnderDirs(paths, 2, [label](const char* ni) {
+       createNiUnderDirs(paths, 2, [label, interval](const char* ni) {
                        smack_(ni, label.c_str());
-       });
+                       if (interval != 0) {
+                               _INFO("sleep %d usec", interval);
+                               usleep(interval);
+                       }
+       }, true);
 
        return 0;
 }