Verify that the assembly is in the TPA list.
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index 88fbb28..69c2dcf 100644 (file)
@@ -36,6 +36,7 @@
 #include <string.h>
 
 #include "ni_common.h"
+#include "tac_common.h"
 #include "path_manager.h"
 #include "plugin_manager.h"
 
@@ -181,14 +182,8 @@ static uintptr_t getNextBaseAddr()
 }
 
 // Save base address of system ni image to file
-static void updateBaseAddrFile(const std::string &dllPath, uintptr_t baseAddr)
+static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
 {
-       std::string absNiPath = getNiFilePath(dllPath);
-       if (absNiPath.empty()) {
-               fprintf(stderr, "Failed to get ni file path for %s\n", dllPath.c_str());
-               return;
-       }
-
        uintptr_t niSize = getFileSize(absNiPath);
        if (niSize == 0) {
                fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
@@ -220,7 +215,7 @@ static bool isTPADll(const std::string &dllPath)
 #endif
 
 // baseAddr should be checked in file before getting here
-static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, uintptr_t baseAddr = 0, bool isAppNI = false)
+static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false)
 {
        if (!isFileExist(dllPath)) {
                fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
@@ -248,6 +243,14 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
                absNiPath = getAppNIPath(absNiPath);
        }
 
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+       uintptr_t baseAddr = 0;
+
+       if (isTPADll(dllPath)) {
+               baseAddr = getNextBaseAddr();
+       }
+#endif
+
        pid_t pid = fork();
        if (pid == -1)
                return NI_ERROR_UNKNOWN;
@@ -260,6 +263,11 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
                        // niEixst() return false for System.Private.Corelib.dll
                        if (isFileExist(absNiPath)) {
                                updateAssemblyInfo(absDllPath, absNiPath);
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+                               if (baseAddr != 0) {
+                                       updateBaseAddrFile(absNiPath, baseAddr);
+                               }
+#endif
                                return NI_ERROR_NONE;
                        } else {
                                fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
@@ -323,44 +331,43 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
                return -1;
        }
 
-       if (removeNiUnderPkgRoot(pkgId) != 0) {
+       if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
                fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
                return -1;
        }
 
+       if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
+               fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
+               return -1;
+       }
+
        // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
-       if (createNiUnderPkgRoot(pkgId, *enableR2R) != 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
+       if (createNiUnderPkgRoot(pkgId, *enableR2R) != NI_ERROR_NONE) {
+               fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
                return -1;
        } else {
                fprintf(stderr, "Complete make application to native image\n");
        }
 
+       if (createTACPackage(pkgId) != TAC_ERROR_NONE) {
+               fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
+               return -1;
+       }else {
+               fprintf(stderr, "Complete make symbolic link file to tac\n");
+       }
+
        return 0;
 }
 
-static void createCoreLibNI(bool enableR2R, bool doGenUniqueBaseSystem)
+static void createCoreLibNI(bool enableR2R)
 {
        std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
        std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
        std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
 
        if (!isFileExist(coreLibBackup)) {
-               uintptr_t baseAddr = 0;
-
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-               if (doGenUniqueBaseSystem) {
-                       baseAddr = getNextBaseAddr();
-               }
-#endif
-
-               if (!crossgen(coreLib, std::string(), enableR2R, baseAddr)) {
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-                       if (doGenUniqueBaseSystem && baseAddr != 0) {
-                               updateBaseAddrFile(coreLib, baseAddr);
-                       }
-#endif
 
+               if (!crossgen(coreLib, std::string(), enableR2R)) {
                        if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
                                fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
                        }
@@ -385,11 +392,11 @@ ni_error_e initNICommon(NiCommonOption* option)
        }
 
        if (initializePluginManager("normal")) {
-               fprintf(stderr, "Fail to initialize plugin manager\n");
+               fprintf(stderr, "Fail to initialize PluginManager\n");
                return NI_ERROR_UNKNOWN;
        }
        if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
-               fprintf(stderr, "Fail to initialize path manager\n");
+               fprintf(stderr, "Fail to initialize PathManager\n");
                return NI_ERROR_UNKNOWN;
        }
 
@@ -413,43 +420,29 @@ void finalizeNICommon()
 }
 
 
-void createNiPlatform(bool enableR2R, bool doGenUniqueBaseSystem)
+void createNiPlatform(bool enableR2R)
 {
        const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
-       createNiUnderDirs(platformDirs, 2, enableR2R, doGenUniqueBaseSystem);
+       createNiUnderDirs(platformDirs, 2, enableR2R);
 }
 
-ni_error_e createNiDll(const std::string& dllPath, bool enableR2R, bool doGenUniqueBaseSystem)
+ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
 {
-       createCoreLibNI(enableR2R, doGenUniqueBaseSystem);
+       createCoreLibNI(enableR2R);
        // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
        // Skip if input dll is System.Private.CoreLib.dll
        if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
                return NI_ERROR_NONE;
        }
 
