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