blob: e09a397965926cb940b78c80a1e5ae2684de4679 [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 Lesinski28e6c0b2017-05-10 14:56:36 -0700117 bool no_png_crunch = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 bool legacy_mode = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 bool verbose = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120};
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122static std::string BuildIntermediateFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 name << data.resource_dir;
125 if (!data.config_str.empty()) {
126 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 }
128 name << "_" << data.name;
129 if (!data.extension.empty()) {
130 name << "." << data.extension;
131 }
132 name << ".flat";
133 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800134}
135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136static bool IsHidden(const StringPiece& filename) {
137 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800138}
139
140/**
141 * Walks the res directory structure, looking for resource files.
142 */
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700143static bool LoadInputFilesFromDir(IAaptContext* context, const CompileOptions& options,
144 std::vector<ResourcePathData>* out_path_data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 const std::string& root_dir = options.res_dir.value();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700146 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 if (!d) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700148 context->GetDiagnostics()->Error(DiagMessage()
149 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 return false;
151 }
152
153 while (struct dirent* entry = readdir(d.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (IsHidden(entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 continue;
156 }
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 std::string prefix_path = root_dir;
159 file::AppendPath(&prefix_path, entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 if (file::GetFileType(prefix_path) != file::FileType::kDirectory) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 continue;
163 }
164
Adam Lesinski06460ef2017-03-14 18:52:13 -0700165 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 if (!subdir) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700167 context->GetDiagnostics()->Error(DiagMessage()
168 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 return false;
170 }
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 while (struct dirent* leaf_entry = readdir(subdir.get())) {
173 if (IsHidden(leaf_entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 continue;
175 }
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 std::string full_path = prefix_path;
178 file::AppendPath(&full_path, leaf_entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 std::string err_str;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700181 Maybe<ResourcePathData> path_data = ExtractResourcePathData(full_path, &err_str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 if (!path_data) {
183 context->GetDiagnostics()->Error(DiagMessage() << err_str);
Adam Lesinskia40e9722015-11-24 19:11:46 -0800184 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 }
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 out_path_data->push_back(std::move(path_data.value()));
Adam Lesinskia40e9722015-11-24 19:11:46 -0800188 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 }
190 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700191}
192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700194 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 const std::string& output_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 ResourceTable table;
197 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700198 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700201 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700203 }
204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 // Parse the values file from XML.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 xml::XmlPullParser xml_parser(fin);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 ResourceParserOptions parser_options;
209 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210
211 // If the filename includes donottranslate, then the default translatable is
212 // false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700213 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700215 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 parser_options);
217 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800219 }
220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 fin.close();
222 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 if (options.pseudolocalize) {
225 // Generate pseudo-localized strings (en-XA and ar-XB).
226 // These are created as weak symbols, and are only generated from default
227 // configuration
228 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 PseudolocaleGenerator pseudolocale_generator;
230 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700232 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237
238 // Assign an ID to any package that has resources.
239 for (auto& pkg : table.packages) {
240 if (!pkg->id) {
241 // If no package ID was set while parsing (public identifiers), auto
242 // assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700249 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 return false;
251 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 {
256 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700257 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(&table);
261 if (!pb_table->SerializeToZeroCopyStream(&copying_adaptor)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700262 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700268 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 return false;
270 }
271 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800272}
273
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700274static bool WriteHeaderAndBufferToWriter(const StringPiece& output_path, const ResourceFile& file,
275 const BigBuffer& buffer, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800276 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 if (!writer->StartEntry(output_path, 0)) {
279 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return false;
281 }
282
283 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 {
286 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700287 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 CopyingOutputStreamAdaptor copying_adaptor(writer);
289 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290
291 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293
Adam Lesinski06460ef2017-03-14 18:52:13 -0700294 std::unique_ptr<pb::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700295 output_stream.WriteCompiledFile(compiled_file.get());
296 output_stream.WriteData(&buffer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 if (output_stream.HadError()) {
299 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800301 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800303
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 if (!writer->FinishEntry()) {
305 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return false;
307 }
308 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800309}
310
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700311static bool WriteHeaderAndMmapToWriter(const StringPiece& output_path, const ResourceFile& file,
312 const android::FileMap& map, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800313 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 if (!writer->StartEntry(output_path, 0)) {
316 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 return false;
318 }
319
320 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 {
323 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 // ZeroCopyOutputStream interface.
325 CopyingOutputStreamAdaptor copying_adaptor(writer);
326 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327
328 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700331 std::unique_ptr<pb::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 output_stream.WriteCompiledFile(compiled_file.get());
333 output_stream.WriteData(map.getDataPtr(), map.getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 if (output_stream.HadError()) {
336 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800338 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800340
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 if (!writer->FinishEntry()) {
342 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700343 return false;
344 }
345 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700346}
347
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700348static bool FlattenXmlToOutStream(IAaptContext* context, const StringPiece& output_path,
349 xml::XmlResource* xmlres, CompiledFileOutputStream* out) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 BigBuffer buffer(1024);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 XmlFlattenerOptions xml_flattener_options;
352 xml_flattener_options.keep_raw_values = true;
353 XmlFlattener flattener(&buffer, xml_flattener_options);
354 if (!flattener.Consume(context, xmlres)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 return false;
356 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700357
Adam Lesinski06460ef2017-03-14 18:52:13 -0700358 std::unique_ptr<pb::CompiledFile> pb_compiled_file = SerializeCompiledFileToPb(xmlres->file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 out->WriteCompiledFile(pb_compiled_file.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 out->WriteData(&buffer);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700361
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 if (out->HadError()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700363 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
365 }
366 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700367}
368
Adam Lesinski776aa952017-04-24 15:09:32 -0700369static bool IsValidFile(IAaptContext* context, const StringPiece& input_path) {
370 const file::FileType file_type = file::GetFileType(input_path);
371 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
372 if (file_type == file::FileType::kDirectory) {
373 context->GetDiagnostics()->Error(DiagMessage(input_path)
374 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700375 } else if (file_type == file::FileType::kNonexistant) {
376 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700377 } else {
378 context->GetDiagnostics()->Error(DiagMessage(input_path)
379 << "not a valid resource file");
380 }
381 return false;
382 }
383 return true;
384}
385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700387 const ResourcePathData& path_data, IArchiveWriter* writer,
388 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700390 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 }
392
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700398 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700400 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700401
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700403
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404 fin.close();
405 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 return false;
409 }
410
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700411 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 xmlres->file.config = path_data.config;
413 xmlres->file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414
415 // Collect IDs that are defined here.
416 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 return false;
419 }
420
421 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 InlineXmlFormatParser inline_xml_format_parser;
423 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return false;
425 }
426
427 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700429 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 return false;
431 }
432
433 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 {
436 // Wrap our IArchiveWriter with an adaptor that implements the
437 // ZeroCopyOutputStream
438 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 CopyingOutputStreamAdaptor copying_adaptor(writer);
440 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
443 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444
445 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 output_stream.WriteLittleEndian32(1 + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700448 if (!FlattenXmlToOutStream(context, output_path, xmlres.get(), &output_stream)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700450 }
451
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 for (auto& inline_xml_doc : inline_documents) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700453 if (!FlattenXmlToOutStream(context, output_path, inline_xml_doc.get(), &output_stream)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700456 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700458
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700460 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 return false;
462 }
463 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700464}
465
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700466static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700467 const ResourcePathData& path_data, IArchiveWriter* writer,
468 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700469 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700470 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 }
472
473 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700475 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 res_file.config = path_data.config;
477 res_file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700478
479 {
480 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700481 if (!android::base::ReadFileToString(path_data.source.path, &content,
482 true /*follow_symlinks*/)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700483 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
484 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700486 }
487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700489 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700490
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 // Ensure that we only keep the chunks we care about if we end up
492 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700494 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 if (!image) {
496 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700497 }
498
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 std::unique_ptr<NinePatch> nine_patch;
500 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700502 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700503 if (!nine_patch) {
504 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700505 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700506 }
507
508 // Remove the 1px border around the NinePatch.
509 // Basically the row array is shifted up by 1, and the length is treated
510 // as height - 2.
511 // For each row, shift the array to the left by 1, and treat the length as
512 // width - 2.
513 image->width -= 2;
514 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700515 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 for (int32_t h = 0; h < image->height; h++) {
517 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
518 }
519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700521 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
522 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700524 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525
526 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700527 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 return false;
529 }
530
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 if (nine_patch != nullptr ||
532 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533 // No matter what, we must use the re-encoded PNG, even if it is larger.
534 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 } else {
537 // The re-encoded PNG is larger than the original, and there is
538 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700540 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
541 << "original PNG is smaller than crunched PNG"
542 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700543 }
544
Adam Lesinski06460ef2017-03-14 18:52:13 -0700545 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700547 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
548 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 }
551
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700553 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
554 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 std::stringstream legacy_stream(content);
556 BigBuffer legacy_buffer(4096);
557 Png png(context->GetDiagnostics());
558 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 return false;
560 }
561
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
563 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 << " new=" << buffer.size());
565 }
566 }
567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 if (!WriteHeaderAndBufferToWriter(output_path, res_file, buffer, writer,
569 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 return false;
571 }
572 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700573}
574
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700576 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 const std::string& output_path) {
578 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700579 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700581
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700583 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700584 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 res_file.config = path_data.config;
586 res_file.source = path_data.source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700587
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 std::string error_str;
589 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700591 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
592 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 return false;
594 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700595
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 if (!WriteHeaderAndMmapToWriter(output_path, res_file, f.value(), writer,
597 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 return false;
599 }
600 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700601}
602
603class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100605 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
606 }
607
Adam Lesinskib522f042017-04-21 16:57:59 -0700608 PackageType GetPackageType() override {
609 // Every compilation unit starts as an app and then gets linked as potentially something else.
610 return PackageType::kApp;
611 }
612
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700613 void SetVerbose(bool val) {
614 verbose_ = val;
615 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800616
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700617 bool IsVerbose() override {
618 return verbose_;
619 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800620
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700621 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100622 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700623 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700624
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 abort();
627 return nullptr;
628 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700629
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 static std::string empty;
632 return empty;
633 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700634
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700635 uint8_t GetPackageId() override {
636 return 0x0;
637 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 abort();
641 return nullptr;
642 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800643
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700644 int GetMinSdkVersion() override {
645 return 0;
646 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700647
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700648 private:
Chris Warrington820d72a2017-04-27 15:27:01 +0100649 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651};
652
653/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 * Entry point for compilation phase. Parses arguments and dispatches to the
655 * correct steps.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700656 */
Chris Warrington820d72a2017-04-27 15:27:01 +0100657int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
658 CompileContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700659 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700660
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 bool verbose = false;
662 Flags flags =
663 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700665 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 "Generate resources for pseudo-locales "
668 "(en-XA and ar-XB)",
669 &options.pseudolocalize)
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700670 .OptionalSwitch("--no-crunch", "Disables PNG processing", &options.no_png_crunch)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700671 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
672 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
674 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 return 1;
676 }
677
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700680 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 std::vector<ResourcePathData> input_data;
683 if (options.res_dir) {
684 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700686 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700687 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700689 }
690
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700691 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700692 return 1;
693 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800694
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700695 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700696
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800699
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 for (const std::string& arg : flags.GetArgs()) {
702 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700703 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700706 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 return 1;
708 }
709 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800710
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700711 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700712 }
713
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700714 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700715 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700716 }
717
718 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700720 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700721 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700722 }
723
Adam Lesinski776aa952017-04-24 15:09:32 -0700724 if (!IsValidFile(&context, path_data.source.path)) {
725 error = true;
726 continue;
727 }
728
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700729 if (path_data.resource_dir == "values") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 // Overwrite the extension.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 path_data.extension = "arsc";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700734 if (!CompileTable(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 error = true;
736 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800737
738 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700740 if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 if (*type != ResourceType::kRaw) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 if (path_data.extension == "xml") {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700743 if (!CompileXml(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 error = true;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800745 }
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700746 } else if (!options.no_png_crunch &&
747 (path_data.extension == "png" || path_data.extension == "9.png")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700748 if (!CompilePng(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700750 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700752 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 error = true;
754 }
755 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700756 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700757 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 error = true;
759 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700760 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700762 context.GetDiagnostics()->Error(DiagMessage() << "invalid file path '" << path_data.source
763 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 error = true;
765 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700766 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700768
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 if (error) {
770 return 1;
771 }
772 return 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700773}
774
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775} // namespace aapt