2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
23 #include "pkgmgr_parser_plugin_interface.h"
38 #include "ni_common.h"
39 #include "path_manager.h"
40 #include "plugin_manager.h"
45 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
48 #error "CROSSGEN_PATH is missed"
52 #define __STR(x) __XSTR(x)
53 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
57 static int __interval = 0;
58 static std::string __tpa;
60 static void waitInterval()
62 // by the recommand, ignore small value for performance.
63 if (__interval > 10000) {
64 fprintf(stderr, "sleep %d usec\n", __interval);
69 static std::string getNiFilePath(const std::string& dllPath)
71 size_t index = dllPath.find_last_of(".");
72 if (index == std::string::npos) {
73 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
76 std::string fName = dllPath.substr(0, index);
77 std::string fExt = dllPath.substr(index, dllPath.length());
79 // crossgen generate file with lower case extension only
80 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
81 std::string niPath = fName + ".ni" + fExt;
86 static std::string getAppNIPath(const std::string& niPath)
88 size_t index = niPath.find_last_of("/");
89 if (index == std::string::npos) {
90 fprintf(stderr, "dllPath doesnot contains path info\n");
94 std::string prevPath = niPath.substr(0, index);
95 std::string fileName = niPath.substr(index, niPath.length());
96 std::string niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
98 if (!isFileExist(niDirPath)) {
99 if (mkdir(niDirPath.c_str(), 0755) == 0) {
100 updateAssemblyInfo(prevPath, niDirPath);
102 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
106 return niDirPath + fileName;
109 static bool niExist(const std::string& path)
111 std::string f = getNiFilePath(path);
116 if (isFileExist(f)) {
120 // native image of System.Private.CoreLib.dll should have to overwrite
121 // original file to support new coreclr
122 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
123 std::string coreLibBackup = path + ".Backup";
124 if (isFileExist(coreLibBackup)) {
132 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false)
134 if (!isFileExist(dllPath)) {
135 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
136 return NI_ERROR_NO_SUCH_FILE;
139 if (!isManagedAssembly(dllPath)) {
140 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
141 return NI_ERROR_INVALID_PARAMETER;
144 if (niExist(dllPath)) {
145 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
146 return NI_ERROR_ALREADY_EXIST;
149 std::string absDllPath = absolutePath(dllPath);
150 std::string absNiPath = getNiFilePath(dllPath);
151 if (absNiPath.empty()) {
152 fprintf(stderr, "Fail to get ni file name\n");
153 return NI_ERROR_UNKNOWN;
157 absNiPath = getAppNIPath(absNiPath);
162 return NI_ERROR_UNKNOWN;
166 waitpid(pid, &status, 0);
167 if (WIFEXITED(status)) {
168 // Do not use niExist() function to check whether ni file created or not.
169 // niEixst() return false for System.Private.Corelib.dll
170 if (isFileExist(absNiPath)) {
171 updateAssemblyInfo(absDllPath, absNiPath);
172 return NI_ERROR_NONE;
174 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
175 return NI_ERROR_NO_SUCH_FILE;
179 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
180 std::vector<const char*> argv = {
183 "/Trusted_Platform_Assemblies", __tpa.c_str(),
184 "/JITPath", jitPath.c_str()
188 argv.push_back("/FragileNonVersionable");
191 argv.push_back("/App_Paths");
192 std::string absAppPath;
193 if (!appPath.empty()) {
194 absAppPath = appPath;
196 absAppPath = baseName(absDllPath);
198 argv.push_back(absAppPath.c_str());
200 argv.push_back("/out");
201 argv.push_back(absNiPath.c_str());
203 argv.push_back(absDllPath.c_str());
204 argv.push_back(nullptr);
206 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
208 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
212 return NI_ERROR_NONE;
215 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
216 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
220 bool* enableR2R = (bool*)userData;
222 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
223 if (ret != PMINFO_R_OK) {
224 fprintf(stderr, "Failed to get pkgid\n");
228 if (removeNiUnderPkgRoot(pkgId) != 0) {
229 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
233 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
234 if (createNiUnderPkgRoot(pkgId, *enableR2R) != 0) {
235 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
238 fprintf(stderr, "Complete make application to native image\n");
244 static void createCoreLibNI(bool enableR2R)
246 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
247 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
248 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
250 if (!isFileExist(coreLibBackup)) {
251 if (!crossgen(coreLib, std::string(), enableR2R)) {
252 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
253 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
255 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
256 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
259 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
264 ni_error_e initNICommon(NiCommonOption* option)
267 // get interval value
268 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
269 std::ifstream inFile(intervalFile);
271 fprintf(stderr, "crossgen_interval.txt is found\n");
272 inFile >> __interval;
275 if (initializePluginManager("normal")) {
276 fprintf(stderr, "Fail to initialize plugin manager\n");
277 return NI_ERROR_UNKNOWN;
279 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
280 fprintf(stderr, "Fail to initialize path manager\n");
281 return NI_ERROR_UNKNOWN;
286 return NI_ERROR_NONE;
288 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
289 return NI_ERROR_NOT_SUPPORTED;
293 void finalizeNICommon()
297 finalizePluginManager();
298 finalizePathManager();
304 void createNiPlatform(bool enableR2R)
306 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
307 createNiUnderDirs(platformDirs, 2, enableR2R);
310 ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
312 createCoreLibNI(enableR2R);
313 // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
314 // Skip if input dll is System.Private.CoreLib.dll
315 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
316 return NI_ERROR_NONE;
319 return crossgen(dllPath, std::string(), enableR2R);
322 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
324 createCoreLibNI(enableR2R);
326 std::string appPaths;
327 for (int i = 0; i < count; i++) {
328 appPaths += rootPaths[i];
332 if (appPaths.back() == ':')
335 auto convert = [&appPaths, enableR2R, isAppNI](const std::string& path, const char* name) {
336 if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
341 for (int i = 0; i < count; i++) {
342 scanFilesInDir(rootPaths[i], convert, 1);
346 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
349 if (getRootPath(pkgId, pkgRoot) != NI_ERROR_NONE) {
350 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
351 return NI_ERROR_INVALID_PACKAGE;
354 std::string binDir = concatPath(pkgRoot, "bin");
355 std::string libDir = concatPath(pkgRoot, "lib");
356 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
357 std::string paths[] = {binDir, libDir, tacDir};
359 createNiUnderDirs(paths, 3, enableR2R, true);
361 return NI_ERROR_NONE;
364 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
367 if (getRootPath(pkgId, pkgRoot) < 0) {
368 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
369 return NI_ERROR_INVALID_PACKAGE;
372 std::string binDir = concatPath(pkgRoot, "bin");
373 std::string libDir = concatPath(pkgRoot, "lib");
374 std::string paths = binDir + ":" + libDir;
376 return crossgen(dllPath, paths, enableR2R, true);
379 void removeNiPlatform()
381 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
382 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
384 if (!isFileExist(coreLibBackup)) {
388 if (remove(coreLib.c_str())) {
389 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
392 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
393 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
396 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
398 removeNiUnderDirs(platformDirs, 2);
401 void removeNiUnderDirs(const std::string rootPaths[], int count)
403 auto convert = [](const std::string& path, std::string name) {
404 if (isNativeImage(path)) {
405 if (remove(path.c_str())) {
406 fprintf(stderr, "Failed to remove %s\n", path.c_str());
411 for (int i = 0; i < count; i++)
412 scanFilesInDir(rootPaths[i], convert, -1);
415 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
418 if (getRootPath(pkgId, pkgRoot) < 0) {
419 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
420 return NI_ERROR_INVALID_PACKAGE;
423 std::string binDir = concatPath(pkgRoot, "bin");
424 std::string libDir = concatPath(pkgRoot, "lib");
425 std::string paths[] = {binDir, libDir};
427 removeNiUnderDirs(paths, 2);
429 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
430 if (isFileExist(binNIDir)) {
431 if (rmdir(binNIDir.c_str()) != 0) {
432 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
436 std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
437 if (isFileExist(libNIDir)) {
438 if (rmdir(libNIDir.c_str()) != 0) {
439 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
443 return NI_ERROR_NONE;
446 ni_error_e regenerateAppNI(bool enableR2R)
449 pkgmgrinfo_appinfo_metadata_filter_h handle;
451 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
452 if (ret != PMINFO_R_OK)
453 return NI_ERROR_UNKNOWN;
455 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
456 if (ret != PMINFO_R_OK) {
457 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
458 return NI_ERROR_UNKNOWN;
461 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
462 if (ret != PMINFO_R_OK) {
463 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
464 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
465 return NI_ERROR_UNKNOWN;
468 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
470 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
471 return NI_ERROR_NONE;