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