d6515e813359a46de4ce5936d833d1e250379852
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
19 #include <aul.h>
20
21 #include "log.h"
22 #include "utils.h"
23 #include "pkgmgr_parser_plugin_interface.h"
24
25 #include <wait.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/resource.h>
30
31 #include <algorithm>
32 #include <string>
33 #include <fstream>
34 #include <sstream>
35
36 #include <grp.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <sqlite3.h>
40 #include <inttypes.h>
41 #include <errno.h>
42
43 #include "ni_common.h"
44 #include "db_manager.h"
45 #include "tac_common.h"
46 #include "path_manager.h"
47 #include "plugin_manager.h"
48 #include "r2r_checker.h"
49
50 #ifdef  LOG_TAG
51 #undef  LOG_TAG
52 #endif
53 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
54
55 #define __XSTR(x) #x
56 #define __STR(x) __XSTR(x)
57 #if defined(__arm__) || defined(__aarch64__)
58 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
59 #endif
60 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
61 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
62
63 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
64 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
65 #endif
66
67 #undef __STR
68 #undef __XSTR
69
70 static std::string CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
71 static std::string CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
72 static std::string CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
73 static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
74 static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
75 static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
76 static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compilation";
77 //static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
78 //static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
79 static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
80 //static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
81 static const char* CROSSGEN_OPT_OPTIMIZE_TIME = "--Ot";
82 static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
83 static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
84 static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
85 static std::vector<std::string> REF_VECTOR;
86 static std::vector<std::string> INPUTBUBBLE_REF_VECTOR;
87 static std::vector<std::string> MIBC_VECTOR;
88
89 static int __interval = 0;
90 static PathManager* __pm = nullptr;
91
92 static NIOption* __ni_option = nullptr;
93
94 // singleton
95 NIOption* getNIOption()
96 {
97         if (__ni_option == nullptr) {
98                 __ni_option = (NIOption*)calloc(sizeof(NIOption), 1);
99                 if (__ni_option == nullptr) {
100                         _SERR("Fail to create NIOption");
101                 }
102         }
103         return __ni_option;
104 }
105
106 static void waitInterval()
107 {
108         // by the recommand, ignore small value for performance.
109         if (__interval > 10000) {
110                 _SOUT("sleep %d usec", __interval);
111                 usleep(__interval);
112         }
113 }
114
115 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
116 // Get next base address to be used for system ni image from file
117 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
118 static uintptr_t getNextBaseAddrFromFile()
119 {
120         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
121         if (pFile == NULL) {
122                 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
123                 return 0;
124         }
125
126         uintptr_t addr = 0;
127         uintptr_t size = 0;
128
129         while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
130         }
131
132         fclose(pFile);
133
134         return addr + size;
135 }
136
137 // Get next base address to be used for system ni image
138 static uintptr_t getNextBaseAddr()
139 {
140         uintptr_t baseAddr = 0;
141
142         if (!isFile(__SYSTEM_BASE_FILE)) {
143                 // This is the starting address for all default base addresses
144                 baseAddr = DEFAULT_BASE_ADDR_START;
145         } else {
146                 baseAddr = getNextBaseAddrFromFile();
147
148                 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
149                 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
150                 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
151         }
152
153         return baseAddr;
154 }
155
156 // Save base address of system ni image to file
157 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
158 {
159         uintptr_t niSize = getSizeOfImage(absNIPath);
160         if (niSize == 0) {
161                 _SERR("File %s doesn't exist", absNIPath.c_str());
162                 return;
163         }
164
165         // Write new entry to the file
166         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
167         if (pFile == NULL) {
168                 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
169                 return;
170         }
171
172         fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
173         fclose(pFile);
174 }
175
176 // check if dll is listed in TPA
177 static bool isTPADll(const std::string& dllPath)
178 {
179         std::string absPath = getBaseName(getAbsolutePath(dllPath));
180
181         std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
182         for (unsigned int i = 0; i < paths.size(); i++) {
183                 if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
184                         return true;
185                 }
186         }
187
188         return false;
189 }
190 #endif
191
192 /**
193  * @brief create the directory including parents directory, and
194  *          copy ownership and smack labels to the created directory.
195  * @param[in] target directory path
196  * @param[in] source directory path to get ownership and smack label
197  * @return if directory created successfully, return true otherwise false
198  */
199 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
200 {
201         struct stat st;
202         mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
203
204         for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
205                 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
206                 std::string newPath = std::string(target_path.begin(), newIter);
207
208                 if (!newPath.empty()) {
209                         if (stat(newPath.c_str(), &st) != 0) {
210                                 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
211                                         _SERR("Fail to create app ni directory (%s)", newPath.c_str());
212                                         return false;
213                                 }
214                                 if (!source.empty()) {
215                                         copySmackAndOwnership(source, newPath);
216                                 }
217                         } else {
218                                 if (!S_ISDIR(st.st_mode)) {
219                                         _SERR("Fail. path is not a dir (%s)", newPath.c_str());
220                                         return false;
221                                 }
222                         }
223                 }
224                 iter = newIter;
225                 if(newIter != target_path.end()) {
226                         ++iter;
227                 }
228         }
229
230         return true;
231 }
232
233 static std::string getNIFilePath(const std::string& absDllPath, NIOption* opt)
234 {
235         std::string dllPath = absDllPath;
236         if (opt->flags & NI_FLAGS_APPNI) {
237                 std::string niDirPath;
238                 std::string niTmpDirPath;
239                 std::string prevPath;
240
241                 prevPath = getBaseName(absDllPath);
242                 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
243                 niTmpDirPath = concatPath(prevPath, APP_NI_SUB_TMP_DIR);
244
245                 if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
246                         niTmpDirPath = replaceAll(niTmpDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
247                         niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
248                         _INFO("App is installed in RO area. Change NI path to RW area(%s).", niTmpDirPath.c_str());
249                 }
250
251                 if (!isDirectory(niDirPath)) {
252                         if (!createDirsAndCopyOwnerShip(niTmpDirPath, prevPath)) {
253                                 niTmpDirPath = prevPath;
254                                 _SERR("fail to create dir (%s)", niTmpDirPath.c_str());
255                         }
256                         dllPath = concatPath(niTmpDirPath, getFileName(absDllPath));
257                 } else {
258                         dllPath = concatPath(niDirPath, getFileName(absDllPath));
259                 }
260         }
261
262         size_t index = dllPath.find_last_of(".");
263         if (index == std::string::npos) {
264                 _SERR("File doesnot contain extension. fail to get NI file name");
265                 return "";
266         }
267         std::string fName = dllPath.substr(0, index);
268         std::string fExt = dllPath.substr(index, dllPath.length());
269
270         // crossgen generate file with lower case extension only
271         std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
272         std::string niPath = fName + ".ni" + fExt;
273
274         return niPath;
275 }
276
277 static bool checkNIExistence(const std::string& absDllPath, NIOption* opt)
278 {
279         std::string absNIPath = getNIFilePath(absDllPath, opt);
280         if (absNIPath.empty()) {
281                 return false;
282         }
283
284         if (isFile(absNIPath)) {
285                 return true;
286         }
287
288         // native image of System.Private.CoreLib.dll should have to overwrite
289         // original file to support new coreclr
290         if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
291                 return isR2RImage(absDllPath);
292         }
293
294         return false;
295 }
296
297 static bool checkDllExistInDir(const std::string& path)
298 {
299         bool ret = false;
300         auto func = [&ret](const std::string& f_path, const std::string& f_name) {
301                 if (isManagedAssembly(f_name) || isR2RImage(f_name)) {
302                         ret = true;
303                 }
304         };
305
306         scanFilesInDirectory(path, func, 0);
307
308         return ret;
309 }
310
311 /*
312  * Get the list of managed files in the specific directory (of Application)
313  * Absolute paths of managed files are stored at the result list.
314  * If native image already exist in the (same / .native_image) directory, managed file is ignored.
315  */
316 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
317 {
318         if (!isDirectory(path)) {
319                 return NI_ERROR_INVALID_PARAMETER;
320         }
321
322         auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
323                 if (isManagedAssembly(f_path) && !checkNIExistence(f_path, opt)) {
324                         fileList.push_back(getAbsolutePath(f_path));
325                 }
326         };
327
328         scanFilesInDirectory(path, func, 0);
329
330         return NI_ERROR_NONE;
331 }
332
333 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
334 {
335         args.push_back(CORERUN_CMD.c_str());
336         if (CROSSGEN2_PATH != "") {
337                 args.push_back(CROSSGEN2_PATH.c_str());
338         }
339         args.push_back(CROSSGEN_OPT_JITPATH);
340         args.push_back(CLRJIT_PATH.c_str());
341         args.push_back(CROSSGEN_OPT_TARGET_ARCH);
342         args.push_back(ARCHITECTURE_IDENTIFIER);
343
344         //args.push_back(OPT_PARALLELISM);
345         //args.push_back(OPT_PARALLELISM_COUNT);
346         args.push_back(CROSSGEN_OPT_RESILIENT);
347
348         args.push_back(CROSSGEN_OPT_OPTIMIZE_TIME);
349
350         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
351                 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
352                 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
353
354                 INPUTBUBBLE_REF_VECTOR.clear();
355                 for (const auto &path : opt->inputBubbleRefFiles) {
356                         INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path);
357                 }
358                 for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
359                         if (find(args.begin(), args.end(), path) == args.end()) {
360                                 args.push_back(path.c_str());
361                         }
362                 }
363         }
364
365         if (opt->flags & NI_FLAGS_MIBC) {
366                 MIBC_VECTOR.clear();
367                 for (const auto &path : opt->mibcPath) {
368                         MIBC_VECTOR.push_back("--mibc:" + path);
369                 }
370                 for (const auto &path : MIBC_VECTOR) {
371                         if (find(args.begin(), args.end(), path) == args.end()) {
372                                 args.push_back(path.c_str());
373                         }
374                 }
375         }
376
377         if (opt->flags & NI_FLAGS_VERBOSE) {
378                 args.push_back(CROSSGEN_OPT_VERBOSE);
379         }
380
381         REF_VECTOR.clear();
382
383         // set reference path
384         for (const auto &path : opt->refFiles) {
385                 REF_VECTOR.push_back("-r:" + path);
386         }
387
388         std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
389         for (const auto &path : paths) {
390                 if (checkDllExistInDir(path)) {
391                         REF_VECTOR.push_back("-r:" + path + "/*.dll");
392                 }
393         }
394
395         if (opt->flags & NI_FLAGS_EXTRA_REF) {
396                 for (const auto &erPath : opt->extraRefPath) {
397                         std::string path = getAbsolutePath(erPath);
398                         if (checkDllExistInDir(path)) {
399                                 REF_VECTOR.push_back("-r:" + path + "/*.dll");
400                         }
401                 }
402         }
403
404         for (const auto &path : refPaths) {
405                 if (checkDllExistInDir(path)) {
406                         REF_VECTOR.push_back("-r:" + path + "/*.dll");
407                 }
408         }
409
410         for (const auto &path : REF_VECTOR) {
411                 if (find(args.begin(), args.end(), path) == args.end()) {
412                         args.push_back(path.c_str());
413                 }
414         }
415 }
416
417 static void clearArgs(std::vector<const char*>& args)
418 {
419         REF_VECTOR.clear();
420         args.clear();
421 }
422
423 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
424 {
425         std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
426         try {
427                 if (exist(pdbPath)) {
428                         std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
429                         if (!exist(targetPDBPath)) {
430                                 bf::create_symlink(pdbPath, targetPDBPath);
431                                 copySmackAndOwnership(pdbPath, targetPDBPath, true);
432                         }
433                 }
434         } catch (const bf::filesystem_error& error) {
435                 _SERR("Fail to create symlink for %s", pdbPath.c_str());
436                 return NI_ERROR_UNKNOWN;
437         }
438
439         return NI_ERROR_NONE;
440 }
441
442 static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
443         std::string outFile = niPath;
444         if (!exist(outFile)) {
445                 return NI_ERROR_NO_SUCH_FILE;
446         }
447         copySmackAndOwnership(dllPath, outFile);
448
449         // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
450         if (opt->flags & NI_FLAGS_APPNI) {
451                 outFile = getNIFilePath(dllPath, opt);
452                 makePdbSymlinkForNI(dllPath, outFile);
453
454                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE) {
455                         outFile = outFile + ".tmp";
456                 }
457
458                 if (niPath != outFile) {
459                         moveFile(niPath, outFile);
460                 }
461         }
462
463         if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
464                 if (!removeFile(dllPath)) {
465                         _SERR("Fail to remove original file : %s", dllPath.c_str());
466                 }
467         }
468
469         if (!(opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE)) {
470                 _SOUT("Native image %s generated successfully.", outFile.c_str());
471         }
472
473         return NI_ERROR_NONE;
474 }
475
476 void setPriority(NIOption* opt)
477 {
478         pid_t pid = getpid();
479         if (setpriority(PRIO_PROCESS, pid, opt->priority) == 0) {
480                 std::string str = " ";
481                 if (opt->priority <= -20) {
482                         str = " highest ";
483                 } else if (opt->priority >= 19) {
484                         str = " lowest ";
485                 }
486                 _SOUT("Success to set the%spriority of the process. pid : [%d], priority : [%d]", str.c_str(), pid, getpriority(PRIO_PROCESS, pid));
487         } else {
488                 _SERR("Failed to set the priority of the process. pid : [%d], priority : [%d]", pid, getpriority(PRIO_PROCESS, pid));
489         }
490 }
491
492 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
493 {
494         // fork crossgen2
495         pid_t pid = fork();
496         if (pid == -1)
497                 return NI_ERROR_UNKNOWN;
498
499         if (pid > 0) {
500                 int status;
501                 waitpid(pid, &status, 0);
502                 if (WIFEXITED(status)) {
503                         for (auto& dllPath: dllList) {
504                                 ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
505                                 if (ret != NI_ERROR_NONE) {
506                                         return ret;
507                                 }
508                         }
509                 } else {
510                         _SERR("Failed. Forked process terminated abnormally");
511                         return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
512                 }
513         } else {
514                 if (opt->flags & NI_FLAGS_SET_PRIORITY) {
515                         setPriority(opt);
516                 }
517                 std::vector<const char*> argv;
518                 makeArgs(argv, refPaths, opt);
519                 argv.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
520                 argv.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
521
522                 // add input files at the end of parameter
523                 for (const auto &input : dllList) {
524                         argv.push_back(input.c_str());
525                         _SOUT("+ %s", input.c_str());
526                 }
527
528                 // end param
529                 argv.push_back(nullptr);
530
531                 // print cmd
532                 if (opt->flags & NI_FLAGS_PRINT_CMD) {
533                         _SOUT("==================== NI Commands =========================");
534                         for (auto &arg: argv) _SOUT("+ %s", arg);
535                 }
536
537                 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
538
539                 clearArgs(argv);
540                 exit(0);
541         }
542
543         return NI_ERROR_NONE;
544 }
545
546 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
547 {
548         for (auto& dllPath : dllList) {
549                 std::string niPath;
550                 niPath = getNIFilePath(dllPath, opt);
551
552 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
553                 uintptr_t baseAddr = 0;
554                 if (isTPADll(dllPath)) {
555                         baseAddr = getNextBaseAddr();
556                 }
557 #endif
558                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
559                         niPath += ".tmp";
560                 }
561
562                 // fork crossgen2
563                 pid_t pid = fork();
564                 if (pid == -1)
565                         return NI_ERROR_UNKNOWN;
566
567                 if (pid > 0) {
568                         int status;
569                         waitpid(pid, &status, 0);
570                         if (WIFEXITED(status)) {
571                                 ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
572                                 if (ret != NI_ERROR_NONE) {
573                                         return ret;
574                                 }
575 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
576                                 if (baseAddr != 0) {
577                                         updateBaseAddrFile(niPath, baseAddr);
578                                 }
579 #endif
580                         } else {
581                                 _SERR("Failed. Forked process terminated abnormally");
582                                 _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
583                                 removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
584                                 return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
585                         }
586                 } else {
587                         if (opt->flags & NI_FLAGS_SET_PRIORITY) {
588                                 setPriority(opt);
589                         }
590                         std::vector<const char*> argv;
591                         makeArgs(argv, refPaths, opt);
592
593 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
594                         std::string baseAddrString;
595                         if (baseAddr != 0) {
596                                 argv.push_back("--imagebase");
597                                 std::stringstream ss;
598                                 ss << "0x" << std::hex << baseAddr;
599                                 baseAddrString = ss.str();
600                                 argv.push_back(baseAddrString.c_str());
601                         }
602 #endif
603                         argv.push_back("-o");
604                         argv.push_back(niPath.c_str());
605
606                         argv.push_back(dllPath.c_str());
607                         _SOUT("+ %s", dllPath.c_str());
608
609                         // end param
610                         argv.push_back(nullptr);
611
612                         // print cmd
613                         if (opt->flags & NI_FLAGS_PRINT_CMD) {
614                                 _SOUT("==================== NI Commands =========================");
615                                 for (auto &arg: argv) _SOUT("+ %s", arg);
616                         }
617
618                         execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
619
620                         clearArgs(argv);
621                         exit(0);
622                 }
623
624                 waitInterval();
625         }
626
627         return NI_ERROR_NONE;
628 }
629
630 static ni_error_e createCoreLibNI(NIOption* opt)
631 {
632         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
633         std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
634         std::string niTmpCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll.tmp");
635         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
636
637         std::vector<std::string> dllList;
638         std::vector<std::string> refPaths;
639         dllList.push_back(getAbsolutePath(coreLib));
640
641         if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
642                 if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE) {
643                         if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
644                                 std::ofstream output(coreLibBackup);
645                                 if (!exist(coreLibBackup)) {
646                                         _SERR("Failed to create System.Private.CoreLib.dll.Backup");
647                                         return NI_ERROR_CORE_NI_FILE;
648                                 }
649                                 copySmackAndOwnership(__pm->getRuntimePath(), coreLibBackup, false);
650                                 output.close();
651                         } else if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
652                                 _SERR("Failed to rename from System.Private.CoreLib.dll to System.Private.CoreLib.dll.Backup");
653                                 return NI_ERROR_CORE_NI_FILE;
654                         }
655                         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
656                                 if (rename(niTmpCoreLib.c_str(), coreLib.c_str())) {
657                                         _SERR("Failed to rename from System.Private.CoreLib.ni.dll.tmp to Private.CoreLib.dll");
658                                         return NI_ERROR_CORE_NI_FILE;
659                                 }
660                         } else {
661                                 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
662                                         _SERR("Failed to rename from System.Private.CoreLib.ni.dll to Private.CoreLib.dll");
663                                         return NI_ERROR_CORE_NI_FILE;
664                                 }
665                         }
666                 } else {
667                         _SERR("Failed to create native image for %s", coreLib.c_str());
668                         return NI_ERROR_CORE_NI_FILE;
669                 }
670         }
671         return NI_ERROR_NONE;
672 }
673
674 static void renameAppNITmpPath(NIOption* opt)
675 {
676         std::string niTmpPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_TMP_DIR;
677         std::string niPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_DIR;
678
679         if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
680                 niTmpPath = replaceAll(niTmpPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
681                 niPath = replaceAll(niPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
682         }
683
684         if (isDirectory(niTmpPath)) {
685                 if (rename(niTmpPath.c_str(), niPath.c_str())) {
686                         _SERR("Fail to rename from .native_image_tmp to .native_image");
687                 } else {
688                         _SOUT("Success to rename from %s to %s", niTmpPath.c_str(), niPath.c_str());
689                 }
690         }
691 }
692
693 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
694 {
695         ni_error_e ret = NI_ERROR_NONE;
696
697         if (dllList.empty()) {
698                 return NI_ERROR_INVALID_PARAMETER;
699         }
700         // When performing AOT for one Dll, an error is returned when an error occurs.
701         // However, when processing multiple dlls at once, only the log for errors is output and skipped.
702
703         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
704         bool hasSPC = false;
705
706         for (auto it = dllList.begin(); it != dllList.end(); it++) {
707                 std::string f = *it;
708                 if (!isFile(f)) {
709                         _SERR("dll file is not exist : %s", f.c_str());
710                         dllList.erase(it--);
711                 }
712                 else if (!isManagedAssembly(f)) {
713                         _SERR("Input file is not a dll file : %s", f.c_str());
714                         dllList.erase(it--);
715                 }
716                 // handle System.Private.CoreLib.dll separately.
717                 // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
718                 else if (f == coreLib) {
719                         hasSPC = true;
720                         dllList.erase(it--);
721                 }
722         }
723
724         // Error : Multiple input files matching same simple name
725         // So, Remove dulicate files from dll list
726         std::sort(dllList.begin(), dllList.end());
727         dllList.erase(unique(dllList.begin(), dllList.end()), dllList.end());
728
729         // In the case of SPC, post-processing is required to change the name of the native image.
730         // In order to avoid repeatedly checking whether the generated native image is an SPC,
731         // the SPC native image generation is performed separately.
732         if (hasSPC) {
733                 ret = createCoreLibNI(opt);
734                 if (ret != NI_ERROR_NONE) {
735                         return ret;
736                 }
737         }
738
739         // if there is no proper input after processing dll list
740         if (dllList.empty()) {
741                 if (hasSPC) {
742                         return ret;
743                 } else {
744                         return NI_ERROR_INVALID_PARAMETER;
745                 }
746         }
747
748         std::vector<std::string> paths;
749         splitPath(refPaths, paths);
750
751         if (opt->flags & NI_FLAGS_NO_PIPELINE) {
752                 ret = crossgen2NoPipeLine(dllList, paths, opt);
753         } else {
754                 std::vector<std::string> notCompiled;
755                 ret = crossgen2PipeLine(dllList, paths, opt);
756                 if (ret != NI_ERROR_NONE) {
757                         _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
758                         for (auto &dll : dllList) {
759                                 std::string tFile = changeExtension(dll, ".dll", ".ni.dll");
760                                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
761                                         tFile += ".tmp";
762                                 }
763                                 if (!exist(tFile)) {
764                                         notCompiled.push_back(dll);
765                                 }
766                         }
767                         _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
768                         ret = crossgen2NoPipeLine(notCompiled, paths, opt);
769                 }
770         }
771
772         if (ret == NI_ERROR_NONE) {
773                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
774                         for (auto &dll : dllList) {
775                                 std::string tmpFile;
776                                 std::string niFile = getNIFilePath(dll, opt);
777                                 tmpFile = niFile + ".tmp";
778
779                                 if (exist(tmpFile)) {
780                                         moveFile(tmpFile, niFile);
781                                         _SOUT("Native image %s generated successfully.", niFile.c_str());
782                                 }
783                         }
784                 }
785                 if (opt->flags & NI_FLAGS_APPNI) {
786                         renameAppNITmpPath(opt);
787                 }
788         }
789
790         return ret;
791 }
792
793 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
794 {
795         if (!isFile(dllFile)) {
796                 _SERR("dll file is not exist : %s", dllFile.c_str());
797                 return NI_ERROR_NO_SUCH_FILE;
798         }
799
800         if (checkNIExistence(dllFile, opt)) {
801                 _SERR("Native image file is already exist : %s", dllFile.c_str());
802                 return NI_ERROR_ALREADY_EXIST;
803         }
804
805         if (!isManagedAssembly(dllFile)) {
806                 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
807                 return NI_ERROR_INVALID_PARAMETER;
808         }
809
810         std::vector<std::string> dllList;
811         dllList.push_back(getAbsolutePath(dllFile));
812         return doAOTList(dllList, refPaths, opt);
813 }
814
815 static ni_error_e removeAndCreateNI(const char* pkgId, NIOption* pOptions)
816 {
817         if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
818                 _SERR("Failed to remove previous dlls from [%s]", pkgId);
819                 return NI_ERROR_UNKNOWN;
820         }
821
822         if (createNIUnderPkgRoot(pkgId, pOptions) != NI_ERROR_NONE) {
823                 _SERR("Failed to generate NI file [%s]", pkgId);
824                 return NI_ERROR_UNKNOWN;
825         } else {
826                 _SOUT("Complete make native image for pkg (%s)", pkgId);
827         }
828
829         return NI_ERROR_NONE;   
830 }
831
832 static bool isReadOnlyPkg(std::string pkgId)
833 {
834         int ret = 0;
835         bool readonly = false;
836         pkgmgrinfo_pkginfo_h handle;
837
838         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
839         if (ret != PMINFO_R_OK) {
840                 _ERR("Fail to get pkginfo");
841                 return false;
842         }
843
844         ret = pkgmgrinfo_pkginfo_is_readonly(handle, &readonly);
845         if (ret != PMINFO_R_OK) {
846                 _ERR("Fail to get is_readonly");
847                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
848                 return false;
849         }
850
851         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
852         return readonly;
853 }
854 #if 0
855 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
856 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
857 {
858         char *pkgId = NULL;
859         int ret = 0;
860         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
861         if (ret != PMINFO_R_OK) {
862                 _SERR("Failed to get pkgid");
863                 return -1;
864         }
865
866         NIOption **pOptions = (NIOption**)userData;
867         if (isReadOnlyPkg(pkgId) && (*pOptions)->flags & NI_FLAGS_SKIP_RO_APP) {
868                 return 0;
869         }
870
871         return removeAndCreateNI(pkgId, *pOptions);
872 }
873
874 // callback function of "pkgmgrinfo_pkginfo_metadata_filter_foreach"
875 static int pkgAotCb(pkgmgrinfo_pkginfo_h handle, void *userData)
876 {
877         char *pkgId = NULL;
878         int ret = 0;
879         NIOption **pOptions = (NIOption**)userData;
880
881         ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgId);
882         if (ret != PMINFO_R_OK) {
883                 _SERR("Failed to get pkgid");
884                 return -1;
885         }
886
887         return removeAndCreateNI(pkgId, *pOptions);
888 }
889 #endif
890 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
891 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
892 {
893         char *pkgId = NULL;
894         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
895         if (ret != PMINFO_R_OK) {
896                 _SERR("Failed to get pkgid");
897                 return -1;
898         }
899
900         std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
901         pkgList->push_back(pkgId);
902
903         return 0;
904 }
905
906 // callback function of "pkgmgrinfo_pkginfo_metadata_filter_foreach"
907 static int pkgAotCb(pkgmgrinfo_pkginfo_h handle, void *userData)
908 {
909         char *pkgId = NULL;
910         int ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgId);
911         if (ret != PMINFO_R_OK) {
912                 _SERR("Failed to get pkgid");
913                 return -1;
914         }
915
916         std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
917         pkgList->push_back(pkgId);
918
919         return 0;
920 }
921
922 ni_error_e initNICommon()
923 {
924 #if defined(__arm__) || defined(__aarch64__)
925
926         char *env = nullptr;
927         env = getenv("MIC_CROSSGEN2_ENABLED");
928         if (env != nullptr && !strcmp(env, "1")) {
929                 CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
930                 CROSSGEN2_PATH = "";
931                 CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
932         }
933
934         // get interval value
935         const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
936         std::ifstream inFile(intervalFile);
937         if (inFile) {
938                 _SOUT("crossgen_interval.txt is found");
939                 inFile >> __interval;
940         }
941
942         if (initializePluginManager("normal")) {
943                 _SERR("Fail to initialize PluginManager");
944                 return NI_ERROR_UNKNOWN;
945         }
946
947         try {
948                 __pm = new PathManager();
949         } catch (const std::exception& e) {
950                 _SERR("Failed to create PathManager");
951                 return NI_ERROR_UNKNOWN;
952         }
953
954         char* pluginDllPaths = pluginGetDllPath();
955         if (pluginDllPaths && pluginDllPaths[0] != '\0') {
956                 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
957         }
958
959         char* pluginNativePaths = pluginGetNativeDllSearchingPath();
960         if (pluginNativePaths && pluginNativePaths[0] != '\0') {
961                 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
962         }
963
964         return NI_ERROR_NONE;
965 #else
966         _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
967         return NI_ERROR_NOT_SUPPORTED;
968 #endif
969 }
970
971 void finalizeNICommon()
972 {
973         __interval = 0;
974
975         finalizePluginManager();
976
977         delete(__pm);
978         __pm = nullptr;
979
980         if (__ni_option) {
981                 free(__ni_option);
982                 __ni_option = nullptr;
983         }
984 }
985
986 ni_error_e createNIPlatform(std::string& extraInputs, NIOption* opt)
987 {
988         extraInputs += ":" + __pm->getRuntimePath();
989         extraInputs += ":" + __pm->getTizenFXPath();
990
991         return createNIUnderDirs(extraInputs, opt);
992 }
993
994 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
995 {
996         return doAOTFile(dllPath, std::string(), opt);
997 }
998
999 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
1000 {
1001         ni_error_e ret;
1002
1003         bool isAppNI = false;
1004         if (opt->flags & NI_FLAGS_APPNI) {
1005                 isAppNI = true;
1006         }
1007
1008         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
1009                 std::vector<std::string> refs;
1010                 splitPath(refPaths, refs);
1011                 for (auto &p: refs) {
1012                         if (isDirectory(p) && checkDllExistInDir(p)) {
1013                                 opt->inputBubbleRefFiles.push_back(p + "/*.dll");
1014                         }
1015                 }
1016         }
1017
1018         // get managed file list from targetPath
1019         std::vector<std::string> dllList;
1020         ret = getTargetDllList(targetPath, dllList, opt);
1021         if (ret != NI_ERROR_NONE) {
1022                 return ret;
1023         }
1024
1025         std::vector<std::string> needNIList;
1026         std::vector<std::string> niList;
1027
1028         for (auto &dll : dllList) {
1029                 if (!checkNIExistence(dll, opt)) {
1030                         needNIList.push_back(dll);
1031                 }
1032                 niList.push_back(getNIFilePath(dll, opt));
1033         }
1034
1035         if (!needNIList.empty()) {
1036                 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
1037                 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
1038                 opt->flags &= ~NI_FLAGS_APPNI;
1039                 ret = doAOTList(needNIList, refPaths, opt);
1040                 if (isAppNI) {
1041                         opt->flags |= NI_FLAGS_APPNI;
1042                 }
1043                 if (ret != NI_ERROR_NONE) {
1044                         return ret;
1045                 }
1046         }
1047
1048         if (isAppNI) {
1049                 for (auto &niPath : niList) {
1050                         if (exist(niPath)) {
1051                                 std::string symNIPath = concatPath(targetPath, getFileName(niPath));
1052                                 if (!exist(symNIPath)) {
1053                                         bf::create_symlink(niPath, symNIPath);
1054                                         copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
1055                                         _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
1056                                         _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
1057                                 }
1058                         }
1059                 }
1060         }
1061
1062         return NI_ERROR_NONE;
1063 }
1064
1065
1066 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
1067 {
1068         ni_error_e ret = NI_ERROR_NONE;
1069
1070         std::vector<std::string> fileList;
1071         std::vector<std::string> paths;
1072         splitPath(rootPaths, paths);
1073
1074         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
1075                 for (auto &p: paths) {
1076                         if (isDirectory(p) && checkDllExistInDir(p)) {
1077                                 opt->inputBubbleRefFiles.push_back(p + "/*.dll");
1078                         }
1079                 }
1080         }
1081
1082         for (const auto &path : paths) {
1083                 if (!exist(path)) {
1084                         continue;
1085                 }
1086
1087                 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
1088                         ret = createNIUnderTAC(path, rootPaths, opt);
1089                         if (ret != NI_ERROR_NONE) {
1090                                 return ret;
1091                         }
1092                 } else {
1093                         ret = getTargetDllList(path, fileList, opt);
1094                         if (ret != NI_ERROR_NONE) {
1095                                 return ret;
1096                         }
1097                 }
1098         }
1099
1100         if (fileList.empty()) {
1101                 return NI_ERROR_NONE;
1102         }
1103
1104         return doAOTList(fileList, rootPaths, opt);
1105 }
1106
1107 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
1108 {
1109         ni_error_e ret;
1110
1111         if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
1112                 _SERR("The native image of System.Private.CoreLib does not exist.\n"
1113                                 "Run the command to create the native image\n"
1114                                 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
1115                 return NI_ERROR_CORE_NI_FILE;
1116         }
1117
1118         std::string rootPath = getRootPath(pkgId);
1119         if (rootPath.empty()) {
1120                 _SERR("Failed to get root path from [%s]", pkgId.c_str());
1121                 return NI_ERROR_INVALID_PACKAGE;
1122         }
1123
1124         char* extraDllPaths = pluginGetExtraDllPath();
1125         if (extraDllPaths && extraDllPaths[0] != '\0') {
1126                 opt->flags |= NI_FLAGS_EXTRA_REF;
1127                 splitPath(extraDllPaths, opt->extraRefPath);
1128         }
1129
1130         if (isRPK(pkgId)) {
1131                 opt->flags &= ~NI_FLAGS_APPNI;
1132                 std::string paths = getResourcePaths(rootPath);
1133                 if (!paths.empty()) {
1134                         ret = createNIUnderDirs(paths, opt);             
1135                 } else {
1136                         _SERR("Failed to get rpk paths from [%s]", pkgId.c_str());
1137                         ret = NI_ERROR_UNKNOWN;
1138                 }
1139         } else {
1140                 __pm->setAppRootPath(rootPath);
1141
1142                 opt->flags |= NI_FLAGS_APPNI;
1143
1144                 if (isReadOnlyArea(rootPath)) {
1145                         opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
1146                         opt->flags |= NI_FLAGS_NO_PIPELINE;
1147                         _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
1148                 } else {
1149                         opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
1150                 }
1151
1152                 // create native image under bin and lib directory
1153                 // tac directory is skipped in the createNIUnderDirs.
1154                 ret = createNIUnderDirs(__pm->getAppPaths(), opt);
1155         }
1156
1157         return ret;
1158 }
1159
1160 void removeNIPlatform()
1161 {
1162         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
1163         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
1164
1165         if (isR2RImage(coreLib)) {
1166                 if (!isFile(coreLibBackup)) {
1167                         return;
1168                 }
1169
1170                 if (remove(coreLib.c_str())) {
1171                         _SERR("Failed to remove System.Private.CoreLib native image file");
1172                 }
1173                 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
1174                         _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
1175                 }
1176         }
1177
1178 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
1179         if (isFile(__SYSTEM_BASE_FILE)) {
1180                 if (remove(__SYSTEM_BASE_FILE)) {
1181                         _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
1182                 }
1183         }
1184 #endif
1185
1186         removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
1187 }
1188
1189 void removeNIUnderDirs(const std::string& rootPaths)
1190 {
1191         auto convert = [](const std::string& path, const std::string& filename) {
1192                 if (isR2RImage(path)) {
1193                         std::string assemblyPath = changeExtension(path, ".ni.dll", ".dll");
1194                         if (exist(assemblyPath)) {
1195                                 if (remove(path.c_str())) {
1196                                         _SERR("Failed to remove %s", path.c_str());
1197                                 }
1198                         } else {
1199                                 _SOUT("%s cannot be removed because there is no %s", path.c_str(), assemblyPath.c_str());
1200                         }
1201                 }
1202         };
1203
1204         std::vector<std::string> paths;
1205         splitPath(rootPaths, paths);
1206         for (const auto &path : paths) {
1207                 scanFilesInDirectory(path, convert, -1);
1208         }
1209 }
1210
1211 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
1212 {
1213         std::vector<std::string> paths;
1214
1215         std::string rootPath = getRootPath(pkgId);
1216         if (rootPath.empty()) {
1217                 _SERR("Failed to get root path from [%s]", pkgId.c_str());
1218                 return NI_ERROR_INVALID_PACKAGE;
1219         }
1220
1221         if (isRPK(pkgId)) {
1222                 std::string rpkPaths = getResourcePaths(rootPath);
1223                 if (rpkPaths.empty()) {
1224                         _SERR("Failed to get rpk path from [%s]", pkgId.c_str());
1225                         return NI_ERROR_UNKNOWN;
1226                 }
1227                 splitPath(rpkPaths, paths);
1228                 for (const auto &path : paths) {
1229                         if (isDirectory(path)) {
1230                                 removeNIUnderDirs(path);
1231                         }
1232                 }               
1233         } else {
1234                 __pm->setAppRootPath(rootPath);
1235
1236                 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
1237                 std::string appNIPaths = __pm->getAppNIPaths();
1238                 splitPath(appNIPaths, paths);
1239                 for (const auto &path : paths) {
1240                         if (!isReadOnlyArea(path)) {
1241                                 // Only the native image inside the TAC should be removed.
1242                                 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
1243                                         removeNIUnderDirs(path);
1244                                 } else {
1245                                         if (isDirectory(path)) {
1246                                                 if (!removeAll(path.c_str())) {
1247                                                         _SERR("Failed to remove app ni dir [%s]", path.c_str());
1248                                                 }
1249                                         }
1250                                 }
1251                         }
1252                 }
1253
1254                 // In special cases, the ni file may exist in the dll location.
1255                 // The code below is to avoid this exceptional case.
1256                 std::string appPaths = __pm->getAppPaths();
1257                 splitPath(appPaths, paths);
1258                 for (const auto &path : paths) {
1259                         if (isDirectory(path)) {
1260                                 removeNIUnderDirs(path);
1261                         }
1262                 }
1263         }
1264
1265         return NI_ERROR_NONE;
1266 }
1267
1268 ni_error_e regeneratePkgNI(NIOption* opt)
1269 {
1270         std::vector<std::string> pkgList;
1271         ni_error_e ret = NI_ERROR_NONE;
1272
1273         // iterates for Packages's metadata (RPK)
1274         pkgmgrinfo_pkginfo_metadata_filter_h pkgHandle;
1275         if (pkgmgrinfo_pkginfo_metadata_filter_create(&pkgHandle) == PMINFO_R_OK) {
1276                 if (pkgmgrinfo_pkginfo_metadata_filter_add(pkgHandle, AOT_METADATA_KEY, METADATA_VALUE_TRUE) == PMINFO_R_OK) {
1277                         if (pkgmgrPkgMDFilterForeach(pkgHandle, pkgAotCb, &pkgList) != 0) {
1278                                 ret = NI_ERROR_UNKNOWN;
1279                                 _ERR("pkgmgrPkgMDFilterForeach failed");
1280                         }
1281                 } else {
1282                         ret = NI_ERROR_UNKNOWN;
1283                         _ERR("pkgmgrinfo_pkginfo_metadata_filter_add failed");
1284                 }
1285                 pkgmgrinfo_pkginfo_metadata_filter_destroy(pkgHandle);
1286         } else {
1287                 ret = NI_ERROR_UNKNOWN;
1288                 _ERR("pkgmgrinfo_pkginfo_metadata_filter_create failed");
1289         }
1290
1291         // iterate for App's metadata
1292         pkgmgrinfo_appinfo_metadata_filter_h appHandle;
1293         if (pkgmgrinfo_appinfo_metadata_filter_create(&appHandle) == PMINFO_R_OK) {
1294                 if (pkgmgrinfo_appinfo_metadata_filter_add(appHandle, AOT_METADATA_KEY, METADATA_VALUE_TRUE) == PMINFO_R_OK) {
1295                         if (pkgmgrAppMDFilterForeach(appHandle, appAotCb, &pkgList) != 0) {
1296                                 ret = NI_ERROR_UNKNOWN;
1297                                 _ERR("pkgmgrAppMDFilterForeach failed");
1298                         }
1299                 } else {
1300                         ret = NI_ERROR_UNKNOWN;
1301                         _ERR("pkgmgrinfo_appinfo_metadata_filter_add failed");
1302                 }
1303                 pkgmgrinfo_appinfo_metadata_filter_destroy(appHandle);
1304         } else {
1305                 ret = NI_ERROR_UNKNOWN;
1306                 _ERR("pkgmgrinfo_appinfo_metadata_filter_create failed");
1307         }
1308
1309         // remove duplicated pkg in the list.
1310         // If one package has multiple apps, there can be duplicate values.
1311         std::sort(pkgList.begin(), pkgList.end());
1312         pkgList.erase(unique(pkgList.begin(), pkgList.end()), pkgList.end());
1313
1314         for (auto pkg : pkgList) {
1315                 if (isReadOnlyPkg(pkg) && opt->flags & NI_FLAGS_SKIP_RO_APP) {
1316                         continue;
1317                 }
1318
1319                 if (removeAndCreateNI(pkg.c_str(), opt) != NI_ERROR_NONE) {
1320                         _SERR("Failed to remove previous dlls from [%s]", pkg.c_str());
1321                         ret = NI_ERROR_UNKNOWN;
1322                 } else {
1323                         _SOUT("Complete make application to native image");
1324                 }
1325         }
1326
1327         return ret;
1328 }
1329
1330 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
1331 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
1332 {
1333         char *pkgId = NULL;
1334         char *root = NULL;
1335         NIOption **pOpt = (NIOption**)userData;
1336
1337         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1338         if (ret != PMINFO_R_OK || pkgId == NULL) {
1339                 _SERR("Failed to get pkgid");
1340                 return -1;
1341         }
1342
1343         ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
1344         if (ret != PMINFO_R_OK) {
1345                 _SERR("Failed to get root path");
1346                 return -1;
1347         }
1348
1349         std::string binPath = concatPath(std::string(root), "bin");
1350         if (exist(concatPath(binPath, PRE_COMPILED_PACKAGE_FILE))) {
1351                 _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId);
1352                 return 0;
1353         }
1354
1355         sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
1356         if (!tac_db) {
1357                 _SERR("Sqlite open error");
1358                 return -1;
1359         }
1360         sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
1361
1362         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
1363         std::vector<std::string> nugets = selectDB(tac_db, sql);
1364         sqlite3_free(sql);
1365
1366         if (tac_db) {
1367                 closeDB(tac_db);
1368                 tac_db = NULL;
1369         }
1370
1371         std::string nugetPaths;
1372         for (const auto &nuget : nugets) {
1373                 if (!nugetPaths.empty()) {
1374                         nugetPaths += ":";
1375                 }
1376                 nugetPaths += concatPath(__DOTNET_DIR, nuget);
1377         }
1378
1379         for (auto& nuget : nugets) {
1380                 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
1381         }
1382
1383         return 0;
1384 }
1385
1386 ni_error_e regenerateTACNI(NIOption* opt)
1387 {
1388         removeNIUnderDirs(__DOTNET_DIR);
1389
1390         pkgmgrinfo_appinfo_metadata_filter_h handle;
1391         int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1392         if (ret != PMINFO_R_OK) {
1393                 return NI_ERROR_UNKNOWN;
1394         }
1395
1396         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
1397         if (ret != PMINFO_R_OK) {
1398                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1399                 return NI_ERROR_UNKNOWN;
1400         }
1401
1402         ret = pkgmgrAppMDFilterForeach(handle, regenTacCb, &opt);
1403         if (ret != 0) {
1404                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1405                 return NI_ERROR_UNKNOWN;
1406         }
1407
1408         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1409
1410         return NI_ERROR_NONE;
1411 }
1412