blob: b64cd8c432d4111e7b4ae7a84020f495fd486882 [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 Lesinski60d9c2f2017-05-17 16:07:45 -0700148 context->GetDiagnostics()->Error(DiagMessage(root_dir) << "failed to open directory: "
Adam Lesinski06460ef2017-03-14 18:52:13 -0700149 << 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 Lesinski60d9c2f2017-05-17 16:07:45 -0700167 context->GetDiagnostics()->Error(DiagMessage(prefix_path) << "failed to open directory: "
Adam Lesinski06460ef2017-03-14 18:52:13 -0700168 << 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) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700183 context->GetDiagnostics()->Error(DiagMessage(full_path) << 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 Lesinski60d9c2f2017-05-17 16:07:45 -0700201 << "failed to open file: "
Adam Lesinski06460ef2017-03-14 18:52:13 -0700202 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204 }
205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 // Parse the values file from XML.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 xml::XmlPullParser xml_parser(fin);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 ResourceParserOptions parser_options;
210 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211
212 // If the filename includes donottranslate, then the default translatable is
213 // false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700214 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700216 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 parser_options);
218 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800220 }
221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 fin.close();
223 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800224
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 if (options.pseudolocalize) {
226 // Generate pseudo-localized strings (en-XA and ar-XB).
227 // These are created as weak symbols, and are only generated from default
228 // configuration
229 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 PseudolocaleGenerator pseudolocale_generator;
231 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700233 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700235
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700237 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238
239 // Assign an ID to any package that has resources.
240 for (auto& pkg : table.packages) {
241 if (!pkg->id) {
242 // If no package ID was set while parsing (public identifiers), auto
243 // assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700250 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 return false;
252 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800253
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 {
257 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700258 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(&table);
262 if (!pb_table->SerializeToZeroCopyStream(&copying_adaptor)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700263 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800267
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700269 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 return false;
271 }
272 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800273}
274
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700275static bool WriteHeaderAndBufferToWriter(const StringPiece& output_path, const ResourceFile& file,
276 const BigBuffer& buffer, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800277 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 if (!writer->StartEntry(output_path, 0)) {
280 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 return false;
282 }
283
284 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 {
287 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700288 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 CopyingOutputStreamAdaptor copying_adaptor(writer);
290 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291
292 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294
Adam Lesinski06460ef2017-03-14 18:52:13 -0700295 std::unique_ptr<pb::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 output_stream.WriteCompiledFile(compiled_file.get());
297 output_stream.WriteData(&buffer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 if (output_stream.HadError()) {
300 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800302 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 if (!writer->FinishEntry()) {
306 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 return false;
308 }
309 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800310}
311
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700312static bool WriteHeaderAndMmapToWriter(const StringPiece& output_path, const ResourceFile& file,
313 const android::FileMap& map, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800314 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 if (!writer->StartEntry(output_path, 0)) {
317 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 return false;
319 }
320
321 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 {
324 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 // ZeroCopyOutputStream interface.
326 CopyingOutputStreamAdaptor copying_adaptor(writer);
327 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328
329 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700332 std::unique_ptr<pb::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 output_stream.WriteCompiledFile(compiled_file.get());
334 output_stream.WriteData(map.getDataPtr(), map.getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 if (output_stream.HadError()) {
337 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800339 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 if (!writer->FinishEntry()) {
343 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return false;
345 }
346 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700347}
348
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700349static bool FlattenXmlToOutStream(IAaptContext* context, const StringPiece& output_path,
350 xml::XmlResource* xmlres, CompiledFileOutputStream* out) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351 BigBuffer buffer(1024);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 XmlFlattenerOptions xml_flattener_options;
353 xml_flattener_options.keep_raw_values = true;
354 XmlFlattener flattener(&buffer, xml_flattener_options);
355 if (!flattener.Consume(context, xmlres)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 return false;
357 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700358
Adam Lesinski06460ef2017-03-14 18:52:13 -0700359 std::unique_ptr<pb::CompiledFile> pb_compiled_file = SerializeCompiledFileToPb(xmlres->file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 out->WriteCompiledFile(pb_compiled_file.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 out->WriteData(&buffer);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700362
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 if (out->HadError()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700364 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 return false;
366 }
367 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700368}
369
Adam Lesinski776aa952017-04-24 15:09:32 -0700370static bool IsValidFile(IAaptContext* context, const StringPiece& input_path) {
371 const file::FileType file_type = file::GetFileType(input_path);
372 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
373 if (file_type == file::FileType::kDirectory) {
374 context->GetDiagnostics()->Error(DiagMessage(input_path)
375 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700376 } else if (file_type == file::FileType::kNonexistant) {
377 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700378 } else {
379 context->GetDiagnostics()->Error(DiagMessage(input_path)
380 << "not a valid resource file");
381 }
382 return false;
383 }
384 return true;
385}
386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700388 const ResourcePathData& path_data, IArchiveWriter* writer,
389 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700391 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 }
393
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700398 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700399 << "failed to open file: "
Adam Lesinski06460ef2017-03-14 18:52:13 -0700400 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700402 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700405
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 fin.close();
407 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700408
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700409 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 return false;
411 }
412
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700413 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 xmlres->file.config = path_data.config;
415 xmlres->file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416
417 // Collect IDs that are defined here.
418 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 return false;
421 }
422
423 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 InlineXmlFormatParser inline_xml_format_parser;
425 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 return false;
427 }
428
429 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700431 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 return false;
433 }
434
435 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 {
438 // Wrap our IArchiveWriter with an adaptor that implements the
439 // ZeroCopyOutputStream
440 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441 CopyingOutputStreamAdaptor copying_adaptor(writer);
442 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
445 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446
447 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700448 output_stream.WriteLittleEndian32(1 + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700450 if (!FlattenXmlToOutStream(context, output_path, xmlres.get(), &output_stream)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700452 }
453
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 for (auto& inline_xml_doc : inline_documents) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700455 if (!FlattenXmlToOutStream(context, output_path, inline_xml_doc.get(), &output_stream)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700456 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700458 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700460
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700462 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 return false;
464 }
465 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700466}
467
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700468static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700469 const ResourcePathData& path_data, IArchiveWriter* writer,
470 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700472 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 }
474
475 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700477 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 res_file.config = path_data.config;
479 res_file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480
481 {
482 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700483 if (!android::base::ReadFileToString(path_data.source.path, &content,
484 true /*follow_symlinks*/)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700485 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700486 << "failed to open file: "
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700487 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700489 }
490
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700491 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700492 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700493
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 // Ensure that we only keep the chunks we care about if we end up
495 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700497 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 if (!image) {
499 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700500 }
501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 std::unique_ptr<NinePatch> nine_patch;
503 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700505 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 if (!nine_patch) {
507 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700508 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 }
510
511 // Remove the 1px border around the NinePatch.
512 // Basically the row array is shifted up by 1, and the length is treated
513 // as height - 2.
514 // For each row, shift the array to the left by 1, and treat the length as
515 // width - 2.
516 image->width -= 2;
517 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700518 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 for (int32_t h = 0; h < image->height; h++) {
520 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
521 }
522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700524 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
525 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700527 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528
529 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700530 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 return false;
532 }
533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 if (nine_patch != nullptr ||
535 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 // No matter what, we must use the re-encoded PNG, even if it is larger.
537 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 } else {
540 // The re-encoded PNG is larger than the original, and there is
541 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700543 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
544 << "original PNG is smaller than crunched PNG"
545 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 }
547
Adam Lesinski06460ef2017-03-14 18:52:13 -0700548 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700550 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
551 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700556 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
557 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558 std::stringstream legacy_stream(content);
559 BigBuffer legacy_buffer(4096);
560 Png png(context->GetDiagnostics());
561 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 return false;
563 }
564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
566 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 << " new=" << buffer.size());
568 }
569 }
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 if (!WriteHeaderAndBufferToWriter(output_path, res_file, buffer, writer,
572 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 return false;
574 }
575 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700576}
577
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700579 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 const std::string& output_path) {
581 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700582 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700584
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700587 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 res_file.config = path_data.config;
589 res_file.source = path_data.source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700590
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 std::string error_str;
592 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700594 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
595 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return false;
597 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 if (!WriteHeaderAndMmapToWriter(output_path, res_file, f.value(), writer,
600 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 return false;
602 }
603 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700604}
605
606class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100608 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
609 }
610
Adam Lesinskib522f042017-04-21 16:57:59 -0700611 PackageType GetPackageType() override {
612 // Every compilation unit starts as an app and then gets linked as potentially something else.
613 return PackageType::kApp;
614 }
615
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700616 void SetVerbose(bool val) {
617 verbose_ = val;
618 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800619
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700620 bool IsVerbose() override {
621 return verbose_;
622 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800623
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700624 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100625 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700626 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700627
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 abort();
630 return nullptr;
631 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700632
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 static std::string empty;
635 return empty;
636 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700637
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700638 uint8_t GetPackageId() override {
639 return 0x0;
640 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700641
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 abort();
644 return nullptr;
645 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800646
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700647 int GetMinSdkVersion() override {
648 return 0;
649 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700650
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 private:
Chris Warrington820d72a2017-04-27 15:27:01 +0100652 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700653 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700654};
655
656/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 * Entry point for compilation phase. Parses arguments and dispatches to the
658 * correct steps.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700659 */
Chris Warrington820d72a2017-04-27 15:27:01 +0100660int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
661 CompileContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700663
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 bool verbose = false;
665 Flags flags =
666 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700668 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700669 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 "Generate resources for pseudo-locales "
671 "(en-XA and ar-XB)",
672 &options.pseudolocalize)
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700673 .OptionalSwitch("--no-crunch", "Disables PNG processing", &options.no_png_crunch)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700674 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
675 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700676 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
677 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700678 return 1;
679 }
680
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700682
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700683 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700685 std::vector<ResourcePathData> input_data;
686 if (options.res_dir) {
687 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700689 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700690 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700692 }
693
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700694 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 return 1;
696 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800697
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700698 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700699
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700700 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800702
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 for (const std::string& arg : flags.GetArgs()) {
705 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700706 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700707 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700709 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700710 return 1;
711 }
712 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800713
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700714 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700715 }
716
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700718 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 }
720
721 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700722 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700724 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725 }
726
Adam Lesinski776aa952017-04-24 15:09:32 -0700727 if (!IsValidFile(&context, path_data.source.path)) {
728 error = true;
729 continue;
730 }
731
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700732 if (path_data.resource_dir == "values") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700733 // Overwrite the extension.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700734 path_data.extension = "arsc";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735
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 (!CompileTable(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 error = true;
739 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800740
741 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700742 const std::string output_filename = BuildIntermediateFilename(path_data);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700743 if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744 if (*type != ResourceType::kRaw) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 if (path_data.extension == "xml") {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700746 if (!CompileXml(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700747 error = true;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800748 }
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700749 } else if (!options.no_png_crunch &&
750 (path_data.extension == "png" || path_data.extension == "9.png")) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700751 if (!CompilePng(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700752 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700753 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700755 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700756 error = true;
757 }
758 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700759 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700760 if (!CompileFile(&context, options, path_data, archive_writer.get(), output_filename)) {
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 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700765 context.GetDiagnostics()->Error(DiagMessage() << "invalid file path '" << path_data.source
766 << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700767 error = true;
768 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700769 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700770 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700771
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 if (error) {
773 return 1;
774 }
775 return 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700776}
777
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778} // namespace aapt