blob: b93c6ecea274a6a2057a83812231ba2528abc046 [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 Lesinski776aa952017-04-24 15:09:32 -0700368static bool IsValidFile(IAaptContext* context, const StringPiece& input_path) {
369 const file::FileType file_type = file::GetFileType(input_path);
370 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
371 if (file_type == file::FileType::kDirectory) {
372 context->GetDiagnostics()->Error(DiagMessage(input_path)
373 << "resource file cannot be a directory");
374 } else {
375 context->GetDiagnostics()->Error(DiagMessage(input_path)
376 << "not a valid resource file");
377 }
378 return false;
379 }
380 return true;
381}
382
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700384 const ResourcePathData& path_data, IArchiveWriter* writer,
385 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700387 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 }
389
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700395 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700397 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700400
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 fin.close();
402 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 return false;
406 }
407
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700408 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 xmlres->file.config = path_data.config;
410 xmlres->file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411
412 // Collect IDs that are defined here.
413 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 return false;
416 }
417
418 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 InlineXmlFormatParser inline_xml_format_parser;
420 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 return false;
422 }
423
424 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700425 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700426 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 return false;
428 }
429
430 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700431 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 {
433 // Wrap our IArchiveWriter with an adaptor that implements the
434 // ZeroCopyOutputStream
435 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 CopyingOutputStreamAdaptor copying_adaptor(writer);
437 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
440 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441
442 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 output_stream.WriteLittleEndian32(1 + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700445 if (!FlattenXmlToOutStream(context, output_path, xmlres.get(), &output_stream)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700447 }
448
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 for (auto& inline_xml_doc : inline_documents) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700450 if (!FlattenXmlToOutStream(context, output_path, inline_xml_doc.get(), &output_stream)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700451 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700452 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700453 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700457 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 return false;
459 }
460 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700461}
462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700464 const ResourcePathData& path_data, IArchiveWriter* writer,
465 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700467 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 }
469
470 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700472 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700473 res_file.config = path_data.config;
474 res_file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475
476 {
477 std::string content;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if (!android::base::ReadFileToString(path_data.source.path, &content)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700479 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
480 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700482 }
483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700485 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 // Ensure that we only keep the chunks we care about if we end up
488 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 PngChunkFilter png_chunk_filter(content);
490 std::unique_ptr<Image> image = ReadPng(context, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 if (!image) {
492 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700493 }
494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495 std::unique_ptr<NinePatch> nine_patch;
496 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700498 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 if (!nine_patch) {
500 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700501 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 }
503
504 // Remove the 1px border around the NinePatch.
505 // Basically the row array is shifted up by 1, and the length is treated
506 // as height - 2.
507 // For each row, shift the array to the left by 1, and treat the length as
508 // width - 2.
509 image->width -= 2;
510 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700511 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 for (int32_t h = 0; h < image->height; h++) {
513 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
514 }
515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700517 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
518 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700520 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521
522 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700523 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 return false;
525 }
526
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 if (nine_patch != nullptr ||
528 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 // No matter what, we must use the re-encoded PNG, even if it is larger.
530 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 } else {
533 // The re-encoded PNG is larger than the original, and there is
534 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700536 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
537 << "original PNG is smaller than crunched PNG"
538 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 }
540
Adam Lesinski06460ef2017-03-14 18:52:13 -0700541 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700543 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
544 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 }
547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700549 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
550 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 std::stringstream legacy_stream(content);
552 BigBuffer legacy_buffer(4096);
553 Png png(context->GetDiagnostics());
554 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 return false;
556 }
557
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
559 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 << " new=" << buffer.size());
561 }
562 }
563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 if (!WriteHeaderAndBufferToWriter(output_path, res_file, buffer, writer,
565 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 return false;
567 }
568 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700569}
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700572 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 const std::string& output_path) {
574 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700575 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700577
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700580 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 res_file.config = path_data.config;
582 res_file.source = path_data.source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700583
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 std::string error_str;
585 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700587 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
588 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 return false;
590 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700591
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 if (!WriteHeaderAndMmapToWriter(output_path, res_file, f.value(), writer,
593 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 return false;
595 }
596 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700597}
598
599class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 public:
Adam Lesinskib522f042017-04-21 16:57:59 -0700601 PackageType GetPackageType() override {
602 // Every compilation unit starts as an app and then gets linked as potentially something else.
603 return PackageType::kApp;
604 }
605
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700606 void SetVerbose(bool val) {
607 verbose_ = val;
608 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800609
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700610 bool IsVerbose() override {
611 return verbose_;
612 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800613
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700614 IDiagnostics* GetDiagnostics() override {
615 return &diagnostics_;
616 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700617
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 abort();
620 return nullptr;
621 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700622
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700624 static std::string empty;
625 return empty;
626 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700627
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700628 uint8_t GetPackageId() override {
629 return 0x0;
630 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700631
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 abort();
634 return nullptr;
635 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800636
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700637 int GetMinSdkVersion() override {
638 return 0;
639 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700640
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 StdErrDiagnostics diagnostics_;
643 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644};
645
646/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 * Entry point for compilation phase. Parses arguments and dispatches to the
648 * correct steps.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700649 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650int Compile(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 CompileContext context;
652 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700653
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 bool verbose = false;
655 Flags flags =
656 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700658 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700660 "Generate resources for pseudo-locales "
661 "(en-XA and ar-XB)",
662 &options.pseudolocalize)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700663 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
664 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
666 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 return 1;
668 }
669
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 std::vector<ResourcePathData> input_data;
675 if (options.res_dir) {
676 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700677 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700678 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700681 }
682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 return 1;
685 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800686
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700687 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700688
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800691
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 for (const std::string& arg : flags.GetArgs()) {
694 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700695 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700698 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 return 1;
700 }
701 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800702
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700703 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 }
705
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700707 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 }
709
710 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700713 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700714 }
715
Adam Lesinski776aa952017-04-24 15:09:32 -0700716 if (!IsValidFile(&context, path_data.source.path)) {
717 error = true;
718 continue;
719 }
720
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700721 if (path_data.resource_dir == "values") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 // Overwrite the extension.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 path_data.extension = "arsc";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700726 if (!CompileTable(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 error = true;
728 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800729
730 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700732 if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 if (*type != ResourceType::kRaw) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700734 if (path_data.extension == "xml") {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700735 if (!CompileXml(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700736 error = true;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800737 }
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700738 } else if (path_data.extension == "png" || path_data.extension == "9.png") {
739 if (!CompilePng(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700741 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700743 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 error = true;
745 }
746 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700747 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700748 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 error = true;
750 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700751 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700753 context.GetDiagnostics()->Error(DiagMessage() << "invalid file path '" << path_data.source
754 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 error = true;
756 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700757 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700759
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 if (error) {
761 return 1;
762 }
763 return 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700764}
765
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766} // namespace aapt