Fixed TAC related bugs caused by function integration patch
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
index 3a2a476..f7b69ad 100644 (file)
 #include <wait.h>
 #include <dirent.h>
 #include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/resource.h>
 
 #include <algorithm>
 #include <string>
 #include <fstream>
+#include <sstream>
 
-#include <pwd.h>
 #include <grp.h>
 #include <unistd.h>
 #include <string.h>
 #include <sqlite3.h>
+#include <inttypes.h>
+#include <errno.h>
 
 #include "ni_common.h"
 #include "db_manager.h"
 #include "tac_common.h"
 #include "path_manager.h"
 #include "plugin_manager.h"
+#include "r2r_checker.h"
 
 #ifdef  LOG_TAG
 #undef  LOG_TAG
 #endif
 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
 
-#ifndef CROSSGEN_PATH
-#error "CROSSGEN_PATH is missed"
-#endif
-
 #define __XSTR(x) #x
 #define __STR(x) __XSTR(x)
+#if defined(__arm__) || defined(__aarch64__)
 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);
+#endif
+static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
+static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
 
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
@@ -64,84 +67,49 @@ static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
 #undef __STR
 #undef __XSTR
 
-static int __interval = 0;
-static std::string __tpa;
+static std::string CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
+static std::string CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
+static std::string CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
+static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
+static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
+static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
+static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compilation";
+//static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
+//static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
+static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
+//static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
+static const char* CROSSGEN_OPT_OPTIMIZE_TIME = "--Ot";
+static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
+static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
+static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
+static std::vector<std::string> REF_VECTOR;
+static std::vector<std::string> INPUTBUBBLE_REF_VECTOR;
+static std::vector<std::string> MIBC_VECTOR;
 
-static void waitInterval()
-{
-       // by the recommand, ignore small value for performance.
-       if (__interval > 10000) {
-               fprintf(stderr, "sleep %d usec\n", __interval);
-               usleep(__interval);
-       }
-}
-
-static std::string getNiFilePath(const std::string& dllPath)
-{
-       size_t index = dllPath.find_last_of(".");
-       if (index == std::string::npos) {
-               fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
-               return "";
-       }
-       std::string fName = dllPath.substr(0, index);
-       std::string fExt = dllPath.substr(index, dllPath.length());
-
-       // crossgen generate file with lower case extension only
-       std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
-       std::string niPath = fName + ".ni" + fExt;
+static int __interval = 0;
+static PathManager* __pm = nullptr;
 
-       return niPath;
-}
+static NIOption* __ni_option = nullptr;
 
-static std::string getAppNIPath(const std::string& niPath)
+// singleton
+NIOption* getNIOption()
 {
-       std::string fileName;
-       std::string niDirPath;
-       std::string prevPath;
-
-       size_t index = niPath.find_last_of("/");
-       if (index != std::string::npos) {
-               prevPath = niPath.substr(0, index);
-               fileName = niPath.substr(index + 1, niPath.length());
-       } else {
-               prevPath = ".";
-               fileName = niPath;
-       }
-
-       niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
-
-       if (!isFileExist(niDirPath)) {
-               if (mkdir(niDirPath.c_str(), 0755) == 0) {
-                       copySmackAndOwnership(prevPath, niDirPath);
-               } else {
-                       fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
+       if (__ni_option == nullptr) {
+               __ni_option = (NIOption*)calloc(sizeof(NIOption), 1);
+               if (__ni_option == nullptr) {
+                       _SERR("Fail to create NIOption");
                }
        }
-
-       return concatPath(niDirPath, fileName);
+       return __ni_option;
 }
 
-static bool niExist(const std::string& path)
+static void waitInterval()
 {
-       std::string f = getNiFilePath(path);
-       if (f.empty()) {
-               return false;
-       }
-
-       if (isFileExist(f)) {
-               return true;
-       }
-
-       // native image of System.Private.CoreLib.dll should have to overwrite
-       // original file to support new coreclr
-       if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
-               std::string coreLibBackup = path + ".Backup";
-               if (isFileExist(coreLibBackup)) {
-                       return true;
-               }
+       // by the recommand, ignore small value for performance.
+       if (__interval > 10000) {
+               _SOUT("sleep %d usec", __interval);
+               usleep(__interval);
        }
-
-       return false;
 }
 
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
@@ -151,14 +119,14 @@ static uintptr_t getNextBaseAddrFromFile()
 {
        FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
        if (pFile == NULL) {
-               fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
+               _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
                return 0;
        }
 
        uintptr_t addr = 0;
        uintptr_t size = 0;
 
-       while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
+       while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
        }
 
        fclose(pFile);
@@ -171,7 +139,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,78 +154,353 @@ 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 = getSizeOfImage(absNIPath);
        if (niSize == 0) {
-               fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
+               _SERR("File %s doesn't exist", absNIPath.c_str());
                return;
        }
 
        // Write new entry to the file
        FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
        if (pFile == NULL) {
-               fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
+               _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
                return;
        }
 
-       fprintf(pFile, "%u %u\n", baseAddr, niSize);
+       fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
        fclose(pFile);
 }
 
 // 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 absPath = getBaseName(getAbsolutePath(dllPath));
 
-       if (__tpa.find(absDllPath) != std::string::npos) {
-               return true;
+       std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
+       for (unsigned int i = 0; i < paths.size(); i++) {
+               if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
+                       return true;
+               }
        }
 
        return false;
 }
 #endif
 
