Code cleanup (#251)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index 6fc442f..a45043e 100644 (file)
@@ -55,7 +55,7 @@
 #define __STR(x) __XSTR(x)
 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
-static const char* __TAC_DIR = __STR(TAC_DIR);
+static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
 
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
@@ -71,12 +71,12 @@ static void waitInterval()
 {
        // by the recommand, ignore small value for performance.
        if (__interval > 10000) {
-               fprintf(stderr, "sleep %d usec\n", __interval);
+               fprintf(stdout, "sleep %d usec\n", __interval);
                usleep(__interval);
        }
 }
 
-static std::string getNiFilePath(const std::string& dllPath)
+static std::string getNIFilePath(const std::string& dllPath)
 {
        size_t index = dllPath.find_last_of(".");
        if (index == std::string::npos) {
@@ -110,9 +110,9 @@ static std::string getAppNIPath(const std::string& niPath)
 
        niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
 
-       if (!isFileExist(niDirPath)) {
+       if (!isFile(niDirPath)) {
                if (mkdir(niDirPath.c_str(), 0755) == 0) {
-                       updateAssemblyInfo(prevPath, niDirPath);
+                       copySmackAndOwnership(prevPath, niDirPath);
                } else {
                        fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
                }
@@ -121,14 +121,14 @@ static std::string getAppNIPath(const std::string& niPath)
        return concatPath(niDirPath, fileName);
 }
 
-static bool niExist(const std::string& path)
+static bool checkNIExistence(const std::string& path)
 {
-       std::string f = getNiFilePath(path);
+       std::string f = getNIFilePath(path);
        if (f.empty()) {
                return false;
        }
 
-       if (isFileExist(f)) {
+       if (isFile(f)) {
                return true;
        }
 
@@ -136,7 +136,7 @@ static bool niExist(const std::string& path)
        // original file to support new coreclr
        if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
                std::string coreLibBackup = path + ".Backup";
-               if (isFileExist(coreLibBackup)) {
+               if (isFile(coreLibBackup)) {
                        return true;
                }
        }
@@ -145,6 +145,17 @@ static bool niExist(const std::string& path)
 }
 
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+static uintptr_t getFileSize(const std::string& path)
+{
+       struct stat sb;
+
+       if (stat(path.c_str(), &sb) == 0) {
+               return sb.st_size;
+       }
+
+       return 0;
+}
+
 // Get next base address to be used for system ni image from file
 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
 static uintptr_t getNextBaseAddrFromFile()
@@ -171,7 +182,7 @@ static uintptr_t getNextBaseAddr()
 {
        uintptr_t baseAddr = 0;
 
-       if (!isFileExist(__SYSTEM_BASE_FILE)) {
+       if (!isFile(__SYSTEM_BASE_FILE)) {
                // This is the starting address for all default base addresses
                baseAddr = DEFAULT_BASE_ADDR_START;
        } else {
@@ -186,11 +197,11 @@ static uintptr_t getNextBaseAddr()
 }
 
 // Save base address of system ni image to file
-static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
+static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
 {
-       uintptr_t niSize = getFileSize(absNiPath);
+       uintptr_t niSize = getFileSize(absNIPath);
        if (niSize == 0) {
-               fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
+               fprintf(stderr, "File %s doesn't exist\n", absNIPath.c_str());
                return;
        }
 
@@ -206,9 +217,9 @@ static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
 }
 
 // check if dll is listed in TPA
-static bool isTPADll(const std::string &dllPath)
+static bool isTPADll(const std::stringdllPath)
 {
-       std::string absDllPath = absolutePath(dllPath);
+       std::string absDllPath = getAbsolutePath(dllPath);
 
        if (__tpa.find(absDllPath) != std::string::npos) {
                return true;
@@ -221,31 +232,33 @@ static bool isTPADll(const std::string &dllPath)
 // baseAddr should be checked in file before getting here
 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
 {
-       if (!isFileExist(dllPath)) {
+       if (!isFile(dllPath)) {
                fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
                return NI_ERROR_NO_SUCH_FILE;
        }
 
        if (!isManagedAssembly(dllPath)) {
-               fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
+               //fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
                return NI_ERROR_INVALID_PARAMETER;
        }
 
-       if (niExist(dllPath)) {
+       if (checkNIExistence(dllPath)) {
                fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
                return NI_ERROR_ALREADY_EXIST;
        }
 
-       std::string absDllPath = absolutePath(dllPath);
-       std::string absNiPath = getNiFilePath(dllPath);
-       if (absNiPath.empty()) {
+       std::string absDllPath = getAbsolutePath(dllPath);
+       std::string absNIPath = getNIFilePath(dllPath);
+
+
+       if (absNIPath.empty()) {
                fprintf(stderr, "Fail to get ni file name\n");
                return NI_ERROR_UNKNOWN;
        }
 
        bool isAppNI = flags & NI_FLAGS_APPNI;
-       if (isAppNI && strstr(absNiPath.c_str(), __TAC_DIR) == NULL) {
-               absNiPath = getAppNIPath(absNiPath);
+       if (isAppNI && strstr(absNIPath.c_str(), __DOTNET_DIR) == NULL) {
+               absNIPath = getAppNIPath(absNIPath);
        }
 
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
@@ -264,13 +277,22 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
                int status;
                waitpid(pid, &status, 0);
                if (WIFEXITED(status)) {
-                       // Do not use niExist() function to check whether ni file created or not.
-                       // niEixst() return false for System.Private.Corelib.dll
-                       if (isFileExist(absNiPath)) {
-                               updateAssemblyInfo(absDllPath, absNiPath);
+                       // Do not use checkNIExistence() function to check whether ni file created or not.
+                       // checkNIExistence() return false for System.Private.Corelib.dll
+                       if (isFile(absNIPath)) {
+                               copySmackAndOwnership(absDllPath, absNIPath);
+                               std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
+                               std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
+                               if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
+                                       if (!copyFile(absPdbPath, pdbFilePath)) {
+                                               fprintf(stderr, "Failed to copy a .pdb file\n");
+                                       } else {
+                                               copySmackAndOwnership(absPdbPath, pdbFilePath);
+                                       }
+                               }
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
                                if (baseAddr != 0) {
-                                       updateBaseAddrFile(absNiPath, baseAddr);
+                                       updateBaseAddrFile(absNIPath, baseAddr);
                                }
 #endif
                                return NI_ERROR_NONE;
@@ -316,17 +338,17 @@ static ni_error_e crossgen(const std::string& dllPath, const std::string& appPat
                if (!appPath.empty()) {
                        absAppPath = appPath;
                } else {
-                       absAppPath = baseName(absDllPath);
+                       absAppPath = getBaseName(absDllPath);
                }
                argv.push_back(absAppPath.c_str());
 
                argv.push_back("/out");
-               argv.push_back(absNiPath.c_str());
+               argv.push_back(absNIPath.c_str());
 
                argv.push_back(absDllPath.c_str());
                argv.push_back(nullptr);
 
-               fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
+               fprintf(stdout, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
 
                execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
                exit(0);
@@ -348,7 +370,7 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
                return -1;
        }
 
-       if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
+       if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
                fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
                return -1;
        }
@@ -359,52 +381,96 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
        }
 
        // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
-       if (createNiUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
+       if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
                fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
                return -1;
        } else {
-               fprintf(stderr, "Complete make application to native image\n");
+               fprintf(stdout, "Complete make application to native image\n");
        }
 
        if (createTACPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
                fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
                return -1;
-       }else {
-               fprintf(stderr, "Complete make symbolic link file to tac\n");
+       } else {
+               fprintf(stdout, "Complete make symbolic link file to tac\n");
        }
 
        return 0;
 }
 
-static void createCoreLibNI(DWORD flags)
+static bool isCoreLibPrepared(DWORD flags)
+{
+       if (flags & NI_FLAGS_ENABLER2R) {
+               return true;
+       }
+
+       std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
+       if (isFile(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 bool hasCoreLibNI()
+{
+       FILE *fp;
+       char buff[1024];
+       std::string ildasm = concatPath(getRuntimeDir(), "ildasm");
+       std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
+       std::string cmd = ildasm + " " + coreLib + " | grep '\\.corflags'";
+       fp = popen(cmd.c_str(), "r");
+       if (fp != NULL) {
+               while (fgets(buff, sizeof(buff), fp) != NULL) {
+                       buff[strlen(buff) - 1] = '\0';
+               }
+               std::string corflag = replaceAll(buff, ".corflags", "");
+               corflag.erase(std::remove(corflag.begin(), corflag.end(), ' '), corflag.end());
+               // CorFlags.ILLibrary=0x00000004 (.ni.dll)
+               if (!strcmp(corflag.substr(0, 10).c_str(), "0x00000004")) {
+                       pclose(fp);
+                       return true;
+               }
+               pclose(fp);
+       }
+       return false;
+}
+
+static ni_error_e createCoreLibNI(DWORD flags)
 {
        std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
        std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
        std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
 
-       if (!isFileExist(coreLibBackup)) {
-
+       if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
                if (!crossgen(coreLib, std::string(), flags)) {
                        if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
                                fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
+                               return NI_ERROR_CORE_NI_FILE;
                        }
                        if (rename(niCoreLib.c_str(), coreLib.c_str())) {
                                fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
+                               return NI_ERROR_CORE_NI_FILE;
                        }
                } else {
                        fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
+                       return NI_ERROR_CORE_NI_FILE;
                }
        }
+       return NI_ERROR_NONE;
 }
 
-ni_error_e initNICommon(NiCommonOption* option)
+ni_error_e initNICommon(NICommonOption* option)
 {
 #if defined(__arm__)
        // get interval value
        const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
        std::ifstream inFile(intervalFile);
        if (inFile) {
-               fprintf(stderr, "crossgen_interval.txt is found\n");
+               fprintf(stdout, "crossgen_interval.txt is found\n");
                inFile >> __interval;
        }
 
@@ -436,39 +502,41 @@ void finalizeNICommon()
        __tpa.clear();
 }
 
-
-void createNiPlatform(DWORD flags)
+ni_error_e createNIPlatform(DWORD flags)
 {
+       if (createCoreLibNI(flags) != NI_ERROR_NONE) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
        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)
+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;
+               return createCoreLibNI(flags);
        }
 
-       ni_error_e status = crossgen(dllPath, std::string(), flags);
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
 
-       return status;
+       return crossgen(dllPath, std::string(), flags);
 }
 
-void createNiUnderTAC(std::vector<std::string> nugets, DWORD flags)
+static void createNIUnderTAC(const std::vector<std::string>& nugets, DWORD flags)
 {
        std::string appPaths;
        for (auto& nuget : nugets) {
-               appPaths += concatPath(__TAC_DIR, nuget);
+               appPaths += concatPath(__DOTNET_DIR, nuget);
                appPaths += ':';
        }
        if (appPaths.back() == ':') {
                appPaths.pop_back();
        }
 
-       auto convert = [&appPaths, flags](const std::string& path, const char* name) {
+       auto convert = [&appPaths, flags](const std::string& path, const std::string& filename) {
                if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
                        return;
                if (!crossgen(path, appPaths.c_str(), flags)) {
@@ -476,20 +544,27 @@ void createNiUnderTAC(std::vector<std::string> nugets, DWORD flags)
                }
        };
        for (auto& nuget : nugets) {
-               scanFilesInDir(concatPath(__TAC_DIR, nuget), convert, -1);
+               scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
        }
 }
 
 ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
 {
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
+       std::string rootPath = getRootPath(pkgId);
+       if (rootPath.empty()) {
+               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
-       std::string binDir = concatPath(pkgRoot, "bin");
+       std::string binDir = concatPath(rootPath, "bin");
+       std::string libDir = concatPath(rootPath, "lib");
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
+       std::string paths = binDir + ":" + libDir + ":" + tacDir;
        if (bf::exists(tacDir)) {
                try {
                        for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
@@ -497,23 +572,23 @@ ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
                                if (bf::is_symlink(symPath)) {
                                        if (!isNativeImage(symPath)) {
                                                std::string originPath = bf::read_symlink(symPath).string();
-                                               std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
-                                               if (!bf::exists(originNiPath)) {
-                                                       if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
+                                               std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
+                                               if (!bf::exists(originNIPath)) {
+                                                       flags |= NI_FLAGS_APPNI;
+                                                       if (crossgen(originPath, paths, flags) != NI_ERROR_NONE) {
                                                                fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
                                                                return NI_ERROR_UNKNOWN;
                                                        }
                                                }
                                                std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
                                                if (!bf::exists(symNIPath)) {
-                                                       bf::create_symlink(originNiPath, symNIPath);
-                                                       fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
-                                                       updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
+                                                       bf::create_symlink(originNIPath, symNIPath);
+                                                       fprintf(stdout, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
+                                                       copySmackAndOwnership(tacDir.c_str(), symNIPath.c_str(), true);
 
-                                                       std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
-                                                       if (!removeFile(concatPath(binNIDir, NIFileName))) {
-                                                               fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
-                                                               return NI_ERROR_UNKNOWN;
+                                                       std::string niFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
+                                                       if (!removeFile(concatPath(binNIDir, niFileName))) {
+                                                               fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, niFileName).c_str());
                                                        }
                                                }
                                        }
@@ -527,9 +602,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++) {
@@ -543,12 +620,12 @@ void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
        std::vector<std::string> tpaAssemblies;
        splitPath(__tpa, tpaAssemblies);
 
-       auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const char* name) {
+       auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const std::string& filename) {
                bool isAppNI = flags & NI_FLAGS_APPNI;
                if (isAppNI) {
                        for (auto& tpa : tpaAssemblies) {
-                               if (!strcmp(replaceAll(tpa.substr(tpa.rfind('/') + 1), ".ni.dll", ".dll").c_str(), name)) {
-                                       fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", name);
+                               if (!strcmp(replaceAll(tpa.substr(tpa.rfind('/') + 1), ".ni.dll", ".dll").c_str(), filename.c_str())) {
+                                       fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", filename.c_str());
                                        return;
                                }
                        }
@@ -559,58 +636,69 @@ void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
        };
 
        for (int i = 0; i < count; i++) {
-               scanFilesInDir(rootPaths[i], convert, 1);
+               scanFilesInDirectory(rootPaths[i], convert, 0);
        }
 
        tpaAssemblies.clear();
+       return NI_ERROR_NONE;
 }
 
-ni_error_e createNiUnderPkgRoot(const std::string& pkgId, DWORD flags)
+ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
 {
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
+       std::string rootPath = getRootPath(pkgId);
+       if (rootPath.empty()) {
+               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string libDir = concatPath(pkgRoot, "lib");
+       std::string binDir = concatPath(rootPath, "bin");
+       std::string libDir = concatPath(rootPath, "lib");
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        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)
+ni_error_e createNIDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
 {
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
+       std::string rootPath = getRootPath(pkgId);
+       if (rootPath.empty()) {
+               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string libDir = concatPath(pkgRoot, "lib");
+       std::string binDir = concatPath(rootPath, "bin");
+       std::string libDir = concatPath(rootPath, "lib");
        std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
+       std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
        std::string paths = binDir + ":" + libDir + ":" + tacDir;
 
        if (bf::is_symlink(dllPath)) {
                if (bf::exists(tacDir)) {
                        if (!isNativeImage(dllPath)) {
                                std::string originPath = bf::read_symlink(dllPath).string();
-                               std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
-                               if (!bf::exists(originNiPath)) {
-                                       if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
+                               std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
+                               if (!bf::exists(originNIPath)) {
+                                       flags |= NI_FLAGS_APPNI;
+                                       if (crossgen(originPath, paths, flags) != NI_ERROR_NONE) {
                                                fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
                                                return NI_ERROR_UNKNOWN;
                                        }
                                }
-                               std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
-                               if (!bf::exists(setNiPath)) {
-                                       bf::create_symlink(originNiPath, setNiPath);
-                                       fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
-                                       updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
+                               std::string setNIPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
+                               if (!bf::exists(setNIPath)) {
+                                       bf::create_symlink(originNIPath, setNIPath);
+                                       fprintf(stdout, "%s symbolic link file generated successfully.\n", setNIPath.c_str());
+                                       copySmackAndOwnership(tacDir.c_str(), setNIPath.c_str(), true);
+                               }
+                               std::string niFileName = setNIPath.substr(setNIPath.rfind('/') + 1);
+                               if (!removeFile(concatPath(binNIDir, niFileName))) {
+                                       fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, niFileName).c_str());
                                }
                        }
                }
@@ -631,39 +719,40 @@ ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string&
        }
 }
 
-void removeNiPlatform()
+void removeNIPlatform()
 {
        std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
        std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
 
-       if (!isFileExist(coreLibBackup)) {
-               return;
+       if (hasCoreLibNI()) {
+               if (!isFile(coreLibBackup)) {
+                       return;
+               }
+
+               if (remove(coreLib.c_str())) {
+                       fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
+               }
+               if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
+                       fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
+               }
        }
 
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-       if (isFileExist(__SYSTEM_BASE_FILE)) {
+       if (isFile(__SYSTEM_BASE_FILE)) {
                if (remove(__SYSTEM_BASE_FILE)) {
                        fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
                }
        }
 #endif
 
-       if (remove(coreLib.c_str())) {
-               fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
-       }
-
-       if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
-               fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
-       }
-
        const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
 
-       removeNiUnderDirs(platformDirs, 2);
+       removeNIUnderDirs(platformDirs, 2);
 }
 
-void removeNiUnderDirs(const std::string rootPaths[], int count)
+void removeNIUnderDirs(const std::string rootPaths[], int count)
 {
-       auto convert = [](const std::string& path, std::string name) {
+       auto convert = [](const std::string& path, const std::string& filename) {
                if (isNativeImage(path)) {
                        if (remove(path.c_str())) {
                                fprintf(stderr, "Failed to remove %s\n", path.c_str());
@@ -672,33 +761,34 @@ void removeNiUnderDirs(const std::string rootPaths[], int count)
        };
 
        for (int i = 0; i < count; i++) {
-               scanFilesInDir(rootPaths[i], convert, -1);
+               scanFilesInDirectory(rootPaths[i], convert, -1);
        }
 }
 
-ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
+ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
 {
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
+       std::string rootPath = getRootPath(pkgId);
+       if (rootPath.empty()) {
+               fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
                return NI_ERROR_INVALID_PACKAGE;
        }
 
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string libDir = concatPath(pkgRoot, "lib");
+       std::string binDir = concatPath(rootPath, "bin");
+       std::string libDir = concatPath(rootPath, "lib");
        std::string paths[] = {binDir, libDir};
 
-       removeNiUnderDirs(paths, 2);
+       removeNIUnderDirs(paths, 2);
 
        std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
-       if (isFileExist(binNIDir)) {
-               if (rmdir(binNIDir.c_str()) != 0) {
+       if (isFile(binNIDir)) {
+               if (!removeAll(binNIDir.c_str())) {
                        fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
                }
        }
 
        std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
-       if (isFileExist(libNIDir)) {
-               if (rmdir(libNIDir.c_str()) != 0) {
+       if (isFile(libNIDir)) {
+               if (!removeAll(libNIDir.c_str())) {
                        fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
                }
        }
@@ -708,6 +798,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;
 
@@ -728,7 +822,7 @@ ni_error_e regenerateAppNI(DWORD flags)
                return NI_ERROR_UNKNOWN;
        }
 
-       fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
+       fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
 
        pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
        return NI_ERROR_NONE;
@@ -761,15 +855,19 @@ static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
                tac_db = NULL;
        }
 
-       createNiUnderTAC(nugets, *pFlags);
+       createNIUnderTAC(nugets, *pFlags);
 
        return 0;
 }
 
 ni_error_e regenerateTACNI(DWORD flags)
 {
-       const std::string tacDir[] = {__TAC_DIR};
-       removeNiUnderDirs(tacDir, 1);
+       if (!isCoreLibPrepared(flags)) {
+               return NI_ERROR_CORE_NI_FILE;
+       }
+
+       const std::string tacDir[] = {__DOTNET_DIR};
+       removeNIUnderDirs(tacDir, 1);
 
        pkgmgrinfo_appinfo_metadata_filter_h handle;
        int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
@@ -789,7 +887,7 @@ ni_error_e regenerateTACNI(DWORD flags)
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;
        }
-       fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
+       fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
 
        pkgmgrinfo_appinfo_metadata_filter_destroy(handle);