Fixed TAC related bugs caused by function integration patch
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index 08d9d6f..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());
        }
@@ -812,22 +821,20 @@ static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPa
        return doAOTList(dllList, refPaths, opt);
 }
 
-// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
-static int getPkgIdCb(pkgmgrinfo_appinfo_h handle, void *userData)
+static ni_error_e removeAndCreateNI(const char* pkgId, NIOption* pOptions)
 {
-       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;
+       if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
+               _SERR("Failed to remove previous dlls from [%s]", pkgId);
+               return NI_ERROR_UNKNOWN;
        }
 
-       std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
-       pkgList->push_back(pkgId);
+       if (createNIUnderPkgRoot(pkgId, pOptions) != NI_ERROR_NONE) {
+               _SERR("Failed to generate NI file [%s]", pkgId);
+               return NI_ERROR_UNKNOWN;
+       }
 
-       return 0;
+       _SOUT("Complete make native image for pkg (%s)", pkgId);
+       return NI_ERROR_NONE;
 }
 
 static bool isReadOnlyPkg(std::string pkgId)
@@ -853,6 +860,38 @@ static bool isReadOnlyPkg(std::string pkgId)
        return readonly;
 }
 
+// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
+static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
+{
+       char *pkgId = NULL;
+       int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+       if (ret != PMINFO_R_OK) {
+               _SERR("Failed to get pkgid");
+               return -1;
+       }
+
+       std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
+       pkgList->push_back(pkgId);
+
+       return 0;
+}
+
+// callback function of "pkgmgrinfo_pkginfo_metadata_filter_foreach"
+static int pkgAotCb(pkgmgrinfo_pkginfo_h handle, void *userData)
+{
+       char *pkgId = NULL;
+       int ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgId);
+       if (ret != PMINFO_R_OK) {
+               _SERR("Failed to get pkgid");
+               return -1;
+       }
+
+       std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
+       pkgList->push_back(pkgId);
+
+       return 0;
+}
+
 ni_error_e initNICommon()
 {
 #if defined(__arm__) || defined(__aarch64__)
@@ -934,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;
        }
 
@@ -967,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));
@@ -996,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;
@@ -1052,7 +1088,6 @@ 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();
@@ -1061,19 +1096,34 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
                splitPath(extraDllPaths, opt->extraRefPath);
        }
 
-       opt->flags |= NI_FLAGS_APPNI;
+       std::string targetDirs;
+       if (isRPK(pkgId)) {
+               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
 
-       if (isReadOnlyArea(rootPath)) {
-               opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
-               opt->flags |= NI_FLAGS_NO_PIPELINE;
-               _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
+               std::string paths = getResourcePaths(rootPath);
+               if (paths.empty()) {
+                       _SERR("Failed to get rpk paths from [%s]", pkgId.c_str());
+                       return NI_ERROR_UNKNOWN;
+               }
+               targetDirs = paths;
        } else {
-               opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
+               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;
+                       opt->flags |= NI_FLAGS_NO_PIPELINE;
+                       _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
+               } else {
+                       opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
+               }
+
+               targetDirs = __pm->getAppPaths();
        }
 
-       // create native image under bin and lib directory
-       // tac directory is skipped in the createNIUnderDirs.
-       return createNIUnderDirs(__pm->getAppPaths(), opt);
+       return createNIUnderDirs(targetDirs, opt);
 }
 
 void removeNIPlatform()
