blob: 2d83a14e541d1630a9fe8df84afa4923a805e522 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskice5e56e2016-10-21 17:56:45 -070017#include <dirent.h>
18
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include <string>
20
Adam Lesinskid5083f62017-01-16 15:07:21 -080021#include "android-base/errors.h"
22#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070023#include "android-base/utf8.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25#include "google/protobuf/io/coded_stream.h"
26#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ConfigDescription.h"
29#include "Diagnostics.h"
30#include "Flags.h"
31#include "ResourceParser.h"
32#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070034#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080036#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070038#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070039#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070040#include "format/proto/ProtoSerialize.h"
Adam Lesinski00451162017-10-03 07:44:08 -070041#include "io/BigBufferStream.h"
42#include "io/FileStream.h"
43#include "io/StringStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070044#include "io/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045#include "util/Files.h"
46#include "util/Maybe.h"
47#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080048#include "xml/XmlDom.h"
49#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070051using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000052using ::aapt::text::Printer;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070053using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070054using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070055using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070056
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057namespace aapt {
58
59struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 std::string name;
63 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070065 // Original config str. We keep this because when we parse the config, we may add on
66 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070};
71
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070072// Resource file paths are expected to look like: [--/res/]type[-config]/name
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path,
74 std::string* out_error) {
75 std::vector<std::string> parts = util::Split(path, file::sDirSep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 return {};
79 }
80
81 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 size_t dash_pos = dir.find('-');
87 if (dash_pos != std::string::npos) {
88 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
89 if (!ConfigDescription::Parse(config_str, &config)) {
90 if (out_error) {
91 std::stringstream err_str;
92 err_str << "invalid configuration '" << config_str << "'";
93 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 }
95 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 std::string& filename = parts[parts.size() - 1];
101 StringPiece name = filename;
102 StringPiece extension;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 size_t dot_pos = filename.find('.');
104 if (dot_pos != std::string::npos) {
105 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
106 name = name.substr(0, dot_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109 return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(),
110 extension.to_string(), config_str.to_string(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111}
112
113struct CompileOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 std::string output_path;
115 Maybe<std::string> res_dir;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000116 Maybe<std::string> generate_text_symbols_path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 bool pseudolocalize = false;
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700118 bool no_png_crunch = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 bool legacy_mode = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 bool verbose = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121};
122
Adam Lesinski00451162017-10-03 07:44:08 -0700123static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 name << data.resource_dir;
126 if (!data.config_str.empty()) {
127 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 }
129 name << "_" << data.name;
130 if (!data.extension.empty()) {
131 name << "." << data.extension;
132 }
133 name << ".flat";
134 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800135}
136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137static bool IsHidden(const StringPiece& filename) {
138 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800139}
140
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700141// Walks the res directory structure, looking for resource files.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700142static bool LoadInputFilesFromDir(IAaptContext* context, const CompileOptions& options,
143 std::vector<ResourcePathData>* out_path_data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 const std::string& root_dir = options.res_dir.value();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700145 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 if (!d) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700147 context->GetDiagnostics()->Error(DiagMessage(root_dir) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700148 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 return false;
150 }
151
152 while (struct dirent* entry = readdir(d.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 if (IsHidden(entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 continue;
155 }
156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 std::string prefix_path = root_dir;
158 file::AppendPath(&prefix_path, entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 if (file::GetFileType(prefix_path) != file::FileType::kDirectory) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 continue;
162 }
163
Adam Lesinski06460ef2017-03-14 18:52:13 -0700164 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 if (!subdir) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700166 context->GetDiagnostics()->Error(DiagMessage(prefix_path) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700167 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 return false;
169 }
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 while (struct dirent* leaf_entry = readdir(subdir.get())) {
172 if (IsHidden(leaf_entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 continue;
174 }
175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 std::string full_path = prefix_path;
177 file::AppendPath(&full_path, leaf_entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 std::string err_str;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700180 Maybe<ResourcePathData> path_data = ExtractResourcePathData(full_path, &err_str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 if (!path_data) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700182 context->GetDiagnostics()->Error(DiagMessage(full_path) << err_str);
Adam Lesinskia40e9722015-11-24 19:11:46 -0800183 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 }
185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 out_path_data->push_back(std::move(path_data.value()));
Adam Lesinskia40e9722015-11-24 19:11:46 -0800187 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 }
Adam Lesinskie6aa6d12017-12-20 14:01:14 -0800189
190 // File-system directory enumeration order is platform-dependent. Sort the result to remove any
191 // inconsistencies between platforms.
192 std::sort(
193 out_path_data->begin(), out_path_data->end(),
194 [](const ResourcePathData& a, const ResourcePathData& b) { return a.source < b.source; });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196}
197
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700199 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 const std::string& output_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 ResourceTable table;
202 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700203 FileInputStream fin(path_data.source.path);
204 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700206 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700208 }
209
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 // Parse the values file from XML.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700211 xml::XmlPullParser xml_parser(&fin);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 ResourceParserOptions parser_options;
214 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700216 // If the filename includes donottranslate, then the default translatable is false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700217 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700219 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 parser_options);
221 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800223 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800225
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 if (options.pseudolocalize) {
227 // Generate pseudo-localized strings (en-XA and ar-XB).
228 // These are created as weak symbols, and are only generated from default
229 // configuration
230 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 PseudolocaleGenerator pseudolocale_generator;
232 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700234 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239
240 // Assign an ID to any package that has resources.
241 for (auto& pkg : table.packages) {
242 if (!pkg->id) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700243 // If no package ID was set while parsing (public identifiers), auto assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700250 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 return false;
252 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800253
Adam Lesinski00451162017-10-03 07:44:08 -0700254 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 {
Adam Lesinski00451162017-10-03 07:44:08 -0700256 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700258 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700260 pb::ResourceTable pb_table;
Ryan Mitchell70414f22018-03-26 11:05:31 -0700261 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700262 if (!container_writer.AddResTableEntry(pb_table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700263 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700269 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 return false;
271 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000272
273 if (options.generate_text_symbols_path) {
274 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
275
276 if (fout_text.HadError()) {
277 context->GetDiagnostics()->Error(DiagMessage()
278 << "failed writing to'"
279 << options.generate_text_symbols_path.value()
280 << "': " << fout_text.GetError());
281 return false;
282 }
283
284 Printer r_txt_printer(&fout_text);
285 for (const auto& package : table.packages) {
286 for (const auto& type : package->types) {
287 for (const auto& entry : type->entries) {
288 // Check access modifiers.
289 switch(entry->visibility.level) {
290 case Visibility::Level::kUndefined :
291 r_txt_printer.Print("default ");
292 break;
293 case Visibility::Level::kPublic :
294 r_txt_printer.Print("public ");
295 break;
296 case Visibility::Level::kPrivate :
297 r_txt_printer.Print("private ");
298 }
299
300 if (type->type != ResourceType::kStyleable) {
301 r_txt_printer.Print("int ");
302 r_txt_printer.Print(to_string(type->type));
303 r_txt_printer.Print(" ");
304 r_txt_printer.Println(entry->name);
305 } else {
306 r_txt_printer.Print("int[] styleable ");
307 r_txt_printer.Println(entry->name);
308
309 if (!entry->values.empty()) {
310 auto styleable = static_cast<const Styleable*>(entry->values.front()->value.get());
311 for (const auto& attr : styleable->entries) {
312 r_txt_printer.Print("default int styleable ");
313 r_txt_printer.Print(entry->name);
314 r_txt_printer.Print("_");
315 r_txt_printer.Println(attr.name.value().entry);
316 }
317 }
318 }
319 }
320 }
321 }
322 }
323
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800325}
326
Adam Lesinski00451162017-10-03 07:44:08 -0700327static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
328 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800329 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 if (!writer->StartEntry(output_path, 0)) {
332 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 return false;
334 }
335
Adam Lesinski00451162017-10-03 07:44:08 -0700336 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 {
Adam Lesinski00451162017-10-03 07:44:08 -0700338 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700339 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700340 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700342 pb::internal::CompiledFile pb_compiled_file;
343 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344
Adam Lesinski00451162017-10-03 07:44:08 -0700345 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
346 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800348 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800350
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 if (!writer->FinishEntry()) {
352 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 return false;
354 }
355 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700356}
357
Adam Lesinski00451162017-10-03 07:44:08 -0700358static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
359 ContainerWriter* container_writer, IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700360 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700361 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700362
Adam Lesinski00451162017-10-03 07:44:08 -0700363 pb::XmlNode pb_xml_node;
364 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
365
366 std::string serialized_xml = pb_xml_node.SerializeAsString();
367 io::StringInputStream serialized_in(serialized_xml);
368
369 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
370 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 return false;
372 }
373 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700374}
375
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700376static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700377 const file::FileType file_type = file::GetFileType(input_path);
378 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
379 if (file_type == file::FileType::kDirectory) {
380 context->GetDiagnostics()->Error(DiagMessage(input_path)
381 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700382 } else if (file_type == file::FileType::kNonexistant) {
383 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700384 } else {
385 context->GetDiagnostics()->Error(DiagMessage(input_path)
386 << "not a valid resource file");
387 }
388 return false;
389 }
390 return true;
391}
392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700394 const ResourcePathData& path_data, IArchiveWriter* writer,
395 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700397 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 }
399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700402 FileInputStream fin(path_data.source.path);
403 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700405 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700407 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 return false;
414 }
415
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700416 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 xmlres->file.config = path_data.config;
418 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700419 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420
421 // Collect IDs that are defined here.
422 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return false;
425 }
426
427 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 InlineXmlFormatParser inline_xml_format_parser;
429 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 return false;
431 }
432
433 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700435 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 return false;
437 }
438
Adam Lesinski00451162017-10-03 07:44:08 -0700439 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
440 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
441
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700442 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700444 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700446 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447
Adam Lesinski00451162017-10-03 07:44:08 -0700448 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
449 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700451 }
452
Adam Lesinski00451162017-10-03 07:44:08 -0700453 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
454 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
455 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700456 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700458 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700462 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 return false;
464 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000465
466 if (options.generate_text_symbols_path) {
467 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
468
469 if (fout_text.HadError()) {
470 context->GetDiagnostics()->Error(DiagMessage()
471 << "failed writing to'"
472 << options.generate_text_symbols_path.value()
473 << "': " << fout_text.GetError());
474 return false;
475 }
476
477 Printer r_txt_printer(&fout_text);
478 for (const auto res : xmlres->file.exported_symbols) {
479 r_txt_printer.Print("default int id ");
480 r_txt_printer.Println(res.name.entry);
481 }
482
483 // And print ourselves.
484 r_txt_printer.Print("default int ");
485 r_txt_printer.Print(path_data.resource_dir);
486 r_txt_printer.Print(" ");
487 r_txt_printer.Println(path_data.name);
488 }
489
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700491}
492
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700494 const ResourcePathData& path_data, IArchiveWriter* writer,
495 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700497 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 }
499
500 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700502 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 res_file.config = path_data.config;
504 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700505 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506
507 {
508 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700509 if (!android::base::ReadFileToString(path_data.source.path, &content,
510 true /*follow_symlinks*/)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700511 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700512 << "failed to open file: "
Adam Lesinski00451162017-10-03 07:44:08 -0700513 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700515 }
516
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700518 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700519
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 // Ensure that we only keep the chunks we care about if we end up
521 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700523 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 if (!image) {
525 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700526 }
527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 std::unique_ptr<NinePatch> nine_patch;
529 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700531 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 if (!nine_patch) {
533 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700534 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 }
536
537 // Remove the 1px border around the NinePatch.
538 // Basically the row array is shifted up by 1, and the length is treated
539 // as height - 2.
540 // For each row, shift the array to the left by 1, and treat the length as
541 // width - 2.
542 image->width -= 2;
543 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700544 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 for (int32_t h = 0; h < image->height; h++) {
546 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
547 }
548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700550 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
551 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700553 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700554
555 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700556 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700557 return false;
558 }
559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 if (nine_patch != nullptr ||
561 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 // No matter what, we must use the re-encoded PNG, even if it is larger.
563 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565 } else {
566 // The re-encoded PNG is larger than the original, and there is
567 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700569 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
570 << "original PNG is smaller than crunched PNG"
571 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 }
573
Adam Lesinski06460ef2017-03-14 18:52:13 -0700574 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700576 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
577 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 }
580
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700582 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
583 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 std::stringstream legacy_stream(content);
585 BigBuffer legacy_buffer(4096);
586 Png png(context->GetDiagnostics());
587 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 return false;
589 }
590
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
592 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 << " new=" << buffer.size());
594 }
595 }
596
Adam Lesinski00451162017-10-03 07:44:08 -0700597 io::BigBufferInputStream buffer_in(&buffer);
598 if (!WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
599 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 return false;
601 }
602 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603}
604
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700606 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 const std::string& output_path) {
608 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700609 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700611
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700614 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 res_file.config = path_data.config;
616 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700617 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700618
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 std::string error_str;
620 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700622 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
623 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 return false;
625 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700626
Adam Lesinski00451162017-10-03 07:44:08 -0700627 io::MmappedData mmapped_in(std::move(f.value()));
628 if (!WriteHeaderAndDataToWriter(output_path, res_file, &mmapped_in, writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 return false;
631 }
632 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700633}
634
635class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100637 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
638 }
639
Adam Lesinskib522f042017-04-21 16:57:59 -0700640 PackageType GetPackageType() override {
641 // Every compilation unit starts as an app and then gets linked as potentially something else.
642 return PackageType::kApp;
643 }
644
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700645 void SetVerbose(bool val) {
646 verbose_ = val;
647 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800648
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700649 bool IsVerbose() override {
650 return verbose_;
651 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800652
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700653 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100654 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700655 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700656
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700658 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 return nullptr;
660 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700661
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 static std::string empty;
664 return empty;
665 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700666
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700667 uint8_t GetPackageId() override {
668 return 0x0;
669 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700670
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700672 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 return nullptr;
674 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800675
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700676 int GetMinSdkVersion() override {
677 return 0;
678 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700679
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700681 DISALLOW_COPY_AND_ASSIGN(CompileContext);
682
Chris Warrington820d72a2017-04-27 15:27:01 +0100683 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700685};
686
Adam Lesinski00451162017-10-03 07:44:08 -0700687// Entry point for compilation phase. Parses arguments and dispatches to the correct steps.
Chris Warrington820d72a2017-04-27 15:27:01 +0100688int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
689 CompileContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700691
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 bool verbose = false;
693 Flags flags =
694 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700696 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000697 .OptionalFlag("--output-text-symbols",
698 "Generates a text file containing the resource symbols in the\n"
699 "specified file",
700 &options.generate_text_symbols_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 "Generate resources for pseudo-locales "
703 "(en-XA and ar-XB)",
704 &options.pseudolocalize)
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700705 .OptionalSwitch("--no-crunch", "Disables PNG processing", &options.no_png_crunch)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700706 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
707 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
709 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 return 1;
711 }
712
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700713 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700715 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 std::vector<ResourcePathData> input_data;
718 if (options.res_dir) {
719 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700721 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700724 }
725
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 return 1;
728 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800729
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700730 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700731
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800734
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 for (const std::string& arg : flags.GetArgs()) {
737 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700738 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700741 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 return 1;
743 }
744 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800745
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700746 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 }
748
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700750 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 }
752
753 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700756 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757 }
758
Adam Lesinski776aa952017-04-24 15:09:32 -0700759 if (!IsValidFile(&context, path_data.source.path)) {
760 error = true;
761 continue;
762 }
763
Adam Lesinski00451162017-10-03 07:44:08 -0700764 // Determine how to compile the file based on its type.
765 auto compile_func = &CompileFile;
Adam Lesinski3a725dc2017-11-02 16:14:59 -0700766 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
Adam Lesinski00451162017-10-03 07:44:08 -0700767 compile_func = &CompileTable;
768 // We use a different extension (not necessary anymore, but avoids altering the existing
769 // build system logic).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700770 path_data.extension = "arsc";
Adam Lesinski00451162017-10-03 07:44:08 -0700771 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
772 if (*type != ResourceType::kRaw) {
773 if (path_data.extension == "xml") {
774 compile_func = &CompileXml;
Izabela Orlowska3c3f9b52018-01-12 14:24:09 +0000775 } else if ((!options.no_png_crunch && path_data.extension == "png") ||
776 path_data.extension == "9.png") {
Adam Lesinski00451162017-10-03 07:44:08 -0700777 compile_func = &CompilePng;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700778 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 }
Adam Lesinski00451162017-10-03 07:44:08 -0700780 } else {
781 context.GetDiagnostics()->Error(DiagMessage()
782 << "invalid file path '" << path_data.source << "'");
783 error = true;
784 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700785 }
786
Adam Lesinski00451162017-10-03 07:44:08 -0700787 // Compile the file.
788 const std::string out_path = BuildIntermediateContainerFilename(path_data);
789 error |= !compile_func(&context, options, path_data, archive_writer.get(), out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 }
Adam Lesinski00451162017-10-03 07:44:08 -0700791 return error ? 1 : 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700792}
793
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794} // namespace aapt