1 /*
2 * Copyright (C) 2015 The Android Open Source Project
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 #ifdef _WIN32
18 // clang-format off
19 #include <windows.h>
20 #include <shellapi.h>
21 // clang-format on
22 #endif
23
24 #include <iostream>
25 #include <vector>
26
27 #include "Diagnostics.h"
28 #include "android-base/stringprintf.h"
29 #include "android-base/utf8.h"
30 #include "androidfw/FileStream.h"
31 #include "androidfw/IDiagnostics.h"
32 #include "androidfw/StringPiece.h"
33 #include "cmd/ApkInfo.h"
34 #include "cmd/Command.h"
35 #include "cmd/Compile.h"
36 #include "cmd/Convert.h"
37 #include "cmd/Diff.h"
38 #include "cmd/Dump.h"
39 #include "cmd/Link.h"
40 #include "cmd/Optimize.h"
41 #include "trace/TraceBuffer.h"
42 #include "util/Files.h"
43 #include "util/Util.h"
44
45 using ::android::StringPiece;
46 using ::android::base::StringPrintf;
47
48 namespace aapt {
49
50 /** Prints the version information of AAPT2. */
51 class VersionCommand : public Command {
52 public:
VersionCommand()53 explicit VersionCommand() : Command("version") {
54 SetDescription("Prints the version of aapt.");
55 }
56
Action(const std::vector<std::string> &)57 int Action(const std::vector<std::string>& /* args */) override {
58 std::cerr << StringPrintf("%s %s", util::GetToolName(), util::GetToolFingerprint().c_str())
59 << std::endl;
60 return 0;
61 }
62 };
63
64 /** The main entry point of AAPT. */
65 class MainCommand : public Command {
66 public:
MainCommand(text::Printer * printer,android::IDiagnostics * diagnostics)67 explicit MainCommand(text::Printer* printer, android::IDiagnostics* diagnostics)
68 : Command("aapt2"), diagnostics_(diagnostics) {
69 AddOptionalSubcommand(util::make_unique<CompileCommand>(diagnostics));
70 AddOptionalSubcommand(util::make_unique<LinkCommand>(diagnostics));
71 AddOptionalSubcommand(util::make_unique<DumpCommand>(printer, diagnostics));
72 AddOptionalSubcommand(util::make_unique<DiffCommand>());
73 AddOptionalSubcommand(util::make_unique<OptimizeCommand>());
74 AddOptionalSubcommand(util::make_unique<ConvertCommand>());
75 AddOptionalSubcommand(util::make_unique<VersionCommand>());
76 AddOptionalSubcommand(util::make_unique<ApkInfoCommand>(diagnostics));
77 }
78
Action(const std::vector<std::string> & args)79 int Action(const std::vector<std::string>& args) override {
80 if (args.size() == 0) {
81 diagnostics_->Error(android::DiagMessage() << "no subcommand specified");
82 } else {
83 diagnostics_->Error(android::DiagMessage() << "unknown subcommand '" << args[0] << "'");
84 }
85
86 Usage(&std::cerr);
87 return -1;
88 }
89
90 private:
91 android::IDiagnostics* diagnostics_;
92 };
93
94 /*
95 * Run in daemon mode. The first line of input is the command. This can be 'quit' which ends
96 * the daemon mode. Each subsequent line is a single parameter to the command. The end of a
97 * invocation is signaled by providing an empty line. At any point, an EOF signal or the
98 * command 'quit' will end the daemon mode.
99 */
100 class DaemonCommand : public Command {
101 public:
DaemonCommand(android::FileOutputStream * out,android::IDiagnostics * diagnostics)102 explicit DaemonCommand(android::FileOutputStream* out, android::IDiagnostics* diagnostics)
103 : Command("daemon", "m"), out_(out), diagnostics_(diagnostics) {
104 SetDescription("Runs aapt in daemon mode. Each subsequent line is a single parameter to the\n"
105 "command. The end of an invocation is signaled by providing an empty line.");
106 AddOptionalFlag("--trace_folder", "Generate systrace json trace fragment to specified folder.",
107 &trace_folder_);
108 }
109
Action(const std::vector<std::string> & arguments)110 int Action(const std::vector<std::string>& arguments) override {
111 TRACE_FLUSH_ARGS(trace_folder_ ? trace_folder_.value() : "", "daemon", arguments);
112 text::Printer printer(out_);
113 std::cout << "Ready" << std::endl;
114
115 while (true) {
116 std::vector<std::string> raw_args;
117 for (std::string line; std::getline(std::cin, line) && !line.empty();) {
118 raw_args.push_back(line);
119 }
120
121 if (!std::cin) {
122 break;
123 }
124
125 // An empty command does nothing.
126 if (raw_args.empty()) {
127 continue;
128 }
129
130 // End the dameon
131 if (raw_args[0] == "quit") {
132 break;
133 }
134
135 std::vector<StringPiece> args;
136 args.insert(args.end(), raw_args.begin(), raw_args.end());
137 int result = MainCommand(&printer, diagnostics_).Execute(args, &std::cerr);
138 out_->Flush();
139 if (result != 0) {
140 std::cerr << "Error" << std::endl;
141 }
142 std::cerr << "Done" << std::endl;
143 }
144 std::cout << "Exiting daemon" << std::endl;
145
146 return 0;
147 }
148
149 private:
150 android::FileOutputStream* out_;
151 android::IDiagnostics* diagnostics_;
152 std::optional<std::string> trace_folder_;
153 };
154
155 } // namespace aapt
156
MainImpl(int argc,char ** argv)157 int MainImpl(int argc, char** argv) {
158 if (argc < 1) {
159 return -1;
160 }
161
162 // Collect the arguments starting after the program name and command name.
163 std::vector<StringPiece> args;
164 for (int i = 1; i < argc; i++) {
165 args.push_back(argv[i]);
166 }
167
168 // Use a smaller buffer so that there is less latency for printing to stdout.
169 constexpr size_t kStdOutBufferSize = 1024u;
170 android::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize);
171 aapt::text::Printer printer(&fout);
172
173 aapt::StdErrDiagnostics diagnostics;
174 aapt::MainCommand main_command(&printer, &diagnostics);
175
176 // Add the daemon subcommand here so it cannot be called while executing the daemon
177 main_command.AddOptionalSubcommand(
178 aapt::util::make_unique<aapt::DaemonCommand>(&fout, &diagnostics));
179 return main_command.Execute(args, &std::cerr);
180 }
181
main(int argc,char ** argv)182 int main(int argc, char** argv) {
183 #ifdef _WIN32
184 LPWSTR* wide_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
185 CHECK(wide_argv != nullptr) << "invalid command line parameters passed to process";
186
187 std::vector<std::string> utf8_args;
188 for (int i = 0; i < argc; i++) {
189 std::string utf8_arg;
190 if (!::android::base::WideToUTF8(wide_argv[i], &utf8_arg)) {
191 std::cerr << "error converting input arguments to UTF-8" << std::endl;
192 return 1;
193 }
194 utf8_args.push_back(std::move(utf8_arg));
195 }
196 LocalFree(wide_argv);
197
198 std::unique_ptr<char* []> utf8_argv(new char*[utf8_args.size()]);
199 for (int i = 0; i < argc; i++) {
200 utf8_argv[i] = const_cast<char*>(utf8_args[i].c_str());
201 }
202 argv = utf8_argv.get();
203 #endif
204 return MainImpl(argc, argv);
205 }
206