bbed3f255af8d3efa23ac8b4fc8425bfc32f424e
[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 bool cmdOptionExists(char** begin, char** end, const std::string& option)
37 {
38         return std::find(begin, end, option) != end;
39 }
40
41 static void help(const char *argv0)
42 {
43         const char* helpDesc =
44                 "%s is deprecated. Please use dotnettool instead.\n"
45                 "Example:\n"
46                 "   # dotnettool --help\n\n";
47         printf(helpDesc, argv0);
48 }
49
50 int main(int argc, char* argv[])
51 {
52         DWORD flags = 0;
53         bool pkgMode = false;
54         bool dllMode = false;
55         bool rmPkgMode = false;
56
57         if (initNICommon() != NI_ERROR_NONE) {
58                 return -1;
59         }
60
61         // Parse optional switches first.
62         if (cmdOptionExists(argv, argv+argc, "--r2r")) {
63                 flags |= NI_FLAGS_ENABLER2R;
64         }
65         if (cmdOptionExists(argv, argv+argc, "--compatibility")) {
66                 flags |= NI_FLAGS_COMPATIBILITY;
67         }
68         if (cmdOptionExists(argv, argv+argc, "--instrument")) {
69                 flags |= NI_FLAGS_INSTRUMENT;
70         }
71         if (cmdOptionExists(argv, argv+argc, "--verbose")) {
72                 flags |= NI_FLAGS_VERBOSE;
73         }
74
75         // The following commands are mutually exclusive.
76         if (cmdOptionExists(argv, argv+argc, "--help")) {
77                 help(argv[0]);
78                 return 0;
79         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
80                 createNIPlatform(flags);
81                 return 0;
82         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
83                 dllMode = true;
84         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
85                 pkgMode = true;
86         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
87                 removeNIPlatform();
88                 return 0;
89         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
90                 rmPkgMode = true;
91         } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
92                 regenerateAppNI(flags);
93                 return 0;
94         } else {
95                 help(argv[0]);
96                 return 0;
97         }
98
99         std::vector<std::string> args = getCmdArgs(argv, argv+argc);
100
101         if (args.size() < 1) {
102                 if (pkgMode)
103                         _SERR("Package name is missing.");
104                 else if (dllMode)
105                         _SERR("DLL path is missing.");
106                 help(argv[0]);
107                 return -1;
108         }
109
110         if (pkgMode) {
111                 for (const std::string pkg : args) {
112                         // if there is AOTed dlls under package root, that is skiped.
113                         int ret = createNIUnderPkgRoot(pkg, flags);
114                         if (ret == NI_ERROR_INVALID_PACKAGE) {
115                                 _SERR("Failed to get root path from [%s]", pkg.c_str());
116                                 return -1;
117                         } else if (ret != NI_ERROR_NONE) {
118                                 _SERR("Failed to generate NI file [%s]", pkg.c_str());
119                                 return -1;
120                         }
121                 }
122         } else if (rmPkgMode) {
123                 for (const std::string pkg : args) {
124                         int ret = removeNIUnderPkgRoot(pkg);
125                         if (ret == NI_ERROR_INVALID_PACKAGE) {
126                                 _SERR("Failed to get root path from [%s]", pkg.c_str());
127                                 return -1;
128                         } else if (ret != NI_ERROR_NONE) {
129                                 _SERR("Failed to remove dlls for given package [%s]", pkg.c_str());
130                                 return -1;
131                         }
132                 }
133         } else if (dllMode) {
134                 // donot return error code for generation failure.
135                 // we have to run crossgen for all input dlls.
136                 for (const std::string dll : args) {
137                         int ret = createNIDll(dll, flags);
138                         if (ret == NI_ERROR_ALREADY_EXIST) {
139                                 // skip for already exist case
140                         } else if (ret != NI_ERROR_NONE) {
141                                 _SERR("Failed to generate NI file [%s]", dll.c_str());
142                         }
143                 }
144         }
145
146         return 0;
147 }