Remove code that create SPC NI first (#373)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / dotnettool.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "utils.h"
18 #include "ni_common.h"
19 #include "tac_common.h"
20 #include "multi_target_resolver.h"
21 #include "log.h"
22
23 #include <vector>
24 #include <cstring>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/time.h>
28 #include <app_common.h>
29
30 void DisplayUsage() {
31         _SOUT(
32                 "\n"
33                 "Dotnet Tool Version: 1.0\n"
34                 "\n"
35                 "Commands:\n"
36                 "       -h, --help                - Show this help message\n"
37                 "       --ni-system               - Create NI under System DLLs\n"
38                 "       --ni-dll                  - Create NI for DLL\n"
39                 "       --ni-pkg                  - Create NI for package\n"
40                 "                                   (If package is installed under RO area, NI files are generated under RW area)\n"
41                 "       --ni-dir                  - Create NI for directory\n"
42                 "       --ni-reset-system         - Remove System NI files\n"
43                 "       --ni-reset-pkg            - Remove App NI files\n"
44                 "       --ni-reset-dir            - Remove NI for directory\n"
45                 "       --ni-regen-all-app        - Re-generate All App NI files\n"
46                 "       --tac-regen-all           - Re-generate All TAC files\n"
47                 "       --tac-restore-db          - Restore TAC Database\n"
48                 "       --tac-disable-pkg         - Disable TAC for package\n"
49                 "       --tac-enable-pkg          - Enable TAC for package\n"
50                 "       --resolve-all-app         - Remove unused multi-targeting files of all apps\n"
51                 "                                   (this option is used for FOTA script or test)\n"
52                 "       --rm-app-profile          - Remove application profile of given packages for all users\n"
53                 "                                   (this option should be run as root)\n"
54                 "       --rm-all-app-profile      - Remove application profile of all packages for all users\n"
55                 "                                   (this option should be run as root)\n"
56                 "\n"
57                 "Options:\n"
58                 "       --ibc-dir                 - Specify a directory containing IBC files\n"
59                 "       --verbose                 - Display verbose information\n"
60                 "       --inputbubble             - Use inputbubble\n"
61                 "       --inputbubbleref          - Path of references for inputbubble (used with --inputbubble option)\n"
62                 "                                   (If not set, all references are included to inputbubble.)\n"
63                 "       --ref                     - Path of references\n"
64                 "                                   (If not set, default system paths are used.)\n"
65                 "       --no-pipeline             - Compile the dlls individually\n"
66                 "       --print-cmd               - Print command and options\n"
67                 "\n"
68                 "Usage: dotnettool [options] [command] [arguments]\n"
69                 "\n"
70                 "Example:\n"
71                 "1. Create native image for dlls and exes under platform directories\n"
72                 "   # dotnettool --ni-system\n"
73                 "2. Create native image for dll\n"
74                 "   # dotnettool --ni-dll /usr/bin/Tizen.Runtime.dll\n"
75                 "3. Create native image under the package's bin and lib directory\n"
76                 "   # dotnettool --ni-pkg org.tizen.FormsGallery\n"
77                 "4. Regenerate native images for all installed .net packages\n"
78                 "   # dotnettool --ni-regen-all-app\n"
79                 "5. Create native image for dll based on the IBC data\n"
80                 "   # dotnettool --ibc-dir /tmp/ibcdata/ --ni-dll /usr/bin/Tizen.Runtime.dll\n"
81                 "6. Remove profile for package\n"
82                 "   # dotnettool --rm-app-profile org.tizen.FormsGallery\n"
83                 "\n");
84 }
85
86 int main(int argc, char* argv[])
87 {
88         argc--;
89         argv++;
90
91         long starttime;
92         long endtime;
93         struct timeval tv;
94         gettimeofday(&tv, NULL);
95         starttime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
96
97         if (argc <= 0) {
98                 DisplayUsage();
99                 return -1;
100         }
101
102         NIOption* opt = getNIOption();
103         if (opt == nullptr) {
104                 _SERR("Fail to create option structure.");
105                 return -1;
106         }
107
108         std::vector<std::string> args;
109         for (int i = 0; i < argc; ++i) {
110                 std::string arg = argv[i];
111
112                 if ((arg == "-?") || (arg == "-h") || (arg == "--h")) {
113                         DisplayUsage();
114                         return 0;
115                 } else if (arg == "--verbose") {
116                         opt->flags |= NI_FLAGS_VERBOSE;
117                 } else if (arg == "--inputbubble") {
118                         opt->flags |= NI_FLAGS_INPUT_BUBBLE;
119                 } else if (arg == "--no-pipeline") {
120                         opt->flags |= NI_FLAGS_NO_PIPELINE;
121                 } else if (arg == "--print-cmd") {
122                         opt->flags |= NI_FLAGS_PRINT_CMD;
123                 } else if (arg == "--ibc-dir") {
124                         ++i;
125                         if (i >= argc) {
126                                 _SOUT("Directory path containing IBC files should be followed for --ibc-dir option");
127                                 DisplayUsage();
128                                 return 0;
129                         }
130
131                         opt->flags |= NI_FLAGS_MIBC;
132
133                         std::vector<std::string> paths;
134                         splitPath(std::string(argv[i]), paths);
135                         for (const auto &path : paths) {
136                                 if (!isDirectory(path)) {
137                                         _SERR("mibc path is missing or not exist");
138                                         return -1;
139                                 }
140                                 opt->mibcPath.push_back(path);
141                         }
142                 } else if (arg == "--inputbubbleref") {
143                         ++i;
144                         if (i >= argc) {
145                                 _SOUT("Path for references should be followed for --inputbubbleref option");
146                                 DisplayUsage();
147                                 return 0;
148                         }
149
150                         opt->flags |= NI_FLAGS_INPUT_BUBBLE_REF;
151
152                         std::vector<std::string> paths;
153                         splitPath(std::string(argv[i]), paths);
154                         for (const auto &path : paths) {
155                                 if (!isDirectory(path)) {
156                                         _SERR("intpubbubbleref path is missing or not exist");
157                                         return -1;
158                                 }
159                                 opt->inputBubbleRefPath.push_back(path);
160                         }
161                 } else if (arg == "--ref") {
162                         ++i;
163                         if (i >= argc) {
164                                 _SOUT("Path for references should be followed for --ref option");
165                                 DisplayUsage();
166                                 return 0;
167                         }
168
169                         opt->flags |= NI_FLAGS_REF;
170
171                         std::vector<std::string> paths;
172                         splitPath(std::string(argv[i]), paths);
173                         for (const auto &path : paths) {
174                                 if (!isDirectory(path)) {
175                                         _SERR("ref path is missing or not exist");
176                                         return -1;
177                                 }
178                                 opt->refPath.push_back(path);
179                         }
180                 } else {
181                         args.push_back(arg);
182                 }
183         }
184
185         if (opt->flags & NI_FLAGS_INPUT_BUBBLE_REF && !(opt->flags & NI_FLAGS_INPUT_BUBBLE)) {
186                 _SERR("--inputbubbleref option should be used with --inputbubble option");
187                 DisplayUsage();
188                 return -1;
189         }
190
191         if (initNICommon() != NI_ERROR_NONE) {
192                 return -1;
193         }
194
195         std::vector<std::string>::iterator it = args.begin();
196         std::string cmd = std::string(*it);
197         it = args.erase(it);
198
199         //sh-3.2# dotnettool --ni-system
200         if (cmd == "--ni-system") {
201                 int ret = createNIPlatform(opt);
202                 if (ret != NI_ERROR_NONE) {
203                         _SERR("Failed to generate system NI");
204                 }
205         }
206         //sh-3.2# dotnettool --ni-dll [assemblyPath] [assemblyPath] ...
207         else if (cmd == "--ni-dll") {
208                 if (args.size() < 1) {
209                         _SERR("DLL path is missing");
210                 }
211                 while (it != args.end()) {
212                         std::string dll = std::string(*it);
213                         int ret = createNIDll(dll, opt);
214                         if (ret != NI_ERROR_NONE && ret != NI_ERROR_ALREADY_EXIST) {
215                                 _SERR("Failed to generate NI file [%s]", dll.c_str());
216                                 break;
217                         }
218                         it = args.erase(it);
219                 }
220         }
221         //sh-3.2# dotnettool --ni-pkg [pkgId] [pkgId] ...
222         else if (cmd == "--ni-pkg") {
223                 if (args.size() < 1) {
224                         _SERR("Package name is missing");
225                 }
226                 while (it != args.end()) {
227                         std::string pkg = std::string(*it);
228                         int ret = createNIUnderPkgRoot(pkg, opt);
229                         if (ret != NI_ERROR_NONE) {
230                                 _SERR("Failed to generate app NI [%s]", pkg.c_str());
231                                 break;
232                         }
233                         it = args.erase(it);
234                 }
235         }
236         //sh-3.2# dotnettool --ni-dir [AssemblyDirectory] [AssemblyDirectory] ...
237         else if (cmd == "--ni-dir") {
238                 if (args.size() < 1) {
239                         _SERR("Directory path is missing");
240                 }
241                 while (it != args.end()) {
242                         const std::string dir = std::string(*it);
243                         int ret = createNIUnderDirs(dir, opt);
244                         if (ret != NI_ERROR_NONE) {
245                                 _SERR("Failed to generate NI directory");
246                                 break;
247                         }
248                         it = args.erase(it);
249                 }
250         }
251         //sh-3.2# dotnettool --ni-reset-system
252         else if (cmd == "--ni-reset-system") {
253                 removeNIPlatform();
254         }
255         //sh-3.2# dotnettool --ni-reset-pkg [pkgId] [pkgId] ...
256         else if (cmd == "--ni-reset-pkg") {
257                 if (args.size() < 1) {
258                         _SERR("Package name is missing");
259                 }
260                 while (it != args.end()) {
261                         std::string pkg = std::string(*it);
262                         int ret = removeNIUnderPkgRoot(pkg);
263                         if (ret != NI_ERROR_NONE) {
264                                 _SERR("Failed to remove dlls for given package [%s]", pkg.c_str());
265                                 break;
266                         }
267                         it = args.erase(it);
268                 }
269         }
270         //sh-3.2# dotnettool --ni-reset-dir [AssemblyDirectory] [AssemblyDirectory] ...
271         else if (cmd == "--ni-reset-dir") {
272                 if (args.size() < 1) {
273                         _SERR("Directory path is missing");
274                 }
275                 while (it != args.end()) {
276                         const std::string dir = std::string(*it);
277                         removeNIUnderDirs(dir);
278                         it = args.erase(it);
279                 }
280         }
281         //sh-3.2# dotnettool --ni-regen-all-app
282         else if (cmd == "--ni-regen-all-app") {
283                 int ret = regenerateAppNI(opt);
284                 if (ret != NI_ERROR_NONE) {
285                         _SERR("Failed to regenerate all app NI");
286                 }
287         }
288         //sh-3.2# dotnettool --tac-regen-all
289         else if (cmd == "--tac-regen-all") {
290                 int ret = regenerateTACNI(opt);
291                 if (ret != NI_ERROR_NONE) {
292                         _SERR("Failed to regenerate all TAC");
293                 }
294         }
295         //sh-3.2# dotnettool --tac-restore-db
296         else if (cmd == "--tac-restore-db") {
297                 int ret = tac_restoreDB();
298                 if (ret != TAC_ERROR_NONE) {
299                         _SERR("Failed to restore TAC db");
300                 }
301                 ret = tlc_restoreDB();
302                 if (ret != TAC_ERROR_NONE) {
303                         _SERR("Failed to restore TLC db");
304                 }
305         }
306         //sh-3.2# dotnettool --tac-enable-pkg [pkgId] [pkgId] ...
307         else if (cmd == "--tac-enable-pkg") {
308                 if (args.size() < 1) {
309                         _SERR("Package name is missing");
310                 }
311                 while (it != args.end()) {
312                         std::string pkg = std::string(*it);
313                         int ret = enableTACPackage(pkg);
314                         if (ret != TAC_ERROR_NONE) {
315                                 _SERR("Failed to enable tac [%s]", pkg.c_str());
316                                 break;
317                         }
318                         it = args.erase(it);
319                 }
320         }
321         //sh-3.2# dotnettool --tac-disable-pkg [pkgId] [pkgId] ...
322         else if (cmd == "--tac-disable-pkg") {
323                 if (args.size() < 1) {
324                         _SERR("Package name is missing");
325                 }
326                 while (it != args.end()) {
327                         std::string pkg = std::string(*it);
328                         int ret = disableTACPackage(pkg);
329                         if (ret != TAC_ERROR_NONE) {
330                                 _SERR("Failed to disable tac [%s]", pkg.c_str());
331                                 break;
332                         }
333                         it = args.erase(it);
334                 }
335         }
336         //sh-3.2# dotnettool --resolve-all-app
337         else if (cmd == "--resolve-all-app") {
338                 int ret = resolveAllApps();
339                 if (ret != 0) {
340                         _SERR("Failed to remove unused multi-targeting files");
341                 }
342         }
343         //sh-3.2# dotnettool --rm-app-profile [pkgId] [pkgId] ...
344         else if (cmd == "--rm-app-profile") {
345                 if (args.size() < 1) {
346                         _SERR("Package name is missing");
347                 }
348                 while (it != args.end()) {
349                         std::string pkg = std::string(*it);
350                         if (removeAppProfileData(pkg) != NI_ERROR_NONE) {
351                                 _SERR("Failed to remove [%s] profile data", pkg.c_str());
352                         }
353                         it = args.erase(it);
354                 }
355         }
356         //sh-3.2# dotnettool --rm-all-app-profile
357         else if (cmd == "--rm-all-app-profile") {
358                 removeAllAppProfileData();
359         }
360         else {
361                 _SERR("Unknown option [%s]", cmd.c_str());
362                 DisplayUsage();
363         }
364
365         finalizeNICommon();
366
367         gettimeofday(&tv, NULL);
368         endtime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
369         _SOUT("\nSpend time for dotnettool is [%d]ms", (int)(endtime - starttime));
370
371         return 0;
372 }