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