-// baseAddr should be checked in file before getting here
-static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
+/**
+ * @brief create the directory including parents directory, and
+ *          copy ownership and smack labels to the created directory.
+ * @param[in] target directory path
+ * @param[in] source directory path to get ownership and smack label
+ * @return if directory created successfully, return true otherwise false
+ */
+static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
 {
-       if (!isFileExist(dllPath)) {
-               fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
-               return NI_ERROR_NO_SUCH_FILE;
+       struct stat st;
+       mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
+
+       for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
+               std::string::iterator newIter = std::find(iter, target_path.end(), '/');
+               std::string newPath = std::string(target_path.begin(), newIter);
+
+               if (!newPath.empty()) {
+                       if (stat(newPath.c_str(), &st) != 0) {
+                               if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
+                                       _SERR("Fail to create app ni directory (%s)", newPath.c_str());
+                                       return false;
+                               }
+                               if (!source.empty()) {
+                                       copySmackAndOwnership(source, newPath);
+                               }
+                       } else {
+                               if (!S_ISDIR(st.st_mode)) {
+                                       _SERR("Fail. path is not a dir (%s)", newPath.c_str());
+                                       return false;
+                               }
+                       }
+               }
+               iter = newIter;
+               if(newIter != target_path.end()) {
+                       ++iter;
+               }
        }
 
-       if (!isManagedAssembly(dllPath)) {
-               //fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
+       return true;
+}
+
+static std::string getNIFilePath(const std::string& absDllPath, NIOption* opt)
+{
+       std::string dllPath = absDllPath;
+       std::string fileName = getFileName(absDllPath);
+       if (opt->flags & NI_FLAGS_APPNI) {
+               std::string niDirPath;
+               std::string niTmpDirPath;
+               std::string prevPath;
+
+               prevPath = getBaseName(absDllPath);
+               niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
+               niTmpDirPath = concatPath(prevPath, APP_NI_SUB_TMP_DIR);
+
+               if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
+                       niTmpDirPath = replaceAll(niTmpDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+                       niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+                       _INFO("App is installed in RO area. Change NI path to RW area(%s).", niTmpDirPath.c_str());
+               }
+
+               if (!isDirectory(niDirPath)) {
+                       if (!createDirsAndCopyOwnerShip(niTmpDirPath, prevPath)) {
+                               niTmpDirPath = prevPath;
+                               _SERR("fail to create dir (%s)", niTmpDirPath.c_str());
+                       }
+                       dllPath = concatPath(niTmpDirPath, fileName);
+               } else {
+                       dllPath = concatPath(niDirPath, fileName);
+               }
+       } else if (opt->flags & NI_FLAGS_RESOURCE_NI) {
+               std::string rpkDir = concatPath(__pm->getAppRootPath(), APP_NI_SUB_DIR);
+               dllPath = createDir(rpkDir) ? concatPath(rpkDir, fileName) : concatPath(__pm->getAppRootPath(), fileName);
+       }
+
+       size_t index = dllPath.find_last_of(".");
+       if (index == std::string::npos) {
+               _SERR("File doesnot contain extension. fail to get NI file name");
+               return "";
+       }
+       std::string fName = dllPath.substr(0, index);
+       std::string fExt = dllPath.substr(index, dllPath.length());
+
+       // crossgen generate file with lower case extension only
+       std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
+       std::string niPath = fName + ".ni" + fExt;
+
+       return niPath;
+}
+
+static bool checkNIExistence(const std::string& absDllPath, NIOption* opt)
+{
+       std::string absNIPath = getNIFilePath(absDllPath, opt);
+       if (absNIPath.empty()) {
+               return false;
+       }
+
+       if (isFile(absNIPath)) {
+               return true;
+       }
+
+       // 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) {
+               return isR2RImage(absDllPath);
+       }
+
+       return false;
+}
+
+static bool checkDllExistInDir(const std::string& path)
+{
+       bool ret = false;
+       auto func = [&ret](const std::string& f_path, const std::string& f_name) {
+               if (isManagedAssembly(f_name) || isR2RImage(f_name)) {
+                       ret = true;
+               }
+       };
+
+       scanFilesInDirectory(path, func, 0);
+
+       return ret;
+}
+
+/*
+ * Get the list of managed files in the specific directory (of Application)
+ * Absolute paths of managed files are stored at the result list.
+ * If native image already exist in the (same / .native_image) directory, managed file is ignored.
+ */
+static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
+{
+       if (!isDirectory(path)) {
                return NI_ERROR_INVALID_PARAMETER;
        }
 
-       if (niExist(dllPath)) {
-               fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
-               return NI_ERROR_ALREADY_EXIST;
+       auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
+               if (isManagedAssembly(f_path) && !checkNIExistence(f_path, opt)) {
+                       fileList.push_back(getAbsolutePath(f_path));
+               }
+       };
+
+       scanFilesInDirectory(path, func, 0);
+
+       return NI_ERROR_NONE;
+}
+
+static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
+{
+       args.push_back(CORERUN_CMD.c_str());
+       if (CROSSGEN2_PATH != "") {
+               args.push_back(CROSSGEN2_PATH.c_str());
        }
+       args.push_back(CROSSGEN_OPT_JITPATH);
+       args.push_back(CLRJIT_PATH.c_str());
+       args.push_back(CROSSGEN_OPT_TARGET_ARCH);
+       args.push_back(ARCHITECTURE_IDENTIFIER);
 
-       std::string absDllPath = absolutePath(dllPath);
-       std::string absNiPath = getNiFilePath(dllPath);
+       //args.push_back(OPT_PARALLELISM);
+       //args.push_back(OPT_PARALLELISM_COUNT);
+       args.push_back(CROSSGEN_OPT_RESILIENT);
 
+       args.push_back(CROSSGEN_OPT_OPTIMIZE_TIME);
+
+       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+               args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
+               args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
+
+               INPUTBUBBLE_REF_VECTOR.clear();
+               for (const auto &path : opt->inputBubbleRefFiles) {
+                       INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path);
+               }
+               for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
+                       if (find(args.begin(), args.end(), path) == args.end()) {
+                               args.push_back(path.c_str());
+                       }
+               }
+       }
+
+       if (opt->flags & NI_FLAGS_MIBC) {
+               MIBC_VECTOR.clear();
+               for (const auto &path : opt->mibcPath) {
+                       MIBC_VECTOR.push_back("--mibc:" + path);
+               }
+               for (const auto &path : MIBC_VECTOR) {
+                       if (find(args.begin(), args.end(), path) == args.end()) {
+                               args.push_back(path.c_str());
+                       }
+               }
+       }
 
