Exchange .ni.dll and .dll extensions of RPK app
authorj-h.choi <j-h.choi@samsung.com>
Fri, 11 Apr 2025 05:43:41 +0000 (14:43 +0900)
committer조웅석/MDE Lab(SR)/삼성전자 <ws77.cho@samsung.com>
Wed, 16 Apr 2025 08:22:18 +0000 (17:22 +0900)
NativeLauncher/inc/ni_common.h
NativeLauncher/tool/ni_common.cc

index 7189b345f86298e9661be7e5624f73594fd6fce1..e0f04bfd5c7121140a657ef5d2645b88bc566aaa 100644 (file)
@@ -65,6 +65,7 @@ typedef struct NIOption{
        std::vector<std::string> mibcPath;
        int priority;
        std::string threadNum;
+       bool isRPK = false;
 } NIOption;
 
 /**
@@ -122,10 +123,9 @@ ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt);
  * @brief create native images for all DLLs in a package with specified path.
  * @param[in] rootPath path of package
  * @param[in] flags additional flags for the image generator
- * @parma[in] rpk if true, generate RPK native image (default value is false)
  * @return ni_error_e
  */
-ni_error_e createNIUnderPkgRootWithPath(const std::string& rootPath, NIOption* opt, bool rpk = false);
+ni_error_e createNIUnderPkgRootWithPath(const std::string& rootPath, NIOption* opt);
 
 /**
  * @brief remove platform native images (.NETCore + TizenFX)
index 8ba02d27a82ac3648b59f35320532d5f4a2be65b..ca659ceaa9fe92d891cc0640af645fbf5c069cf0 100644 (file)
@@ -717,6 +717,36 @@ static void removeAppNITmpPath(NIOption* opt)
        }
 }
 
+static void swapExtensionRpkFiles(std::vector<std::string>& paths, NIOption* opt)
+{
+       std::vector<std::string> dllList;
+       std::vector<std::string> niList;
+       std::string path = paths.back();
+       try {
+               for (auto& file : bf::recursive_directory_iterator(path)) {
+                       std::string filePath = file.path().string();
+               if (isNativeImage(filePath)) {
+                               niList.push_back(filePath);
+               }
+                       else if (isManagedAssembly(filePath)) {
+                               dllList.push_back(filePath);
+                       }
+               }
+       } catch (const bf::filesystem_error& error) {
+               _ERR("Failed to recursive directory: %s", error.what());
+               return;
+       }
+
+       for (auto& dll : dllList) {
+               rename(dll.c_str(), changeExtension(dll, ".dll", ".dll.backup").c_str());
+       }
+       for (auto& ni : niList) {
+               std::string dll = changeExtension(ni, ".ni.dll", ".dll");
+               bf::create_symlink(getFileName(ni), dll);
+               copySmackAndOwnership(ni, dll, true);
+       }
+}
+
 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
 {
        ni_error_e ret = NI_ERROR_NONE;
@@ -832,6 +862,10 @@ static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string
                renameAppNITmpPath(opt);
        }
 
+       if (opt->isRPK) {
+               swapExtensionRpkFiles(paths, opt);
+       }
+
        return ret;
 }
 
@@ -1096,11 +1130,12 @@ 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;
        }
+       opt->isRPK = isRPK(pkgId);
 
-       return createNIUnderPkgRootWithPath(rootPath, opt, isRPK(pkgId));
+       return createNIUnderPkgRootWithPath(rootPath, opt);
 }
 
-ni_error_e createNIUnderPkgRootWithPath(const std::string& rootPath, NIOption* opt, bool rpk)
+ni_error_e createNIUnderPkgRootWithPath(const std::string& rootPath, NIOption* opt)
 {
        if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
                _SERR("The native image of System.Private.CoreLib does not exist.\n"
@@ -1122,7 +1157,7 @@ ni_error_e createNIUnderPkgRootWithPath(const std::string& rootPath, NIOption* o
        }
 
        std::string targetDirs;
-       if (rpk) {
+       if (opt->isRPK) {
                opt->flags &= ~NI_FLAGS_APPNI; // added to exclude logic of APP_NI
                opt->flags |= NI_FLAGS_NO_PIPELINE; // added the flag to set the output path
 
@@ -1189,15 +1224,31 @@ void removeNIPlatform()
 void removeNIUnderDirs(const std::string& rootPaths)
 {
        auto convert = [](const std::string& path, const std::string& filename) {
-               //The .ni.dll file can be renamed .dll.
-               if (isR2RImage(path) && isNativeImage(path)) {
-                       std::string assemblyPath = changeExtension(path, ".ni.dll", ".dll");
-                       if (exist(assemblyPath)) {
-                               if (remove(path.c_str())) {
-                                       _SERR("Failed to remove %s", path.c_str());
+               std::string assemblyBackupPath = changeExtension(path, ".ni.dll", ".dll.backup");
+               if (exist(assemblyBackupPath)) {
+                       // The RPK type is that .dll is a .ni.dll file and .dll.backup is a .dll file.
+                       // So, change the name of the .dll.backup file to the .dll file.
+                       if (strstr(filename.c_str(), ".dll.backup") != NULL) {
+                               std::string originPath = changeExtension(path, ".dll.backup", ".dll");
+                               std::string niFile = changeExtension(path, ".dll.backup", ".ni.dll");
+                               if (rename(path.c_str(), originPath.c_str())) {
+                                       _SERR("Failed to rename %s", path.c_str());
+                               }
+                               if (remove(niFile.c_str())) {
+                                       _SERR("Failed to remove %s", niFile.c_str());
+                               }
+                       }
+               } else {
+                       //The .ni.dll file can be renamed .dll.
+                       if (isR2RImage(path) && isNativeImage(path)) {
+                               std::string assemblyPath = changeExtension(path, ".ni.dll", ".dll");
+                               if (exist(assemblyPath)) {
+                                       if (remove(path.c_str())) {
+                                               _SERR("Failed to remove %s", path.c_str());
+                                       }
+                               } else {
+                                       _SOUT("%s cannot be removed because there is no %s", path.c_str(), assemblyPath.c_str());
                                }
-                       } else {
-                               _SOUT("%s cannot be removed because there is no %s", path.c_str(), assemblyPath.c_str());
                        }
                }
        };