blob: 578a8fb423dc8d80dd1d3acbe8d8d8b491045e1c [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
19#include <fstream>
20#include <string>
21
Adam Lesinskid5083f62017-01-16 15:07:21 -080022#include "android-base/errors.h"
23#include "android-base/file.h"
24#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 Lesinskia40e9722015-11-24 19:11:46 -080038#include "flatten/Archive.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039#include "flatten/XmlFlattener.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070040#include "io/BigBufferOutputStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070041#include "io/Util.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080042#include "proto/ProtoSerialize.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043#include "util/Files.h"
44#include "util/Maybe.h"
45#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080046#include "xml/XmlDom.h"
47#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinskid5083f62017-01-16 15:07:21 -080049using android::StringPiece;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070050using google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070051
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052namespace aapt {
53
54struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 std::string name;
58 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 // Original config str. We keep this because when we parse the config, we may
61 // add on
62 // version qualifiers. We want to preserve the original input so the output is
63 // easily
64 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067};
68
69/**
70 * Resource file paths are expected to look like:
71 * [--/res/]type[-config]/name
72 */
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;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 bool pseudolocalize = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 bool legacy_mode = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 bool verbose = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119};
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121static std::string BuildIntermediateFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 name << data.resource_dir;
124 if (!data.config_str.empty()) {
125 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
127 name << "_" << data.name;
128 if (!data.extension.empty()) {
129 name << "." << data.extension;
130 }
131 name << ".flat";
132 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800133}
134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135static bool IsHidden(const StringPiece& filename) {
136 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800137}
138
139/**
140 * Walks the res directory structure, looking for resource files.
141 */
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 Lesinski06460ef2017-03-14 18:52:13 -0700147 context->GetDiagnostics()->Error(DiagMessage()
148 << android::base::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 Lesinski06460ef2017-03-14 18:52:13 -0700166 context->GetDiagnostics()->Error(DiagMessage()
167 << android::base::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) {
182 context->GetDiagnostics()->Error(DiagMessage() << 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 }
189 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190}
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700193 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 const std::string& output_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 ResourceTable table;
196 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700200 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700202 }
203
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 // Parse the values file from XML.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 xml::XmlPullParser xml_parser(fin);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 ResourceParserOptions parser_options;
208 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209
210 // If the filename includes donottranslate, then the default translatable is
211 // false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700212 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700214 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 parser_options);
216 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800218 }
219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 fin.close();
221 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 if (options.pseudolocalize) {
224 // Generate pseudo-localized strings (en-XA and ar-XB).
225 // These are created as weak symbols, and are only generated from default
226 // configuration
227 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 PseudolocaleGenerator pseudolocale_generator;
229 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700231 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236
237 // Assign an ID to any package that has resources.
238 for (auto& pkg : table.packages) {
239 if (!pkg->id) {
240 // If no package ID was set while parsing (public identifiers), auto
241 // assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700248 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 return false;
250 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 {
255 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700256 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(&table);
260 if (!pb_table->SerializeToZeroCopyStream(&copying_adaptor)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700261 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700267 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 return false;
269 }
270 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800271}
272
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700273static bool WriteHeaderAndBufferToWriter(const StringPiece& output_path, const ResourceFile& file,
274 const BigBuffer& buffer, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800275 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 if (!writer->StartEntry(output_path, 0)) {
278 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 return false;
280 }
281
282 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 {
285 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700286 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 CopyingOutputStreamAdaptor copying_adaptor(writer);
288 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289
290 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292
Adam Lesinski06460ef2017-03-14 18:52:13 -0700293 std::unique_ptr<pb::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 output_stream.WriteCompiledFile(compiled_file.get());
295 output_stream.WriteData(&buffer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 if (output_stream.HadError()) {
298 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800300 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800302
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 if (!writer->FinishEntry()) {
304 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 return false;
306 }
307 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800308}
309
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700310static bool WriteHeaderAndMmapToWriter(const StringPiece& output_path, const ResourceFile& file,
311 const android::FileMap& map, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800312 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (!writer->StartEntry(output_path, 0)) {
315 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return false;
317 }
318
319 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 {
322 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 // ZeroCopyOutputStream interface.
324 CopyingOutputStreamAdaptor copying_adaptor(writer);
325 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326
327 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700330 std::unique_ptr<pb::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700331 output_stream.WriteCompiledFile(compiled_file.get());
332 output_stream.WriteData(map.getDataPtr(), map.getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 if (output_stream.HadError()) {
335 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800337 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 if (!writer->FinishEntry()) {
341 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342 return false;
343 }
344 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700345}
346
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700347static bool FlattenXmlToOutStream(IAaptContext* context, const StringPiece& output_path,
348 xml::XmlResource* xmlres, CompiledFileOutputStream* out) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 BigBuffer buffer(1024);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 XmlFlattenerOptions xml_flattener_options;
351 xml_flattener_options.keep_raw_values = true;
352 XmlFlattener flattener(&buffer, xml_flattener_options);
353 if (!flattener.Consume(context, xmlres)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 return false;
355 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700356
Adam Lesinski06460ef2017-03-14 18:52:13 -0700357 std::unique_ptr<pb::CompiledFile> pb_compiled_file = SerializeCompiledFileToPb(xmlres->file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 out->WriteCompiledFile(pb_compiled_file.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 out->WriteData(&buffer);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700360
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 if (out->HadError()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700362 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 return false;
364 }
365 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700366}
367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700369 const ResourcePathData& path_data, IArchiveWriter* writer,
370 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700372 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 }
374
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700380 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700382 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700383
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700385
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 fin.close();
387 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700388
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 return false;
391 }
392
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700393 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 xmlres->file.config = path_data.config;
395 xmlres->file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396
397 // Collect IDs that are defined here.
398 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 return false;
401 }
402
403 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 InlineXmlFormatParser inline_xml_format_parser;
405 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 return false;
407 }
408
409 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700411 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 return false;
413 }
414
415 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 {
418 // Wrap our IArchiveWriter with an adaptor that implements the
419 // ZeroCopyOutputStream
420 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 CopyingOutputStreamAdaptor copying_adaptor(writer);
422 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
425 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426
427 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 output_stream.WriteLittleEndian32(1 + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700430 if (!FlattenXmlToOutStream(context, output_path, xmlres.get(), &output_stream)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700432 }
433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 for (auto& inline_xml_doc : inline_documents) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700435 if (!FlattenXmlToOutStream(context, output_path, inline_xml_doc.get(), &output_stream)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700436 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700438 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700442 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 return false;
444 }
445 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700446}
447
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700449 const ResourcePathData& path_data, IArchiveWriter* writer,
450 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700452 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 }
454
455 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700457 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700458 res_file.config = path_data.config;
459 res_file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460
461 {
462 std::string content;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 if (!android::base::ReadFileToString(path_data.source.path, &content)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700464 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
465 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700467 }
468
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700470 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700471
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 // Ensure that we only keep the chunks we care about if we end up
473 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 PngChunkFilter png_chunk_filter(content);
475 std::unique_ptr<Image> image = ReadPng(context, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 if (!image) {
477 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700478 }
479
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 std::unique_ptr<NinePatch> nine_patch;
481 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700483 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 if (!nine_patch) {
485 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 }
488
489 // Remove the 1px border around the NinePatch.
490 // Basically the row array is shifted up by 1, and the length is treated
491 // as height - 2.
492 // For each row, shift the array to the left by 1, and treat the length as
493 // width - 2.
494 image->width -= 2;
495 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700496 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 for (int32_t h = 0; h < image->height; h++) {
498 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
499 }
500
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700501 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700502 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
503 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700505 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506
507 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700508 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 return false;
510 }
511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 if (nine_patch != nullptr ||
513 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 // No matter what, we must use the re-encoded PNG, even if it is larger.
515 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 } else {
518 // The re-encoded PNG is larger than the original, and there is
519 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700521 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
522 << "original PNG is smaller than crunched PNG"
523 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 }
525
Adam Lesinski06460ef2017-03-14 18:52:13 -0700526 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700528 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
529 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 }
532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700534 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
535 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 std::stringstream legacy_stream(content);
537 BigBuffer legacy_buffer(4096);
538 Png png(context->GetDiagnostics());
539 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 return false;
541 }
542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
544 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 << " new=" << buffer.size());
546 }
547 }
548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 if (!WriteHeaderAndBufferToWriter(output_path, res_file, buffer, writer,
550 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 return false;
552 }
553 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700554}
555
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700557 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 const std::string& output_path) {
559 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700560 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700562
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700565 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 res_file.config = path_data.config;
567 res_file.source = path_data.source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700568
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 std::string error_str;
570 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571 if (!f) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700572 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 return false;
574 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700575
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 if (!WriteHeaderAndMmapToWriter(output_path, res_file, f.value(), writer,
577 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 return false;
579 }
580 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700581}
582
583class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700584 public:
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700585 void SetVerbose(bool val) {
586 verbose_ = val;
587 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800588
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700589 bool IsVerbose() override {
590 return verbose_;
591 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800592
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700593 IDiagnostics* GetDiagnostics() override {
594 return &diagnostics_;
595 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 abort();
599 return nullptr;
600 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700601
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 static std::string empty;
604 return empty;
605 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700607 uint8_t GetPackageId() override {
608 return 0x0;
609 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 abort();
613 return nullptr;
614 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800615
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700616 int GetMinSdkVersion() override {
617 return 0;
618 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700619
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 StdErrDiagnostics diagnostics_;
622 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623};
624
625/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 * Entry point for compilation phase. Parses arguments and dispatches to the
627 * correct steps.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700628 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629int Compile(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 CompileContext context;
631 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700632
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 bool verbose = false;
634 Flags flags =
635 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700637 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 "Generate resources for pseudo-locales "
640 "(en-XA and ar-XB)",
641 &options.pseudolocalize)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700642 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
643 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
645 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 return 1;
647 }
648
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700649 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700653 std::vector<ResourcePathData> input_data;
654 if (options.res_dir) {
655 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700657 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700660 }
661
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700662 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 return 1;
664 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800665
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700666 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700667
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800670
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 for (const std::string& arg : flags.GetArgs()) {
673 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700674 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700677 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 return 1;
679 }
680 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800681
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700682 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 }
684
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700686 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700687 }
688
689 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700692 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700693 }
694
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 if (path_data.resource_dir == "values") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700696 // Overwrite the extension.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700697 path_data.extension = "arsc";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700698
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700699 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700700 if (!CompileTable(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 error = true;
702 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800703
704 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700706 if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 if (*type != ResourceType::kRaw) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 if (path_data.extension == "xml") {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700709 if (!CompileXml(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 error = true;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800711 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700712 } else if (path_data.extension == "png" || path_data.extension == "9.png") {
713 if (!CompilePng(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700715 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700717 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700718 error = true;
719 }
720 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700721 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700722 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 error = true;
724 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700725 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700727 context.GetDiagnostics()->Error(DiagMessage() << "invalid file path '" << path_data.source
728 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729 error = true;
730 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700731 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700733
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 if (error) {
735 return 1;
736 }
737 return 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700738}
739
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740} // namespace aapt