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