Fixed TAC related bugs caused by function integration patch
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index d6515e8..f7b69ad 100644 (file)
@@ -233,6 +233,7 @@ static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::stri
 static std::string getNIFilePath(const std::string& absDllPath, NIOption* opt)
 {
        std::string dllPath = absDllPath;
+       std::string fileName = getFileName(absDllPath);
        if (opt->flags & NI_FLAGS_APPNI) {
                std::string niDirPath;
                std::string niTmpDirPath;
@@ -253,10 +254,13 @@ static std::string getNIFilePath(const std::string& absDllPath, NIOption* opt)
                                niTmpDirPath = prevPath;
                                _SERR("fail to create dir (%s)", niTmpDirPath.c_str());
                        }
-                       dllPath = concatPath(niTmpDirPath, getFileName(absDllPath));
+                       dllPath = concatPath(niTmpDirPath, fileName);
                } else {
-                       dllPath = concatPath(niDirPath, getFileName(absDllPath));
+                       dllPath = concatPath(niDirPath, fileName);
                }
+       } else if (opt->flags & NI_FLAGS_RESOURCE_NI) {
+               std::string rpkDir = concatPath(__pm->getAppRootPath(), APP_NI_SUB_DIR);
+               dllPath = createDir(rpkDir) ? concatPath(rpkDir, fileName) : concatPath(__pm->getAppRootPath(), fileName);
        }
 
        size_t index = dllPath.find_last_of(".");
@@ -466,6 +470,11 @@ static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::str
                }
        }
 
+       if (opt->flags & NI_FLAGS_RESOURCE_NI) {
+               moveFile(niPath, changeExtension(dllPath, ".dll", ".ni.dll"));
+               removeAll(concatPath(__pm->getAppRootPath(), APP_NI_SUB_DIR));
+       }
+
        if (!(opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE)) {
                _SOUT("Native image %s generated successfully.", outFile.c_str());
        }
@@ -822,11 +831,10 @@ static ni_error_e removeAndCreateNI(const char* pkgId, NIOption* pOptions)
        if (createNIUnderPkgRoot(pkgId, pOptions) != NI_ERROR_NONE) {
                _SERR("Failed to generate NI file [%s]", pkgId);
                return NI_ERROR_UNKNOWN;
-       } else {
-               _SOUT("Complete make native image for pkg (%s)", pkgId);
        }
 
-       return NI_ERROR_NONE;   
+       _SOUT("Complete make native image for pkg (%s)", pkgId);
+       return NI_ERROR_NONE;
 }
 
 static bool isReadOnlyPkg(std::string pkgId)
@@ -851,42 +859,7 @@ static bool isReadOnlyPkg(std::string pkgId)
        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
        return readonly;
 }
-#if 0
-// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
-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) {
-               _SERR("Failed to get pkgid");
-               return -1;
-       }
-
-       NIOption **pOptions = (NIOption**)userData;
-       if (isReadOnlyPkg(pkgId) && (*pOptions)->flags & NI_FLAGS_SKIP_RO_APP) {
-               return 0;
-       }
-
-       return removeAndCreateNI(pkgId, *pOptions);
-}
-
-// callback function of "pkgmgrinfo_pkginfo_metadata_filter_foreach"
-static int pkgAotCb(pkgmgrinfo_pkginfo_h handle, void *userData)
-{
-       char *pkgId = NULL;
-       int ret = 0;
-       NIOption **pOptions = (NIOption**)userData;
 
-       ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgId);
-       if (ret != PMINFO_R_OK) {
-               _SERR("Failed to get pkgid");
-               return -1;
-       }
-
-       return removeAndCreateNI(pkgId, *pOptions);
-}
-#endif
 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
 {
@@ -1000,8 +973,11 @@ ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& re
 {
        ni_error_e ret;
 
+       // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
+       // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
        bool isAppNI = false;
        if (opt->flags & NI_FLAGS_APPNI) {
+               opt->flags &= ~NI_FLAGS_APPNI;
                isAppNI = true;
        }
 
@@ -1033,19 +1009,14 @@ ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& re
        }
 
        if (!needNIList.empty()) {
-               // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
-               // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
-               opt->flags &= ~NI_FLAGS_APPNI;
                ret = doAOTList(needNIList, refPaths, opt);
-               if (isAppNI) {
-                       opt->flags |= NI_FLAGS_APPNI;
-               }
                if (ret != NI_ERROR_NONE) {
                        return ret;
                }
        }
 
        if (isAppNI) {
+               opt->flags |= NI_FLAGS_APPNI;
                for (auto &niPath : niList) {
                        if (exist(niPath)) {
                                std::string symNIPath = concatPath(targetPath, getFileName(niPath));
@@ -1062,7 +1033,6 @@ ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& re
        return NI_ERROR_NONE;
 }
 
-
 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
 {
        ni_error_e ret = NI_ERROR_NONE;
@@ -1106,8 +1076,6 @@ ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
 
 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
 {
-       ni_error_e ret;
-
        if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
                _SERR("The native image of System.Private.CoreLib does not exist.\n"
                                "Run the command to create the native image\n"
@@ -1120,6 +1088,7 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
                _SERR("Failed to get root path from [%s]", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
+       __pm->setAppRootPath(rootPath);
 
        char* extraDllPaths = pluginGetExtraDllPath();
        if (extraDllPaths && extraDllPaths[0] != '\0') {
@@ -1127,19 +1096,21 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
                splitPath(extraDllPaths, opt->extraRefPath);
        }
 
+       std::string targetDirs;
        if (isRPK(pkgId)) {
-               opt->flags &= ~NI_FLAGS_APPNI;
+               opt->flags &= ~NI_FLAGS_APPNI; // added to exclude logic of APP_NI
+               opt->flags |= NI_FLAGS_RESOURCE_NI; // added flag for RPK type
+               opt->flags |= NI_FLAGS_NO_PIPELINE; // added the flag to set the output path
+
                std::string paths = getResourcePaths(rootPath);
-               if (!paths.empty()) {
-                       ret = createNIUnderDirs(paths, opt);             
-               } else {
+               if (paths.empty()) {
                        _SERR("Failed to get rpk paths from [%s]", pkgId.c_str());
-                       ret = NI_ERROR_UNKNOWN;
+                       return NI_ERROR_UNKNOWN;
                }
+               targetDirs = paths;
        } else {
-               __pm->setAppRootPath(rootPath);
-
                opt->flags |= NI_FLAGS_APPNI;
+               opt->flags &= ~NI_FLAGS_RESOURCE_NI; // added to exclude logic of RESOURCE_NI
 
                if (isReadOnlyArea(rootPath)) {
                        opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
@@ -1149,12 +1120,10 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
                        opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
                }
 
-               // create native image under bin and lib directory
-               // tac directory is skipped in the createNIUnderDirs.
-               ret = createNIUnderDirs(__pm->getAppPaths(), opt);
+               targetDirs = __pm->getAppPaths();
        }
 
-       return ret;
+       return createNIUnderDirs(targetDirs, opt);
 }
 
 void removeNIPlatform()
@@ -1229,7 +1198,7 @@ ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
                        if (isDirectory(path)) {
                                removeNIUnderDirs(path);
                        }
-               }               
+               }
        } else {
                __pm->setAppRootPath(rootPath);