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 | |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 28 | #include "android-base/utf8.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 31 | #include "Diagnostics.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" |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 39 | #include "util/Files.h" |
| 40 | #include "util/Util.h" |
| 41 | |
| 42 | using ::android::StringPiece; |
| 43 | using ::android::base::StringPrintf; |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 44 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 45 | namespace aapt { |
Adam Lesinski | 5886a92 | 2015-04-15 20:29:22 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 0368ebf | 2016-07-26 12:55:51 -0700 | [diff] [blame] | 47 | // DO NOT UPDATE, this is more of a marketing version. |
| 48 | static const char* sMajorVersion = "2"; |
| 49 | |
| 50 | // Update minor version whenever a feature or flag is added. |
Adam Lesinski | 3420eed | 2017-09-13 14:46:00 -0700 | [diff] [blame] | 51 | static const char* sMinorVersion = "19"; |
Adam Lesinski | 0368ebf | 2016-07-26 12:55:51 -0700 | [diff] [blame] | 52 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 53 | /** Prints the version information of AAPT2. */ |
| 54 | class VersionCommand : public Command { |
| 55 | public: |
| 56 | explicit VersionCommand() : Command("version") { |
| 57 | SetDescription("Prints the version of aapt."); |
| 58 | } |
Adam Lesinski | 0368ebf | 2016-07-26 12:55:51 -0700 | [diff] [blame] | 59 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 60 | int Action(const std::vector<std::string>& /* args */) override { |
| 61 | std::cerr << StringPrintf("Android Asset Packaging Tool (aapt) %s:%s", sMajorVersion, |
| 62 | sMinorVersion) |
| 63 | << std::endl; |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 64 | return 0; |
| 65 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 66 | }; |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 67 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 68 | /** The main entry point of AAPT. */ |
| 69 | class MainCommand : public Command { |
| 70 | public: |
| 71 | explicit MainCommand(IDiagnostics* diagnostics) : Command("aapt2"), diagnostics_(diagnostics) { |
| 72 | AddOptionalSubcommand(util::make_unique<CompileCommand>(diagnostics)); |
| 73 | AddOptionalSubcommand(util::make_unique<LinkCommand>(diagnostics)); |
Ryan Mitchell | 5d27551 | 2018-07-19 14:29:00 -0700 | [diff] [blame] | 74 | AddOptionalSubcommand(util::make_unique<DumpCommand>(diagnostics)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 75 | AddOptionalSubcommand(util::make_unique<DiffCommand>()); |
| 76 | AddOptionalSubcommand(util::make_unique<OptimizeCommand>()); |
| 77 | AddOptionalSubcommand(util::make_unique<ConvertCommand>()); |
| 78 | AddOptionalSubcommand(util::make_unique<VersionCommand>()); |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 79 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 80 | |
| 81 | int Action(const std::vector<std::string>& args) override { |
| 82 | if (args.size() == 0) { |
| 83 | diagnostics_->Error(DiagMessage() << "no subcommand specified"); |
| 84 | } else { |
| 85 | diagnostics_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'"); |
| 86 | } |
| 87 | |
| 88 | Usage(&std::cerr); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | IDiagnostics* diagnostics_; |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * Run in daemon mode. The first line of input is the command. This can be 'quit' which ends |
| 98 | * the daemon mode. Each subsequent line is a single parameter to the command. The end of a |
| 99 | * invocation is signaled by providing an empty line. At any point, an EOF signal or the |
| 100 | * command 'quit' will end the daemon mode. |
| 101 | */ |
| 102 | class DaemonCommand : public Command { |
| 103 | public: |
| 104 | explicit DaemonCommand(IDiagnostics* diagnostics) : Command("daemon", "m"), |
| 105 | diagnostics_(diagnostics) { |
| 106 | SetDescription("Runs aapt in daemon mode. Each subsequent line is a single parameter to the\n" |
| 107 | "command. The end of an invocation is signaled by providing an empty line."); |
| 108 | } |
| 109 | |
| 110 | int Action(const std::vector<std::string>& /* args */) override { |
| 111 | std::cout << "Ready" << std::endl; |
| 112 | |
| 113 | while (true) { |
| 114 | std::vector<std::string> raw_args; |
| 115 | for (std::string line; std::getline(std::cin, line) && !line.empty();) { |
| 116 | raw_args.push_back(line); |
| 117 | } |
| 118 | |
| 119 | if (!std::cin) { |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | // An empty command does nothing. |
| 124 | if (raw_args.empty()) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // End the dameon |
| 129 | if (raw_args[0] == "quit") { |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | std::vector<StringPiece> args; |
| 134 | args.insert(args.end(), raw_args.begin(), raw_args.end()); |
| 135 | if (MainCommand(diagnostics_).Execute(args, &std::cerr) != 0) { |
| 136 | std::cerr << "Error" << std::endl; |
| 137 | } |
| 138 | std::cerr << "Done" << std::endl; |
| 139 | } |
| 140 | std::cout << "Exiting daemon" << std::endl; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | IDiagnostics* diagnostics_; |
| 147 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 148 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | } // namespace aapt |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 150 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 151 | int MainImpl(int argc, char** argv) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 152 | if (argc < 1) { |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 153 | return -1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 156 | // Collect the arguments starting after the program name and command name. |
| 157 | std::vector<StringPiece> args; |
| 158 | for (int i = 1; i < argc; i++) { |
| 159 | args.push_back(argv[i]); |
| 160 | } |
| 161 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 162 | // Add the daemon subcommand here so it cannot be called while executing the daemon |
| 163 | aapt::StdErrDiagnostics diagnostics; |
| 164 | auto main_command = new aapt::MainCommand(&diagnostics); |
| 165 | main_command->AddOptionalSubcommand(aapt::util::make_unique<aapt::DaemonCommand>(&diagnostics)); |
Adam Lesinski | 448a15c | 2017-07-25 10:59:26 -0700 | [diff] [blame] | 166 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 167 | return main_command->Execute(args, &std::cerr); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 168 | } |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 169 | |
| 170 | int main(int argc, char** argv) { |
| 171 | #ifdef _WIN32 |
| 172 | LPWSTR* wide_argv = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 173 | CHECK(wide_argv != nullptr) << "invalid command line parameters passed to process"; |
| 174 | |
| 175 | std::vector<std::string> utf8_args; |
| 176 | for (int i = 0; i < argc; i++) { |
| 177 | std::string utf8_arg; |
| 178 | if (!::android::base::WideToUTF8(wide_argv[i], &utf8_arg)) { |
| 179 | std::cerr << "error converting input arguments to UTF-8" << std::endl; |
| 180 | return 1; |
| 181 | } |
| 182 | utf8_args.push_back(std::move(utf8_arg)); |
| 183 | } |
| 184 | LocalFree(wide_argv); |
| 185 | |
| 186 | std::unique_ptr<char* []> utf8_argv(new char*[utf8_args.size()]); |
| 187 | for (int i = 0; i < argc; i++) { |
| 188 | utf8_argv[i] = const_cast<char*>(utf8_args[i].c_str()); |
| 189 | } |
| 190 | argv = utf8_argv.get(); |
| 191 | #endif |
| 192 | return MainImpl(argc, argv); |
| 193 | } |