namespace dotnetcore {
CoreRuntime::CoreRuntime() :
- InitializeClr(nullptr),
- ExecuteAssembly(nullptr),
- Shutdown(nullptr),
- CreateDelegate(nullptr),
- coreclrLib(nullptr),
- hostHandle(nullptr),
- domainId(-1),
- PreparedFunction(nullptr),
- LaunchFunction(nullptr)
+ initializeClr(nullptr),
+ executeAssembly(nullptr),
+ shutdown(nullptr),
+ createDelegate(nullptr),
+ __coreclrLib(nullptr),
+ __hostHandle(nullptr),
+ __domainId(-1),
+ preparedFunction(nullptr),
+ launchFunction(nullptr)
{
#define __XSTR(x) #x
#define __STR(x) __XSTR(x)
#ifdef DEVICE_API_DIR
- DeviceAPIDirectory = __STR(DEVICE_API_DIR);
+ __deviceAPIDirectory = __STR(DEVICE_API_DIR);
#endif
#ifdef RUNTIME_DIR
- RuntimeDirectory = __STR(RUNTIME_DIR);
+ __runtimeDirectory = __STR(RUNTIME_DIR);
#endif
+#ifdef NATIVE_LIB_DIR
+ __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
+#endif
+
+#ifdef USE_MANAGED_LAUNCHER
#ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
- LauncherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
+ __launcherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
+#endif
#endif
#undef __STR
#undef __XSTR
- _DBG("Constructor called!!");
+ _DBG("Constructor called!!");
}
CoreRuntime::~CoreRuntime()
{
- Dispose();
+ dispose();
}
-int CoreRuntime::Initialize(bool standalone)
+int CoreRuntime::initialize(bool standalone)
{
+ if (standalone) {
+ const char *deviceApiDirectory = getenv("__deviceAPIDirectory");
+ const char *runtimeDirectory = getenv("__runtimeDirectory");
+ if (deviceApiDirectory != nullptr)
+ __deviceAPIDirectory = deviceApiDirectory;
+ if (runtimeDirectory != nullptr)
+ __runtimeDirectory = runtimeDirectory;
+
+#ifdef USE_MANAGED_LAUNCHER
+ const char *launcherAssembly = getenv("__launcherAssembly");
+ if (launcherAssembly != nullptr)
+ __launcherAssembly = launcherAssembly;
+#endif
+ }
+
+ if (__deviceAPIDirectory.empty()) {
+ _ERR("Empty Device API Directory");
+ return 1;
+ } else {
+ __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
+ }
+
+ if (__runtimeDirectory.empty()) {
+ _ERR("Empty Runtime Directory");
+ return 1;
+ } else {
+ __runtimeDirectory = absolutePath(__runtimeDirectory);
+ }
+
+#ifdef USE_MANAGED_LAUNCHER
+ if (__launcherAssembly.empty()) {
+ _ERR("Empty Launcher Assembly");
+ return 1;
+ } else {
+ __launcherAssembly = absolutePath(__launcherAssembly);
+ }
+#endif
- if (standalone)
- {
- const char *_deviceapi_directory = getenv("DeviceAPIDirectory");
- const char *_runtime_directory = getenv("RuntimeDirectory");
- const char *_launcher_assembly = getenv("LauncherAssembly");
- if (_deviceapi_directory != nullptr)
- DeviceAPIDirectory = _deviceapi_directory;
- if (_runtime_directory != nullptr)
- RuntimeDirectory = _runtime_directory;
- if (_launcher_assembly != nullptr)
- LauncherAssembly = _launcher_assembly;
- }
-
- if (DeviceAPIDirectory.empty())
- {
- _ERR("Empty Device API Directory");
- return 1;
- }
- else
- {
- DeviceAPIDirectory = AbsolutePath(DeviceAPIDirectory);
- }
- if (RuntimeDirectory.empty())
- {
- _ERR("Empty Runtime Directory");
- return 1;
- }
- else
- {
- RuntimeDirectory = AbsolutePath(RuntimeDirectory);
- }
- if (LauncherAssembly.empty())
- {
- _ERR("Empty Launcher Assembly");
- return 1;
- }
- else
- {
- LauncherAssembly = AbsolutePath(LauncherAssembly);
- }
-
- std::string libcoreclr(ConcatPath(RuntimeDirectory, "libcoreclr.so"));
-
- _DBG("libcoreclr : %s", libcoreclr.c_str());
-
- coreclrLib = dlopen(libcoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
- if (coreclrLib == nullptr)
- {
- char *err = dlerror();
- _ERR("dlopen failed to open libcoreclr.so with error %s", err);
- return 1;
- }
+ std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
-#define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
- do { variable = (type)dlsym(coreclrLib, name); \
- if (variable == nullptr) { \
- _ERR(name " is not found in the libcoreclr.so"); \
- return 1; \
- }} while(0)
+ _DBG("libcoreclr : %s", libCoreclr.c_str());
- CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, InitializeClr, "coreclr_initialize");
- CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, ExecuteAssembly, "coreclr_execute_assembly");
- CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, Shutdown, "coreclr_shutdown");
- CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, CreateDelegate, "coreclr_create_delegate");
+ __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
+ if (__coreclrLib == nullptr) {
+ char *err = dlerror();
+ _ERR("dlopen failed to open libcoreclr.so with error %s", err);
+ return 1;
+ }
-#undef CORELIB_RETURN_IF_NOSYM
+#define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
+ do { \
+ variable = (type)dlsym(__coreclrLib, name); \
+ if (variable == nullptr) { \
+ _ERR(name " is not found in the libcoreclr.so"); \
+ return 1; \
+ } \
+ } while (0)
+
+ CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
+ CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
+ CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
+ CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
- _DBG("libcoreclr dlopen and dlsym success");
+#undef CORELIB_RETURN_IF_NOSYM
- _DBG("this addr : %x", this);
- _DBG("coreclr_initialize : %x", InitializeClr);
+ _DBG("libcoreclr dlopen and dlsym success");
+ _DBG("this addr : %x", this);
+ _DBG("coreclr_initialize : %x", initializeClr);
- return 0;
+ return 0;
}
-bool CoreRuntime::InitializeCoreClr(const char* assembly_probe_paths, const char* pinvoke_probe_paths)
+bool CoreRuntime::initializeCoreClr(const char* appId,
+ const char* assemblyProbePaths,
+ const char* pinvokeProbePaths,
+ const char* tpaList)
{
- std::vector<std::string> platformDirectories = {
- RuntimeDirectory, DeviceAPIDirectory
- };
-
- std::string trusted_assemblies;
- AssembliesInDirectory(platformDirectories, trusted_assemblies);
-
- const char *propertyKeys[] =
- {
- "TRUSTED_PLATFORM_ASSEMBLIES",
- "APP_PATHS",
- "APP_NI_PATHS",
- "NATIVE_DLL_SEARCH_DIRECTORIES",
- "AppDomainCompatSwitch"
- };
-
- const char *propertyValues[] =
- {
- trusted_assemblies.c_str(),
- assembly_probe_paths,
- assembly_probe_paths,
- pinvoke_probe_paths,
- "UseLatestBehaviorWhenTFMNotSpecified"
- };
-
- std::string selfPath = ReadSelfPath();
-
- int st = InitializeClr(
- selfPath.c_str(),
- "dotnet-launcher",
- sizeof(propertyKeys) / sizeof(propertyKeys[0]),
- propertyKeys,
- propertyValues,
- &hostHandle,
- &domainId);
-
- if (st < 0)
- {
- _ERR("initialize core clr fail! (0x%08x)", st);
- return false;
- }
-
- _DBG("Initialize core clr success");
- return true;
+ const char *propertyKeys[] = {
+ "TRUSTED_PLATFORM_ASSEMBLIES",
+ "APP_PATHS",
+ "APP_NI_PATHS",
+ "NATIVE_DLL_SEARCH_DIRECTORIES",
+ "AppDomainCompatSwitch"
+ };
+
+ const char *propertyValues[] = {
+ tpaList,
+ assemblyProbePaths,
+ assemblyProbePaths,
+ pinvokeProbePaths,
+ "UseLatestBehaviorWhenTFMNotSpecified"
+ };
+
+ std::string selfPath = readSelfPath();
+
+ int st = initializeClr(selfPath.c_str(),
+ appId,
+ sizeof(propertyKeys) / sizeof(propertyKeys[0]),
+ propertyKeys,
+ propertyValues,
+ &__hostHandle,
+ &__domainId);
+
+ if (st < 0) {
+ _ERR("initialize core clr fail! (0x%08x)", st);
+ return false;
+ }
+
+ _DBG("Initialize core clr success");
+ return true;
}
-int CoreRuntime::RunManagedLauncher()
+int CoreRuntime::runManagedLauncher(const char* appId, const char* appBase, const char* tpaList)
{
- if (FileNotExist(LauncherAssembly))
- {
- _ERR("Launcher assembly is not exist in %s", LauncherAssembly.c_str());
- return 1;
- }
-
- std::string launcherDir = Basename(LauncherAssembly);
- std::vector<std::string> searchDirectories = {
- RuntimeDirectory, DeviceAPIDirectory
- };
-
- std::string trusted_directories = JoinStrings(searchDirectories, ":");
-
- _DBG("coreclr_dir : %s", RuntimeDirectory.c_str());
- _DBG("native_so_search_dir : %s", trusted_directories.c_str());
- _DBG("launcher_assembly : %s", LauncherAssembly.c_str());
- _DBG("launcher_dir : %s", launcherDir.c_str());
-
- if (!InitializeCoreClr(launcherDir.c_str(), launcherDir.c_str()))
- {
- _ERR("Failed to initialize coreclr");
- return 1;
- }
-
- void *preparedFunctionDelegate;
- int st = CreateDelegate(hostHandle, domainId,
- "Tizen.Runtime.Coreclr",
- "Tizen.Runtime.Coreclr.AssemblyManager",
- "Prepared", &preparedFunctionDelegate);
- if (st < 0)
- {
- _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
- return 1;
- }
- PreparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
-
- if(PreparedFunction != nullptr)
- {
- PreparedFunction();
- }
-
- void *launchFunctionDelegate;
- st = CreateDelegate(hostHandle, domainId,
- "Tizen.Runtime.Coreclr",
- "Tizen.Runtime.Coreclr.AssemblyManager",
- "Launch", &launchFunctionDelegate);
- if (st < 0)
- {
- _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
- return 1;
- }
- LaunchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
-
- return 0;
+ if (fileNotExist(__launcherAssembly)) {
+ _ERR("Launcher assembly is not exist in %s", __launcherAssembly.c_str());
+ return 1;
+ }
+
+ if (!initializeCoreClr(appId, appBase, appBase, tpaList)) {
+ _ERR("Failed to initialize coreclr");
+ return 1;
+ }
+
+#ifdef USE_MANAGED_LAUNCHER
+ void *preparedFunctionDelegate;
+ int st = createDelegate(__hostHandle, __domainId,
+ "Tizen.Runtime",
+ "Tizen.Runtime.Coreclr.AssemblyManager",
+ "Prepared", &preparedFunctionDelegate);
+ if (st < 0) {
+ _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
+ return 1;
+ }
+ preparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
+
+ if (preparedFunction != nullptr)
+ preparedFunction();
+
+ void *launchFunctionDelegate;
+ st = createDelegate(__hostHandle, __domainId,
+ "Tizen.Runtime",
+ "Tizen.Runtime.Coreclr.AssemblyManager",
+ "Launch", &launchFunctionDelegate);
+ if (st < 0) {
+ _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
+ return 1;
+ }
+ launchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
+#endif
+ return 0;
}
-void CoreRuntime::Dispose()
+void CoreRuntime::dispose()
{
- if (hostHandle != nullptr)
- {
- int st = Shutdown(hostHandle, domainId);
- if (st < 0)
- {
- _ERR("shutdown core clr fail! (0x%08x)", st);
- }
- }
-
- if (dlclose(coreclrLib) != 0)
- {
- _ERR("libcoreclr.so close failed");
- }
- coreclrLib = nullptr;
-
- _DBG("Dotnet runtime disposed");
+ if (__hostHandle != nullptr) {
+ int st = shutdown(__hostHandle, __domainId);
+ if (st < 0)
+ _ERR("shutdown core clr fail! (0x%08x)", st);
+ }
+
+ if (dlclose(__coreclrLib) != 0)
+ _ERR("libcoreclr.so close failed");
+
+ __coreclrLib = nullptr;
+
+ _DBG("Dotnet runtime disposed");
}
-int CoreRuntime::Launch(const char* root, const char* path, int argc, char* argv[])
+int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
{
- if (path == nullptr)
- {
- _ERR("executable path is null");
- return 1;
- }
-
- std::string cpppath(path);
-
- if (IsManagedAssembly(cpppath) && !IsNativeImage(cpppath))
- {
- size_t extindex = cpppath.size() - 4;
- cpppath = cpppath.substr(0, extindex) + ".ni" + cpppath.substr(extindex, 4);
- if (!FileNotExist(cpppath))
- {
- path = cpppath.c_str();
- }
- }
-
- if (FileNotExist(path))
- {
- _ERR("File not exist : %s", path);
- return 1;
- }
-
- bool success = false;
- if (LaunchFunction != nullptr)
- {
- success = LaunchFunction(root, path, argc, argv);
- if (!success)
- {
- _ERR("Failed to launch Application %s", path);
- }
- return success ? 0 : 1;
- }
- else
- {
- std::string appRoot = root;
- std::string appBin = ConcatPath(appRoot, "bin");
- std::string appLib = ConcatPath(appRoot, "lib");
- std::string probePath = appBin + ":" + appLib;
-
- int st = InitializeCoreClr(probePath.c_str(), probePath.c_str());
- unsigned int ret = 0;
- st = ExecuteAssembly(hostHandle, domainId, argc, (const char**)argv, path, &ret);
- if (st < 0)
- {
- _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
- }
- return ret;
- }
+ if (path == nullptr) {
+ _ERR("executable path is null");
+ return 1;
+ }
+
+ if (fileNotExist(path)) {
+ _ERR("File not exist : %s", path);
+ return 1;
+ }
+
+ std::string tpa;
+ std::string appRoot = root;
+ std::string appBin = concatPath(appRoot, "bin");
+ std::string appLib = concatPath(appRoot, "lib");
+ std::string probePath = appBin + ":" + appLib + ":" + __nativeLibDirectory;
+
+ std::vector<std::string> searchDirectories;
+ searchDirectories.push_back(appBin);
+ searchDirectories.push_back(appLib);
+ searchDirectories.push_back(__runtimeDirectory);
+ searchDirectories.push_back(__deviceAPIDirectory);
+#ifdef USE_MANAGED_LAUNCHER
+ searchDirectories.push_back(baseName(__launcherAssembly));
+#endif
+
+ assembliesInDirectory(searchDirectories, tpa);
+
+#ifdef USE_MANAGED_LAUNCHER
+ runManagedLauncher(appId, probePath.c_str(), tpa.c_str());
+
+ bool success = false;
+ if (launchFunction != nullptr) {
+ std::string cppPath(path);
+
+ if (isManagedAssembly(cppPath) && !isNativeImage(cppPath)) {
+ size_t extindex = cppPath.size() - 4;
+ cppPath = cppPath.substr(0, extindex) + ".ni" + cppPath.substr(extindex, 4);
+ if (!fileNotExist(cppPath))
+ path = cppPath.c_str();
+ }
+
+ success = launchFunction(root, path, argc, argv);
+ if (!success)
+ _ERR("Failed to launch Application %s", path);
+ return success ? 0 : 1;
+ } else {
+ _ERR("Failed to find launch function");
+ return 1;
+ }
+#else
+ int st = initializeCoreClr(appId, probePath.c_str(), probePath.c_str(), tpa.c_str());
+ unsigned int ret = 0;
+ st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
+ if (st < 0)
+ _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
+ return ret;
+#endif
}
} // namespace dotnetcore