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"
39 #include <sys/smack.h>
41 #include "ni_common.h"
42 #include "path_manager.h"
43 #include "plugin_manager.h"
48 #define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
51 #error "CROSSGEN_PATH is missed"
55 #define __STR(x) __XSTR(x)
56 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
60 static int __interval = 0;
61 static std::string __tpa;
63 static void waitInterval()
65 // by the recommand, ignore small value for performance.
66 if (__interval > 10000) {
67 fprintf(stderr, "sleep %d usec\n", __interval);
72 static std::string getNiFileName(const std::string& dllPath)
74 size_t index = dllPath.find_last_of(".");
75 if (index == std::string::npos) {
76 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
79 std::string fName = dllPath.substr(0, index);
80 std::string fExt = dllPath.substr(index, dllPath.length());
82 // crossgen generate file with lower case extension only
83 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
84 std::string niPath = fName + ".ni" + fExt;
89 static bool niExist(const std::string& path)
91 std::string f = getNiFileName(path);
100 // native image of System.Private.CoreLib.dll should have to overwrite
101 // original file to support new coreclr
102 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
103 std::string coreLibBackup = path + ".Backup";
104 if (isFileExist(coreLibBackup)) {
112 static void updateNiFileInfo(const std::string& dllPath, const std::string& niPath)
116 // change smack label
117 if (smack_getlabel(dllPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
118 if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
119 fprintf(stderr, "Fail to set smack label\n");
124 // change owner and groups for generated ni file.
126 if (!stat(dllPath.c_str(), &info)) {
127 if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
128 fprintf(stderr, "Failed to change owner and group name\n");
132 static int crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R)
134 if (!isFileExist(dllPath)) {
135 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
139 if (!isManagedAssembly(dllPath)) {
140 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
144 if (niExist(dllPath)) {
145 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
149 std::string absDllPath = absolutePath(dllPath);
150 std::string absNiPath = getNiFileName(dllPath);
151 if (absNiPath.empty()) {
152 fprintf(stderr, "Fail to get ni file name\n");
162 waitpid(pid, &status, 0);
163 if (WIFEXITED(status)) {
164 // Do not use niExist() function to check whether ni file created or not.
165 // niEixst() return false for System.Private.Corelib.dll
166 if (isFileExist(absNiPath)) {
167 updateNiFileInfo(absDllPath, absNiPath);
170 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
175 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
176 std::vector<const char*> argv = {
179 "/Trusted_Platform_Assemblies", __tpa.c_str(),
180 "/JITPath", jitPath.c_str()
184 argv.push_back("/FragileNonVersionable");
187 argv.push_back("/App_Paths");
188 std::string absAppPath;
189 if (!appPath.empty()) {
190 absAppPath = appPath;
192 absAppPath = baseName(absDllPath);
194 argv.push_back(absAppPath.c_str());
196 argv.push_back("/out");
197 argv.push_back(absNiPath.c_str());
199 argv.push_back(absDllPath.c_str());
200 argv.push_back(nullptr);
202 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
204 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
211 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
215 bool* enableR2R = (bool*)userData;
217 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
218 if (ret != PMINFO_R_OK) {
219 fprintf(stderr, "Failed to get pkgid\n");
223 if (removeNiUnderPkgRoot(pkgId) != 0) {
224 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
228 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
229 if (createNiUnderPkgRoot(pkgId, *enableR2R) != 0) {
230 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
233 fprintf(stderr, "Complete make application to native image\n");
239 static void createCoreLibNI(bool enableR2R)
241 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
242 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
243 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
245 if (!isFileExist(coreLibBackup)) {
246 if (!crossgen(coreLib, std::string(), enableR2R)) {
247 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
248 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
250 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
251 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
254 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
259 int initNICommon(NiCommonOption* option)
262 // get interval value
263 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
264 std::ifstream inFile(intervalFile);
266 fprintf(stderr, "crossgen_interval.txt is found\n");
267 inFile >> __interval;
270 if (initializePluginManager("normal")) {
271 fprintf(stderr, "Fail to initialize plugin manager\n");
274 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
275 fprintf(stderr, "Fail to initialize path manager\n");
283 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
288 void finalizeNICommon()
292 finalizePluginManager();
293 finalizePathManager();
299 void createNiPlatform(bool enableR2R)
301 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
302 createNiUnderDirs(platformDirs, 2, enableR2R);
305 int createNiDll(const std::string& dllPath, bool enableR2R)
307 createCoreLibNI(enableR2R);
308 return crossgen(dllPath, std::string(), enableR2R);
311 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
313 createCoreLibNI(enableR2R);
315 std::string appPaths;
316 for (int i = 0; i < count; i++) {
317 appPaths += rootPaths[i];
321 if (appPaths.back() == ':')
324 auto convert = [&appPaths, enableR2R](const std::string& path, const char* name) {
325 if (!crossgen(path, appPaths.c_str(), enableR2R)) {
330 for (int i = 0; i < count; i++) {
331 scanFilesInDir(rootPaths[i], convert, 1);
335 int createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
338 if (getRootPath(pkgId, pkgRoot) < 0) {
339 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
343 std::string binDir = concatPath(pkgRoot, "bin");
344 std::string libDir = concatPath(pkgRoot, "lib");
345 std::string appTAC = concatPath(binDir, ".TAC.Release");
346 std::string paths[] = {binDir, libDir, appTAC};
348 createNiUnderDirs(paths, 3, enableR2R);
353 int createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
356 if (getRootPath(pkgId, pkgRoot) < 0) {
357 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
361 std::string binDir = concatPath(pkgRoot, "bin");
362 std::string libDir = concatPath(pkgRoot, "lib");
363 std::string paths = binDir + ":" + libDir;
365 return crossgen(dllPath, paths, enableR2R);
368 void removeNiPlatform()
370 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
371 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
373 if (!isFileExist(coreLibBackup)) {
377 if (remove(coreLib.c_str())) {
378 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
381 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
382 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
385 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
387 removeNiUnderDirs(platformDirs, 2);
390 void removeNiUnderDirs(const std::string rootPaths[], int count)
392 auto convert = [](const std::string& path, std::string name) {
393 if (isNativeImage(path)) {
394 if (remove(path.c_str())) {
395 fprintf(stderr, "Failed to remove %s\n", path.c_str());
400 for (int i = 0; i < count; i++)
401 scanFilesInDir(rootPaths[i], convert, -1);
404 int removeNiUnderPkgRoot(const std::string& pkgId)
407 if (getRootPath(pkgId, pkgRoot) < 0) {
408 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
412 std::string binDir = concatPath(pkgRoot, "bin");
413 std::string libDir = concatPath(pkgRoot, "lib");
414 std::string paths[] = {binDir, libDir};
416 removeNiUnderDirs(paths, 2);
421 int regenerateAppNI(bool enableR2R)
424 pkgmgrinfo_appinfo_metadata_filter_h handle;
426 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
427 if (ret != PMINFO_R_OK)
430 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, "http://tizen.org/metadata/prefer_dotnet_aot", "true");
431 if (ret != PMINFO_R_OK) {
432 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
436 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
437 if (ret != PMINFO_R_OK) {
438 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
439 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
443 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
445 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);