Modify the problem that the original assembly is deleted (#80)
[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                 "%s is deprecated. Please use dotnettool instead.\n"
40                 "Example:\n"
41                 "   # dotnettool --help\n\n";
42         printf(helpDesc, argv0);
43 }
44
45 int main(int argc, char* argv[])
46 {
47         bool pkgMode = false;
48         bool dllMode = false;
49         bool dirMode = false;
50         bool rmPkgMode = false;
51         bool enableR2R = false;
52         bool pkgDllMode = false;
53
54         bool doGenUniqueBaseSystem = true;
55
56         NiCommonOption option = {std::string(), std::string(), std::string()};
57         if (initNICommon(&option) != NI_ERROR_NONE) {
58                 fprintf(stderr, "Fail to initialize NI Common\n");
59                 return -1;
60         }
61
62         if (cmdOptionExists(argv, argv+argc, "--r2r")) {
63                 enableR2R = true;
64         }
65
66         if (cmdOptionExists(argv, argv+argc, "--help")) {
67                 help(argv[0]);
68                 return 0;
69         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
70                 createNiPlatform(enableR2R, doGenUniqueBaseSystem);
71                 return 0;
72         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
73                 dllMode = true;
74         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
75                 pkgMode = true;
76         } else if (cmdOptionExists(argv, argv+argc, "--dir")) {
77                 dirMode = true;
78         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
79                 removeNiPlatform();
80                 return 0;
81         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
82                 rmPkgMode = true;
83         } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
84                 regenerateAppNI(enableR2R);
85                 return 0;
86         } else if (cmdOptionExists(argv, argv+argc, "--pkg-dll")) {
87                 pkgDllMode = true;
88         } else {
89                 help(argv[0]);
90                 return 0;
91         }
92
93         std::vector<std::string> args = getCmdArgs(argv, argv+argc);
94
95         if (args.size() < 1) {
96                 if (pkgMode)
97                         fprintf(stderr, "Package name is missed\n");
98                 else if (dllMode)
99                         fprintf(stderr, "DLL path is missed\n");
100                 help(argv[0]);
101                 return 1;
102         }
103
104         if (pkgMode) {
105                 for (const std::string pkg : args) {
106                         // if there is AOTed dlls under package root, that is skiped.
107                         int ret = createNiUnderPkgRoot(pkg, enableR2R);
108                         if (ret == NI_ERROR_INVALID_PACKAGE) {
109                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
110                                 return -1;
111                         } else if (ret != NI_ERROR_NONE) {
112                                 fprintf(stderr, "Failed to generate NI file [%s]\n", pkg.c_str());
113                                 return -1;
114                         }
115                 }
116         } else if (pkgDllMode) {
117                 int ret = createNiDllUnderPkgRoot(args[0], args[1], enableR2R);
118                 if (ret == NI_ERROR_INVALID_PACKAGE) {
119                         fprintf(stderr, "Failed to get root path from [%s]\n", args[0].c_str());
120                         return -1;
121                 } else if (ret == NI_ERROR_ALREADY_EXIST) {
122                         // skip for already exist case
123                         return -1;
124                 } else if (ret != NI_ERROR_NONE) {
125                         fprintf(stderr, "Failed to generate NI file [%s]\n", args[1].c_str());
126                         return -1;
127                 }
128         } else if (rmPkgMode) {
129                 for (const std::string pkg : args) {
130                         int ret = removeNiUnderPkgRoot(pkg);
131                         if (ret == NI_ERROR_INVALID_PACKAGE) {
132                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
133                                 return -1;
134                         } else if (ret != NI_ERROR_NONE) {
135                                 fprintf(stderr, "Failed to remove dlls for given package [%s]\n", pkg.c_str());
136                                 return -1;
137                         }
138                 }
139         } else if (dllMode) {
140                 // donot return error code for generation failure.
141                 // we have to run crossgen for all input dlls.
142                 for (const std::string dll : args) {
143                         int ret = createNiDll(dll, enableR2R, doGenUniqueBaseSystem);
144                         if (ret == NI_ERROR_ALREADY_EXIST) {
145                                 // skip for already exist case
146                         } else if (ret != NI_ERROR_NONE) {
147                                 fprintf(stderr, "Failed to generate NI file [%s]\n", dll.c_str());
148                         }
149                 }
150         } else if (dirMode) {
151                 createNiUnderDirs(args.data(), args.size(), enableR2R, doGenUniqueBaseSystem);
152         }
153
154         return 0;
155 }