blob: a06140cff54224c76fe8d0c03ce138dce7f38e4f [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 Lesinski1ab598f2015-08-14 14:26:04 -070022#include "ConfigDescription.h"
23#include "Diagnostics.h"
24#include "Flags.h"
25#include "ResourceParser.h"
26#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070028#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080030#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "compile/XmlIdCollector.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080032#include "flatten/Archive.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "flatten/XmlFlattener.h"
Adam Lesinski59e04c62016-02-04 15:59:23 -080034#include "proto/ProtoSerialize.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "util/Files.h"
36#include "util/Maybe.h"
37#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080038#include "xml/XmlDom.h"
39#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041#include "android-base/errors.h"
42#include "android-base/file.h"
43#include "google/protobuf/io/coded_stream.h"
44#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070046using google::protobuf::io::CopyingOutputStreamAdaptor;
47using google::protobuf::io::ZeroCopyOutputStream;
48
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049namespace aapt {
50
51struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 std::string name;
55 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 // Original config str. We keep this because when we parse the config, we may
58 // add on
59 // version qualifiers. We want to preserve the original input so the output is
60 // easily
61 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064};
65
66/**
67 * Resource file paths are expected to look like:
68 * [--/res/]type[-config]/name
69 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path,
71 std::string* out_error) {
72 std::vector<std::string> parts = util::Split(path, file::sDirSep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return {};
76 }
77
78 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 size_t dash_pos = dir.find('-');
84 if (dash_pos != std::string::npos) {
85 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
86 if (!ConfigDescription::Parse(config_str, &config)) {
87 if (out_error) {
88 std::stringstream err_str;
89 err_str << "invalid configuration '" << config_str << "'";
90 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 }
92 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 std::string& filename = parts[parts.size() - 1];
98 StringPiece name = filename;
99 StringPiece extension;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 size_t dot_pos = filename.find('.');
101 if (dot_pos != std::string::npos) {
102 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
103 name = name.substr(0, dot_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 return ResourcePathData{Source(path), dir_str.ToString(),
107 name.ToString(), extension.ToString(),
108 config_str.ToString(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109}
110
111struct CompileOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 std::string output_path;
113 Maybe<std::string> res_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 bool pseudolocalize = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 bool legacy_mode = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 bool verbose = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117};
118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119static std::string BuildIntermediateFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 name << data.resource_dir;
122 if (!data.config_str.empty()) {
123 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 }
125 name << "_" << data.name;
126 if (!data.extension.empty()) {
127 name << "." << data.extension;
128 }
129 name << ".flat";
130 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800131}
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133static bool IsHidden(const StringPiece& filename) {
134 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800135}
136
137/**
138 * Walks the res directory structure, looking for resource files.
139 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140static bool LoadInputFilesFromDir(
141 IAaptContext* context, const CompileOptions& options,
142 std::vector<ResourcePathData>* out_path_data) {
143 const std::string& root_dir = options.res_dir.value();
144 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 closedir);
146 if (!d) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 context->GetDiagnostics()->Error(DiagMessage() << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 return false;
149 }
150
151 while (struct dirent* entry = readdir(d.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 if (IsHidden(entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 continue;
154 }
155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 std::string prefix_path = root_dir;
157 file::AppendPath(&prefix_path, entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 if (file::GetFileType(prefix_path) != file::FileType::kDirectory) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 continue;
161 }
162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 std::unique_ptr<DIR, decltype(closedir)*> subdir(
164 opendir(prefix_path.data()), closedir);
165 if (!subdir) {
166 context->GetDiagnostics()->Error(DiagMessage() << strerror(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 return false;
168 }
169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 while (struct dirent* leaf_entry = readdir(subdir.get())) {
171 if (IsHidden(leaf_entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 continue;
173 }
174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 std::string full_path = prefix_path;
176 file::AppendPath(&full_path, leaf_entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 std::string err_str;
179 Maybe<ResourcePathData> path_data =
180 ExtractResourcePathData(full_path, &err_str);
181 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 Lesinskicacb28f2016-10-19 12:18:14 -0700201 << strerror(errno));
202 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)) {
251 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 << "failed to open");
253 return false;
254 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800255
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 {
259 // Wrap our IArchiveWriter with an adaptor that implements the
260 // ZeroCopyOutputStream
261 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(&table);
265 if (!pb_table->SerializeToZeroCopyStream(&copying_adaptor)) {
266 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 << "failed to write");
268 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 if (!writer->FinishEntry()) {
273 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 << "failed to finish entry");
275 return false;
276 }
277 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800278}
279
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280static bool WriteHeaderAndBufferToWriter(const StringPiece& output_path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 const ResourceFile& file,
282 const BigBuffer& buffer,
283 IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800284 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 if (!writer->StartEntry(output_path, 0)) {
287 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 return false;
289 }
290
291 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 {
294 // Wrap our IArchiveWriter with an adaptor that implements the
295 // ZeroCopyOutputStream
296 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 CopyingOutputStreamAdaptor copying_adaptor(writer);
298 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299
300 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700303 std::unique_ptr<pb::CompiledFile> compiled_file =
304 SerializeCompiledFileToPb(file);
305 output_stream.WriteCompiledFile(compiled_file.get());
306 output_stream.WriteData(&buffer);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 if (output_stream.HadError()) {
309 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800311 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (!writer->FinishEntry()) {
315 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return false;
317 }
318 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800319}
320
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700321static bool WriteHeaderAndMmapToWriter(const StringPiece& output_path,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 const ResourceFile& file,
323 const android::FileMap& map,
324 IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800325 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 if (!writer->StartEntry(output_path, 0)) {
328 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 return false;
330 }
331
332 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 {
335 // Wrap our IArchiveWriter with an adaptor that implements the
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 // ZeroCopyOutputStream interface.
337 CopyingOutputStreamAdaptor copying_adaptor(writer);
338 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339
340 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 output_stream.WriteLittleEndian32(1);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 std::unique_ptr<pb::CompiledFile> compiled_file =
344 SerializeCompiledFileToPb(file);
345 output_stream.WriteCompiledFile(compiled_file.get());
346 output_stream.WriteData(map.getDataPtr(), map.getDataLength());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 if (output_stream.HadError()) {
349 diag->Error(DiagMessage(output_path) << "failed to write data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800351 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 if (!writer->FinishEntry()) {
355 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 return false;
357 }
358 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700359}
360
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700361static bool FlattenXmlToOutStream(IAaptContext* context,
362 const StringPiece& output_path,
363 xml::XmlResource* xmlres,
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700364 CompiledFileOutputStream* out) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 BigBuffer buffer(1024);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366 XmlFlattenerOptions xml_flattener_options;
367 xml_flattener_options.keep_raw_values = true;
368 XmlFlattener flattener(&buffer, xml_flattener_options);
369 if (!flattener.Consume(context, xmlres)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 return false;
371 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700372
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373 std::unique_ptr<pb::CompiledFile> pb_compiled_file =
374 SerializeCompiledFileToPb(xmlres->file);
375 out->WriteCompiledFile(pb_compiled_file.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 out->WriteData(&buffer);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700377
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 if (out->HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 << "failed to write data");
381 return false;
382 }
383 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700384}
385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386static bool CompileXml(IAaptContext* context, const CompileOptions& options,
387 const ResourcePathData& path_data,
388 IArchiveWriter* writer, const std::string& output_path) {
389 if (context->IsVerbose()) {
390 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 << "compiling XML");
392 }
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 Lesinskicacb28f2016-10-19 12:18:14 -0700399 << strerror(errno));
400 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700401 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700402
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700404
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 fin.close();
406 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 return false;
410 }
411
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 xmlres->file.name = ResourceName(
413 {}, *ParseResourceType(path_data.resource_dir), path_data.name);
414 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)) {
431 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 << "failed to open file");
433 return false;
434 }
435
436 // Make sure CopyingOutputStreamAdaptor is deleted before we call
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 // writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 {
439 // Wrap our IArchiveWriter with an adaptor that implements the
440 // ZeroCopyOutputStream
441 // interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700442 CopyingOutputStreamAdaptor copying_adaptor(writer);
443 CompiledFileOutputStream output_stream(&copying_adaptor);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
446 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447
448 // Number of CompiledFiles.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 output_stream.WriteLittleEndian32(1 + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 if (!FlattenXmlToOutStream(context, output_path, xmlres.get(),
452 &output_stream)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454 }
455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 for (auto& inline_xml_doc : inline_documents) {
457 if (!FlattenXmlToOutStream(context, output_path, inline_xml_doc.get(),
458 &output_stream)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700459 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700461 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 if (!writer->FinishEntry()) {
465 context->GetDiagnostics()->Error(DiagMessage(output_path)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 << "failed to finish writing data");
467 return false;
468 }
469 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700470}
471
Adam Lesinski21efb682016-09-14 17:35:43 -0700472class BigBufferOutputStream : public io::OutputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 explicit BigBufferOutputStream(BigBuffer* buffer) : buffer_(buffer) {}
Adam Lesinski21efb682016-09-14 17:35:43 -0700475
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 bool Next(void** data, int* len) override {
477 size_t count;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 *data = buffer_->NextBlock(&count);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 *len = static_cast<int>(count);
480 return true;
481 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700482
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 void BackUp(int count) override { buffer_->BackUp(count); }
Adam Lesinski21efb682016-09-14 17:35:43 -0700484
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700485 int64_t ByteCount() const override { return buffer_->size(); }
Adam Lesinski21efb682016-09-14 17:35:43 -0700486
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 bool HadError() const override { return false; }
Adam Lesinski21efb682016-09-14 17:35:43 -0700488
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 BigBuffer* buffer_;
Adam Lesinski21efb682016-09-14 17:35:43 -0700491
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 DISALLOW_COPY_AND_ASSIGN(BigBufferOutputStream);
Adam Lesinski21efb682016-09-14 17:35:43 -0700493};
494
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700495static bool CompilePng(IAaptContext* context, const CompileOptions& options,
496 const ResourcePathData& path_data,
497 IArchiveWriter* writer, const std::string& output_path) {
498 if (context->IsVerbose()) {
499 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700500 << "compiling PNG");
501 }
502
503 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504 ResourceFile res_file;
505 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir),
506 path_data.name);
507 res_file.config = path_data.config;
508 res_file.source = path_data.source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509
510 {
511 std::string content;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 if (!android::base::ReadFileToString(path_data.source.path, &content)) {
513 context->GetDiagnostics()->Error(
514 DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 << android::base::SystemErrorCodeToString(errno));
516 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700517 }
518
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 BigBuffer crunched_png_buffer(4096);
520 BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 // Ensure that we only keep the chunks we care about if we end up
523 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 PngChunkFilter png_chunk_filter(content);
525 std::unique_ptr<Image> image = ReadPng(context, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 if (!image) {
527 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700528 }
529
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 std::unique_ptr<NinePatch> nine_patch;
531 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 std::string err;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 nine_patch = NinePatch::Create(image->rows.get(), image->width,
534 image->height, &err);
535 if (!nine_patch) {
536 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 }
539
540 // Remove the 1px border around the NinePatch.
541 // Basically the row array is shifted up by 1, and the length is treated
542 // as height - 2.
543 // For each row, shift the array to the left by 1, and treat the length as
544 // width - 2.
545 image->width -= 2;
546 image->height -= 2;
547 memmove(image->rows.get(), image->rows.get() + 1,
548 image->height * sizeof(uint8_t**));
549 for (int32_t h = 0; h < image->height; h++) {
550 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
551 }
552
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 if (context->IsVerbose()) {
554 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
555 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700557 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558
559 // Write the crunched PNG.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 if (!WritePng(context, image.get(), nine_patch.get(),
561 &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 return false;
563 }
564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 if (nine_patch != nullptr ||
566 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 // No matter what, we must use the re-encoded PNG, even if it is larger.
568 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 } else {
571 // The re-encoded PNG is larger than the original, and there is
572 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700573 if (context->IsVerbose()) {
574 context->GetDiagnostics()->Note(
575 DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 << "original PNG is smaller than crunched PNG"
577 << ", using original");
578 }
579
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 PngChunkFilter png_chunk_filter_again(content);
581 BigBuffer filtered_png_buffer(4096);
582 BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
583 io::Copy(&filtered_png_buffer_out, &png_chunk_filter_again);
584 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700585 }
586
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700587 if (context->IsVerbose()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 // For debugging only, use the legacy PNG cruncher and compare the
589 // resulting file sizes.
590 // This will help catch exotic cases where the new code may generate
591 // larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 std::stringstream legacy_stream(content);
593 BigBuffer legacy_buffer(4096);
594 Png png(context->GetDiagnostics());
595 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return false;
597 }
598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
600 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 << " new=" << buffer.size());
602 }
603 }
604
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700605 if (!WriteHeaderAndBufferToWriter(output_path, res_file, buffer, writer,
606 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 return false;
608 }
609 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610}
611
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700612static bool CompileFile(IAaptContext* context, const CompileOptions& options,
613 const ResourcePathData& path_data,
614 IArchiveWriter* writer,
615 const std::string& output_path) {
616 if (context->IsVerbose()) {
617 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 << "compiling file");
619 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700620
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622 ResourceFile res_file;
623 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir),
624 path_data.name);
625 res_file.config = path_data.config;
626 res_file.source = path_data.source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700627
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 std::string error_str;
629 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 if (!f) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
632 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 return false;
634 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 if (!WriteHeaderAndMmapToWriter(output_path, res_file, f.value(), writer,
637 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 return false;
639 }
640 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700641}
642
643class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 void SetVerbose(bool val) { verbose_ = val; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800646
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 bool IsVerbose() override { return verbose_; }
Adam Lesinski355f2852016-02-13 20:26:45 -0800648
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700649 IDiagnostics* GetDiagnostics() override { return &diagnostics_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700650
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 NameMangler* GetNameMangler() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700652 abort();
653 return nullptr;
654 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700655
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700656 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 static std::string empty;
658 return empty;
659 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700660
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700661 uint8_t GetPackageId() override { return 0x0; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700662
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 SymbolTable* GetExternalSymbols() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700664 abort();
665 return nullptr;
666 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800667
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 int GetMinSdkVersion() override { return 0; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700669
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 StdErrDiagnostics diagnostics_;
672 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700673};
674
675/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 * Entry point for compilation phase. Parses arguments and dispatches to the
677 * correct steps.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700678 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679int Compile(const std::vector<StringPiece>& args) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 CompileContext context;
681 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700682
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700683 bool verbose = false;
684 Flags flags =
685 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700686 .RequiredFlag("-o", "Output path", &options.output_path)
687 .OptionalFlag("--dir", "Directory to scan for resources",
688 &options.res_dir)
689 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 "Generate resources for pseudo-locales "
691 "(en-XA and ar-XB)",
692 &options.pseudolocalize)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 .OptionalSwitch(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700694 "--legacy",
695 "Treat errors that used to be valid in AAPT as warnings",
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700696 &options.legacy_mode)
697 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
698 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700699 return 1;
700 }
701
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700702 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700704 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 std::vector<ResourcePathData> input_data;
707 if (options.res_dir) {
708 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 // Can't have both files and a resource directory.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700710 context.GetDiagnostics()->Error(DiagMessage()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700711 << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700714 }
715
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700716 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700717 return 1;
718 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800719
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700720 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(),
721 options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700722
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700723 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700724 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800725
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700726 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700727 for (const std::string& arg : flags.GetArgs()) {
728 std::string error_str;
729 if (Maybe<ResourcePathData> path_data =
730 ExtractResourcePathData(arg, &error_str)) {
731 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 context.GetDiagnostics()->Error(DiagMessage() << error_str << " ("
734 << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 return 1;
736 }
737 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800738
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700739 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(),
740 options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 }
742
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700743 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700744 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700745 }
746
747 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700748 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 if (options.verbose) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 context.GetDiagnostics()->Note(DiagMessage(path_data.source)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 << "processing");
752 }
753
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700754 if (path_data.resource_dir == "values") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 // Overwrite the extension.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700756 path_data.extension = "arsc";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700757
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700758 const std::string output_filename = BuildIntermediateFilename(path_data);
759 if (!CompileTable(&context, options, path_data, archive_writer.get(),
760 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761 error = true;
762 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800763
764 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 const std::string output_filename = BuildIntermediateFilename(path_data);
766 if (const ResourceType* type =
767 ParseResourceType(path_data.resource_dir)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 if (*type != ResourceType::kRaw) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700769 if (path_data.extension == "xml") {
770 if (!CompileXml(&context, options, path_data, archive_writer.get(),
771 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 error = true;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800773 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700774 } else if (path_data.extension == "png" ||
775 path_data.extension == "9.png") {
776 if (!CompilePng(&context, options, path_data, archive_writer.get(),
777 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 error = true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700779 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700780 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 if (!CompileFile(&context, options, path_data, archive_writer.get(),
782 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 error = true;
784 }
785 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700786 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700787 if (!CompileFile(&context, options, path_data, archive_writer.get(),
788 output_filename)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700789 error = true;
790 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700791 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700792 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700793 context.GetDiagnostics()->Error(
794 DiagMessage() << "invalid file path '" << path_data.source << "'");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700795 error = true;
796 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700797 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700799
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 if (error) {
801 return 1;
802 }
803 return 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700804}
805
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700806} // namespace aapt