blob: b351d6ee4f3349b8aaa4ee46b03c55452f89686d [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070017#ifdef _WIN32
18// clang-format off
19#include <windows.h>
20#include <shellapi.h>
21// clang-format on
22#endif
23
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <iostream>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include <vector>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026
Iurii Makhnof2480742022-04-26 14:51:24 +000027#include "Diagnostics.h"
Adam Lesinski448a15c2017-07-25 10:59:26 -070028#include "android-base/stringprintf.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070029#include "android-base/utf8.h"
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000030#include "androidfw/FileStream.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031#include "androidfw/IDiagnostics.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080032#include "androidfw/StringPiece.h"
Iurii Makhnof2480742022-04-26 14:51:24 +000033#include "cmd/ApkInfo.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070034#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"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080041#include "trace/TraceBuffer.h"
Adam Lesinski448a15c2017-07-25 10:59:26 -070042#include "util/Files.h"
43#include "util/Util.h"
44
45using ::android::StringPiece;
46using ::android::base::StringPrintf;
Chris Warrington820d72a2017-04-27 15:27:01 +010047
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048namespace aapt {
Adam Lesinski5886a922015-04-15 20:29:22 -070049
Ryan Mitchell833a1a62018-07-10 13:51:36 -070050/** Prints the version information of AAPT2. */
51class VersionCommand : public Command {
52 public:
53 explicit VersionCommand() : Command("version") {
54 SetDescription("Prints the version of aapt.");
55 }
Adam Lesinski0368ebf2016-07-26 12:55:51 -070056
Ryan Mitchell833a1a62018-07-10 13:51:36 -070057 int Action(const std::vector<std::string>& /* args */) override {
Ryan Mitchell34039b22019-03-18 08:57:47 -070058 std::cerr << StringPrintf("%s %s", util::GetToolName(), util::GetToolFingerprint().c_str())
Ryan Mitchell833a1a62018-07-10 13:51:36 -070059 << std::endl;
Adam Lesinski448a15c2017-07-25 10:59:26 -070060 return 0;
61 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -070062};
Adam Lesinski448a15c2017-07-25 10:59:26 -070063
Ryan Mitchell833a1a62018-07-10 13:51:36 -070064/** The main entry point of AAPT. */
65class MainCommand : public Command {
66 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +000067 explicit MainCommand(text::Printer* printer, android::IDiagnostics* diagnostics)
Ryan Mitchell214846d2018-09-19 16:57:01 -070068 : Command("aapt2"), diagnostics_(diagnostics) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -070069 AddOptionalSubcommand(util::make_unique<CompileCommand>(diagnostics));
70 AddOptionalSubcommand(util::make_unique<LinkCommand>(diagnostics));
Ryan Mitchell214846d2018-09-19 16:57:01 -070071 AddOptionalSubcommand(util::make_unique<DumpCommand>(printer, diagnostics));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070072 AddOptionalSubcommand(util::make_unique<DiffCommand>());
73 AddOptionalSubcommand(util::make_unique<OptimizeCommand>());
74 AddOptionalSubcommand(util::make_unique<ConvertCommand>());
75 AddOptionalSubcommand(util::make_unique<VersionCommand>());
Iurii Makhnof2480742022-04-26 14:51:24 +000076 AddOptionalSubcommand(util::make_unique<ApkInfoCommand>(diagnostics));
Adam Lesinski448a15c2017-07-25 10:59:26 -070077 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -070078
79 int Action(const std::vector<std::string>& args) override {
80 if (args.size() == 0) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000081 diagnostics_->Error(android::DiagMessage() << "no subcommand specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -070082 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000083 diagnostics_->Error(android::DiagMessage() << "unknown subcommand '" << args[0] << "'");
Ryan Mitchell833a1a62018-07-10 13:51:36 -070084 }
85
86 Usage(&std::cerr);
87 return -1;
88 }
89
90 private:
Jeremy Meyer56f36e82022-05-20 20:35:42 +000091 android::IDiagnostics* diagnostics_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070092};
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 */
100class DaemonCommand : public Command {
101 public:
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000102 explicit DaemonCommand(android::FileOutputStream* out, android::IDiagnostics* diagnostics)
Ryan Mitchell214846d2018-09-19 16:57:01 -0700103 : Command("daemon", "m"), out_(out), diagnostics_(diagnostics) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700104 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.");
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800106 AddOptionalFlag("--trace_folder", "Generate systrace json trace fragment to specified folder.",
107 &trace_folder_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700108 }
109
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800110 int Action(const std::vector<std::string>& arguments) override {
111 TRACE_FLUSH_ARGS(trace_folder_ ? trace_folder_.value() : "", "daemon", arguments);
Ryan Mitchell214846d2018-09-19 16:57:01 -0700112 text::Printer printer(out_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700113 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());
Ryan Mitchell214846d2018-09-19 16:57:01 -0700137 int result = MainCommand(&printer, diagnostics_).Execute(args, &std::cerr);
138 out_->Flush();
139 if (result != 0) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700140 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:
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000150 android::FileOutputStream* out_;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000151 android::IDiagnostics* diagnostics_;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700152 std::optional<std::string> trace_folder_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700153};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155} // namespace aapt
Adam Lesinski330edcd2015-05-04 17:40:56 -0700156
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700157int MainImpl(int argc, char** argv) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700158 if (argc < 1) {
Adam Lesinski448a15c2017-07-25 10:59:26 -0700159 return -1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 }
161
Adam Lesinski448a15c2017-07-25 10:59:26 -0700162 // 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
Ryan Mitchell214846d2018-09-19 16:57:01 -0700168 // Use a smaller buffer so that there is less latency for printing to stdout.
169 constexpr size_t kStdOutBufferSize = 1024u;
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000170 android::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize);
Ryan Mitchell214846d2018-09-19 16:57:01 -0700171 aapt::text::Printer printer(&fout);
Adam Lesinski448a15c2017-07-25 10:59:26 -0700172
Ryan Mitchell214846d2018-09-19 16:57:01 -0700173 aapt::StdErrDiagnostics diagnostics;
Ryan Mitchellac55e412019-09-24 16:26:20 -0700174 aapt::MainCommand main_command(&printer, &diagnostics);
Ryan Mitchell214846d2018-09-19 16:57:01 -0700175
176 // Add the daemon subcommand here so it cannot be called while executing the daemon
Ryan Mitchellac55e412019-09-24 16:26:20 -0700177 main_command.AddOptionalSubcommand(
Ryan Mitchell214846d2018-09-19 16:57:01 -0700178 aapt::util::make_unique<aapt::DaemonCommand>(&fout, &diagnostics));
Ryan Mitchellac55e412019-09-24 16:26:20 -0700179 return main_command.Execute(args, &std::cerr);
Steven Moreland2700afd2019-09-19 11:08:47 -0700180}
181
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700182int 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}