Refactoring the dotnettool (#231)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index 88cc6a1..ed30fdb 100644 (file)
@@ -387,6 +387,23 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
        return 0;
 }
 
+static bool isCoreLibPrepared(DWORD flags)
+{
+       if (flags & NI_FLAGS_ENABLER2R) {
+               return true;
+       }
+
+       std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
+       if (isFileExist(coreLibBackup)) {
+               return true;
+       } else {
+               fprintf(stderr, "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\n");
+               return false;
+       }
+}
+
 static void createCoreLibNI(DWORD flags)
 {
        std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
@@ -394,7 +411,6 @@ static void createCoreLibNI(DWORD flags)
        std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
 
        if (!isFileExist(coreLibBackup)) {
-
                if (!crossgen(coreLib, std::string(), flags)) {
                        if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
                                fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
@@ -448,19 +464,18 @@ void finalizeNICommon()
 }
 
 
-void createNiPlatform(DWORD flags)
+ni_error_e createNiPlatform(DWORD flags)
 {
+       createCoreLibNI(flags);
+
        const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
-       createNiUnderDirs(platformDirs, 2, flags);
+       return createNiUnderDirs(platformDirs, 2, flags);
 }
 
 ni_error_e createNiDll(const std::string& dllPath, DWORD flags)
 {
-       createCoreLibNI(flags);
-       // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
-       // Skip if input dll is System.Private.CoreLib.dll
-       if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
-               return NI_ERROR_NONE;
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
        }
 
        return crossgen(dllPath, std::string(), flags);
@@ -491,6 +506,10 @@ void createNiUnderTAC(std::vector<std::string> nugets, DWORD flags)
 
 ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
 {
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
                return NI_ERROR_INVALID_PACKAGE;
@@ -538,9 +557,11 @@ ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
        return NI_ERROR_NONE;
 }
 
-void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
+ni_error_e createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
 {
-       createCoreLibNI(flags);
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
 
        std::string appPaths;
        for (int i = 0; i < count; i++) {
@@ -574,6 +595,7 @@ void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
        }
 
        tpaAssemblies.clear();
+       return NI_ERROR_NONE;
 }
 
 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, DWORD flags)
@@ -589,13 +611,15 @@ ni_error_e createNiUnderPkgRoot(const std::string& pkgId, DWORD flags)
        std::string paths[] = {binDir, libDir, tacDir};
 
        flags |= NI_FLAGS_APPNI;
-       createNiUnderDirs(paths, 3, flags);
-
-       return NI_ERROR_NONE;
+       return createNiUnderDirs(paths, 3, flags);
 }
 
 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
 {
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
        std::string pkgRoot;
        if (getRootPath(pkgId, pkgRoot) < 0) {
                return NI_ERROR_INVALID_PACKAGE;
@@ -725,6 +749,10 @@ ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
 
 ni_error_e regenerateAppNI(DWORD flags)
 {
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
        int ret = 0;
        pkgmgrinfo_appinfo_metadata_filter_h handle;
 
@@ -785,6 +813,10 @@ static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
 
 ni_error_e regenerateTACNI(DWORD flags)
 {
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
        const std::string tacDir[] = {__DOTNET_DIR};
        removeNiUnderDirs(tacDir, 1);