989fb422c0781cbf145df158e6b409ee210a446f
[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 "common.h"
18
19 #include <cstdio>
20 #include <cstring>
21
22 #include <algorithm>
23 #include <vector>
24
25 std::vector<const char*> getCmdArgs(char** begin, char** end)
26 {
27         std::vector<const char*> 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                 "               --reset-system      - Remove System NI files\n"
50                 "               --reset-pkg         - Remove App NI files\n"
51                 "               --regen-all-app     - Re-generate All App NI files\n"
52                 "\n"
53                 "Example:\n"
54                 "Create native image for dlls and exes under platform directories\n"
55                 "%s --system\n"
56                 "Create native image for dll\n"
57                 "%s --dll /usr/bin/Tizen.Runtime.dll\n"
58                 "Create native image under the package's bin and lib directory\n"
59                 "%s --pkg org.tizen.FormsGallery\n\n";
60         printf(helpDesc, 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
70         if (cmdOptionExists(argv, argv+argc, "--help")) {
71                 help(argv[0]);
72                 return 0;
73         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
74                 createNiPlatform();
75                 return 0;
76         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
77                 dllMode = true;
78         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
79                 pkgMode = true;
80         } else if (cmdOptionExists(argv, argv+argc, "--dir")) {
81                 dirMode = true;
82         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
83                 removeNiPlatform();
84                 return 0;
85         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
86                 rmPkgMode = true;
87         } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
88                 regenerateAppNI();
89                 return 0;
90         } else {
91                 help(argv[0]);
92                 return 1;
93         }
94
95         std::vector<const char*> args = getCmdArgs(argv, argv+argc);
96
97         if (args.size() < 1) {
98                 if (pkgMode)
99                         fprintf(stderr, "Package name is missed\n");
100                 else if (dllMode)
101                         fprintf(stderr, "DLL path is missed\n");
102                 help(argv[0]);
103                 return 1;
104         }
105
106         if (pkgMode) {
107                 for (const char* pkg : args) {
108                         if (createNiUnderPkgRoot(pkg) != 0) {
109                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg);
110                                 return 1;
111                         }
112                 }
113         } else if (rmPkgMode) {
114                 for (const char* pkg : args) {
115                         if (removeNiUnderPkgRoot(pkg) != 0) {
116                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg);
117                                 return 1;
118                         }
119                 }
120         } else if (dllMode) {
121                 for (const char* dll : args)
122                         createNiSelect(dll);
123         } else if (dirMode) {
124                 createNiUnderDirs(args.data(), args.size(), false);
125         }
126
127         return 0;
128 }