From 8b947fc48230312d4ba251fdaeae71bbff039c6b Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Tue, 4 Jun 2019 10:54:33 +0900 Subject: [PATCH] generate native image files in the .native_image folder for the pkg aot. --- NativeLauncher/inc/launcher_env.h | 26 ++++++ NativeLauncher/inc/utils.h | 2 + NativeLauncher/installer-plugin/ni_common.cc | 101 +++++++++++++++------ NativeLauncher/installer-plugin/ni_common.h | 3 +- .../installer-plugin/prefer_dotnet_aot_plugin.cc | 4 +- NativeLauncher/launcher/dotnet/dotnet_launcher.cc | 9 +- NativeLauncher/launcher/dotnet/dotnet_launcher.h | 2 +- NativeLauncher/util/plugin_manager.cc | 2 - 8 files changed, 107 insertions(+), 42 deletions(-) create mode 100644 NativeLauncher/inc/launcher_env.h diff --git a/NativeLauncher/inc/launcher_env.h b/NativeLauncher/inc/launcher_env.h new file mode 100644 index 0000000..55f600d --- /dev/null +++ b/NativeLauncher/inc/launcher_env.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LAUNCHER_ENV_H_ +#define __LAUNCHER_ENV_H_ + +#define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so" +#define ENV_FILE_PATH "/usr/share/dotnet.tizen/lib/coreclr_env.list" +#define AOT_METADATA_KEY "http://tizen.org/metadata/prefer_dotnet_aot" +#define AOT_METADATA_VALUE "true" +#define APP_NI_SUB_DIR "/.native_image" + +#endif //__LAUNCHER_ENV_H_ \ No newline at end of file diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index d353f92..79e8489 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -21,6 +21,8 @@ #include #include +#include + #ifndef PATH_SEPARATOR #define PATH_SEPARATOR '/' #endif diff --git a/NativeLauncher/installer-plugin/ni_common.cc b/NativeLauncher/installer-plugin/ni_common.cc index cd2c473..4a1e284 100644 --- a/NativeLauncher/installer-plugin/ni_common.cc +++ b/NativeLauncher/installer-plugin/ni_common.cc @@ -69,7 +69,27 @@ static void waitInterval() } } -static std::string getNiFileName(const std::string& dllPath) +static void updateNiFileInfo(const std::string& dllPath, const std::string& niPath) +{ + char* label = NULL; + + // change smack label + if (smack_getlabel(dllPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) { + if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) { + fprintf(stderr, "Fail to set smack label\n"); + } + free(label); + } + + // change owner and groups for generated ni file. + struct stat info; + if (!stat(dllPath.c_str(), &info)) { + if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1) + fprintf(stderr, "Failed to change owner and group name\n"); + } +} + +static std::string getNiFilePath(const std::string& dllPath) { size_t index = dllPath.find_last_of("."); if (index == std::string::npos) { @@ -86,9 +106,32 @@ static std::string getNiFileName(const std::string& dllPath) return niPath; } +static std::string getAppNIPath(const std::string& niPath) +{ + size_t index = niPath.find_last_of("/"); + if (index == std::string::npos) { + fprintf(stderr, "dllPath doesnot contains path info\n"); + return ""; + } + + std::string prevPath = niPath.substr(0, index); + std::string fileName = niPath.substr(index, niPath.length()); + std::string niDirPath = prevPath + APP_NI_SUB_DIR; + + if (!isFileExist(niDirPath)) { + if (mkdir(niDirPath.c_str(), 0755) == 0) { + updateNiFileInfo(prevPath, niDirPath); + } else { + fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str()); + } + } + + return niDirPath + fileName; +} + static bool niExist(const std::string& path) { - std::string f = getNiFileName(path); + std::string f = getNiFilePath(path); if (f.empty()) { return false; } @@ -109,27 +152,7 @@ static bool niExist(const std::string& path) return false; } -static void updateNiFileInfo(const std::string& dllPath, const std::string& niPath) -{ - char* label = NULL; - - // change smack label - if (smack_getlabel(dllPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) { - if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) { - fprintf(stderr, "Fail to set smack label\n"); - } - free(label); - } - - // change owner and groups for generated ni file. - struct stat info; - if (!stat(dllPath.c_str(), &info)) { - if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1) - fprintf(stderr, "Failed to change owner and group name\n"); - } -} - -static int crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R) +static int crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false) { if (!isFileExist(dllPath)) { fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str()); @@ -147,12 +170,16 @@ static int crossgen(const std::string& dllPath, const std::string& appPath, bool } std::string absDllPath = absolutePath(dllPath); - std::string absNiPath = getNiFileName(dllPath); + std::string absNiPath = getNiFilePath(dllPath); if (absNiPath.empty()) { fprintf(stderr, "Fail to get ni file name\n"); return -1; } + if (isAppNI) { + absNiPath = getAppNIPath(absNiPath); + } + pid_t pid = fork(); if (pid == -1) return -1; @@ -343,7 +370,7 @@ int createNiDll(const std::string& dllPath, bool enableR2R) return crossgen(dllPath, std::string(), enableR2R); } -void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R) +void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI) { createCoreLibNI(enableR2R); @@ -356,8 +383,8 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R) if (appPaths.back() == ':') appPaths.pop_back(); - auto convert = [&appPaths, enableR2R](const std::string& path, const char* name) { - if (!crossgen(path, appPaths.c_str(), enableR2R)) { + auto convert = [&appPaths, enableR2R, isAppNI](const std::string& path, const char* name) { + if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) { waitInterval(); } }; @@ -379,7 +406,7 @@ int createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R) std::string libDir = concatPath(pkgRoot, "lib"); std::string paths[] = {binDir, libDir}; - createNiUnderDirs(paths, 2, enableR2R); + createNiUnderDirs(paths, 2, enableR2R, true); return 0; } @@ -396,7 +423,7 @@ int createNiDllUnderPkgRoot(const std::string& pkgName, const std::string& dllPa std::string libDir = concatPath(pkgRoot, "lib"); std::string paths = binDir + ":" + libDir; - return crossgen(dllPath, paths, enableR2R); + return crossgen(dllPath, paths, enableR2R, true); } void removeNiPlatform() @@ -450,6 +477,20 @@ int removeNiUnderPkgRoot(const std::string& pkgName) removeNiUnderDirs(paths, 2); + std::string binNIDir = binDir + APP_NI_SUB_DIR; + if (isFileExist(binNIDir)) { + if (rmdir(binNIDir.c_str()) != 0) { + fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str()); + } + } + + std::string libNIDir = libDir + APP_NI_SUB_DIR; + if (isFileExist(libNIDir)) { + if (rmdir(libNIDir.c_str()) != 0) { + fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str()); + } + } + return 0; } @@ -462,7 +503,7 @@ int regenerateAppNI(bool enableR2R) if (ret != PMINFO_R_OK) return -1; - ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, "http://tizen.org/metadata/prefer_dotnet_aot", "true"); + ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, AOT_METADATA_VALUE); if (ret != PMINFO_R_OK) { pkgmgrinfo_appinfo_metadata_filter_destroy(handle); return -1; diff --git a/NativeLauncher/installer-plugin/ni_common.h b/NativeLauncher/installer-plugin/ni_common.h index b36282f..a98bf5a 100644 --- a/NativeLauncher/installer-plugin/ni_common.h +++ b/NativeLauncher/installer-plugin/ni_common.h @@ -61,8 +61,9 @@ int createNiDll(const std::string& dllPath, bool enableR2R); * @param[i] rootPaths directories whicn contains DLLs * @param[i] count number of rootPath * @param[i] enableR2R enable ready-to-run mode + * @param[i] isAppNI if you want to create ni files under nativeImage directory, set it true */ -void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R); +void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI = false); /** * @brief create native images for specific package. (All DLLs) diff --git a/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc b/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc index 799861a..a4592b4 100644 --- a/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc +++ b/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc @@ -33,8 +33,6 @@ typedef struct Metadata { const char *value; } Metadata; -const std::string valueType = "true"; -const std::string mdKey = "http://tizen.org/metadata/prefer_dotnet_aot"; extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list) { GList *tag = NULL; @@ -43,7 +41,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app tag = g_list_first(list); while (tag) { mdInfo = (Metadata*)tag->data; - if (mdInfo->key == mdKey && mdInfo->value == valueType) { + if (mdInfo->key == AOT_METADATA_KEY && mdInfo->value == AOT_METADATA_VALUE) { _DBG("Prefer dotnet application AOT set TRUE"); mdValue = true; } diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc index 1464d9b..247c671 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc @@ -45,9 +45,6 @@ #include "path_manager.h" #include "log_manager.h" -#define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so" -#define ENV_FILE_PATH "/usr/share/dotnet.tizen/lib/coreclr_env.list" - namespace tizen { namespace runtime { namespace dotnetcore { @@ -370,11 +367,12 @@ int CoreRuntime::initialize(bool standalone) std::string appBin = concatPath(appRoot, "bin"); std::string appLib = concatPath(appRoot, "lib"); std::string probePath = appBin + ":" + appLib; + std::string NIprobePath = appBin + APP_NI_SUB_DIR + ":" + appLib + APP_NI_SUB_DIR; std::string tpa = getTPA(); std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory; std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid()); - if (!initializeCoreClr(appName.c_str(), probePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) { + if (!initializeCoreClr(appName.c_str(), probePath.c_str(), NIprobePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) { _ERR("Failed to initialize coreclr"); return -1; } @@ -388,6 +386,7 @@ int CoreRuntime::initialize(bool standalone) bool CoreRuntime::initializeCoreClr(const char* appId, const char* assemblyProbePaths, + const char* NIProbePaths, const char* pinvokeProbePaths, const char* tpaList) { @@ -402,7 +401,7 @@ bool CoreRuntime::initializeCoreClr(const char* appId, const char *propertyValues[] = { tpaList, assemblyProbePaths, - assemblyProbePaths, + NIProbePaths, pinvokeProbePaths, "UseLatestBehaviorWhenTFMNotSpecified" }; diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.h b/NativeLauncher/launcher/dotnet/dotnet_launcher.h index 62b1859..b05009d 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.h +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.h @@ -35,7 +35,7 @@ class CoreRuntime int launch(const char* appId, const char* root, const char* path, int argc, char* argv[]); private: - bool initializeCoreClr(const char* appId, const char* assemblyProbePaths, const char* pinvokeProbePaths, const char* tpaList); + bool initializeCoreClr(const char* appId, const char* assemblyProbePaths, const char* NIProbePaths, const char* pinvokeProbePaths, const char* tpaList); coreclr_initialize_ptr initializeClr; coreclr_execute_assembly_ptr executeAssembly; coreclr_shutdown_ptr shutdown; diff --git a/NativeLauncher/util/plugin_manager.cc b/NativeLauncher/util/plugin_manager.cc index 5065adb..f8f008b 100644 --- a/NativeLauncher/util/plugin_manager.cc +++ b/NativeLauncher/util/plugin_manager.cc @@ -22,8 +22,6 @@ static PluginFunc* __pluginFunc = NULL; static void* __pluginLib; -#define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so" - int initializePluginManager(const char* mode) { if (isFileExist(PLUGIN_PATH)) { -- 2.7.4