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 bool niExist(const std::string& path, std::string& ni)
74 size_t index = path.find_last_of(".");
75 if (index == std::string::npos) {
78 std::string fName = path.substr(0, index);
79 std::string fExt = path.substr(index, path.length());
81 // crossgen generate file with lower case extension only
82 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
83 std::string f = fName + ".ni" + fExt;
89 // native image of System.Private.CoreLib.dll should have to overwrite
90 // original file to support new coreclr
91 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
92 std::string coreLibBackup = path + ".Backup";
93 if (isFileExist(coreLibBackup)) {
102 static void updateNiFileInfo(const std::string& path)
107 if (niExist(path, niPath)) {
108 // change smack label
109 if (smack_getlabel(path.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
110 if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
111 fprintf(stderr, "Fail to set smack label\n");
116 // change owner and groups for generated ni file.
118 if (!stat(path.c_str(), &info)) {
119 if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
120 fprintf(stderr, "Failed to change owner and group name\n");
125 static void crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R)
127 std::string absDllPath = absolutePath(dllPath);
135 waitpid(pid, &status, 0);
136 if (WIFEXITED(status)) {
137 updateNiFileInfo(absDllPath);
141 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
142 std::vector<const char*> argv = {
145 "/Trusted_Platform_Assemblies", __tpa.c_str(),
146 "/JITPath", jitPath.c_str()
150 argv.push_back("/FragileNonVersionable");
153 argv.push_back("/App_Paths");
154 std::string absAppPath;
155 if (!appPath.empty()) {
156 absAppPath = appPath;
158 absAppPath = baseName(absDllPath);
160 argv.push_back(absAppPath.c_str());
162 argv.push_back(absDllPath.c_str());
163 argv.push_back(nullptr);
165 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
167 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
172 static int getRootPath(std::string pkgId, std::string& rootPath)
179 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
180 _ERR("Failed to get UID");
184 pkgmgrinfo_pkginfo_h handle;
186 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
187 if (ret != PMINFO_R_OK)
190 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
191 if (ret != PMINFO_R_OK)
195 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
196 if (ret != PMINFO_R_OK) {
197 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
201 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
207 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
212 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
213 if (ret != PMINFO_R_OK) {
214 fprintf(stderr, "Failed to get pkgid\n");
218 if (removeNiUnderPkgRoot(pkgId) != 0) {
219 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
223 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
224 if (createNiUnderPkgRoot(pkgId, true) != 0) {
225 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
228 fprintf(stderr, "Complete make application to native image\n");
234 static void createCoreLibNI(bool enableR2R)
236 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
237 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
238 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
240 if (!niExist(coreLib, niCoreLib)) {
241 crossgen(coreLib, std::string(), enableR2R);
242 if (isFileExist(niCoreLib)) {
243 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
244 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
246 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
247 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
250 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
255 int initNICommon(NiCommonOption* option)
257 // get interval value
258 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
259 std::ifstream inFile(intervalFile);
261 fprintf(stderr, "crossgen_interval.txt is found\n");
262 inFile >> __interval;
265 if (initializePluginManager("normal")) {
266 fprintf(stderr, "Fail to initialize plugin manager\n");
269 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
270 fprintf(stderr, "Fail to initialize path manager\n");
279 void finalizeNICommon()
283 finalizePluginManager();
284 finalizePathManager();
290 void createNiPlatform(bool enableR2R)
292 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
294 createNiUnderDirs(platformDirs, 2, enableR2R);
297 void createNiDll(const std::string& dllPath, bool enableR2R)
299 createCoreLibNI(enableR2R);
301 if (!isFileExist(dllPath)) {
302 fprintf(stderr, "Failed to find dll : %s\n", dllPath.c_str());
307 if (niExist(dllPath, niPath)) {
308 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
312 crossgen(dllPath, std::string(), enableR2R);
313 if (!niExist(dllPath, niPath)) {
314 fprintf(stderr, "Failed to create native image for %s\n", dllPath.c_str());
318 void createNiUnderDirs(const std::string rootPaths[], int count, const std::string ignores[], int igcount, afterCreate cb, bool enableR2R)
320 createCoreLibNI(enableR2R);
322 std::string appPaths;
323 for (int i = 0; i < count; i++) {
324 appPaths += rootPaths[i];
328 if (appPaths.back() == ':')
331 auto convert = [&appPaths, ignores, igcount, &cb, enableR2R](const std::string& path, const char* name) {
332 for (int i = 0; i < igcount; i++) {
333 if (path == ignores[i])
337 if (isManagedAssembly(path)) {
338 if (niExist(path, niPath)) {
339 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
342 crossgen(path, appPaths.c_str(), enableR2R);
343 if (niExist(path, niPath)) {
347 fprintf(stderr, "Failed to create native image for %s\n", path.c_str());
353 for (int i = 0; i < count; i++)
354 scanFilesInDir(rootPaths[i], convert, 1);
356 void createNiUnderDirs(const std::string rootPaths[], int count, afterCreate cb, bool enableR2R)
358 createNiUnderDirs(rootPaths, count, nullptr, 0, cb, enableR2R);
360 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
362 createNiUnderDirs(rootPaths, count, nullptr, enableR2R);
365 int createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R)
368 if (getRootPath(pkgName, pkgRoot) < 0)
371 std::string binDir = concatPath(pkgRoot, "bin");
372 std::string libDir = concatPath(pkgRoot, "lib");
373 std::string paths[] = {binDir, libDir};
375 createNiUnderDirs(paths, 2, enableR2R);
380 int createNiDllUnderPkgRoot(const std::string& pkgName, const std::string& dllPath, bool enableR2R)
383 if (getRootPath(pkgName, pkgRoot) < 0)
386 std::string binDir = concatPath(pkgRoot, "bin");
387 std::string libDir = concatPath(pkgRoot, "lib");
388 std::string appPaths = binDir + ":" + libDir;
390 if (!isFileExist(dllPath)) {
391 fprintf(stderr, "Failed to find dll : %s\n", dllPath.c_str());
396 if (niExist(dllPath, niPath)) {
397 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
401 crossgen(dllPath, appPaths, enableR2R);
402 if (!niExist(dllPath, niPath)) {
403 fprintf(stderr, "Failed to create native image for %s\n", dllPath.c_str());
410 void removeNiPlatform()
412 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
413 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
415 if (!isFileExist(coreLibBackup)) {
419 if (remove(coreLib.c_str())) {
420 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
423 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
424 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
427 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
429 removeNiUnderDirs(platformDirs, 2);
432 void removeNiUnderDirs(const std::string rootPaths[], int count)
434 auto convert = [](const std::string& path, std::string name) {
436 if (isNativeImage(path)) {
437 if (remove(path.c_str())) {
438 fprintf(stderr, "Failed to remove %s\n", path.c_str());
443 for (int i = 0; i < count; i++)
444 scanFilesInDir(rootPaths[i], convert, -1);
447 int removeNiUnderPkgRoot(const std::string& pkgName)
450 if (getRootPath(pkgName, pkgRoot) < 0)
453 std::string binDir = concatPath(pkgRoot, "bin");
454 std::string libDir = concatPath(pkgRoot, "lib");
455 std::string paths[] = {binDir, libDir};
457 removeNiUnderDirs(paths, 2);
462 int regenerateAppNI()
465 pkgmgrinfo_appinfo_metadata_filter_h handle;
467 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
468 if (ret != PMINFO_R_OK)
471 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, "http://tizen.org/metadata/prefer_dotnet_aot", "true");
472 if (ret != PMINFO_R_OK) {
473 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
477 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, NULL);
478 if (ret != PMINFO_R_OK) {
479 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
480 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
484 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
486 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);