AOT support for RPK
authorWoongsuk Cho <ws77.cho@samsung.com>
Wed, 31 Jan 2024 07:17:32 +0000 (16:17 +0900)
committerWoongsuk <ws77.cho@samsung.com>
Mon, 14 Apr 2025 02:12:09 +0000 (11:12 +0900)
The RPK (Resource Package) can contain dlls for resources and NUIGadget.
For RPK, Dlls can be located under lib and res/allowed directory.

AOT for RPK is enabled by metadata "http://tizen.org/metadata/prefer_dotnet_aot".
It triggered by metadata installer plugin and dotnettool(--ni-pkg, --ni-regen-all-app).

NativeLauncher/inc/ni_common.h
NativeLauncher/inc/utils.h
NativeLauncher/tool/dotnettool.cc
NativeLauncher/tool/ni_common.cc
NativeLauncher/tool/nitool.cc
NativeLauncher/tool/tac_common.cc
NativeLauncher/util/utils.cc

index 65b21f06541a717abd156cbfd86a11302e00ad76..9e89af935d5c69f3ff1421932f7d8e76e80394a7 100644 (file)
@@ -132,11 +132,11 @@ void removeNIUnderDirs(const std::string& rootPaths);
 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId);
 
 /**
- * @brief regenerate native images of all installed applications
+ * @brief regenerate native images of all installed packages (tpk, rpk)
  * @param[in] flags additional flags for the image generator
  * @return ni_error_e
  */
-ni_error_e regenerateAppNI(NIOption* opt);
+ni_error_e regeneratePkgNI(NIOption* opt);
 
 /**
  * @brief regenerate native image of TAC for all shared assembly.
index 927b213ac83bf884a4f0da9969db2154708fad07..3c07d8526e22acd93715cd862c2b2c9b416f63bf 100644 (file)
@@ -296,17 +296,29 @@ int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle);
 int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle);
 
 /**
- * @brief Executes the metadata filter query for all the installed packages.
+ * @brief Executes the metadata filter query for appinfo.
  *        This function is a wrapper of pkgmgrinfo_appinfo_metadata_filter_foreach() to handle multi-user case
  * @param[in] metadata filter handle
  * @param[in] callback function
  * @param[in] user data
  * @return 0 if success, otherwise -1
  */
-int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
+int pkgmgrAppMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
                                         pkgmgrinfo_app_list_cb app_cb,
                                         void *user_data);
 
+/**
+ * @brief Executes the metadata filter query for pkginfo.
+ *        This function is a wrapper of pkgmgrinfo_pkginfo_metadata_filter_foreach()
+ * @param[in] metadata filter handle
+ * @param[in] callback function
+ * @param[in] user data
+ * @return 0 if success, otherwise -1
+ */
+int pkgmgrPkgMDFilterForeach(pkgmgrinfo_pkginfo_metadata_filter_h handle,
+                                        pkgmgrinfo_pkg_list_cb app_cb,
+                                        void *user_data);
+
 /**
  * @brief Prints HW Clock log
  * @param[in] format `printf`-like format string
@@ -326,4 +338,18 @@ const char* getNCDBStartupHook();
  */
 bool isNCDBStartupHookProvided();
 
+/**
+ * @brief get paths where the resource dll is located in RPK package
+ * @param[in] root path
+ * @return std::string colon-separated string of rpk target paths
+ */
+std::string getResourcePaths(const std::string& rootPath);
+
+/**
+ * @brief check the package is rpk type
+ * @param[in] pkgId package ID
+ * @return bool
+ */
+bool isRPK(const std::string& pkgId);
+
 #endif /* __UTILS_H__ */
index 4343b49ae5df73f2d790b4133509228bffd1f7f5..49d0d704162da8f824c0ae11a3bdafefe67698d3 100644 (file)
@@ -321,7 +321,7 @@ int main(int argc, char* argv[])
                        return -1;
                }
 
-               int ret = regenerateAppNI(opt);
+               int ret = regeneratePkgNI(opt);
                if (ret != NI_ERROR_NONE) {
                        _SERR("Failed to regenerate all app NI");
                }