-       if (absNiPath.empty()) {
-               fprintf(stderr, "Fail to get ni file name\n");
+       if (opt->flags & NI_FLAGS_VERBOSE) {
+               args.push_back(CROSSGEN_OPT_VERBOSE);
+       }
+
+       REF_VECTOR.clear();
+
+       // set reference path
+       for (const auto &path : opt->refFiles) {
+               REF_VECTOR.push_back("-r:" + path);
+       }
+
+       std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
+       for (const auto &path : paths) {
+               if (checkDllExistInDir(path)) {
+                       REF_VECTOR.push_back("-r:" + path + "/*.dll");
+               }
+       }
+
+       if (opt->flags & NI_FLAGS_EXTRA_REF) {
+               for (const auto &erPath : opt->extraRefPath) {
+                       std::string path = getAbsolutePath(erPath);
+                       if (checkDllExistInDir(path)) {
+                               REF_VECTOR.push_back("-r:" + path + "/*.dll");
+                       }
+               }
+       }
+
+       for (const auto &path : refPaths) {
+               if (checkDllExistInDir(path)) {
+                       REF_VECTOR.push_back("-r:" + path + "/*.dll");
+               }
+       }
+
+       for (const auto &path : REF_VECTOR) {
+               if (find(args.begin(), args.end(), path) == args.end()) {
+                       args.push_back(path.c_str());
+               }
+       }
+}
+
+static void clearArgs(std::vector<const char*>& args)
+{
+       REF_VECTOR.clear();
+       args.clear();
+}
+
+static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
+{
+       std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
+       try {
+               if (exist(pdbPath)) {
+                       std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
+                       if (!exist(targetPDBPath)) {
+                               bf::create_symlink(pdbPath, targetPDBPath);
+                               copySmackAndOwnership(pdbPath, targetPDBPath, true);
+                       }
+               }
+       } catch (const bf::filesystem_error& error) {
+               _SERR("Fail to create symlink for %s", pdbPath.c_str());
                return NI_ERROR_UNKNOWN;
        }
 
-       bool isAppNI = flags & NI_FLAGS_APPNI;
-       if (isAppNI && strstr(absNiPath.c_str(), __TAC_DIR) == NULL) {
-               absNiPath = getAppNIPath(absNiPath);
+       return NI_ERROR_NONE;
+}
+
+static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
+       std::string outFile = niPath;
+       if (!exist(outFile)) {
+               return NI_ERROR_NO_SUCH_FILE;
+       }
+       copySmackAndOwnership(dllPath, outFile);
+
+       // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
+       if (opt->flags & NI_FLAGS_APPNI) {
+               outFile = getNIFilePath(dllPath, opt);
+               makePdbSymlinkForNI(dllPath, outFile);
+
+               if (opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE) {
+                       outFile = outFile + ".tmp";
+               }
+
+               if (niPath != outFile) {
+                       moveFile(niPath, outFile);
+               }
+       }
+
+       if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
+               if (!removeFile(dllPath)) {
+                       _SERR("Fail to remove original file : %s", dllPath.c_str());
+               }
        }
 
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-       uintptr_t baseAddr = 0;
+       if (opt->flags & NI_FLAGS_RESOURCE_NI) {
+               moveFile(niPath, changeExtension(dllPath, ".dll", ".ni.dll"));
+               removeAll(concatPath(__pm->getAppRootPath(), APP_NI_SUB_DIR));
+       }
 
