Support regen-app-ni for read-only app (#301)
[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
22 #include <vector>
23 #include <cstring>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/time.h>
27
28 void DisplayUsage() {
29         fprintf(stdout,
30                 "\n"
31                 "Dotnet Tool Version: 1.0\n"
32                 "\n"
33                 "Commands:\n"
34                 "       -h, --help                - Show this help message\n"
35                 "       --ni-system               - Create NI under System DLLs\n"
36                 "       --ni-dll                  - Create NI for DLL\n"
37                 "       --ni-pkg                  - Create NI for package\n"
38                 "       --ni-dir                  - Create NI for directory\n"
39                 "       --ni-reset-system         - Remove System NI files\n"
40                 "       --ni-reset-pkg            - Remove App NI files\n"
41                 "       --ni-reset-dir            - Remove NI for directory\n"
42                 "       --ni-regen-all-app        - Re-generate All App NI files\n"
43                 "       --ni-regen-all-ro-app     - Re-generate All read-only type App NI files\n"
44                 "       --tac-regen-all           - Re-generate All TAC files\n"
45                 "       --tac-restore-db          - Restore TAC Database\n"
46                 "       --tac-disable-pkg         - Disable TAC for package\n"
47                 "       --tac-enable-pkg          - Enable TAC for package\n"
48                 "       --ibc-dir                 - Specify a directory containing IBC files\n"
49                 "       --resolve-all-app         - Remove unused multi-targeting files of all apps\n"
50                 "                                   (this option is used for FOTA script or test)\n"
51                 "\n"
52                 "Options:\n"
53                 "       --r2r                     - Generate a Ready-To-Run image (disable: /FragileNonVersionable)\n"
54                 "       --compatibility           - Compatibility mode for older versions of crossgen\n"
55                 "                                   (replaces /r with /Trusted_Platform_Assemblies)\n"
56                 "       --verbose                 - Display verbose information\n"
57                 "       --instrument              - Generate an instrumented image for profiling (enable: /Tuning)\n"
58                 "\n"
59                 "Usage: dotnettool [options] [command] [arguments]\n"
60                 "\n"
61                 "Example:\n"
62                 "1. Create native image for dlls and exes under platform directories\n"
63                 "   # dotnettool --ni-system\n"
64                 "2. Create native image for dll\n"
65                 "   # dotnettool --ni-dll /usr/bin/Tizen.Runtime.dll\n"
66                 "3. Create native image under the package's bin and lib directory\n"
67                 "   # dotnettool --ni-pkg org.tizen.FormsGallery\n"
68                 "4. Regenerate native images for all installed .net packages with ready-to-run option\n"
69                 "   # dotnettool --r2r --ni-regen-all-app\n"
70                 "5. Create native image for dll based on the IBC data\n"
71                 "   # dotnettool --ibc-dir /tmp/ibcdata/ --ni-dll /usr/bin/Tizen.Runtime.dll\n"
72                 "\n");
73 }
74
75 int main(int argc, char* argv[])
76 {
77         argc--;
78         argv++;
79
80         long starttime;
81         long endtime;
82         struct timeval tv;
83         gettimeofday(&tv, NULL);
84         starttime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
85
86         if (argc <= 0) {
87                 DisplayUsage();
88                 return -1;
89         }
90
91         //sh-3.2# dotnettool --[r2r|compatibility|instrument|verbose]
92         DWORD flags = 0;
93         std::vector<std::string> args;
94         for (char** it = argv; it != argv+argc; it++) {
95                 if (!strncmp(*it, "-?", 2) || !strncmp(*it, "-h", 2) || !strncmp(*it, "--help", 6)) {
96                         DisplayUsage();
97                         return 0;
98                 } else if (!strncmp(*it, "--r2r", 5)) {
99                         flags |= NI_FLAGS_ENABLER2R;
100                 } else if (!strncmp(*it, "--compatibility", 15)) {
101                         flags |= NI_FLAGS_COMPATIBILITY;
102                 } else if (!strncmp(*it, "--instrument", 12)) {
103                         flags |= NI_FLAGS_INSTRUMENT;
104                 } else if (!strncmp(*it, "--verbose", 9)) {
105                         flags |= NI_FLAGS_VERBOSE;
106                 } else {
107                         args.push_back(*it);
108                 }
109         }
110
111         //sh-3.2# dotnettool --ibc-dir [ibcDirectory]
112         for (auto it = args.begin(); it != args.end(); ) {
113                 if (*it == "--ibc-dir") {
114                         it = args.erase(it);
115
116                         std::string ibcFilePath = std::string(*it);
117                         if (!isDirectory(ibcFilePath)) {
118                                 fprintf(stderr, "IBC path is missing or not exist\n");
119                                 return -1;
120                         }
121
122                         setenv("COMPlus_IBCFileDir", const_cast<char *>(ibcFilePath.c_str()), 1);
123                         setenv("COMPlus_UseIBCFile", const_cast<char *>("1"), 1);
124                         it = args.erase(it);
125                 } else {
126                         ++it;
127                 }
128         }
129
130         if (initNICommon() != NI_ERROR_NONE) {
131                 return -1;
132         }
133
134         std::vector<std::string>::iterator it = args.begin();
135         std::string cmd = std::string(*it);
136         it = args.erase(it);
137
138         //sh-3.2# dotnettool --ni-system
139         if (cmd == "--ni-system") {
140                 int ret = createNIPlatform(flags);
141                 if (ret != NI_ERROR_NONE) {
142                         fprintf(stderr, "Failed to generate system NI\n");
143                 }
144         }
145         //sh-3.2# dotnettool --ni-dll [assemblyPath] [assemblyPath] ...
146         else if (cmd == "--ni-dll") {
147                 if (args.size() < 1) {
148                         fprintf(stderr, "DLL path is missing\n");
149                 }
150                 while (it != args.end()) {
151                         std::string dll = std::string(*it);
152                         int ret = createNIDll(dll, flags);
153                         if (ret != NI_ERROR_NONE) {
154                                 fprintf(stderr, "Failed to generate NI file [%s]\n", dll.c_str());
155                                 break;
156                         }
157                         it = args.erase(it);
158                 }
159         }
160         //sh-3.2# dotnettool --ni-pkg [pkgId] [pkgId] ...
161         else if (cmd == "--ni-pkg") {
162                 if (args.size() < 1) {
163                         fprintf(stderr, "Package name is missing\n");
164                 }
165                 while (it != args.end()) {
166                         std::string pkg = std::string(*it);
167                         // if there is AOTed dlls under package root, that is skiped.
168                         int ret = createNIUnderPkgRoot(pkg, flags);
169                         if (ret != NI_ERROR_NONE) {
170                                 fprintf(stderr, "Failed to generate NI file [%s]\n", pkg.c_str());
171                                 break;
172                         }
173                         it = args.erase(it);
174                 }
175         }
176         //sh-3.2# dotnettool --ni-dir [AssemblyDirectory] [AssemblyDirectory] ...
177         else if (cmd == "--ni-dir") {
178                 if (args.size() < 1) {
179                         fprintf(stderr, "Directory path is missing\n");
180                 }
181                 while (it != args.end()) {
182                         const std::string dir = std::string(*it);
183                         int ret = createNIUnderDirs(dir, flags);
184                         if (ret != NI_ERROR_NONE) {
185                                 fprintf(stderr, "Failed to generate NI directory\n");
186                                 break;
187                         }
188                         it = args.erase(it);
189                 }
190         }
191         //sh-3.2# dotnettool --ni-reset-system
192         else if (cmd == "--ni-reset-system") {
193                 removeNIPlatform();
194         }
195         //sh-3.2# dotnettool --ni-reset-pkg [pkgId] [pkgId] ...
196         else if (cmd == "--ni-reset-pkg") {
197                 if (args.size() < 1) {
198                         fprintf(stderr, "Package name is missing\n");
199                 }
200                 while (it != args.end()) {
201                         std::string pkg = std::string(*it);
202                         int ret = removeNIUnderPkgRoot(pkg);
203                         if (ret != NI_ERROR_NONE) {
204                                 fprintf(stderr, "Failed to remove dlls for given package [%s]\n", pkg.c_str());
205                                 break;
206                         }
207                         it = args.erase(it);
208                 }
209         }
210         //sh-3.2# dotnettool --ni-reset-dir [AssemblyDirectory] [AssemblyDirectory] ...
211         else if (cmd == "--ni-reset-dir") {
212                 if (args.size() < 1) {
213                         fprintf(stderr, "Directory path is missing\n");
214                 }
215                 while (it != args.end()) {
216                         const std::string dir = std::string(*it);
217                         removeNIUnderDirs(dir);
218                         it = args.erase(it);
219                 }
220         }
221         //sh-3.2# dotnettool --ni-regen-all-app
222         else if (cmd == "--ni-regen-all-app") {
223                 int ret = regenerateAppNI(flags);
224                 if (ret != NI_ERROR_NONE) {
225                         fprintf(stderr, "Failed to regenerate all app NI\n");
226                 }
227         }
228         //sh-3.2# dotnettool --ni-regen-readonly-app
229         else if (cmd == "--ni-regen-all-ro-app") {
230                 flags |= NI_FLAGS_READONLY_APP;
231                 int ret = regenerateAppNI(flags);
232                 if (ret != NI_ERROR_NONE) {
233                         fprintf(stderr, "Failed to regenerate read-only app NI\n");
234                 }               
235         }
236         //sh-3.2# dotnettool --tac-regen-all
237         else if (cmd == "--tac-regen-all") {
238                 int ret = regenerateTACNI(flags);
239                 if (ret != NI_ERROR_NONE) {
240                         fprintf(stderr, "Failed to regenerate all TAC\n");
241                 }
242         }
243         //sh-3.2# dotnettool --tac-restore-db
244         else if (cmd == "--tac-restore-db") {
245                 int ret = tac_restoreDB();
246                 if (ret != TAC_ERROR_NONE) {
247                         fprintf(stderr, "Failed to restore TAC db\n");
248                 }
249                 ret = tlc_restoreDB();
250                 if (ret != TAC_ERROR_NONE) {
251                         fprintf(stderr, "Failed to restore TLC db\n");
252                 }
253         }
254         //sh-3.2# dotnettool --tac-enable-pkg [pkgId] [pkgId] ...
255         else if (cmd == "--tac-enable-pkg") {
256                 if (args.size() < 1) {
257                         fprintf(stderr, "Package name is missing\n");
258                 }
259                 while (it != args.end()) {
260                         std::string pkg = std::string(*it);
261                         int ret = enableTACPackage(pkg);
262                         if (ret != TAC_ERROR_NONE) {
263                                 fprintf(stderr, "Failed to enable tac [%s]\n", pkg.c_str());
264                                 break;
265                         }
266                         it = args.erase(it);
267                 }
268         }
269         //sh-3.2# dotnettool --tac-disable-pkg [pkgId] [pkgId] ...
270         else if (cmd == "--tac-disable-pkg") {
271                 if (args.size() < 1) {
272                         fprintf(stderr, "Package name is missing\n");
273                 }
274                 while (it != args.end()) {
275                         std::string pkg = std::string(*it);
276                         int ret = disableTACPackage(pkg);
277                         if (ret != TAC_ERROR_NONE) {
278                                 fprintf(stderr, "Failed to disable tac [%s]\n", pkg.c_str());
279                                 break;
280                         }
281                         it = args.erase(it);
282                 }
283         }
284         //sh-3.2# dotnettool --resolve-all-app
285         else if (cmd == "--resolve-all-app") {
286                 int ret = resolveAllApps();
287                 if (ret != 0) {
288                         fprintf(stderr, "Failed to remove unused multi-targeting files\n");
289                 }
290         }
291         else {
292                 fprintf(stderr, "Unknown option [%s]\n", cmd.c_str());
293                 DisplayUsage();
294         }
295
296         finalizeNICommon();
297
298         gettimeofday(&tv, NULL);
299         endtime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
300         fprintf(stdout, "\nSpend time for dotnettool is [%d]ms\n", (int)(endtime - starttime));
301
302         return 0;
303 }