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