-       if (isTPADll(dllPath)) {
-               baseAddr = getNextBaseAddr();
+       if (!(opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE)) {
+               _SOUT("Native image %s generated successfully.", outFile.c_str());
        }
-#endif
 
+       return NI_ERROR_NONE;
+}
+
+void setPriority(NIOption* opt)
+{
+       pid_t pid = getpid();
+       if (setpriority(PRIO_PROCESS, pid, opt->priority) == 0) {
+               std::string str = " ";
+               if (opt->priority <= -20) {
+                       str = " highest ";
+               } else if (opt->priority >= 19) {
+                       str = " lowest ";
+               }
+               _SOUT("Success to set the%spriority of the process. pid : [%d], priority : [%d]", str.c_str(), pid, getpriority(PRIO_PROCESS, pid));
+       } else {
+               _SERR("Failed to set the priority of the process. pid : [%d], priority : [%d]", pid, getpriority(PRIO_PROCESS, pid));
+       }
+}
+
+static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
+{
+       // fork crossgen2
        pid_t pid = fork();
        if (pid == -1)
                return NI_ERROR_UNKNOWN;
@@ -266,171 +509,434 @@ 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)) {
-                               copySmackAndOwnership(absDllPath, absNiPath);
-                               std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
-                               std::string pdbFilePath = replaceAll(absNiPath, ".ni.dll", ".pdb");
-                               if (isFileExist(absPdbPath) && (absPdbPath != pdbFilePath)) {
-                                       if (!copyFile(absPdbPath, pdbFilePath)) {
-                                               fprintf(stderr, "Failed to copy a .pdb file\n");
-                                       }
+                       for (auto& dllPath: dllList) {
+                               ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
+                               if (ret != NI_ERROR_NONE) {
+                                       return ret;
+                               }
+                       }
+               } else {
+                       _SERR("Failed. Forked process terminated abnormally");
+                       return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
+               }
+       } else {
+               if (opt->flags & NI_FLAGS_SET_PRIORITY) {
+                       setPriority(opt);
+               }
+               std::vector<const char*> argv;
+               makeArgs(argv, refPaths, opt);
+               argv.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
+               argv.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
+
+               // add input files at the end of parameter
+               for (const auto &input : dllList) {
+                       argv.push_back(input.c_str());
+                       _SOUT("+ %s", input.c_str());
+               }
+
+               // end param
+               argv.push_back(nullptr);
+
+               // print cmd
+               if (opt->flags & NI_FLAGS_PRINT_CMD) {
+                       _SOUT("==================== NI Commands =========================");
+                       for (auto &arg: argv) _SOUT("+ %s", arg);
+               }
+
+               execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
+
+               clearArgs(argv);
+               exit(0);
+       }
+
+       return NI_ERROR_NONE;
+}
+
+static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
+{
+       for (auto& dllPath : dllList) {
+               std::string niPath;
+               niPath = getNIFilePath(dllPath, opt);
+
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+               uintptr_t baseAddr = 0;
+               if (isTPADll(dllPath)) {
+                       baseAddr = getNextBaseAddr();
+               }
+#endif
+               if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                       niPath += ".tmp";
+               }
+
+               // fork crossgen2
+               pid_t pid = fork();
+               if (pid == -1)
+                       return NI_ERROR_UNKNOWN;
+
+               if (pid > 0) {
+                       int status;
+                       waitpid(pid, &status, 0);
+                       if (WIFEXITED(status)) {
+                               ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
+                               if (ret != NI_ERROR_NONE) {
+                                       return ret;
                                }
 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
                                if (baseAddr != 0) {
-                                       updateBaseAddrFile(absNiPath, baseAddr);
+                                       updateBaseAddrFile(niPath, baseAddr);
                                }
 #endif
-                               return NI_ERROR_NONE;
                        } else {
-                               fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
-                               return NI_ERROR_NO_SUCH_FILE;
+                               _SERR("Failed. Forked process terminated abnormally");
+                               _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
+                               removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
+                               return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
+                       }
+               } else {
+                       if (opt->flags & NI_FLAGS_SET_PRIORITY) {
+                               setPriority(opt);
+                       }
+                       std::vector<const char*> argv;
+                       makeArgs(argv, refPaths, opt);
+
+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+                       std::string baseAddrString;
+                       if (baseAddr != 0) {
+                               argv.push_back("--imagebase");
+                               std::stringstream ss;
+                               ss << "0x" << std::hex << baseAddr;
+                               baseAddrString = ss.str();
+                               argv.push_back(baseAddrString.c_str());
+                       }
+#endif
+                       argv.push_back("-o");
+                       argv.push_back(niPath.c_str());
+
+                       argv.push_back(dllPath.c_str());
+                       _SOUT("+ %s", dllPath.c_str());
+
+                       // end param
+                       argv.push_back(nullptr);
+
+                       // print cmd
+                       if (opt->flags & NI_FLAGS_PRINT_CMD) {
+                               _SOUT("==================== NI Commands =========================");
+                               for (auto &arg: argv) _SOUT("+ %s", arg);
                        }
+
+                       execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
+
+                       clearArgs(argv);
+                       exit(0);
                }
-       } else {
-               std::string jitPath = getRuntimeDir() + "/libclrjit.so";
-               std::vector<const char*> argv = {
-                       __CROSSGEN_PATH,
-                       "/nologo",
-                       "/JITPath", jitPath.c_str()
-               };
 
-               bool compat = flags & NI_FLAGS_COMPATIBILITY;
-               argv.push_back(compat ? "/Trusted_Platform_Assemblies" : "/r");
-               argv.push_back(__tpa.c_str());
+               waitInterval();
+       }
+
+       return NI_ERROR_NONE;
+}
 