-       uintptr_t baseAddr = 0;
-
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-       if (doGenUniqueBaseSystem && isTPADll(dllPath)) {
-               baseAddr = getNextBaseAddr();
-       }
-#endif
-
-       ni_error_e status = crossgen(dllPath, std::string(), enableR2R, baseAddr);
-
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-       if (doGenUniqueBaseSystem && baseAddr != 0) {
-               updateBaseAddrFile(dllPath, baseAddr);
-       }
-#endif
+       ni_error_e status = crossgen(dllPath, std::string(), enableR2R);
 
        return status;
 }
 
-void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool doGenUniqueBaseSystem, bool isAppNI)
+void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
 {
-       createCoreLibNI(enableR2R, doGenUniqueBaseSystem);
+       createCoreLibNI(enableR2R);
 
        std::string appPaths;
        for (int i = 0; i < count; i++) {
@@ -460,22 +453,25 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R,
        if (appPaths.back() == ':')
                appPaths.pop_back();
 
-       auto convert = [&appPaths, enableR2R, doGenUniqueBaseSystem, isAppNI](const std::string& path, const char* name) {
-               uintptr_t baseAddr = 0;
-
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-               if (doGenUniqueBaseSystem && !isAppNI && isTPADll(path)) {
-                       baseAddr = getNextBaseAddr();
-               }
-#endif
-
-               if (!crossgen(path, appPaths.c_str(), enableR2R, baseAddr, isAppNI)) {
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-                       if (doGenUniqueBaseSystem && !isAppNI && baseAddr != 0) {
-                               updateBaseAddrFile(path, baseAddr);
+       std::vector<std::string> tpaAssemblies;
+       splitPath(__tpa, tpaAssemblies);
+
+       auto convert = [&appPaths, enableR2R, isAppNI, tpaAssemblies](const std::string& path, const char* name) {
+               if (isAppNI) {
+                       std::string assembly = path.substr(path.rfind('/') + 1);
+                       bool isExist = false;
+                       for (auto& tpa : tpaAssemblies) {
+                               if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
+                                       isExist = true;
+                                       break;
+                               }
                        }
-#endif
-
+                       if (isExist) {
+                               fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", path.c_str());
+                               return;
+                       }
+               }
+               if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
                        waitInterval();
                }
        };
@@ -483,13 +479,14 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R,
        for (int i = 0; i < count; i++) {
                scanFilesInDir(rootPaths[i], convert, 1);
        }
+
+       tpaAssemblies.clear();
 }
 
 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
 {
        std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) != NI_ERROR_NONE) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
+       if (getRootPath(pkgId, pkgRoot) < 0) {
                return NI_ERROR_INVALID_PACKAGE;
        }
 
@@ -498,7 +495,7 @@ ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        std::string paths[] = {binDir, libDir, tacDir};
 
-       createNiUnderDirs(paths, 3, enableR2R, false, true);
+       createNiUnderDirs(paths, 3, enableR2R, true);
 
        return NI_ERROR_NONE;
 }
@@ -507,16 +504,53 @@ ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string&
 {
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
        std::string binDir = concatPath(pkgRoot, "bin");
        std::string libDir = concatPath(pkgRoot, "lib");
-       std::string appTAC = concatPath(binDir, ".TAC.Release");
-       std::string paths = binDir + ":" + libDir + ":" + appTAC;
-
-       return crossgen(dllPath, paths, enableR2R, 0, true);
+       std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
+       std::string paths = binDir + ":" + libDir + ":" + tacDir;
+
+       if (bf::is_symlink(dllPath)) {
+               if (bf::exists(tacDir)) {
+                       if (!isNativeImage(dllPath)) {
+                               std::string originPath = bf::read_symlink(dllPath).string();
+                               std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
+                               if (!bf::exists(originNiPath)) {
+                                       if(createNiDll(originPath, false) != NI_ERROR_NONE) {
+                                               fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
+                                               return NI_ERROR_UNKNOWN;
+                                       }
+                               }
+                               std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
+                               if (!bf::exists(setNiPath)) {
+                                       bf::create_symlink(originNiPath, setNiPath);
+                                       fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
+                                       updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
+                               }
+                       }
+               }
+               return NI_ERROR_NONE;
+       } else {
+               std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
+               std::vector<std::string> tpaAssemblies;
+               splitPath(__tpa, tpaAssemblies);
+               bool isExist = false;
+               for (auto& tpa : tpaAssemblies) {
+                       if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
+                               isExist = true;
+                               break;
+                       }
+               }
+               tpaAssemblies.clear();
+               if (isExist) {
+                       fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", dllPath.c_str());
+                       return NI_ERROR_NONE;
+               } else {
+                       return crossgen(dllPath, paths, enableR2R, true);
+               }
+       }
 }
 
 void removeNiPlatform()
@@ -567,7 +601,6 @@ ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
 {
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
-               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }