Remove code that create SPC NI first (#373)
author조웅석/Common Platform Lab(SR)/삼성전자 <ws77.cho@samsung.com>
Wed, 9 Feb 2022 05:37:26 +0000 (14:37 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 9 Feb 2022 05:37:26 +0000 (14:37 +0900)
* Add isR2RImage insteadof ildasm

Use internal API to reduce unnecessary fork

* Remove code that create SPC NI first.

crossgen2 can do AOTC without SPC native image.
So, remove unnecessary code.

NativeLauncher/tool/dotnettool.cc
NativeLauncher/tool/ni_common.cc
tests/TCs/Utils.py

index e05076c..4ecba24 100644 (file)
@@ -211,7 +211,7 @@ int main(int argc, char* argv[])
                while (it != args.end()) {
                        std::string dll = std::string(*it);
                        int ret = createNIDll(dll, opt);
-                       if (ret != NI_ERROR_NONE) {
+                       if (ret != NI_ERROR_NONE && ret != NI_ERROR_ALREADY_EXIST) {
                                _SERR("Failed to generate NI file [%s]", dll.c_str());
                                break;
                        }
index 88fb06c..c4b535a 100644 (file)
@@ -102,38 +102,6 @@ NIOption* getNIOption()
        return __ni_option;
 }
 
-static bool isCoreLibPrepared()
-{
-       std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
-       if (isFile(coreLibBackup)) {
-               return true;
-       } else {
-               _SERR("The native image of System.Private.CoreLib does not exist\n"
-                                       "Run the command to create the native image\n"
-                                       "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n");
-               return false;
-       }
-}
-
-static bool hasCoreLibNI()
-{
-       std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
-       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
-       std::string cmd = ildasm + " " + coreLib + " -noil -stats | grep '.rsrc'";
-
-       FILE *fp;
-       fp = popen(cmd.c_str(), "r");
-       if (fp != NULL) {
-               char buff[1024];
-               if (fgets(buff, sizeof(buff), fp) != NULL) {
-                       pclose(fp);
-                       return false;
-               }
-               pclose(fp);
-       }
-       return true;
-}
-
 static void waitInterval()
 {
        // by the recommand, ignore small value for performance.
@@ -239,9 +207,7 @@ static bool checkNIExistence(const std::string& absDllPath)
        // native image of System.Private.CoreLib.dll should have to overwrite
        // original file to support new coreclr
        if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
-               if (hasCoreLibNI()) {
-                       return true;
-               }
+               return isR2RImage(absDllPath);
        }
 
        return false;
@@ -265,8 +231,6 @@ static bool checkDllExistInDir(const std::string& path)
  * Get the list of managed files in the specific directory
  * Absolute paths of managed files are stored at the result list.
  * If native image already exist in the same directory, managed file is ignored.
- *
- * Note: System.Private.CoreLib is excluded from the result list.
  */
 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
 {
@@ -276,9 +240,7 @@ static ni_error_e getTargetDllList(const std::string& path, std::vector<std::str
 
        auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
                if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
-                       if (f_path.find("System.Private.CoreLib") == std::string::npos) {
-                               fileList.push_back(getAbsolutePath(f_path));
-                       }
+                       fileList.push_back(getAbsolutePath(f_path));
                }
        };
 
@@ -522,14 +484,47 @@ static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, c
        return NI_ERROR_NONE;
 }
 
+static ni_error_e createCoreLibNI(NIOption* opt)
+{
+       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
+       std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
+       std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
+
+       std::vector<std::string> dllList;
+       std::vector<std::string> refPaths;
+       dllList.push_back(getAbsolutePath(coreLib));
+
+       if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
+               if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE && exist(niCoreLib)) {
+                       if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
+                               _SERR("Failed to rename System.Private.CoreLib.dll");
+                               return NI_ERROR_CORE_NI_FILE;
+                       }
+                       if (rename(niCoreLib.c_str(), coreLib.c_str())) {
+                               _SERR("Failed to rename System.Private.CoreLib.ni.dll");
+                               return NI_ERROR_CORE_NI_FILE;
+                       }
+               } else {
+                       _SERR("Failed to create native image for %s", coreLib.c_str());
+                       return NI_ERROR_CORE_NI_FILE;
+               }
+       }
+       return NI_ERROR_NONE;
+}
+
 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
 {
+       ni_error_e ret = NI_ERROR_NONE;
+
        if (dllList.empty()) {
                return NI_ERROR_INVALID_PARAMETER;
        }
        // When performing AOT for one Dll, an error is returned when an error occurs.
        // However, when processing multiple dlls at once, only the log for errors is output and skipped.
 
+       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
+       bool hasSPC = false;
+
        for (auto it = dllList.begin(); it != dllList.end(); it++) {
                std::string f = *it;
                if (!isFile(f)) {
@@ -540,18 +535,43 @@ static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string
                        _SERR("Input file is not a dll file : %s", f.c_str());
                        dllList.erase(it--);
                }
+               // handle System.Private.CoreLib.dll separately.
+               // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
+               if (f == coreLib) {
+                       hasSPC = true;
+                       dllList.erase(it--);
+               }
+       }
+
+       // In the case of SPC, post-processing is required to change the name of the native image.
+       // In order to avoid repeatedly checking whether the generated native image is an SPC,
+       // the SPC native image generation is performed separately.
+       if (hasSPC) {
+               ret = createCoreLibNI(opt);
+               if (ret != NI_ERROR_NONE) {
+                       return ret;
+               }
+       }
+
+       // if there is no proper input after processing dll list
+       if (dllList.empty()) {
+               if (hasSPC) {
+                       return ret;
+               } else {
+                       return NI_ERROR_INVALID_PARAMETER;
+               }
        }
 
        std::vector<std::string> paths;
        splitPath(refPaths, paths);
 
        if (opt->flags & NI_FLAGS_NO_PIPELINE) {
-               crossgen2NoPipeLine(dllList, paths, opt);
+               ret = crossgen2NoPipeLine(dllList, paths, opt);
        } else {
-               crossgen2PipeLine(dllList, paths, opt);
+               ret = crossgen2PipeLine(dllList, paths, opt);
        }
 
-       return NI_ERROR_NONE;
+       return ret;
 }
 
 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
@@ -566,6 +586,11 @@ static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPa
                return NI_ERROR_INVALID_PARAMETER;
        }
 
+       if (checkNIExistence(dllFile)) {
+               _SERR("Native image file is already exist : %s", dllFile.c_str());
+               return NI_ERROR_ALREADY_EXIST;
+       }
+
        std::vector<std::string> dllList;
        dllList.push_back(getAbsolutePath(dllFile));
        return doAOTList(dllList, refPaths, opt);
@@ -599,30 +624,6 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
        return 0;
 }
 
-static ni_error_e createCoreLibNI(NIOption* opt)
-{
-       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
-       std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
-       std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
-
-       if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
-               if (doAOTFile(coreLib, std::string(), opt) == NI_ERROR_NONE && exist(niCoreLib)) {
-                       if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
-                               _SERR("Failed to rename System.Private.CoreLib.dll");
-                               return NI_ERROR_CORE_NI_FILE;
-                       }
-                       if (rename(niCoreLib.c_str(), coreLib.c_str())) {
-                               _SERR("Failed to rename System.Private.CoreLib.ni.dll");
-                               return NI_ERROR_CORE_NI_FILE;
-                       }
-               } else {
-                       _SERR("Failed to create native image for %s", coreLib.c_str());
-                       return NI_ERROR_CORE_NI_FILE;
-               }
-       }
-       return NI_ERROR_NONE;
-}
-
 ni_error_e initNICommon()
 {
 #if defined(__arm__) || defined(__aarch64__)
@@ -689,12 +690,7 @@ void finalizeNICommon()
 
 ni_error_e createNIPlatform(NIOption* opt)
 {
-       ni_error_e ret = createCoreLibNI(opt);
-       if (ret != NI_ERROR_NONE) {
-               return ret;
-       }
-
-       ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
+       ni_error_e ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
        if (ret != NI_ERROR_NONE) {
                return ret;
        }
@@ -704,14 +700,6 @@ ni_error_e createNIPlatform(NIOption* opt)
 
 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
 {
-       if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
-               return createCoreLibNI(opt);
-       }
-
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
-
        return doAOTFile(dllPath, std::string(), opt);
 }
 
@@ -766,9 +754,6 @@ ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& re
 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
 {
        ni_error_e ret = NI_ERROR_NONE;
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
 
        std::vector<std::string> fileList;
        std::vector<std::string> paths;
@@ -834,7 +819,7 @@ void removeNIPlatform()
        std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
        std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
 
-       if (hasCoreLibNI()) {
+       if (isR2RImage(coreLib)) {
                if (!isFile(coreLibBackup)) {
                        return;
                }
@@ -919,10 +904,6 @@ ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
 
 ni_error_e regenerateAppNI(NIOption* opt)
 {
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
-
        int ret = 0;
        pkgmgrinfo_appinfo_metadata_filter_h handle;
 
@@ -991,10 +972,6 @@ static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
 
 ni_error_e regenerateTACNI(NIOption* opt)
 {
-       if (!isCoreLibPrepared()) {
-               return NI_ERROR_CORE_NI_FILE;
-       }
-
        removeNIUnderDirs(__DOTNET_DIR);
 
        pkgmgrinfo_appinfo_metadata_filter_h handle;
index ae6a785..0a25754 100755 (executable)
@@ -114,7 +114,7 @@ def create_spc_ni():
     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")
     if f"{RUNTIME_DIR}{SPC_DLL}.Backup" not in raw:
         raw = cmd(f"shell dotnettool --ni-dll {RUNTIME_DIR}{SPC_DLL}")
-        if "System.Private.CoreLib.dll generated successfully." not in raw:
+        if "System.Private.CoreLib.ni.dll generated successfully." not in raw:
             return "FAIL"
 
     raw = cmd(f"shell find {RUNTIME_DIR} -name {SPC_DLL}.Backup")