-               bool enableR2R = flags & NI_FLAGS_ENABLER2R;
-               if (!enableR2R) {
-                       argv.push_back("/FragileNonVersionable");
+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 niTmpCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll.tmp");
+       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) {
+                       if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
+                               std::ofstream output(coreLibBackup);
+                               if (!exist(coreLibBackup)) {
+                                       _SERR("Failed to create System.Private.CoreLib.dll.Backup");
+                                       return NI_ERROR_CORE_NI_FILE;
+                               }
+                               copySmackAndOwnership(__pm->getRuntimePath(), coreLibBackup, false);
+                               output.close();
+                       } else if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
+                               _SERR("Failed to rename from System.Private.CoreLib.dll to System.Private.CoreLib.dll.Backup");
+                               return NI_ERROR_CORE_NI_FILE;
+                       }
+                       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                               if (rename(niTmpCoreLib.c_str(), coreLib.c_str())) {
+                                       _SERR("Failed to rename from System.Private.CoreLib.ni.dll.tmp to Private.CoreLib.dll");
+                                       return NI_ERROR_CORE_NI_FILE;
+                               }
+                       } else {
+                               if (rename(niCoreLib.c_str(), coreLib.c_str())) {
+                                       _SERR("Failed to rename from System.Private.CoreLib.ni.dll to Private.CoreLib.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 void renameAppNITmpPath(NIOption* opt)
+{
+       std::string niTmpPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_TMP_DIR;
+       std::string niPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_DIR;
+
+       if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
+               niTmpPath = replaceAll(niTmpPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+               niPath = replaceAll(niPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+       }
 
-               if (flags & NI_FLAGS_VERBOSE) {
-                       argv.push_back("/verbose");
+       if (isDirectory(niTmpPath)) {
+               if (rename(niTmpPath.c_str(), niPath.c_str())) {
+                       _SERR("Fail to rename from .native_image_tmp to .native_image");
+               } else {
+                       _SOUT("Success to rename from %s to %s", niTmpPath.c_str(), niPath.c_str());
                }
+       }
+}
+
+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;
 
-               if (flags & NI_FLAGS_INSTRUMENT) {
-                       argv.push_back("/Tuning");
+       for (auto it = dllList.begin(); it != dllList.end(); it++) {
+               std::string f = *it;
+               if (!isFile(f)) {
+                       _SERR("dll file is not exist : %s", f.c_str());
+                       dllList.erase(it--);
+               }
+               else if (!isManagedAssembly(f)) {
+                       _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
+               else if (f == coreLib) {
+                       hasSPC = true;
+                       dllList.erase(it--);
                }
+       }
 
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-               if (baseAddr != 0) {
-                       argv.push_back("/BaseAddress");
-                       argv.push_back(std::to_string(baseAddr).c_str());
+       // Error : Multiple input files matching same simple name
+       // So, Remove dulicate files from dll list
+       std::sort(dllList.begin(), dllList.end());
+       dllList.erase(unique(dllList.begin(), dllList.end()), dllList.end());
+
+       // 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;
                }
-#endif
+       }
 
-               argv.push_back("/App_Paths");
-               std::string absAppPath;
-               if (!appPath.empty()) {
-                       absAppPath = appPath;
+       // if there is no proper input after processing dll list
+       if (dllList.empty()) {
+               if (hasSPC) {
+                       return ret;
                } else {
-                       absAppPath = baseName(absDllPath);
+                       return NI_ERROR_INVALID_PARAMETER;
                }
-               argv.push_back(absAppPath.c_str());
+       }
 
-               argv.push_back("/out");
-               argv.push_back(absNiPath.c_str());
+       std::vector<std::string> paths;
+       splitPath(refPaths, paths);
 
-               argv.push_back(absDllPath.c_str());
-               argv.push_back(nullptr);
+       if (opt->flags & NI_FLAGS_NO_PIPELINE) {
+               ret = crossgen2NoPipeLine(dllList, paths, opt);
+       } else {
+               std::vector<std::string> notCompiled;
+               ret = crossgen2PipeLine(dllList, paths, opt);
+               if (ret != NI_ERROR_NONE) {
+                       _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
+                       for (auto &dll : dllList) {
+                               std::string tFile = changeExtension(dll, ".dll", ".ni.dll");
+                               if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                                       tFile += ".tmp";
+                               }
+                               if (!exist(tFile)) {
+                                       notCompiled.push_back(dll);
+                               }
+                       }
+                       _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
+                       ret = crossgen2NoPipeLine(notCompiled, paths, opt);
+               }
+       }
 
-               fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
+       if (ret == NI_ERROR_NONE) {
+               if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+                       for (auto &dll : dllList) {
+                               std::string tmpFile;
+                               std::string niFile = getNIFilePath(dll, opt);
+                               tmpFile = niFile + ".tmp";
 
-               execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
-               exit(0);
+                               if (exist(tmpFile)) {
+                                       moveFile(tmpFile, niFile);
+                                       _SOUT("Native image %s generated successfully.", niFile.c_str());
+                               }
+                       }
+               }
+               if (opt->flags & NI_FLAGS_APPNI) {
+                       renameAppNITmpPath(opt);
+               }
+       }
+
+       return ret;
+}
+
+static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
+{
+       if (!isFile(dllFile)) {
+               _SERR("dll file is not exist : %s", dllFile.c_str());
+               return NI_ERROR_NO_SUCH_FILE;
+       }
+
+       if (checkNIExistence(dllFile, opt)) {
+               _SERR("Native image file is already exist : %s", dllFile.c_str());
+               return NI_ERROR_ALREADY_EXIST;
+       }
+
+       if (!isManagedAssembly(dllFile)) {
+               _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
+               return NI_ERROR_INVALID_PARAMETER;
+       }
+
+       std::vector<std::string> dllList;
+       dllList.push_back(getAbsolutePath(dllFile));
+       return doAOTList(dllList, refPaths, opt);
+}
+
+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;
+       }
+
+       _SOUT("Complete make native image for pkg (%s)", pkgId);
        return NI_ERROR_NONE;
 }
 
-// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
-static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
+static bool isReadOnlyPkg(std::string pkgId)
 {
-       char *pkgId = NULL;
        int ret = 0;
-       DWORD *pFlags = (DWORD*)userData;
+       bool readonly = false;
+       pkgmgrinfo_pkginfo_h handle;
 
-       ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
        if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed to get pkgid\n");
-               return -1;
+               _ERR("Fail to get pkginfo");
+               return false;
        }
 
-       if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
-               fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
-               return -1;
+       ret = pkgmgrinfo_pkginfo_is_readonly(handle, &readonly);
+       if (ret != PMINFO_R_OK) {
+               _ERR("Fail to get is_readonly");
+               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+               return false;
        }
 
-       if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
-               fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
-               return -1;
-       }
+       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       return readonly;
+}
 
-       // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
-       if (createNiUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
-               fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
+// 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;
-       } else {
-               fprintf(stderr, "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");
-       }
+       std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
+       pkgList->push_back(pkgId);
 
        return 0;
 }
 
-static void createCoreLibNI(DWORD flags)
+// callback function of "pkgmgrinfo_pkginfo_metadata_filter_foreach"
+static int pkgAotCb(pkgmgrinfo_pkginfo_h handle, void *userData)
 {
-       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");
+       char *pkgId = NULL;
+       int ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgId);
+       if (ret != PMINFO_R_OK) {
+               _SERR("Failed to get pkgid");
+               return -1;
+       }
 
-       if (!isFileExist(coreLibBackup)) {
+       std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
+       pkgList->push_back(pkgId);
 
-               if (!crossgen(coreLib, std::string(), flags)) {
-                       if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
-                               fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
-                       }
-                       if (rename(niCoreLib.c_str(), coreLib.c_str())) {
-                               fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
-                       }
-               } else {
-                       fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
-               }
-       }
+       return 0;
 }
 
-ni_error_e initNICommon(NiCommonOption* option)
+ni_error_e initNICommon()
 {
-#if defined(__arm__)
+#if defined(__arm__) || defined(__aarch64__)
+
+       char *env = nullptr;
+       env = getenv("MIC_CROSSGEN2_ENABLED");
+       if (env != nullptr && !strcmp(env, "1")) {
+               CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
+               CROSSGEN2_PATH = "";
+               CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
+       }
+
        // 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");
+               _SOUT("crossgen_interval.txt is found");
                inFile >> __interval;
        }
 
        if (initializePluginManager("normal")) {
-               fprintf(stderr, "Fail to initialize PluginManager\n");
+               _SERR("Fail to initialize PluginManager");
                return NI_ERROR_UNKNOWN;
        }
-       if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
-               fprintf(stderr, "Fail to initialize PathManager\n");
+
+       try {
+               __pm = new PathManager();
+       } catch (const std::exception& e) {
+               _SERR("Failed to create PathManager");
                return NI_ERROR_UNKNOWN;
        }
 
-       __tpa = getTPA();
+       char* pluginDllPaths = pluginGetDllPath();
+       if (pluginDllPaths && pluginDllPaths[0] != '\0') {
+               __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
+       }
+
+       char* pluginNativePaths = pluginGetNativeDllSearchingPath();
+       if (pluginNativePaths && pluginNativePaths[0] != '\0') {
+               __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
+       }
 
        return NI_ERROR_NONE;
 #else
-       fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
+       _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
        return NI_ERROR_NOT_SUPPORTED;
 #endif
 }
@@ -440,351 +946,415 @@ void finalizeNICommon()
        __interval = 0;
 
        finalizePluginManager();
-       finalizePathManager();
 
-       __tpa.clear();
-}
+       delete(__pm);
+       __pm = nullptr;
 
+       if (__ni_option) {
+               free(__ni_option);
+               __ni_option = nullptr;
+       }
+}
 
-void createNiPlatform(DWORD flags)
+ni_error_e createNIPlatform(std::string& extraInputs, NIOption* opt)
 {
-       const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
-       createNiUnderDirs(platformDirs, 2, flags);
+       extraInputs += ":" + __pm->getRuntimePath();
+       extraInputs += ":" + __pm->getTizenFXPath();
+
+       return createNIUnderDirs(extraInputs, opt);
 }
 
-ni_error_e createNiDll(const std::string& dllPath, DWORD flags)
+ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
 {
-       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 crossgen(dllPath, std::string(), flags);
+       return doAOTFile(dllPath, std::string(), opt);
 }
 
-void createNiUnderTAC(std::vector<std::string> nugets, DWORD flags)
+ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
 {
-       std::string appPaths;
-       for (auto& nuget : nugets) {
-               appPaths += concatPath(__TAC_DIR, nuget);
-               appPaths += ':';
+       ni_error_e ret;
+
+       // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
+       // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
+       bool isAppNI = false;
+       if (opt->flags & NI_FLAGS_APPNI) {
+               opt->flags &= ~NI_FLAGS_APPNI;
+               isAppNI = true;
        }
-       if (appPaths.back() == ':') {
-               appPaths.pop_back();
+
+       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+               std::vector<std::string> refs;
+               splitPath(refPaths, refs);
+               for (auto &p: refs) {
+                       if (isDirectory(p) && checkDllExistInDir(p)) {
+                               opt->inputBubbleRefFiles.push_back(p + "/*.dll");
+                       }
+               }
        }
 
-       auto convert = [&appPaths, flags](const std::string& path, const char* name) {
-               if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
-                       return;
-               if (!crossgen(path, appPaths.c_str(), flags)) {
-                       waitInterval();
+       // get managed file list from targetPath
+       std::vector<std::string> dllList;
+       ret = getTargetDllList(targetPath, dllList, opt);
+       if (ret != NI_ERROR_NONE) {
+               return ret;
+       }
+
+       std::vector<std::string> needNIList;
+       std::vector<std::string> niList;
+
+       for (auto &dll : dllList) {
+               if (!checkNIExistence(dll, opt)) {
+                       needNIList.push_back(dll);
                }
-       };
-       for (auto& nuget : nugets) {
-               scanFilesInDir(concatPath(__TAC_DIR, nuget), convert, -1);
+               niList.push_back(getNIFilePath(dll, opt));
        }
-}
 
-ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags)
-{
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
-               return NI_ERROR_INVALID_PACKAGE;
+       if (!needNIList.empty()) {
+               ret = doAOTList(needNIList, refPaths, opt);
+               if (ret != NI_ERROR_NONE) {
+                       return ret;
+               }
        }
 
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string libDir = concatPath(pkgRoot, "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)) {
-                               std::string symPath = symlinkAssembly.path().string();
-                               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)) {
-                                                       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());
-                                                       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());
-                                                       }
-                                               }
-                                       }
+       if (isAppNI) {
+               opt->flags |= NI_FLAGS_APPNI;
+               for (auto &niPath : niList) {
+                       if (exist(niPath)) {
+                               std::string symNIPath = concatPath(targetPath, getFileName(niPath));
+                               if (!exist(symNIPath)) {
+                                       bf::create_symlink(niPath, symNIPath);
+                                       copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
+                                       _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
+                                       _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
                                }
                        }
-               } catch (const bf::filesystem_error& error) {
-                       fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
-                       return NI_ERROR_UNKNOWN;
                }
        }
+
        return NI_ERROR_NONE;
 }
 
-void createNiUnderDirs(const std::string rootPaths[], int count, DWORD flags)
+ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
 {
-       createCoreLibNI(flags);
+       ni_error_e ret = NI_ERROR_NONE;
 
-       std::string appPaths;
-       for (int i = 0; i < count; i++) {
-               appPaths += rootPaths[i];
-               appPaths += ':';
-       }
+       std::vector<std::string> fileList;
+       std::vector<std::string> paths;
+       splitPath(rootPaths, paths);
 
-       if (appPaths.back() == ':')
-               appPaths.pop_back();
+       if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+               for (auto &p: paths) {
+                       if (isDirectory(p) && checkDllExistInDir(p)) {
+                               opt->inputBubbleRefFiles.push_back(p + "/*.dll");
+                       }
+               }
+       }
 
-       std::vector<std::string> tpaAssemblies;
-       splitPath(__tpa, tpaAssemblies);
+       for (const auto &path : paths) {
+               if (!exist(path)) {
+                       continue;
+               }
 
-       auto convert = [&appPaths, flags, tpaAssemblies](const std::string& path, const char* name) {
-               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);
-                                       return;
-                               }
+               if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
+                       ret = createNIUnderTAC(path, rootPaths, opt);
+                       if (ret != NI_ERROR_NONE) {
+                               return ret;
+                       }
+               } else {
+                       ret = getTargetDllList(path, fileList, opt);
+                       if (ret != NI_ERROR_NONE) {
+                               return ret;
                        }
                }
-               if (!crossgen(path, appPaths.c_str(), flags)) {
-                       waitInterval();
-               }
-       };
+       }
 
-       for (int i = 0; i < count; i++) {
-               scanFilesInDir(rootPaths[i], convert, 1);
+       if (fileList.empty()) {
+               return NI_ERROR_NONE;
        }
 
-       tpaAssemblies.clear();
+       return doAOTList(fileList, rootPaths, opt);
 }
 
-ni_error_e createNiUnderPkgRoot(const std::string& pkgId, DWORD flags)
+ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
 {
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
-               return NI_ERROR_INVALID_PACKAGE;
+       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"
+                               "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
+               return NI_ERROR_CORE_NI_FILE;
        }
 
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string libDir = concatPath(pkgRoot, "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;
-}
-
-ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, DWORD flags)
-{
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
+       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);
 
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string libDir = concatPath(pkgRoot, "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)) {
-                                       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());
-                                       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());
-                               }
-                       }
+       char* extraDllPaths = pluginGetExtraDllPath();
+       if (extraDllPaths && extraDllPaths[0] != '\0') {
+               opt->flags |= NI_FLAGS_EXTRA_REF;
+               splitPath(extraDllPaths, opt->extraRefPath);
+       }
+
+       std::string targetDirs;
+       if (isRPK(pkgId)) {
+               opt->flags &= ~NI_FLAGS_APPNI; // added to exclude logic of APP_NI
+               opt->flags |= NI_FLAGS_RESOURCE_NI; // added flag for RPK type
+               opt->flags |= NI_FLAGS_NO_PIPELINE; // added the flag to set the output path
+
+               std::string paths = getResourcePaths(rootPath);
+               if (paths.empty()) {
+                       _SERR("Failed to get rpk paths from [%s]", pkgId.c_str());
+                       return NI_ERROR_UNKNOWN;
                }
-               return NI_ERROR_NONE;
+               targetDirs = paths;
        } else {
-               std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
-               std::vector<std::string> tpaAssemblies;
-               splitPath(__tpa, tpaAssemblies);
-               for (auto& tpa : tpaAssemblies) {
-                       if (!strcmp(replaceAll(tpa.substr(tpa.rfind('/') + 1), ".ni.dll", ".dll").c_str(), assembly.c_str())) {
-                               fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", assembly.c_str());
-                               return NI_ERROR_NONE;
-                       }
+               opt->flags |= NI_FLAGS_APPNI;
+               opt->flags &= ~NI_FLAGS_RESOURCE_NI; // added to exclude logic of RESOURCE_NI
+
+               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;
                }
-               tpaAssemblies.clear();
-               flags |= NI_FLAGS_APPNI;
-               return crossgen(dllPath, paths, flags);
+
+               targetDirs = __pm->getAppPaths();
        }