index 96247a896b11b2a0c11dfd640bb92905e782dae4..41aa18d5a8598ad9ebdcc13b196236e887cd5b89 100644 (file)
@@ -801,43 +801,75 @@ 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 appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
+static ni_error_e removeAndCreateNI(const char* pkgId, NIOption* pOptions)
+{
+       if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
+               _SERR("Failed to remove previous dlls from [%s]", pkgId);
+               return NI_ERROR_UNKNOWN;
+       }
+
+       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;   
+}
+
+static bool isReadOnlyPkg(std::string pkgId)
 {
-       char *pkgId = NULL;
        int ret = 0;
-       NIOption **pOptions = (NIOption**)userData;
-
-       if ((*pOptions)->flags & NI_FLAGS_SKIP_RO_APP) {
-               bool isSystem = false;
-               int ret = pkgmgrinfo_appinfo_is_system(handle, &isSystem);
-               if (ret != PMINFO_R_OK) {
-                       _SERR("Failed to check that app is System or not\n");
-                       return -1;
-               }
-               if (isSystem) {
-                       return 0;
-               }
+       bool readonly = false;
+       pkgmgrinfo_pkginfo_h handle;
+
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
+       if (ret != PMINFO_R_OK) {
+               _ERR("Fail to get pkginfo");
+               return false;
        }
 
-       ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+       ret = pkgmgrinfo_pkginfo_is_readonly(handle, &readonly);
        if (ret != PMINFO_R_OK) {
-               _SERR("Failed to get pkgid");
-               return -1;
+               _ERR("Fail to get is_readonly");
+               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+               return false;
        }
 
-       if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
-               _SERR("Failed to remove previous dlls from [%s]", pkgId);
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       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;
        }
 
-       if (createNIUnderPkgRoot(pkgId, *pOptions) != NI_ERROR_NONE) {
-               _SERR("Failed to generate NI file [%s]", pkgId);
+       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;
-       } else {
-               _SOUT("Complete make application to native image");
        }
 
+       std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
+       pkgList->push_back(pkgId);
+
        return 0;
 }
 
@@ -1033,6 +1065,8 @@ 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"
@@ -1046,31 +1080,40 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
                return NI_ERROR_INVALID_PACKAGE;
        }
 
-       __pm->setAppRootPath(rootPath);
-
        char* extraDllPaths = pluginGetExtraDllPath();
        if (extraDllPaths && extraDllPaths[0] != '\0') {
                opt->flags |= NI_FLAGS_EXTRA_REF;
                splitPath(extraDllPaths, opt->extraRefPath);
        }
 
-       opt->flags |= NI_FLAGS_APPNI;
-
-       if (isReadOnlyArea(rootPath)) {
-               opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
-               opt->flags |= NI_FLAGS_NO_PIPELINE;
-               std::string tmpPath = replaceAll(rootPath, getBaseName(rootPath), __READ_ONLY_APP_UPDATE_DIR);
-               if (!removeAll(tmpPath)){
-                       _SERR("Fail to remove RO App update path : %s", tmpPath.c_str());
+       if (isRPK(pkgId)) {
+               opt->flags &= ~NI_FLAGS_APPNI;
+               std::string paths = getResourcePaths(rootPath);
+               if (!paths.empty()) {
+                       ret = createNIUnderDirs(paths, opt);             
+               } else {
+                       _SERR("Failed to get rpk paths from [%s]", pkgId.c_str());
+                       ret = NI_ERROR_UNKNOWN;
                }
-               _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
        } else {
-               opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
+               __pm->setAppRootPath(rootPath);
+
+               opt->flags |= NI_FLAGS_APPNI;
+
+               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;
+               }
+
+               // create native image under bin and lib directory
+               // tac directory is skipped in the createNIUnderDirs.
+               ret = createNIUnderDirs(__pm->getAppPaths(), opt);
        }
 
-       // create native image under bin and lib directory
-       // tac directory is skipped in the createNIUnderDirs.
-       return createNIUnderDirs(__pm->getAppPaths(), opt);
+       return ret;
 }
 
 void removeNIPlatform()
@@ -1126,69 +1169,121 @@ 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;
+       // 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 = 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;
+       // 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");
        }
 
