add missing else
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / 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 "ni_common.h"
18
19 #include <cstdio>
20 #include <cstring>
21
22 #include <algorithm>
23 #include <vector>
24
25 std::vector<std::string> getCmdArgs(char** begin, char** end)
26 {
27         std::vector<std::string> list;
28         for (char** itr = begin+1; itr != end; itr++) {
29                 if (strncmp(*itr, "--", 2) != 0)
30                         list.push_back(*itr);
31         }
32         return list;
33 }
34
35 bool cmdOptionExists(char** begin, char** end, const std::string& option)
36 {
37         return std::find(begin, end, option) != end;
38 }
39
40 static void help(const char *argv0)
41 {
42         const char* helpDesc =
43                 "Usage: %s [args] <root paths or pkg name>\n"
44                 "               --help              - Display this screen\n"
45                 "               --system            - Create NI under System DLLs\n"
46                 "               --dll               - Create NI for DLL\n"
47                 "               --pkg               - Create NI for package\n"
48                 "               --dir               - Create NI for directory\n"
49                 "               --r2r               - Use ready-to-run option (default: FNV)\n"
50                 "               --reset-system      - Remove System NI files\n"
51                 "               --reset-pkg         - Remove App NI files\n"
52                 "               --regen-all-app     - Re-generate All App NI files\n"
53                 "\n"
54                 "Example:\n"
55                 "Create native image for dlls and exes under platform directories\n"
56                 "%s --system\n"
57                 "Create native image for dll\n"
58                 "%s --dll /usr/bin/Tizen.Runtime.dll\n"
59                 "Create native image under the package's bin and lib directory\n"
60                 "%s --pkg org.tizen.FormsGallery\n\n";
61         printf(helpDesc, argv0, argv0, argv0, argv0);
62 }
63
64 int main(int argc, char* argv[])
65 {
66         bool pkgMode = false;
67         bool dllMode = false;
68         bool dirMode = false;
69         bool rmPkgMode = false;
70         bool enableR2R = false;
71         bool pkgDllMode = false;
72
73         NiCommonOption option = {std::string(), std::string(), std::string()};
74         if (initNICommon(&option) < 0) {
75                 fprintf(stderr, "Fail to initialize NI Common\n");
76                 return -1;
77         }
78
79         if (cmdOptionExists(argv, argv+argc, "--r2r")) {
80                 enableR2R = true;
81         }
82
83         if (cmdOptionExists(argv, argv+argc, "--help")) {
84                 help(argv[0]);
85                 return 0;
86         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
87                 createNiPlatform(enableR2R);
88                 return 0;
89         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
90                 dllMode = true;
91         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
92                 pkgMode = true;
93         } else if (cmdOptionExists(argv, argv+argc, "--dir")) {
94                 dirMode = true;
95         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
96                 removeNiPlatform();
97                 return 0;
98         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
99                 rmPkgMode = true;
100         } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
101                 regenerateAppNI();
102                 return 0;
103         } else if (cmdOptionExists(argv, argv+argc, "--pkg-dll")) {
104                 pkgDllMode = true;
105         } else {
106                 help(argv[0]);
107                 return 0;
108         }
109
110         std::vector<std::string> args = getCmdArgs(argv, argv+argc);
111
112         if (args.size() < 1) {
113                 if (pkgMode)
114                         fprintf(stderr, "Package name is missed\n");
115                 else if (dllMode)
116                         fprintf(stderr, "DLL path is missed\n");
117                 help(argv[0]);
118                 return 1;
119         }
120
121         if (pkgMode) {
122                 for (const std::string pkg : args) {
123                         if (createNiUnderPkgRoot(pkg, enableR2R) != 0) {
124                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
125                                 return -1;
126                         }
127                 }
128         } else if (pkgDllMode) {
129                 if (createNiDllUnderPkgRoot(args[0], args[1], enableR2R) != 0) {
130                         fprintf(stderr, "Failed to get root path from [%s]\n", args[0].c_str());
131                         return -1;
132                 }
133         } else if (rmPkgMode) {
134                 for (const std::string pkg : args) {
135                         if (removeNiUnderPkgRoot(pkg) != 0) {
136                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
137                                 return -1;
138                         }
139                 }
140         } else if (dllMode) {
141                 for (const std::string dll : args)
142                         createNiDll(dll, enableR2R);
143         } else if (dirMode) {
144                 createNiUnderDirs(args.data(), args.size(), enableR2R);
145         }
146
147         return 0;
148 }