blob: c192d698b50028c8925b0410af1abcfe13a14110 [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");
375 } else {
376 context->GetDiagnostics()->Error(DiagMessage(input_path)
377 << "not a valid resource file");
378 }
379 return false;
380 }
381 return true;
382}
383
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700385 const ResourcePathData& path_data, IArchiveWriter* writer,
386 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700388 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700389 }
390
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700395 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700396 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700398 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700399
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700401
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 fin.close();
403 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700404
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700405 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 return false;
407 }
408
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700409 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 xmlres->file.config = path_data.config;
411 xmlres->file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412
413 // Collect IDs that are defined here.
414 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 return false;
417 }
418
419 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 InlineXmlFormatParser inline_xml_format_parser;
421 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 return false;
423 }
424
425 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700427 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 return false;
429 }
430
431 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 {
434 // Wrap our IArchiveWriter with an adaptor that implements the
435 // ZeroCopyOutputStream
436 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 CopyingOutputStreamAdaptor copying_adaptor(writer);
438 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
441 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442
443 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 output_stream.WriteLittleEndian32(1 + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700446 if (!FlattenXmlToOutStream(context, output_path, xmlres.get(), &output_stream)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700448 }
449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450 for (auto& inline_xml_doc : inline_documents) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700451 if (!FlattenXmlToOutStream(context, output_path, inline_xml_doc.get(), &output_stream)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700452 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700455 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700456
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700458 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 return false;
460 }
461 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700462}
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700465 const ResourcePathData& path_data, IArchiveWriter* writer,
466 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700468 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 }
470
471 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700473 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 res_file.config = path_data.config;
475 res_file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476
477 {
478 std::string content;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 if (!android::base::ReadFileToString(path_data.source.path, &content)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700480 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
481 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700483 }
484
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700486 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700487
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 // Ensure that we only keep the chunks we care about if we end up
489 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 PngChunkFilter png_chunk_filter(content);
491 std::unique_ptr<Image> image = ReadPng(context, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 if (!image) {
493 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700494 }
495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 std::unique_ptr<NinePatch> nine_patch;
497 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700499 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700500 if (!nine_patch) {
501 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 }
504
505 // Remove the 1px border around the NinePatch.
506 // Basically the row array is shifted up by 1, and the length is treated
507 // as height - 2.
508 // For each row, shift the array to the left by 1, and treat the length as
509 // width - 2.
510 image->width -= 2;
511 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700512 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 for (int32_t h = 0; h < image->height; h++) {
514 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
515 }
516
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700517 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700518 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
519 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522
523 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700524 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 return false;
526 }
527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 if (nine_patch != nullptr ||
529 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 // No matter what, we must use the re-encoded PNG, even if it is larger.
531 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533 } else {
534 // The re-encoded PNG is larger than the original, and there is
535 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700537 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
538 << "original PNG is smaller than crunched PNG"
539 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 }
541
Adam Lesinski06460ef2017-03-14 18:52:13 -0700542 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700544 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
545 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700546 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 }
548
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700550 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
551 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 std::stringstream legacy_stream(content);
553 BigBuffer legacy_buffer(4096);
554 Png png(context->GetDiagnostics());
555 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 return false;
557 }
558
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700559 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
560 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700561 << " new=" << buffer.size());
562 }
563 }
564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 if (!WriteHeaderAndBufferToWriter(output_path, res_file, buffer, writer,
566 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 return false;
568 }
569 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700570}
571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700573 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 const std::string& output_path) {
575 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700576 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700578
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700581 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700582 res_file.config = path_data.config;
583 res_file.source = path_data.source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 std::string error_str;
586 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700588 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
589 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 return false;
591 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700592
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 if (!WriteHeaderAndMmapToWriter(output_path, res_file, f.value(), writer,
594 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 return false;
596 }
597 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700598}
599
600class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100602 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
603 }
604
Adam Lesinskib522f042017-04-21 16:57:59 -0700605 PackageType GetPackageType() override {
606 // Every compilation unit starts as an app and then gets linked as potentially something else.
607 return PackageType::kApp;
608 }
609
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700610 void SetVerbose(bool val) {
611 verbose_ = val;
612 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800613
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700614 bool IsVerbose() override {
615 return verbose_;
616 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800617
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700618 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100619 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700620 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 abort();
624 return nullptr;
625 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700626
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700627 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700628 static std::string empty;
629 return empty;
630 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700631
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700632 uint8_t GetPackageId() override {
633 return 0x0;
634 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 abort();
638 return nullptr;
639 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800640
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700641 int GetMinSdkVersion() override {
642 return 0;
643 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700644
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 private:
Chris Warrington820d72a2017-04-27 15:27:01 +0100646 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700648};
649
650/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 * Entry point for compilation phase. Parses arguments and dispatches to the
652 * correct steps.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700653 */
Chris Warrington820d72a2017-04-27 15:27:01 +0100654int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
655 CompileContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700657
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 bool verbose = false;
659 Flags flags =
660 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700662 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 "Generate resources for pseudo-locales "
665 "(en-XA and ar-XB)",
666 &options.pseudolocalize)
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700667 .OptionalSwitch("--no-crunch", "Disables PNG processing", &options.no_png_crunch)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700668 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
669 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
671 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700672 return 1;
673 }
674
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700675 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 std::vector<ResourcePathData> input_data;
680 if (options.res_dir) {
681 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700683 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700686 }
687
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700689 return 1;
690 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800691
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700692 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700693
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800696
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700698 for (const std::string& arg : flags.GetArgs()) {
699 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700700 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700702 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700703 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700704 return 1;
705 }
706 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800707
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700708 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 }
710
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700711 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700712 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 }
714
715 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700718 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 }
720
Adam Lesinski776aa952017-04-24 15:09:32 -0700721 if (!IsValidFile(&context, path_data.source.path)) {
722 error = true;
723 continue;
724 }
725
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 if (path_data.resource_dir == "values") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727 // Overwrite the extension.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 path_data.extension = "arsc";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700731 if (!CompileTable(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 error = true;
733 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800734
735 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700737 if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 if (*type != ResourceType::kRaw) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 if (path_data.extension == "xml") {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700740 if (!CompileXml(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 error = true;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800742 }
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700743 } else if (!options.no_png_crunch &&
744 (path_data.extension == "png" || path_data.extension == "9.png")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700745 if (!CompilePng(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700747 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700749 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 error = true;
751 }
752 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700753 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700754 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
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 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700759 context.GetDiagnostics()->Error(DiagMessage() << "invalid file path '" << path_data.source
760 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 error = true;
762 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700763 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700764 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700765
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 if (error) {
767 return 1;
768 }
769 return 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700770}
771
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772} // namespace aapt