Enable generation of unique default base address for system dlls by default
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / nitool.cc
1 /*
2  * Copyright (c) 2016 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
20 #include <cstdio>
21 #include <cstring>
22
23 #include <algorithm>
24 #include <vector>
25
26 std::vector<std::string> getCmdArgs(char** begin, char** end)
27 {
28         std::vector<std::string> list;
29         for (char** itr = begin+1; itr != end; itr++) {
30                 if (strncmp(*itr, "--", 2) != 0)
31                         list.push_back(*itr);
32         }
33         return list;
34 }
35
36 static void help(const char *argv0)
37 {
38         const char* helpDesc =
39                 "Usage: %s [args] <root paths or pkg name>\n"
40                 "       --help              - Display this screen\n"
41                 "       --system            - Create NI under System DLLs\n"
42                 "       --dll               - Create NI for DLL\n"
43                 "       --pkg               - Create NI for package\n"
44                 "       --dir               - Create NI for directory\n"
45                 "       --r2r               - Use ready-to-run option (default: FNV)\n"
46                 "                             (This option should be used with other options)\n"
47                 "       --reset-system      - Remove System NI files\n"
48                 "       --reset-pkg         - Remove App NI files\n"
49                 "       --regen-all-app     - Re-generate All App NI files\n"
50                 "\n"
51                 "Example:\n"
52                 "1. Create native image for dlls and exes under platform directories\n"
53                 "   # %s --system\n"
54                 "2. Create native image for dll\n"
55                 "   # %s --dll /usr/bin/Tizen.Runtime.dll\n"
56                 "3. Create native image under the package's bin and lib directory\n"
57                 "   # %s --pkg org.tizen.FormsGallery\n"
58                 "4. Regenerate native images for all installed .net packages with ready-to-run option\n"
59                 "   # %s --r2r --regen-all-app\n\n";
60         printf(helpDesc, argv0, argv0, argv0, argv0, argv0);
61 }
62
63 int main(int argc, char* argv[])
64 {
65         bool pkgMode = false;
66         bool dllMode = false;
67         bool dirMode = false;
68         bool rmPkgMode = false;
69         bool enableR2R = false;
70         bool pkgDllMode = false;
71
72         bool doGenUniqueBaseSystem = true;
73
74         NiCommonOption option = {std::string(), std::string(), std::string()};
75         if (initNICommon(&option) != NI_ERROR_NONE) {
76                 fprintf(stderr, "Fail to initialize NI Common\n");
77                 return -1;
78         }
79
80         if (cmdOptionExists(argv, argv+argc, "--r2r")) {
81                 enableR2R = true;
82         }
83
84         if (cmdOptionExists(argv, argv+argc, "--help")) {
85                 help(argv[0]);
86                 return 0;
87         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
88                 createNiPlatform(enableR2R, doGenUniqueBaseSystem);
89                 return 0;
90         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
91                 dllMode = true;
92         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
93                 pkgMode = true;
94         } else if (cmdOptionExists(argv, argv+argc, "--dir")) {
95                 dirMode = true;
96         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
97                 removeNiPlatform();
98                 return 0;
99         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
100                 rmPkgMode = true;
101         } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
102                 regenerateAppNI(enableR2R);
103                 return 0;
104         } else if (cmdOptionExists(argv, argv+argc, "--pkg-dll")) {
105                 pkgDllMode = true;
106         } else {
107                 help(argv[0]);
108                 return 0;
109         }
110
111         std::vector<std::string> args = getCmdArgs(argv, argv+argc);
112
113         if (args.size() < 1) {
114                 if (pkgMode)
115                         fprintf(stderr, "Package name is missed\n");
116                 else if (dllMode)
117                         fprintf(stderr, "DLL path is missed\n");
118                 help(argv[0]);
119                 return 1;
120         }
121
122         if (pkgMode) {
123                 for (const std::string pkg : args) {
124                         // if there is AOTed dlls under package root, that is skiped.
125                         int ret = createNiUnderPkgRoot(pkg, enableR2R);
126                         if (ret == NI_ERROR_INVALID_PACKAGE) {
127                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
128                                 return -1;
129                         } else if (ret != NI_ERROR_NONE) {
130                                 fprintf(stderr, "Failed to generate NI file [%s]\n", args[1].c_str());
131                                 return -1;
132                         }
133                 }
134         } else if (pkgDllMode) {
135                 int ret = createNiDllUnderPkgRoot(args[0], args[1], enableR2R);
136                 if (ret == NI_ERROR_INVALID_PACKAGE) {
137                         fprintf(stderr, "Failed to get root path from [%s]\n", args[0].c_str());
138                         return -1;
139                 } else if (ret == NI_ERROR_ALREADY_EXIST) {
140                         // skip for already exist case
141                         return -1;
142                 } else if (ret != NI_ERROR_NONE) {
143                         fprintf(stderr, "Failed to generate NI file [%s]\n", args[1].c_str());
144                         return -1;
145                 }
146         } else if (rmPkgMode) {
147                 for (const std::string pkg : args) {
148                         int ret = removeNiUnderPkgRoot(pkg);
149                         if (ret == NI_ERROR_INVALID_PACKAGE) {
150                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
151                                 return -1;
152                         } else if (ret != NI_ERROR_NONE) {
153                                 fprintf(stderr, "Failed to remove dlls for given package [%s]\n", pkg.c_str());
154                                 return -1;
155                         }
156                 }
157         } else if (dllMode) {
158                 // donot return error code for generation failure.
159                 // we have to run crossgen for all input dlls.
160                 for (const std::string dll : args) {
161                         int ret = createNiDll(dll, enableR2R, doGenUniqueBaseSystem);
162                         if (ret == NI_ERROR_ALREADY_EXIST) {
163                                 // skip for already exist case
164                         } else if (ret != NI_ERROR_NONE) {
165                                 fprintf(stderr, "Failed to generate NI file [%s]\n", dll.c_str());
166                         }
167                 }
168         } else if (dirMode) {
169                 createNiUnderDirs(args.data(), args.size(), enableR2R, doGenUniqueBaseSystem);
170         }
171
172         return 0;
173 }