blob: adf85b0ea8e886bc84f60255a0bb37d389bccb68 [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
Adam Lesinski448a15c2017-07-25 10:59:26 -070027#include "android-base/stringprintf.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070028#include "android-base/utf8.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080029#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030
Chris Warrington820d72a2017-04-27 15:27:01 +010031#include "Diagnostics.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070032#include "cmd/Command.h"
33#include "cmd/Compile.h"
34#include "cmd/Convert.h"
35#include "cmd/Diff.h"
36#include "cmd/Dump.h"
37#include "cmd/Link.h"
38#include "cmd/Optimize.h"
Ryan Mitchell214846d2018-09-19 16:57:01 -070039#include "io/FileStream.h"
Adam Lesinski448a15c2017-07-25 10:59:26 -070040#include "util/Files.h"
41#include "util/Util.h"
42
43using ::android::StringPiece;
44using ::android::base::StringPrintf;
Chris Warrington820d72a2017-04-27 15:27:01 +010045
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046namespace aapt {
Adam Lesinski5886a922015-04-15 20:29:22 -070047
Adam Lesinski0368ebf2016-07-26 12:55:51 -070048// DO NOT UPDATE, this is more of a marketing version.
49static const char* sMajorVersion = "2";
50
51// Update minor version whenever a feature or flag is added.
Adam Lesinski3420eed2017-09-13 14:46:00 -070052static const char* sMinorVersion = "19";
Adam Lesinski0368ebf2016-07-26 12:55:51 -070053
Ryan Mitchell833a1a62018-07-10 13:51:36 -070054/** Prints the version information of AAPT2. */
55class VersionCommand : public Command {
56 public:
57 explicit VersionCommand() : Command("version") {
58 SetDescription("Prints the version of aapt.");
59 }
Adam Lesinski0368ebf2016-07-26 12:55:51 -070060
Ryan Mitchell833a1a62018-07-10 13:51:36 -070061 int Action(const std::vector<std::string>& /* args */) override {
62 std::cerr << StringPrintf("Android Asset Packaging Tool (aapt) %s:%s", sMajorVersion,
63 sMinorVersion)
64 << std::endl;
Adam Lesinski448a15c2017-07-25 10:59:26 -070065 return 0;
66 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -070067};
Adam Lesinski448a15c2017-07-25 10:59:26 -070068
Ryan Mitchell833a1a62018-07-10 13:51:36 -070069/** The main entry point of AAPT. */
70class MainCommand : public Command {
71 public:
Ryan Mitchell214846d2018-09-19 16:57:01 -070072 explicit MainCommand(text::Printer* printer, IDiagnostics* diagnostics)
73 : Command("aapt2"), diagnostics_(diagnostics) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -070074 AddOptionalSubcommand(util::make_unique<CompileCommand>(diagnostics));
75 AddOptionalSubcommand(util::make_unique<LinkCommand>(diagnostics));
Ryan Mitchell214846d2018-09-19 16:57:01 -070076 AddOptionalSubcommand(util::make_unique<DumpCommand>(printer, diagnostics));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070077 AddOptionalSubcommand(util::make_unique<DiffCommand>());
78 AddOptionalSubcommand(util::make_unique<OptimizeCommand>());
79 AddOptionalSubcommand(util::make_unique<ConvertCommand>());
80 AddOptionalSubcommand(util::make_unique<VersionCommand>());
Adam Lesinski448a15c2017-07-25 10:59:26 -070081 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -070082
83 int Action(const std::vector<std::string>& args) override {
84 if (args.size() == 0) {
85 diagnostics_->Error(DiagMessage() << "no subcommand specified");
86 } else {
87 diagnostics_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'");
88 }
89
90 Usage(&std::cerr);
91 return -1;
92 }
93
94 private:
95 IDiagnostics* diagnostics_;
96};
97
98/*
99 * Run in daemon mode. The first line of input is the command. This can be 'quit' which ends
100 * the daemon mode. Each subsequent line is a single parameter to the command. The end of a
101 * invocation is signaled by providing an empty line. At any point, an EOF signal or the
102 * command 'quit' will end the daemon mode.
103 */
104class DaemonCommand : public Command {
105 public:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700106 explicit DaemonCommand(io::FileOutputStream* out, IDiagnostics* diagnostics)
107 : Command("daemon", "m"), out_(out), diagnostics_(diagnostics) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700108 SetDescription("Runs aapt in daemon mode. Each subsequent line is a single parameter to the\n"
109 "command. The end of an invocation is signaled by providing an empty line.");
110 }
111
112 int Action(const std::vector<std::string>& /* args */) override {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700113 text::Printer printer(out_);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700114 std::cout << "Ready" << std::endl;
115
116 while (true) {
117 std::vector<std::string> raw_args;
118 for (std::string line; std::getline(std::cin, line) && !line.empty();) {
119 raw_args.push_back(line);
120 }
121
122 if (!std::cin) {
123 break;
124 }
125
126 // An empty command does nothing.
127 if (raw_args.empty()) {
128 continue;
129 }
130
131 // End the dameon
132 if (raw_args[0] == "quit") {
133 break;
134 }
135
136 std::vector<StringPiece> args;
137 args.insert(args.end(), raw_args.begin(), raw_args.end());
Ryan Mitchell214846d2018-09-19 16:57:01 -0700138 int result = MainCommand(&printer, diagnostics_).Execute(args, &std::cerr);
139 out_->Flush();
140 if (result != 0) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700141 std::cerr << "Error" << std::endl;
142 }
143 std::cerr << "Done" << std::endl;
144 }
145 std::cout << "Exiting daemon" << std::endl;
146
147 return 0;
148 }
149
150 private:
Ryan Mitchell214846d2018-09-19 16:57:01 -0700151 io::FileOutputStream* out_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700152 IDiagnostics* diagnostics_;
153};
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;
170 aapt::io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize);
171 aapt::text::Printer printer(&fout);
Adam Lesinski448a15c2017-07-25 10:59:26 -0700172
Ryan Mitchell214846d2018-09-19 16:57:01 -0700173 aapt::StdErrDiagnostics diagnostics;
174 auto main_command = new aapt::MainCommand(&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));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700179 return main_command->Execute(args, &std::cerr);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180}
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700181
182int 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}