blob: 1fe30f0b14781b2e60120107798a5aae352ed593 [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 Lesinski59e04c62016-02-04 15:59:23 -080041#include "proto/ProtoSerialize.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042#include "util/Files.h"
43#include "util/Maybe.h"
44#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080045#include "xml/XmlDom.h"
46#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinskid5083f62017-01-16 15:07:21 -080048using android::StringPiece;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070049using google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070050
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051namespace aapt {
52
53struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 std::string name;
57 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 // Original config str. We keep this because when we parse the config, we may
60 // add on
61 // version qualifiers. We want to preserve the original input so the output is
62 // easily
63 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066};
67
68/**
69 * Resource file paths are expected to look like:
70 * [--/res/]type[-config]/name
71 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path,
73 std::string* out_error) {
74 std::vector<std::string> parts = util::Split(path, file::sDirSep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 return {};
78 }
79
80 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 size_t dash_pos = dir.find('-');
86 if (dash_pos != std::string::npos) {
87 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
88 if (!ConfigDescription::Parse(config_str, &config)) {
89 if (out_error) {
90 std::stringstream err_str;
91 err_str << "invalid configuration '" << config_str << "'";
92 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
94 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 std::string& filename = parts[parts.size() - 1];
100 StringPiece name = filename;
101 StringPiece extension;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 size_t dot_pos = filename.find('.');
103 if (dot_pos != std::string::npos) {
104 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
105 name = name.substr(0, dot_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107
Adam Lesinskid5083f62017-01-16 15:07:21 -0800108 return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(),
109 extension.to_string(), config_str.to_string(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110}
111
112struct CompileOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 std::string output_path;
114 Maybe<std::string> res_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 bool pseudolocalize = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 bool legacy_mode = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 bool verbose = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118};
119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120static std::string BuildIntermediateFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 name << data.resource_dir;
123 if (!data.config_str.empty()) {
124 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
126 name << "_" << data.name;
127 if (!data.extension.empty()) {
128 name << "." << data.extension;
129 }
130 name << ".flat";
131 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800132}
133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134static bool IsHidden(const StringPiece& filename) {
135 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800136}
137
138/**
139 * Walks the res directory structure, looking for resource files.
140 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141static bool LoadInputFilesFromDir(
142 IAaptContext* context, const CompileOptions& options,
143 std::vector<ResourcePathData>* out_path_data) {
144 const std::string& root_dir = options.res_dir.value();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700145 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 if (!d) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700147 context->GetDiagnostics()->Error(DiagMessage()
148 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 return false;
150 }
151
152 while (struct dirent* entry = readdir(d.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 if (IsHidden(entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 continue;
155 }
156
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 std::string prefix_path = root_dir;
158 file::AppendPath(&prefix_path, entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 if (file::GetFileType(prefix_path) != file::FileType::kDirectory) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 continue;
162 }
163
Adam Lesinski06460ef2017-03-14 18:52:13 -0700164 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 if (!subdir) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700166 context->GetDiagnostics()->Error(DiagMessage()
167 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 return false;
169 }
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 while (struct dirent* leaf_entry = readdir(subdir.get())) {
172 if (IsHidden(leaf_entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 continue;
174 }
175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 std::string full_path = prefix_path;
177 file::AppendPath(&full_path, leaf_entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 std::string err_str;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700180 Maybe<ResourcePathData> path_data = ExtractResourcePathData(full_path, &err_str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 if (!path_data) {
182 context->GetDiagnostics()->Error(DiagMessage() << err_str);
Adam Lesinskia40e9722015-11-24 19:11:46 -0800183 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 }
185
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 out_path_data->push_back(std::move(path_data.value()));
Adam Lesinskia40e9722015-11-24 19:11:46 -0800187 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 }
189 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190}
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192static bool CompileTable(IAaptContext* context, const CompileOptions& options,
193 const ResourcePathData& path_data,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 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 Lesinskice5e56e2016-10-21 17:56:45 -0700213 parser_options.translatable =
214 path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 ResourceParser res_parser(context->GetDiagnostics(), &table,
217 path_data.source, path_data.config,
218 parser_options);
219 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800221 }
222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 fin.close();
224 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800225
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 if (options.pseudolocalize) {
227 // Generate pseudo-localized strings (en-XA and ar-XB).
228 // These are created as weak symbols, and are only generated from default
229 // configuration
230 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 PseudolocaleGenerator pseudolocale_generator;
232 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700234 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239
240 // Assign an ID to any package that has resources.
241 for (auto& pkg : table.packages) {
242 if (!pkg->id) {
243 // If no package ID was set while parsing (public identifiers), auto
244 // assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700250 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700251 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 return false;
253 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800254
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 {
258 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700259 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(&table);
263 if (!pb_table->SerializeToZeroCopyStream(&copying_adaptor)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700264 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700266 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700270 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 return false;
272 }
273 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800274}
275
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276static bool WriteHeaderAndBufferToWriter(const StringPiece& output_path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 const ResourceFile& file,
278 const BigBuffer& buffer,
279 IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800280 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 if (!writer->StartEntry(output_path, 0)) {
283 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 return false;
285 }
286
287 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 {
290 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinski06460ef2017-03-14 18:52:13 -0700291 // ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 CopyingOutputStreamAdaptor copying_adaptor(writer);
293 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294
295 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297
Adam Lesinski06460ef2017-03-14 18:52:13 -0700298 std::unique_ptr<pb::CompiledFile> compiled_file = SerializeCompiledFileToPb(file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 output_stream.WriteCompiledFile(compiled_file.get());
300 output_stream.WriteData(&buffer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 if (output_stream.HadError()) {
303 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800305 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 if (!writer->FinishEntry()) {
309 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 return false;
311 }
312 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800313}
314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315static bool WriteHeaderAndMmapToWriter(const StringPiece& output_path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 const ResourceFile& file,
317 const android::FileMap& map,
318 IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800319 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321 if (!writer->StartEntry(output_path, 0)) {
322 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323 return false;
324 }
325
326 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 {
329 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700330 // ZeroCopyOutputStream interface.
331 CopyingOutputStreamAdaptor copying_adaptor(writer);
332 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333
334 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 std::unique_ptr<pb::CompiledFile> compiled_file =
338 SerializeCompiledFileToPb(file);
339 output_stream.WriteCompiledFile(compiled_file.get());
340 output_stream.WriteData(map.getDataPtr(), map.getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 if (output_stream.HadError()) {
343 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800345 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 if (!writer->FinishEntry()) {
349 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return false;
351 }
352 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700353}
354
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355static bool FlattenXmlToOutStream(IAaptContext* context,
356 const StringPiece& output_path,
357 xml::XmlResource* xmlres,
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700358 CompiledFileOutputStream* out) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 BigBuffer buffer(1024);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 XmlFlattenerOptions xml_flattener_options;
361 xml_flattener_options.keep_raw_values = true;
362 XmlFlattener flattener(&buffer, xml_flattener_options);
363 if (!flattener.Consume(context, xmlres)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
365 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700366
Adam Lesinski06460ef2017-03-14 18:52:13 -0700367 std::unique_ptr<pb::CompiledFile> pb_compiled_file = SerializeCompiledFileToPb(xmlres->file);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 out->WriteCompiledFile(pb_compiled_file.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 out->WriteData(&buffer);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700370
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 if (out->HadError()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700372 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 return false;
374 }
375 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700376}
377
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378static bool CompileXml(IAaptContext* context, const CompileOptions& options,
379 const ResourcePathData& path_data,
380 IArchiveWriter* writer, const std::string& output_path) {
381 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700382 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 }
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387 std::ifstream fin(path_data.source.path, std::ifstream::binary);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 if (!fin) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700389 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700390 << android::base::SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700392 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700393
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700394 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396 fin.close();
397 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700399 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 return false;
401 }
402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 xmlres->file.name = ResourceName(
404 {}, *ParseResourceType(path_data.resource_dir), path_data.name);
405 xmlres->file.config = path_data.config;
406 xmlres->file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407
408 // Collect IDs that are defined here.
409 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700411 return false;
412 }
413
414 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 InlineXmlFormatParser inline_xml_format_parser;
416 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 return false;
418 }
419
420 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 if (!writer->StartEntry(output_path, 0)) {
422 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 << "failed to open file");
424 return false;
425 }
426
427 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 {
430 // Wrap our IArchiveWriter with an adaptor that implements the
431 // ZeroCopyOutputStream
432 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700433 CopyingOutputStreamAdaptor copying_adaptor(writer);
434 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700436 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
437 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438
439 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 output_stream.WriteLittleEndian32(1 + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 if (!FlattenXmlToOutStream(context, output_path, xmlres.get(),
443 &output_stream)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700445 }
446
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 for (auto& inline_xml_doc : inline_documents) {
448 if (!FlattenXmlToOutStream(context, output_path, inline_xml_doc.get(),
449 &output_stream)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700450 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700452 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700455 if (!writer->FinishEntry()) {
456 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 << "failed to finish writing data");
458 return false;
459 }
460 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700461}
462
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463static bool CompilePng(IAaptContext* context, const CompileOptions& options,
464 const ResourcePathData& path_data,
465 IArchiveWriter* writer, const std::string& output_path) {
466 if (context->IsVerbose()) {
467 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 << "compiling PNG");
469 }
470
471 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 ResourceFile res_file;
473 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir),
474 path_data.name);
475 res_file.config = path_data.config;
476 res_file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477
478 {
479 std::string content;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700480 if (!android::base::ReadFileToString(path_data.source.path, &content)) {
481 context->GetDiagnostics()->Error(
482 DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 << android::base::SystemErrorCodeToString(errno));
484 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700485 }
486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700488 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700489
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 // Ensure that we only keep the chunks we care about if we end up
491 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 PngChunkFilter png_chunk_filter(content);
493 std::unique_ptr<Image> image = ReadPng(context, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 if (!image) {
495 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700496 }
497
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700498 std::unique_ptr<NinePatch> nine_patch;
499 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700501 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 if (!nine_patch) {
503 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700504 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 }
506
507 // Remove the 1px border around the NinePatch.
508 // Basically the row array is shifted up by 1, and the length is treated
509 // as height - 2.
510 // For each row, shift the array to the left by 1, and treat the length as
511 // width - 2.
512 image->width -= 2;
513 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700514 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 for (int32_t h = 0; h < image->height; h++) {
516 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
517 }
518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 if (context->IsVerbose()) {
520 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
521 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700523 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524
525 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700526 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 return false;
528 }
529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 if (nine_patch != nullptr ||
531 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 // No matter what, we must use the re-encoded PNG, even if it is larger.
533 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 } else {
536 // The re-encoded PNG is larger than the original, and there is
537 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700539 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
540 << "original PNG is smaller than crunched PNG"
541 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 }
543
Adam Lesinski06460ef2017-03-14 18:52:13 -0700544 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700546 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
547 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 }
550
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700552 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
553 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700554 std::stringstream legacy_stream(content);
555 BigBuffer legacy_buffer(4096);
556 Png png(context->GetDiagnostics());
557 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 return false;
559 }
560
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700561 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
562 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 << " new=" << buffer.size());
564 }
565 }
566
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700567 if (!WriteHeaderAndBufferToWriter(output_path, res_file, buffer, writer,
568 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 return false;
570 }
571 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700572}
573
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574static bool CompileFile(IAaptContext* context, const CompileOptions& options,
575 const ResourcePathData& path_data,
576 IArchiveWriter* writer,
577 const std::string& output_path) {
578 if (context->IsVerbose()) {
579 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 << "compiling file");
581 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700582
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 ResourceFile res_file;
585 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir),
586 path_data.name);
587 res_file.config = path_data.config;
588 res_file.source = path_data.source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700589
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 std::string error_str;
591 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 if (!f) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
594 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 return false;
596 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700597
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 if (!WriteHeaderAndMmapToWriter(output_path, res_file, f.value(), writer,
599 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700600 return false;
601 }
602 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603}
604
605class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800608
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 bool IsVerbose() override { return verbose_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800610
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700611 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700612
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700614 abort();
615 return nullptr;
616 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700617
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700619 static std::string empty;
620 return empty;
621 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700622
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700623 uint8_t GetPackageId() override { return 0x0; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700624
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700625 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 abort();
627 return nullptr;
628 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800629
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 int GetMinSdkVersion() override { return 0; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700631
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 StdErrDiagnostics diagnostics_;
634 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700635};
636
637/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 * Entry point for compilation phase. Parses arguments and dispatches to the
639 * correct steps.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700640 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641int Compile(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700642 CompileContext context;
643 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700645 bool verbose = false;
646 Flags flags =
647 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 .RequiredFlag("-o", "Output path", &options.output_path)
649 .OptionalFlag("--dir", "Directory to scan for resources",
650 &options.res_dir)
651 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 "Generate resources for pseudo-locales "
653 "(en-XA and ar-XB)",
654 &options.pseudolocalize)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655 .OptionalSwitch(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700656 "--legacy",
657 "Treat errors that used to be valid in AAPT as warnings",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700658 &options.legacy_mode)
659 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
660 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 return 1;
662 }
663
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700664 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700665
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700666 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 std::vector<ResourcePathData> input_data;
669 if (options.res_dir) {
670 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700671 // Can't have both files and a resource directory.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700672 context.GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700675 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700676 }
677
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700678 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700679 return 1;
680 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(),
683 options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700684
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800687
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700688 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700689 for (const std::string& arg : flags.GetArgs()) {
690 std::string error_str;
691 if (Maybe<ResourcePathData> path_data =
692 ExtractResourcePathData(arg, &error_str)) {
693 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 context.GetDiagnostics()->Error(DiagMessage() << error_str << " ("
696 << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 return 1;
698 }
699 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800700
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(),
702 options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 }
704
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700705 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700706 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700707 }
708
709 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 if (options.verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 context.GetDiagnostics()->Note(DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 << "processing");
714 }
715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 if (path_data.resource_dir == "values") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 // Overwrite the extension.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700718 path_data.extension = "arsc";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 const std::string output_filename = BuildIntermediateFilename(path_data);
721 if (!CompileTable(&context, options, path_data, archive_writer.get(),
722 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 error = true;
724 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800725
726 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 const std::string output_filename = BuildIntermediateFilename(path_data);
728 if (const ResourceType* type =
729 ParseResourceType(path_data.resource_dir)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700730 if (*type != ResourceType::kRaw) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700731 if (path_data.extension == "xml") {
732 if (!CompileXml(&context, options, path_data, archive_writer.get(),
733 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 error = true;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800735 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 } else if (path_data.extension == "png" ||
737 path_data.extension == "9.png") {
738 if (!CompilePng(&context, options, path_data, archive_writer.get(),
739 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700741 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700742 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 if (!CompileFile(&context, options, path_data, archive_writer.get(),
744 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 error = true;
746 }
747 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700748 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700749 if (!CompileFile(&context, options, path_data, archive_writer.get(),
750 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 error = true;
752 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700753 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700755 context.GetDiagnostics()->Error(
756 DiagMessage() << "invalid file path '" << path_data.source << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757 error = true;
758 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700759 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700760 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700761
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 if (error) {
763 return 1;
764 }
765 return 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700766}
767
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768} // namespace aapt