@@ -1129,66 +1179,100 @@ void removeNIUnderDirs(const std::string& rootPaths)
 
 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
 {
+       std::vector<std::string> paths;
+
        std::string rootPath = getRootPath(pkgId);
        if (rootPath.empty()) {
                _SERR("Failed to get root path from [%s]", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
-       __pm->setAppRootPath(rootPath);
-
-       // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
-       std::string appNIPaths = __pm->getAppNIPaths();
-       std::vector<std::string> paths;
-       splitPath(appNIPaths, paths);
-       for (const auto &path : paths) {
-               if (!isReadOnlyArea(path)) {
-                       // Only the native image inside the TAC should be removed.
-                       if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
+       if (isRPK(pkgId)) {
+               std::string rpkPaths = getResourcePaths(rootPath);
+               if (rpkPaths.empty()) {
+                       _SERR("Failed to get rpk path from [%s]", pkgId.c_str());
+                       return NI_ERROR_UNKNOWN;
+               }
+               splitPath(rpkPaths, paths);
+               for (const auto &path : paths) {
+                       if (isDirectory(path)) {
                                removeNIUnderDirs(path);
-                       } else {
-                               if (isDirectory(path)) {
-                                       if (!removeAll(path.c_str())) {
-                                               _SERR("Failed to remove app ni dir [%s]", path.c_str());
+                       }
+               }
+       } else {
+               __pm->setAppRootPath(rootPath);
+
+               // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
+               std::string appNIPaths = __pm->getAppNIPaths();
+               splitPath(appNIPaths, paths);
+               for (const auto &path : paths) {
+                       if (!isReadOnlyArea(path)) {
+                               // Only the native image inside the TAC should be removed.
+                               if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
+                                       removeNIUnderDirs(path);
+                               } else {
+                                       if (isDirectory(path)) {
+                                               if (!removeAll(path.c_str())) {
+                                                       _SERR("Failed to remove app ni dir [%s]", path.c_str());
+                                               }
                                        }
                                }
                        }
                }
-       }
 
-       // In special cases, the ni file may exist in the dll location.
-       // The code below is to avoid this exceptional case.
-       std::string appPaths = __pm->getAppPaths();
-       splitPath(appPaths, paths);
-       for (const auto &path : paths) {
-               if (isDirectory(path)) {
-                       removeNIUnderDirs(path);
+               // In special cases, the ni file may exist in the dll location.
+               // The code below is to avoid this exceptional case.
+               std::string appPaths = __pm->getAppPaths();
+               splitPath(appPaths, paths);
+               for (const auto &path : paths) {
+                       if (isDirectory(path)) {
+                               removeNIUnderDirs(path);
+                       }
                }
        }
 
        return NI_ERROR_NONE;
 }
 
-ni_error_e regenerateAppNI(NIOption* opt)
+ni_error_e regeneratePkgNI(NIOption* opt)
 {
-       int ret = 0;
-       pkgmgrinfo_appinfo_metadata_filter_h handle;
        std::vector<std::string> pkgList;
+       ni_error_e ret = NI_ERROR_NONE;
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
-       if (ret != PMINFO_R_OK)
-               return NI_ERROR_UNKNOWN;
-
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE_TRUE);
-       if (ret != PMINFO_R_OK) {
-               pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-               return NI_ERROR_UNKNOWN;
+       // iterates for Packages's metadata (RPK)
+       pkgmgrinfo_pkginfo_metadata_filter_h pkgHandle;
+       if (pkgmgrinfo_pkginfo_metadata_filter_create(&pkgHandle) == PMINFO_R_OK) {
+               if (pkgmgrinfo_pkginfo_metadata_filter_add(pkgHandle, AOT_METADATA_KEY, METADATA_VALUE_TRUE) == PMINFO_R_OK) {
+                       if (pkgmgrPkgMDFilterForeach(pkgHandle, pkgAotCb, &pkgList) != 0) {
+                               ret = NI_ERROR_UNKNOWN;
+                               _ERR("pkgmgrPkgMDFilterForeach failed");
+                       }
+               } else {
+                       ret = NI_ERROR_UNKNOWN;
+                       _ERR("pkgmgrinfo_pkginfo_metadata_filter_add failed");
+               }
+               pkgmgrinfo_pkginfo_metadata_filter_destroy(pkgHandle);
+       } else {
+               ret = NI_ERROR_UNKNOWN;
+               _ERR("pkgmgrinfo_pkginfo_metadata_filter_create failed");
        }
 
-       ret = pkgmgrMDFilterForeach(handle, getPkgIdCb, &pkgList);
-       if (ret != 0) {
-               pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-               return NI_ERROR_UNKNOWN;
+       // iterate for App's metadata
+       pkgmgrinfo_appinfo_metadata_filter_h appHandle;
+       if (pkgmgrinfo_appinfo_metadata_filter_create(&appHandle) == PMINFO_R_OK) {
+               if (pkgmgrinfo_appinfo_metadata_filter_add(appHandle, AOT_METADATA_KEY, METADATA_VALUE_TRUE) == PMINFO_R_OK) {
+                       if (pkgmgrAppMDFilterForeach(appHandle, appAotCb, &pkgList) != 0) {
+                               ret = NI_ERROR_UNKNOWN;
+                               _ERR("pkgmgrAppMDFilterForeach failed");
+                       }
+               } else {
+                       ret = NI_ERROR_UNKNOWN;
+                       _ERR("pkgmgrinfo_appinfo_metadata_filter_add failed");
+               }
+               pkgmgrinfo_appinfo_metadata_filter_destroy(appHandle);
+       } else {
+               ret = NI_ERROR_UNKNOWN;
+               _ERR("pkgmgrinfo_appinfo_metadata_filter_create failed");
        }
 
        // remove duplicated pkg in the list.
@@ -1201,21 +1285,15 @@ ni_error_e regenerateAppNI(NIOption* opt)
                        continue;
                }
 
-               if (removeNIUnderPkgRoot(pkg) != NI_ERROR_NONE) {
+               if (removeAndCreateNI(pkg.c_str(), opt) != NI_ERROR_NONE) {
                        _SERR("Failed to remove previous dlls from [%s]", pkg.c_str());
-                       return NI_ERROR_UNKNOWN;
-               }
-
-               if (createNIUnderPkgRoot(pkg, opt) != NI_ERROR_NONE) {
-                       _SERR("Failed to generate NI file [%s]", pkg.c_str());
-                       return NI_ERROR_UNKNOWN;
+                       ret = NI_ERROR_UNKNOWN;
                } else {
                        _SOUT("Complete make application to native image");
                }
        }
 
-       pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-       return NI_ERROR_NONE;
+       return ret;
 }
 
 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
@@ -1290,7 +1368,7 @@ ni_error_e regenerateTACNI(NIOption* opt)
                return NI_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrMDFilterForeach(handle, regenTacCb, &opt);
+       ret = pkgmgrAppMDFilterForeach(handle, regenTacCb, &opt);
        if (ret != 0) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;