+
+       return createNIUnderDirs(targetDirs, opt);
 }
 
-void removeNiPlatform()
+void removeNIPlatform()
 {
-       std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
-       std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
+       std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
+       std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
 
-       if (!isFileExist(coreLibBackup)) {
-               return;
+       if (isR2RImage(coreLib)) {
+               if (!isFile(coreLibBackup)) {
+                       return;
+               }
+
+               if (remove(coreLib.c_str())) {
+                       _SERR("Failed to remove System.Private.CoreLib native image file");
+               }
+               if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
+                       _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
+               }
        }
 
 #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);
+                       _SERR("Failed to remove %s", __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(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
 }
 
-void removeNiUnderDirs(const std::string rootPaths[], int count)
+void removeNIUnderDirs(const std::string& rootPaths)
 {
-       auto convert = [](const std::string& path, std::string name) {
-               if (isNativeImage(path)) {
-                       if (remove(path.c_str())) {
-                               fprintf(stderr, "Failed to remove %s\n", path.c_str());
+       auto convert = [](const std::string& path, const std::string& filename) {
+               if (isR2RImage(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());
                        }
                }
        };
 
-       for (int i = 0; i < count; i++) {
-               scanFilesInDir(rootPaths[i], convert, -1);
+       std::vector<std::string> paths;
+       splitPath(rootPaths, paths);
+       for (const auto &path : paths) {
+               scanFilesInDirectory(path, 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::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;
        }
 
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string libDir = concatPath(pkgRoot, "lib");
-       std::string paths[] = {binDir, libDir};
-
-       removeNiUnderDirs(paths, 2);
-
-       std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
-       if (isFileExist(binNIDir)) {
-               if (!removeAll(binNIDir.c_str())) {
-                       fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
+       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 {
+               __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());
+                                               }
+                                       }
+                               }
+                       }
                }
-       }
 
-       std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
-       if (isFileExist(libNIDir)) {
-               if (!removeAll(libNIDir.c_str())) {
-                       fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.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);
+                       }
                }
        }
 
        return NI_ERROR_NONE;
 }
 
-ni_error_e regenerateAppNI(DWORD flags)
+ni_error_e regeneratePkgNI(NIOption* opt)
 {
-       int ret = 0;
-       pkgmgrinfo_appinfo_metadata_filter_h handle;
-
-       ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
-       if (ret != PMINFO_R_OK)
-               return NI_ERROR_UNKNOWN;
-
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
-       if (ret != PMINFO_R_OK) {
-               pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-               return NI_ERROR_UNKNOWN;
+       std::vector<std::string> pkgList;
+       ni_error_e ret = NI_ERROR_NONE;
+
+       // 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_foreach(handle, appAotCb, &flags);
-       if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
-               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");
        }
 
-       fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
+       // 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());
 
-       pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
-       return NI_ERROR_NONE;
+       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");
+               }
+       }
+
+       return ret;
 }
 
 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
 {
        char *pkgId = NULL;
-       DWORD *pFlags = (DWORD*)userData;
+       char *root = NULL;
+       NIOption **pOpt = (NIOption**)userData;
 
        int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+       if (ret != PMINFO_R_OK || pkgId == NULL) {
+               _SERR("Failed to get pkgid");
+               return -1;
+       }
+
+       ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
        if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed to get pkgid\n");
+               _SERR("Failed to get root path");
                return -1;
        }
 
-       sqlite3 *tac_db = dbOpen(TAC_APP_LIST_DB);
+       std::string binPath = concatPath(std::string(root), "bin");
+       if (exist(concatPath(binPath, PRE_COMPILED_PACKAGE_FILE))) {
+               _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId);
+               return 0;
+       }
+
+       sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
        if (!tac_db) {
-               fprintf(stderr, "Sqlite open error\n");
+               _SERR("Sqlite open error");
                return -1;
        }
+       sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
 
        char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
-       std::vector<std::string> nugets = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
+       std::vector<std::string> nugets = selectDB(tac_db, sql);
        sqlite3_free(sql);
 
        if (tac_db) {
-               dbClose(tac_db);
+               closeDB(tac_db);
                tac_db = NULL;
        }
 
-       createNiUnderTAC(nugets, *pFlags);
+       std::string nugetPaths;
+       for (const auto &nuget : nugets) {
+               if (!nugetPaths.empty()) {
+                       nugetPaths += ":";
+               }
+               nugetPaths += concatPath(__DOTNET_DIR, nuget);
+       }
+
+       for (auto& nuget : nugets) {
+               createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
+       }
 
        return 0;
 }
 
-ni_error_e regenerateTACNI(DWORD flags)
+ni_error_e regenerateTACNI(NIOption* opt)
 {
-       const std::string tacDir[] = {__TAC_DIR};
-       removeNiUnderDirs(tacDir, 1);
+       removeNIUnderDirs(__DOTNET_DIR);
 
        pkgmgrinfo_appinfo_metadata_filter_h handle;
        int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
@@ -792,21 +1362,20 @@ ni_error_e regenerateTACNI(DWORD flags)
                return NI_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
+       ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
        if (ret != PMINFO_R_OK) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;
        }
 
-       ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, regenTacCb, &flags);
-       if (ret != PMINFO_R_OK) {
-               fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
+       ret = pkgmgrAppMDFilterForeach(handle, regenTacCb, &opt);
+       if (ret != 0) {
                pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
                return NI_ERROR_UNKNOWN;
        }
-       fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
 
        pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
 
        return NI_ERROR_NONE;
 }
+