Fixed the bug to the '--ibc-dir' option (#239)
[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
21 #include <vector>
22 #include <stdlib.h>
23 #include <sys/time.h>
24
25 void DisplayUsage() {
26         fprintf(stdout,
27                 "\n"
28                 "Dotnet Tool Version: 1.0\n"
29                 "\n"
30                 "Commands:\n"
31                 "       -h, --help                - Show this help message\n"
32                 "       --ni-system               - Create NI under System DLLs\n"
33                 "       --ni-dll                  - Create NI for DLL\n"
34                 "       --ni-pkg                  - Create NI for package\n"
35                 "       --ni-pkg-dll              - Create NI for DLL in package\n"
36                 "       --ni-dir                  - Create NI for directory\n"
37                 "       --ni-reset-system         - Remove System NI files\n"
38                 "       --ni-reset-pkg            - Remove App NI files\n"
39                 "       --ni-reset-dir            - Remove NI for directory\n"
40                 "       --ni-regen-all-app        - Re-generate All App NI files\n"
41                 "       --tac-regen-all           - Re-generate All TAC files\n"
42                 "       --tac-restore-db          - Restore TAC Database\n"
43                 "       --tac-disable-pkg         - Disable TAC for package\n"
44                 "       --tac-enable-pkg          - Enable TAC for package\n"
45                 "       --ibc-dir                 - Specify a directory containing IBC files\n"
46                 "\n"
47                 "Options:\n"
48                 "       --r2r                     - Generate a Ready-To-Run image (disable: /FragileNonVersionable)\n"
49                 "       --compatibility           - Compatibility mode for older versions of crossgen\n"
50                 "                                   (replaces /r with /Trusted_Platform_Assemblies)\n"
51                 "       --verbose                 - Display verbose information\n"
52                 "       --instrument              - Generate an instrumented image for profiling (enable: /Tuning)\n"
53                 "\n"
54                 "Usage: dotnettool [options] [command] [arguments]\n"
55                 "\n"
56                 "Example:\n"
57                 "1. Create native image for dlls and exes under platform directories\n"
58                 "   # dotnettool --ni-system\n"
59                 "2. Create native image for dll\n"
60                 "   # dotnettool --ni-dll /usr/bin/Tizen.Runtime.dll\n"
61                 "3. Create native image under the package's bin and lib directory\n"
62                 "   # dotnettool --ni-pkg org.tizen.FormsGallery\n"
63                 "4. Regenerate native images for all installed .net packages with ready-to-run option\n"
64                 "   # dotnettool --r2r --ni-regen-all-app\n"
65                 "5. Create native image for dll based on the IBC data\n"
66                 "   # dotnettool --ibc-dir /tmp/ibcdata/ --ni-dll /usr/bin/Tizen.Runtime.dll\n"
67                 "\n");
68 }
69
70 int main(int argc, char* argv[])
71 {
72         argc--;
73         argv++;
74
75         long starttime;
76         long endtime;
77         struct timeval tv;
78         gettimeofday(&tv, NULL);
79         starttime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
80
81         if (argc <= 0) {
82                 DisplayUsage();
83                 return -1;
84         }
85
86         NiCommonOption option = {std::string(), std::string(), std::string()};
87         if (initNICommon(&option) != NI_ERROR_NONE) {
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 (!isDirectoryExist(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         std::vector<std::string>::iterator it = args.begin();
131         std::string cmd = std::string(*it);
132         it = args.erase(it);
133
134         //sh-3.2# dotnettool --ni-system
135         if (cmd == "--ni-system") {
136                 int ret = createNiPlatform(flags);
137                 if (ret != NI_ERROR_NONE) {
138                         fprintf(stderr, "Failed to generate system NI\n");
139                 }
140         }
141         //sh-3.2# dotnettool --ni-dll [assemblyPath] [assemblyPath] ...
142         else if (cmd == "--ni-dll") {
143                 if (args.size() < 1) {
144                         fprintf(stderr, "DLL path is missing\n");
145                 }
146                 while (it != args.end()) {
147                         std::string dll = std::string(*it);
148                         int ret = createNiDll(dll, flags);
149                         if (ret != NI_ERROR_NONE) {
150                                 fprintf(stderr, "Failed to generate NI file [%s]\n", dll.c_str());
151                                 break;
152                         }
153                         it = args.erase(it);
154                 }
155         }
156         //sh-3.2# dotnettool --ni-pkg [pkgId] [pkgId] ...
157         else if (cmd == "--ni-pkg") {
158                 if (args.size() < 1) {
159                         fprintf(stderr, "Package name is missing\n");
160                 }
161                 while (it != args.end()) {
162                         std::string pkg = std::string(*it);
163                         // if there is AOTed dlls under package root, that is skiped.
164                         int ret = createNiUnderPkgRoot(pkg, flags);
165                         if (ret != NI_ERROR_NONE) {
166                                 fprintf(stderr, "Failed to generate NI file [%s]\n", pkg.c_str());
167                                 break;
168                         }
169                         ret = createTACPkgRoot(pkg, flags);
170                         if (ret != NI_ERROR_NONE) {
171                                 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkg.c_str());
172                                 break;
173                         }
174                         it = args.erase(it);
175                 }
176         }
177         //sh-3.2# dotnettool --ni-pkg-dll [pkgId] [pkgAssemblyPath] [pkgAssemblyPath] ...
178         else if (cmd == "--ni-pkg-dll") {
179                 if (args.size() < 2) {
180                         fprintf(stderr, "Package name or DLL path is missing\n");
181                 } else {
182                         std::string pkg = std::string(*it);
183                         it = args.erase(it);
184                         while (it != args.end()) {
185                                 std::string dll = std::string(*it);
186                                 int ret = createNiDllUnderPkgRoot(pkg, dll, flags);
187                                 if (ret != NI_ERROR_NONE) {
188                                         fprintf(stderr, "Failed to generate NI file [%s]\n", dll.c_str());
189                                         break;
190                                 }
191                                 it = args.erase(it);
192                         }
193                 }
194         }
195         //sh-3.2# dotnettool --ni-dir [AssemblyDirectory] [AssemblyDirectory] ...
196         else if (cmd == "--ni-dir") {
197                 if (args.size() < 1) {
198                         fprintf(stderr, "Directory path is missing\n");
199                 }
200                 while (it != args.end()) {
201                         const std::string dir[] = {std::string(*it)};
202                         int ret = createNiUnderDirs(dir, 1, flags);
203                         if (ret != NI_ERROR_NONE) {
204                                 fprintf(stderr, "Failed to generate NI directory\n");
205                                 break;
206                         }
207                         it = args.erase(it);
208                 }
209         }
210         //sh-3.2# dotnettool --ni-reset-system
211         else if (cmd == "--ni-reset-system") {
212                 removeNiPlatform();
213         }
214         //sh-3.2# dotnettool --ni-reset-pkg [pkgId] [pkgId] ...
215         else if (cmd == "--ni-reset-pkg") {
216                 if (args.size() < 1) {
217                         fprintf(stderr, "Package name is missing\n");
218                 }
219                 while (it != args.end()) {
220                         std::string pkg = std::string(*it);
221                         int ret = removeNiUnderPkgRoot(pkg);
222                         if (ret != NI_ERROR_NONE) {
223                                 fprintf(stderr, "Failed to remove dlls for given package [%s]\n", pkg.c_str());
224                                 break;
225                         }
226                         ret = resetTACPackage(pkg);
227                         if (ret != TAC_ERROR_NONE) {
228                                 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkg.c_str());
229                                 break;
230                         }
231                         it = args.erase(it);
232                 }
233         }
234         //sh-3.2# dotnettool --ni-reset-dir [AssemblyDirectory] [AssemblyDirectory] ...
235         else if (cmd == "--ni-reset-dir") {
236                 if (args.size() < 1) {
237                         fprintf(stderr, "Directory path is missing\n");
238                 }
239                 while (it != args.end()) {
240                         const std::string dir[] = {std::string(*it)};
241                         removeNiUnderDirs(dir, 1);
242                         it = args.erase(it);
243                 }
244         }
245         //sh-3.2# dotnettool --ni-regen-all-app
246         else if (cmd == "--ni-regen-all-app") {
247                 int ret = regenerateAppNI(flags);
248                 if (ret != NI_ERROR_NONE) {
249                         fprintf(stderr, "Failed to regenerate all app NI\n");
250                 }
251         }
252         //sh-3.2# dotnettool --tac-regen-all
253         else if (cmd == "--tac-regen-all") {
254                 int ret = regenerateTACNI(flags);
255                 if (ret != NI_ERROR_NONE) {
256                         fprintf(stderr, "Failed to regenerate all TAC\n");
257                 }
258         }
259         //sh-3.2# dotnettool --tac-restore-db
260         else if (cmd == "--tac-restore-db") {
261                 int ret = restoreTACDB();
262                 if (ret != TAC_ERROR_NONE) {
263                         fprintf(stderr, "Failed to restore TAC db\n");
264                 }
265         }
266         //sh-3.2# dotnettool --tac-enable-pkg [pkgId] [pkgId] ...
267         else if (cmd == "--tac-enable-pkg") {
268                 if (args.size() < 1) {
269                         fprintf(stderr, "Package name is missing\n");
270                 }
271                 while (it != args.end()) {
272                         std::string pkg = std::string(*it);
273                         int ret = enableTACPackage(pkg);
274                         if (ret != TAC_ERROR_NONE) {
275                                 fprintf(stderr, "Failed to enable tac [%s]\n", pkg.c_str());
276                                 break;
277                         }
278                         it = args.erase(it);
279                 }
280         }
281         //sh-3.2# dotnettool --tac-disable-pkg [pkgId] [pkgId] ...
282         else if (cmd == "--tac-disable-pkg") {
283                 if (args.size() < 1) {
284                         fprintf(stderr, "Package name is missing\n");
285                 }
286                 while (it != args.end()) {
287                         std::string pkg = std::string(*it);
288                         int ret = disableTACPackage(pkg);
289                         if (ret != TAC_ERROR_NONE) {
290                                 fprintf(stderr, "Failed to disable tac [%s]\n", pkg.c_str());
291                                 break;
292                         }
293                         it = args.erase(it);
294                 }
295         }
296         else {
297                 fprintf(stderr, "Unknown option [%s]\n", cmd.c_str());
298                 DisplayUsage();
299         }
300
301         gettimeofday(&tv, NULL);
302         endtime = tv.tv_sec * 1000l + tv.tv_usec / 1000l;
303         fprintf(stdout, "\nSpend time for dotnettool is [%d]ms\n", (int)(endtime - starttime));
304
305         return 0;
306 }