2 * Copyright (c) 2020 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.
18 #include <pkgmgr-info.h>
22 #include "multi_target_resolver.h"
24 static const char* __TIZEN_RID_VERSION_KEY = "db/dotnet/tizen_rid_version"; //8.0.0:7.0.0:6.5.0:6.0.0:5.5.0:5.0.0:4.0.0
25 static const char* __TIZEN_TFM_SUPPORT_KEY = "db/dotnet/tizen_tfm_support"; //net6.0-tizen8.0:net6.0-tizen:net6.0:tizen10.0:tizen90:tizen80:tizen70:tizen60:tizen50:tizen40
29 ---------|---------------------|-----------------------
30 1 | tizen.X.Y.Z-{arch} | netX.Y-tizenX.Y ~ 6.5
31 2 | tizen.X.Y.Z | netX.Y-tizen
32 3 | tizen-{arch}, tizen | netX.Y
33 4 | linux-{arch}, linux | tizen10.0 ~ 40
34 5 | unix-{arch}, unix | net5.0
35 6 | any | netcoreapp3.1 ~ 1.0
36 7 | base | netstandard2.1 ~ 1.0
39 // Guided by Appfw, vconf daemon does not run at mic stage.
40 // So vconf_get_* api does not work normally.
41 // Therefore, it is changed to manage as a file instead of using vconf API.
42 // /usr/share/dotnet.tizen/lib/dotnet_resolving.info
43 static std::string getDotnetResolvingInfo(const char* key)
45 std::ifstream ifs(DOTNET_RESOLVING_INFO_PATH);
47 while (std::getline(ifs, line)) {
48 if (!strcmp(line.substr(0, line.find(" ")).c_str(), key)) {
50 return line.substr(line.rfind(" ") + 1);
56 static std::vector<std::string> getRidFallbackGraph()
58 std::string tizen_rid_version = getDotnetResolvingInfo(__TIZEN_RID_VERSION_KEY);
59 std::vector<std::string> rid_version;
60 std::vector<std::string> RID_FALLBACK_GRAPH;
61 splitPath(tizen_rid_version, rid_version);
62 for (auto& ridVersion : rid_version) {
63 RID_FALLBACK_GRAPH.push_back(std::string("tizen." + ridVersion + "-" + ARCHITECTURE_IDENTIFIER));
64 RID_FALLBACK_GRAPH.push_back(std::string("tizen." + ridVersion));
67 std::vector<std::string> RID_FALLBACK_OS = {"tizen", "linux", "unix"};
68 for (auto& os : RID_FALLBACK_OS) {
69 RID_FALLBACK_GRAPH.push_back(std::string(os + "-" + ARCHITECTURE_IDENTIFIER));
70 RID_FALLBACK_GRAPH.push_back(std::string(os));
72 RID_FALLBACK_GRAPH.push_back("any");
73 RID_FALLBACK_GRAPH.push_back("base");
75 return RID_FALLBACK_GRAPH;
78 static std::vector<std::string> getTfmFallbackGraph()
80 std::string tizen_tfm_support = getDotnetResolvingInfo(__TIZEN_TFM_SUPPORT_KEY);
81 std::vector<std::string> TFM_FALLBACK_GRAPH;
82 splitPath(tizen_tfm_support, TFM_FALLBACK_GRAPH);
83 TFM_FALLBACK_GRAPH.push_back("net5.0");
85 std::vector<std::string> netcoreapp_version = {"3.1", "3.0", "2.2", "2.1", "2.0", "1.1", "1.0"};
86 for (auto& version : netcoreapp_version) {
87 TFM_FALLBACK_GRAPH.push_back(std::string("netcoreapp" + version));
89 std::vector<std::string> netstandard_version = {"2.1", "2.0", "1.6", "1.5", "1.4", "1.3", "1.2", "1.1", "1.0"};
90 for (auto& version : netstandard_version) {
91 TFM_FALLBACK_GRAPH.push_back(std::string("netstandard" + version));
94 return TFM_FALLBACK_GRAPH;
97 // move all files under a certain directory to another directory
98 // ownership / permission / smack label are not changed
99 static bool moveAllFilesTo(const std::string& from, const std::string& to)
101 std::vector<std::string> files;
103 // make file list to move
104 for (auto& path : bf::recursive_directory_iterator(from)) {
105 std::string filePath = path.path().string();
106 if (isFile(filePath)) {
107 files.push_back(filePath);
111 // move files to target directory
112 for (auto& f : files) {
113 bf::rename(f, concatPath(to, getFileName(f)));
115 } catch (const bf::filesystem_error& error) {
116 _ERR("Failed to iterate directory: %s", error.what());
123 int resolvePlatformSpecificFiles(const std::string& rootPath)
125 std::string appBinPath = concatPath(rootPath, "bin");
126 std::string runtimesPath = concatPath(appBinPath, "runtimes");
128 // if runtimes directory doesnot exist, return 0
129 if (!isDirectory(runtimesPath)) {
133 // found best matched rid and tfm directory and copy all files to bin directory
134 std::vector<std::string> ridFallbackGraph = getRidFallbackGraph();
136 for (auto& rid : bf::recursive_directory_iterator(runtimesPath)) {
137 std::string ridPath = rid.path().string();
138 if (bf::is_directory(ridPath) && strstr(ridPath.c_str(), ARCHITECTURE_IDENTIFIER) != NULL) {
139 for (auto& ridFG : ridFallbackGraph) {
140 if (!strcmp(getFileName(ridPath).c_str(), ridFG.c_str())) {
141 std::string nativePath = concatPath(ridPath, "native");
142 if (isDirectory(nativePath)) {
143 _INFO("Found best matched rid (%s)", ridFG.c_str());
144 // copy all files from bin/runtimes/${rid}/native to appBintPath if exist
145 if (!moveAllFilesTo(nativePath, appBinPath)) {
146 _ERR("Failed to copy files from native path");
151 // found best matched tfm folder in the found rid folder
152 std::string libPath = concatPath(ridPath, "lib");
153 std::vector<std::string> tfmFallbackGraph = getTfmFallbackGraph();
154 for (auto& tfmFG : tfmFallbackGraph) {
155 std::string tfmPath = concatPath(libPath, tfmFG);
156 if (isDirectory(tfmPath)) {
157 _INFO("Found best matched tfm (%s)", tfmFG .c_str());
158 // copy all files from bin/runtimes/${rid}/lib/${tfm} to appBintPath if exist
159 if (!moveAllFilesTo(tfmPath, appBinPath)) {
160 _ERR("Failed to copy files from tfm path");
172 } catch (const bf::filesystem_error& error) {
173 _ERR("Failed to recursive directory: %s", error.what());
176 // remove runtimes directory
177 if (!removeAll(runtimesPath)) {
178 _ERR("Failed to remove bin/runtimes directory");
185 // callback function of "pkgmgrinfo_appinfo_filter_foreach_appinfo"
186 static int appResolveCb(pkgmgrinfo_appinfo_h handle, void *user_data)
191 ret = pkgmgrinfo_appinfo_get_root_path(handle, &rootPath);
192 if (ret != PMINFO_R_OK) {
193 _SERR("Failed to get root path");
197 if (resolvePlatformSpecificFiles(rootPath) != 0) {
198 _SERR("Failed to resolve platform specific resources (%s)", rootPath);
205 void resolveAllApps()
209 pkgmgrinfo_appinfo_filter_h handle;
210 ret = pkgmgrinfo_appinfo_filter_create(&handle);
211 if (ret != PMINFO_R_OK) {
212 _SERR("Failed to create pkgmgrinfo");
216 ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_TYPE, "dotnet");
217 if (ret != PMINFO_R_OK) {
218 pkgmgrinfo_appinfo_filter_destroy(handle);
219 _SERR("Failed to add pkgmgrinfo filter app type");
223 ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, appResolveCb, NULL);
224 if (ret != PMINFO_R_OK) {
225 pkgmgrinfo_appinfo_filter_destroy(handle);
226 _SERR("Failed to remove unused multi-targeting files");
229 pkgmgrinfo_appinfo_filter_destroy(handle);