-       ret = pkgmgrMDFilterForeach(handle, appAotCb, &opt);
-       if (ret != 0) {
-               pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-               return NI_ERROR_UNKNOWN;
+       // remove duplicated pkg in the list.
+       // If one package has multiple apps, there can be duplicate values.
+       std::sort(pkgList.begin(), pkgList.end());
+       pkgList.erase(unique(pkgList.begin(), pkgList.end()), pkgList.end());
+
+       for (auto pkg : pkgList) {
+               if (isReadOnlyPkg(pkg) && opt->flags & NI_FLAGS_SKIP_RO_APP) {
+                       continue;
+               }
+
+               if (removeAndCreateNI(pkg.c_str(), opt) != NI_ERROR_NONE) {
+                       _SERR("Failed to remove previous dlls from [%s]", pkg.c_str());
+                       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"
@@ -1263,7 +1358,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;
index 943852e50df41aad9b89b1025e9657135bde538f..f323f7ae979020821789c972dac0ae8a94781e96 100644 (file)
@@ -85,7 +85,7 @@ int main(int argc, char* argv[])
        } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
                rmPkgMode = true;
        } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
-               regenerateAppNI(opt->flags);
+               regeneratePkgNI(opt->flags);
                return 0;
        } else {
                help(argv[0]);
index dcd5c1d954bc5380671826285db45302fbac5db0..cffea6dfe1e2c94c30f19b4bf63f6bfb7465739b 100644 (file)
@@ -168,7 +168,7 @@ tac_error_e tac_restoreDB()
                return TAC_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrMDFilterForeach(handle, tac_restoreDBCb, NULL);
+       ret = pkgmgrAppMDFilterForeach(handle, tac_restoreDBCb, NULL);
        if (ret != 0) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return TAC_ERROR_UNKNOWN;
@@ -515,7 +515,7 @@ tac_error_e tlc_restoreDB()
                return TAC_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL);
+       ret = pkgmgrAppMDFilterForeach(handle, tlc_restoreDBCb, NULL);
        if (ret != 0) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return TAC_ERROR_UNKNOWN;
index 963cb7f4f4747c806f9d9f25338194bf9e46791e..36d0a810b5b93bd4e0f5b155883da98011fe05be 100644 (file)
@@ -779,7 +779,7 @@ int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle)
        return 0;
 }
 
-int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
+int pkgmgrAppMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
                                                        pkgmgrinfo_app_list_cb app_cb,
                                                        void *user_data)
 {
@@ -791,7 +791,6 @@ int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
                return -1;
        }
 
-
        ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, uid);
        if (ret != PMINFO_R_OK) {
                _ERR("Failed to execute the metadata filter query (%d)", ret);
@@ -801,6 +800,21 @@ int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
        return 0;
 }
 
+int pkgmgrPkgMDFilterForeach(pkgmgrinfo_pkginfo_metadata_filter_h handle,
+                                                       pkgmgrinfo_pkg_list_cb pkg_cb,
+                                                       void *user_data)
+{
+       int ret = 0;
+
+       ret = pkgmgrinfo_pkginfo_metadata_filter_foreach(handle, pkg_cb, user_data);
+       if (ret != PMINFO_R_OK) {
+               _ERR("Failed to execute the metadata filter query (%d)", ret);
+               return -1;
+       }
+
+       return 0;
+}
+
 void printHWClockLog(const char* format, ...)
 {
        char buf[1024] = {0,};
@@ -851,3 +865,33 @@ bool isNCDBStartupHookProvided()
 
        return false;
 }
+
+std::string getResourcePaths(const std::string& rootPath)
+{
+       return rootPath + "/lib" + ":" + rootPath + "/res/allowed";
+}
+
+bool isRPK(const std::string& pkgId)
+{
+       char *type = NULL;
+       pkgmgrinfo_pkginfo_h pkg_handle;
+       int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
+       if (ret != 0) {
+               return false;
+       }
+       ret = pkgmgrinfo_pkginfo_get_type(pkg_handle, &type);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return false;
+       }
+
+       if (strncmp("rpk", type, 3) == 0) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+               return true;
+       }
+
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
+
+       return false;
+}
+