Merge pull request #45 from dotnet/update_ni_common_to_handle_exe_extension
[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                 "                             (This option should be used with other options)\n"
51                 "       --reset-system      - Remove System NI files\n"
52                 "       --reset-pkg         - Remove App NI files\n"
53                 "       --regen-all-app     - Re-generate All App NI files\n"
54                 "\n"
55                 "Example:\n"
56                 "1. Create native image for dlls and exes under platform directories\n"
57                 "   # %s --system\n"
58                 "2. Create native image for dll\n"
59                 "   # %s --dll /usr/bin/Tizen.Runtime.dll\n"
60                 "3. Create native image under the package's bin and lib directory\n"
61                 "   # %s --pkg org.tizen.FormsGallery\n"
62                 "4. Regenerate native images for all installed .net packages with ready-to-run option\n"
63                 "   # %s --r2r --regen-all-app\n\n";
64         printf(helpDesc, argv0, argv0, argv0, argv0, argv0);
65 }
66
67 int main(int argc, char* argv[])
68 {
69         bool pkgMode = false;
70         bool dllMode = false;
71         bool dirMode = false;
72         bool rmPkgMode = false;
73         bool enableR2R = false;
74         bool pkgDllMode = false;
75
76         NiCommonOption option = {std::string(), std::string(), std::string()};
77         if (initNICommon(&option) < 0) {
78                 fprintf(stderr, "Fail to initialize NI Common\n");
79                 return -1;
80         }
81
82         if (cmdOptionExists(argv, argv+argc, "--r2r")) {
83                 enableR2R = true;
84         }
85
86         if (cmdOptionExists(argv, argv+argc, "--help")) {
87                 help(argv[0]);
88                 return 0;
89         } else if (cmdOptionExists(argv, argv+argc, "--system")) {
90                 createNiPlatform(enableR2R);
91                 return 0;
92         } else if (cmdOptionExists(argv, argv+argc, "--dll")) {
93                 dllMode = true;
94         } else if (cmdOptionExists(argv, argv+argc, "--pkg")) {
95                 pkgMode = true;
96         } else if (cmdOptionExists(argv, argv+argc, "--dir")) {
97                 dirMode = true;
98         } else if (cmdOptionExists(argv, argv+argc, "--reset-system")) {
99                 removeNiPlatform();
100                 return 0;
101         } else if (cmdOptionExists(argv, argv+argc, "--reset-pkg")) {
102                 rmPkgMode = true;
103         } else if (cmdOptionExists(argv, argv+argc, "--regen-all-app")) {
104                 regenerateAppNI(enableR2R);
105                 return 0;
106         } else if (cmdOptionExists(argv, argv+argc, "--pkg-dll")) {
107                 pkgDllMode = true;
108         } else {
109                 help(argv[0]);
110                 return 0;
111         }
112
113         std::vector<std::string> args = getCmdArgs(argv, argv+argc);
114
115         if (args.size() < 1) {
116                 if (pkgMode)
117                         fprintf(stderr, "Package name is missed\n");
118                 else if (dllMode)
119                         fprintf(stderr, "DLL path is missed\n");
120                 help(argv[0]);
121                 return 1;
122         }
123
124         if (pkgMode) {
125                 for (const std::string pkg : args) {
126                         if (createNiUnderPkgRoot(pkg, enableR2R) != 0) {
127                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
128                                 return -1;
129                         }
130                 }
131         } else if (pkgDllMode) {
132                 if (createNiDllUnderPkgRoot(args[0], args[1], enableR2R) != 0) {
133                         fprintf(stderr, "Failed to get root path from [%s]\n", args[0].c_str());
134                         return -1;
135                 }
136         } else if (rmPkgMode) {
137                 for (const std::string pkg : args) {
138                         if (removeNiUnderPkgRoot(pkg) != 0) {
139                                 fprintf(stderr, "Failed to get root path from [%s]\n", pkg.c_str());
140                                 return -1;
141                         }
142                 }
143         } else if (dllMode) {
144                 for (const std::string dll : args) {
145                         if (createNiDll(dll, enableR2R) != 0) {
146                                 fprintf(stderr, "Failed to generate NI file [%s]\n", dll.c_str());
147                         }
148                 }
149         } else if (dirMode) {
150                 createNiUnderDirs(args.data(), args.size(), enableR2R);
151         }
152
153         return 0;
154 }