1 /*
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
17 #include "Compile.h"
18
19 #include <dirent.h>
20
21 #include <string>
22
23 #include "ResourceParser.h"
24 #include "ResourceTable.h"
25 #include "android-base/errors.h"
26 #include "android-base/file.h"
27 #include "android-base/utf8.h"
28 #include "androidfw/BigBufferStream.h"
29 #include "androidfw/ConfigDescription.h"
30 #include "androidfw/FileStream.h"
31 #include "androidfw/IDiagnostics.h"
32 #include "androidfw/Image.h"
33 #include "androidfw/Png.h"
34 #include "androidfw/StringPiece.h"
35 #include "cmd/Util.h"
36 #include "compile/IdAssigner.h"
37 #include "compile/InlineXmlFormatParser.h"
38 #include "compile/PseudolocaleGenerator.h"
39 #include "compile/XmlIdCollector.h"
40 #include "format/Archive.h"
41 #include "format/Container.h"
42 #include "format/proto/ProtoSerialize.h"
43 #include "google/protobuf/io/coded_stream.h"
44 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
45 #include "io/FileSystem.h"
46 #include "io/StringStream.h"
47 #include "io/Util.h"
48 #include "io/ZipArchive.h"
49 #include "process/ProductFilter.h"
50 #include "trace/TraceBuffer.h"
51 #include "util/Files.h"
52 #include "util/Util.h"
53 #include "xml/XmlDom.h"
54 #include "xml/XmlPullParser.h"
55
56 using ::aapt::text::Printer;
57 using ::android::ConfigDescription;
58 using ::android::FileInputStream;
59 using ::android::StringPiece;
60 using ::android::base::SystemErrorCodeToString;
61 using ::google::protobuf::io::CopyingOutputStreamAdaptor;
62
63 namespace aapt {
64
65 struct ResourcePathData {
66 android::Source source;
67 std::string resource_dir;
68 std::string name;
69 std::string extension;
70 std::string flag_name;
71
72 // Original config str. We keep this because when we parse the config, we may add on
73 // version qualifiers. We want to preserve the original input so the output is easily
74 // computed before hand.
75 std::string config_str;
76 ConfigDescription config;
77 };
78
79 // Resource file paths are expected to look like: [--/res/]type[-config]/name
ExtractResourcePathData(const std::string & path,const char dir_sep,std::string * out_error,const CompileOptions & options)80 static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path,
81 const char dir_sep,
82 std::string* out_error,
83 const CompileOptions& options) {
84 std::vector<std::string> parts = util::Split(path, dir_sep);
85
86 std::string flag_name;
87 // Check for a flag
88 for (auto iter = parts.begin(); iter != parts.end();) {
89 if (iter->starts_with("flag(") && iter->ends_with(")")) {
90 if (!flag_name.empty()) {
91 if (out_error) *out_error = "resource path cannot contain more than one flag directory";
92 return {};
93 }
94 flag_name = iter->substr(5, iter->size() - 6);
95 iter = parts.erase(iter);
96 } else {
97 ++iter;
98 }
99 }
100
101 if (parts.size() < 2) {
102 if (out_error) *out_error = "bad resource path";
103 return {};
104 }
105
106 std::string& dir = parts[parts.size() - 2];
107 StringPiece dir_str = dir;
108
109 StringPiece config_str;
110 ConfigDescription config;
111 size_t dash_pos = dir.find('-');
112 if (dash_pos != std::string::npos) {
113 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
114 if (!ConfigDescription::Parse(config_str, &config)) {
115 if (out_error) {
116 std::stringstream err_str;
117 err_str << "invalid configuration '" << config_str << "'";
118 *out_error = err_str.str();
119 }
120 return {};
121 }
122 dir_str = dir_str.substr(0, dash_pos);
123 }
124
125 std::string& filename = parts[parts.size() - 1];
126 StringPiece name = filename;
127 StringPiece extension;
128
129 const std::string kNinePng = ".9.png";
130 if (filename.size() > kNinePng.size()
131 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
132 // Split on .9.png if this extension is present at the end of the file path
133 name = name.substr(0, filename.size() - kNinePng.size());
134 extension = "9.png";
135 } else {
136 // Split on the last period occurrence
137 size_t dot_pos = filename.rfind('.');
138 if (dot_pos != std::string::npos) {
139 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
140 name = name.substr(0, dot_pos);
141 }
142 }
143
144 const android::Source res_path =
145 options.source_path ? StringPiece(options.source_path.value()) : StringPiece(path);
146
147 return ResourcePathData{res_path,
148 std::string(dir_str),
149 std::string(name),
150 std::string(extension),
151 std::move(flag_name),
152 std::string(config_str),
153 config};
154 }
155
BuildIntermediateContainerFilename(const ResourcePathData & data)156 static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
157 std::stringstream name;
158 name << data.resource_dir;
159 if (!data.config_str.empty()) {
160 name << "-" << data.config_str;
161 }
162 name << "_" << data.name;
163 if (!data.flag_name.empty()) {
164 name << ".(" << data.flag_name << ")";
165 }
166 if (!data.extension.empty()) {
167 name << "." << data.extension;
168 }
169 name << ".flat";
170 return name.str();
171 }
172
CompileTable(IAaptContext * context,const CompileOptions & options,const ResourcePathData & path_data,io::IFile * file,IArchiveWriter * writer,const std::string & output_path)173 static bool CompileTable(IAaptContext* context, const CompileOptions& options,
174 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
175 const std::string& output_path) {
176 TRACE_CALL();
177 // Filenames starting with "donottranslate" are not localizable
178 bool translatable_file = path_data.name.find("donottranslate") != 0;
179 ResourceTable table;
180 {
181 auto fin = file->OpenInputStream();
182 if (fin->HadError()) {
183 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
184 << "failed to open file: " << fin->GetError());
185 return false;
186 }
187 // Parse the values file from XML.
188 xml::XmlPullParser xml_parser(fin.get());
189
190 ResourceParserOptions parser_options;
191 parser_options.error_on_positional_arguments = !options.legacy_mode;
192 parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables;
193 parser_options.translatable = translatable_file;
194 parser_options.feature_flag_values = options.feature_flag_values;
195
196 // If visibility was forced, we need to use it when creating a new resource and also error if
197 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
198 parser_options.visibility = options.visibility;
199 parser_options.flag = ParseFlag(path_data.flag_name);
200
201 if (parser_options.flag) {
202 std::string error;
203 auto flag_status = GetFlagStatus(parser_options.flag, options.feature_flag_values, &error);
204 if (flag_status) {
205 parser_options.flag_status = std::move(flag_status.value());
206 } else {
207 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error);
208 return false;
209 }
210 }
211
212 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
213 parser_options);
214 if (!res_parser.Parse(&xml_parser)) {
215 return false;
216 }
217
218 if (options.product_.has_value()) {
219 if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true)
220 .Consume(context, &table)) {
221 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
222 << "failed to filter product");
223 return false;
224 }
225 }
226 }
227
228 if (options.pseudolocalize && translatable_file) {
229 // Generate pseudo-localized strings (en-XA and ar-XB).
230 // These are created as weak symbols, and are only generated from default
231 // configuration
232 // strings and plurals.
233 std::string grammatical_gender_values;
234 std::string grammatical_gender_ratio;
235 if (options.pseudo_localize_gender_values) {
236 grammatical_gender_values = options.pseudo_localize_gender_values.value();
237 } else {
238 grammatical_gender_values = "f,m,n";
239 }
240 if (options.pseudo_localize_gender_ratio) {
241 grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value();
242 } else {
243 grammatical_gender_ratio = "1.0";
244 }
245 PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values,
246 grammatical_gender_ratio);
247 if (!pseudolocale_generator.Consume(context, &table)) {
248 return false;
249 }
250 }
251
252 // Create the file/zip entry.
253 if (!writer->StartEntry(output_path, 0)) {
254 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
255 return false;
256 }
257
258 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
259 {
260 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
261 CopyingOutputStreamAdaptor copying_adaptor(writer);
262 ContainerWriter container_writer(©ing_adaptor, 1u);
263
264 pb::ResourceTable pb_table;
265 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
266 if (!container_writer.AddResTableEntry(pb_table)) {
267 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
268 return false;
269 }
270 }
271
272 if (!writer->FinishEntry()) {
273 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
274 return false;
275 }
276
277 if (options.generate_text_symbols_path) {
278 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
279
280 if (fout_text.HadError()) {
281 context->GetDiagnostics()->Error(android::DiagMessage()
282 << "failed writing to'"
283 << options.generate_text_symbols_path.value()
284 << "': " << fout_text.GetError());
285 return false;
286 }
287
288 Printer r_txt_printer(&fout_text);
289 for (const auto& package : table.packages) {
290 // Only print resources defined locally, e.g. don't write android attributes.
291 if (package->name.empty()) {
292 for (const auto& type : package->types) {
293 for (const auto& entry : type->entries) {
294 // Check access modifiers.
295 switch (entry->visibility.level) {
296 case Visibility::Level::kUndefined :
297 r_txt_printer.Print("default ");
298 break;
299 case Visibility::Level::kPublic :
300 r_txt_printer.Print("public ");
301 break;
302 case Visibility::Level::kPrivate :
303 r_txt_printer.Print("private ");
304 }
305
306 if (type->named_type.type != ResourceType::kStyleable) {
307 r_txt_printer.Print("int ");
308 r_txt_printer.Print(type->named_type.to_string());
309 r_txt_printer.Print(" ");
310 r_txt_printer.Println(entry->name);
311 } else {
312 r_txt_printer.Print("int[] styleable ");
313 r_txt_printer.Println(entry->name);
314
315 if (!entry->values.empty()) {
316 auto styleable =
317 static_cast<const Styleable*>(entry->values.front()->value.get());
318 for (const auto& attr : styleable->entries) {
319 // The visibility of the children under the styleable does not matter as they are
320 // nested under their parent and use its visibility.
321 r_txt_printer.Print("default int styleable ");
322 r_txt_printer.Print(entry->name);
323 // If the package name is present, also include it in the mangled name (e.g.
324 // "android")
325 if (!attr.name.value().package.empty()) {
326 r_txt_printer.Print("_");
327 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
328 }
329 r_txt_printer.Print("_");
330 r_txt_printer.Println(attr.name.value().entry);
331 }
332 }
333 }
334 }
335 }
336 }
337 }
338 }
339
340 return true;
341 }
342
WriteHeaderAndDataToWriter(StringPiece output_path,const ResourceFile & file,android::KnownSizeInputStream * in,IArchiveWriter * writer,android::IDiagnostics * diag)343 static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file,
344 android::KnownSizeInputStream* in, IArchiveWriter* writer,
345 android::IDiagnostics* diag) {
346 TRACE_CALL();
347 // Start the entry so we can write the header.
348 if (!writer->StartEntry(output_path, 0)) {
349 diag->Error(android::DiagMessage(output_path) << "failed to open file");
350 return false;
351 }
352
353 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
354 {
355 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
356 CopyingOutputStreamAdaptor copying_adaptor(writer);
357 ContainerWriter container_writer(©ing_adaptor, 1u);
358
359 pb::internal::CompiledFile pb_compiled_file;
360 SerializeCompiledFileToPb(file, &pb_compiled_file);
361
362 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
363 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
364 return false;
365 }
366 }
367
368 if (!writer->FinishEntry()) {
369 diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
370 return false;
371 }
372 return true;
373 }
374
FlattenXmlToOutStream(StringPiece output_path,const xml::XmlResource & xmlres,ContainerWriter * container_writer,android::IDiagnostics * diag)375 static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres,
376 ContainerWriter* container_writer, android::IDiagnostics* diag) {
377 pb::internal::CompiledFile pb_compiled_file;
378 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
379
380 pb::XmlNode pb_xml_node;
381 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
382
383 std::string serialized_xml = pb_xml_node.SerializeAsString();
384 io::StringInputStream serialized_in(serialized_xml);
385
386 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
387 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
388 return false;
389 }
390 return true;
391 }
392
IsValidFile(IAaptContext * context,const std::string & input_path)393 static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
394 const file::FileType file_type = file::GetFileType(input_path);
395 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
396 if (file_type == file::FileType::kDirectory) {
397 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
398 << "resource file cannot be a directory");
399 } else if (file_type == file::FileType::kNonExistant) {
400 context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
401 } else {
402 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
403 << "not a valid resource file");
404 }
405 return false;
406 }
407 return true;
408 }
409
CompileXml(IAaptContext * context,const CompileOptions & options,const ResourcePathData & path_data,io::IFile * file,IArchiveWriter * writer,const std::string & output_path)410 static bool CompileXml(IAaptContext* context, const CompileOptions& options,
411 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
412 const std::string& output_path) {
413 TRACE_CALL();
414 if (context->IsVerbose()) {
415 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
416 }
417
418 std::unique_ptr<xml::XmlResource> xmlres;
419 {
420 auto fin = file->OpenInputStream();
421 if (fin->HadError()) {
422 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
423 << "failed to open file: " << fin->GetError());
424 return false;
425 }
426
427 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
428 if (!xmlres) {
429 return false;
430 }
431 }
432
433 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
434 xmlres->file.config = path_data.config;
435 xmlres->file.source = path_data.source;
436 xmlres->file.type = ResourceFile::Type::kProtoXml;
437 xmlres->file.flag = ParseFlag(path_data.flag_name);
438
439 if (xmlres->file.flag) {
440 std::string error;
441 auto flag_status = GetFlagStatus(xmlres->file.flag, options.feature_flag_values, &error);
442 if (flag_status) {
443 xmlres->file.flag_status = flag_status.value();
444 } else {
445 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error);
446 return false;
447 }
448 }
449
450 // Collect IDs that are defined here.
451 XmlIdCollector collector;
452 if (!collector.Consume(context, xmlres.get())) {
453 return false;
454 }
455
456 // Look for and process any <aapt:attr> tags and create sub-documents.
457 InlineXmlFormatParser inline_xml_format_parser;
458 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
459 return false;
460 }
461
462 // Start the entry so we can write the header.
463 if (!writer->StartEntry(output_path, 0)) {
464 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
465 return false;
466 }
467
468 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
469 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
470
471 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
472 {
473 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
474 CopyingOutputStreamAdaptor copying_adaptor(writer);
475 ContainerWriter container_writer(©ing_adaptor, 1u + inline_documents.size());
476
477 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
478 context->GetDiagnostics())) {
479 return false;
480 }
481
482 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
483 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
484 context->GetDiagnostics())) {
485 return false;
486 }
487 }
488 }
489
490 if (!writer->FinishEntry()) {
491 context->GetDiagnostics()->Error(android::DiagMessage(output_path)
492 << "failed to finish writing data");
493 return false;
494 }
495
496 if (options.generate_text_symbols_path) {
497 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
498
499 if (fout_text.HadError()) {
500 context->GetDiagnostics()->Error(android::DiagMessage()
501 << "failed writing to'"
502 << options.generate_text_symbols_path.value()
503 << "': " << fout_text.GetError());
504 return false;
505 }
506
507 Printer r_txt_printer(&fout_text);
508 for (const auto& res : xmlres->file.exported_symbols) {
509 r_txt_printer.Print("default int id ");
510 r_txt_printer.Println(res.name.entry);
511 }
512
513 // And print ourselves.
514 r_txt_printer.Print("default int ");
515 r_txt_printer.Print(path_data.resource_dir);
516 r_txt_printer.Print(" ");
517 r_txt_printer.Println(path_data.name);
518 }
519
520 return true;
521 }
522
CompilePng(IAaptContext * context,const CompileOptions & options,const ResourcePathData & path_data,io::IFile * file,IArchiveWriter * writer,const std::string & output_path)523 static bool CompilePng(IAaptContext* context, const CompileOptions& options,
524 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
525 const std::string& output_path) {
526 TRACE_CALL();
527 if (context->IsVerbose()) {
528 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
529 }
530
531 android::BigBuffer buffer(4096);
532 ResourceFile res_file;
533 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
534 res_file.config = path_data.config;
535 res_file.source = path_data.source;
536 res_file.type = ResourceFile::Type::kPng;
537
538 if (!path_data.flag_name.empty()) {
539 FeatureFlagAttribute flag;
540 auto name = path_data.flag_name;
541 if (name.starts_with('!')) {
542 flag.negated = true;
543 flag.name = name.substr(1);
544 } else {
545 flag.name = name;
546 }
547 res_file.flag = flag;
548
549 std::string error;
550 auto flag_status = GetFlagStatus(flag, options.feature_flag_values, &error);
551 if (flag_status) {
552 res_file.flag_status = flag_status.value();
553 } else {
554 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error);
555 return false;
556 }
557 }
558
559 {
560 auto data = file->OpenAsData();
561 if (!data) {
562 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
563 << "failed to open file ");
564 return false;
565 }
566
567 android::BigBuffer crunched_png_buffer(4096);
568 android::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
569
570 // Ensure that we only keep the chunks we care about if we end up
571 // using the original PNG instead of the crunched one.
572 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
573 android::PngChunkFilter png_chunk_filter(content);
574 android::SourcePathDiagnostics source_diag(path_data.source, context->GetDiagnostics());
575 auto image = android::ReadPng(&png_chunk_filter, &source_diag);
576 if (!image) {
577 return false;
578 }
579
580 std::unique_ptr<android::NinePatch> nine_patch;
581 if (path_data.extension == "9.png") {
582 std::string err;
583 nine_patch = android::NinePatch::Create(image->rows.get(), image->width, image->height, &err);
584 if (!nine_patch) {
585 context->GetDiagnostics()->Error(android::DiagMessage() << err);
586 return false;
587 }
588
589 // Remove the 1px border around the NinePatch.
590 // Basically the row array is shifted up by 1, and the length is treated
591 // as height - 2.
592 // For each row, shift the array to the left by 1, and treat the length as
593 // width - 2.
594 image->width -= 2;
595 image->height -= 2;
596 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
597 for (int32_t h = 0; h < image->height; h++) {
598 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
599 }
600
601 if (context->IsVerbose()) {
602 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
603 << "9-patch: " << *nine_patch);
604 }
605 }
606
607 // Write the crunched PNG.
608 if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out,
609 {.compression_level = options.png_compression_level_int}, &source_diag,
610 context->IsVerbose())) {
611 return false;
612 }
613
614 if (nine_patch != nullptr ||
615 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
616 // No matter what, we must use the re-encoded PNG, even if it is larger.
617 // 9-patch images must be re-encoded since their borders are stripped.
618 buffer.AppendBuffer(std::move(crunched_png_buffer));
619 } else {
620 // The re-encoded PNG is larger than the original, and there is
621 // no mandatory transformation. Use the original.
622 if (context->IsVerbose()) {
623 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
624 << "original PNG is smaller than crunched PNG"
625 << ", using original");
626 }
627
628 png_chunk_filter.Rewind();
629 android::BigBuffer filtered_png_buffer(4096);
630 android::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
631 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
632 buffer.AppendBuffer(std::move(filtered_png_buffer));
633 }
634
635 if (context->IsVerbose()) {
636 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
637 // This will help catch exotic cases where the new code may generate larger PNGs.
638 std::stringstream legacy_stream{std::string(content)};
639 android::BigBuffer legacy_buffer(4096);
640 android::Png png(context->GetDiagnostics());
641 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
642 return false;
643 }
644
645 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
646 << "legacy=" << legacy_buffer.size()
647 << " new=" << buffer.size());
648 }
649 }
650
651 android::BigBufferInputStream buffer_in(&buffer);
652 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
653 context->GetDiagnostics());
654 }
655
CompileFile(IAaptContext * context,const CompileOptions & options,const ResourcePathData & path_data,io::IFile * file,IArchiveWriter * writer,const std::string & output_path)656 static bool CompileFile(IAaptContext* context, const CompileOptions& options,
657 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
658 const std::string& output_path) {
659 TRACE_CALL();
660 if (context->IsVerbose()) {
661 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
662 }
663
664 ResourceFile res_file;
665 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
666 res_file.config = path_data.config;
667 res_file.source = path_data.source;
668 res_file.type = ResourceFile::Type::kUnknown;
669
670 auto data = file->OpenAsData();
671 if (!data) {
672 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
673 << "failed to open file ");
674 return false;
675 }
676
677 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
678 context->GetDiagnostics());
679 }
680
681 class CompileContext : public IAaptContext {
682 public:
CompileContext(android::IDiagnostics * diagnostics)683 explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
684 }
685
GetPackageType()686 PackageType GetPackageType() override {
687 // Every compilation unit starts as an app and then gets linked as potentially something else.
688 return PackageType::kApp;
689 }
690
SetVerbose(bool val)691 void SetVerbose(bool val) {
692 verbose_ = val;
693 diagnostics_->SetVerbose(val);
694 }
695
IsVerbose()696 bool IsVerbose() override {
697 return verbose_;
698 }
699
GetDiagnostics()700 android::IDiagnostics* GetDiagnostics() override {
701 return diagnostics_;
702 }
703
GetNameMangler()704 NameMangler* GetNameMangler() override {
705 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
706 return nullptr;
707 }
708
GetCompilationPackage()709 const std::string& GetCompilationPackage() override {
710 static std::string empty;
711 return empty;
712 }
713
GetPackageId()714 uint8_t GetPackageId() override {
715 return 0x0;
716 }
717
GetExternalSymbols()718 SymbolTable* GetExternalSymbols() override {
719 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
720 return nullptr;
721 }
722
GetMinSdkVersion()723 int GetMinSdkVersion() override {
724 return 0;
725 }
726
GetSplitNameDependencies()727 const std::set<std::string>& GetSplitNameDependencies() override {
728 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
729 static std::set<std::string> empty;
730 return empty;
731 }
732
733 private:
734 DISALLOW_COPY_AND_ASSIGN(CompileContext);
735
736 android::IDiagnostics* diagnostics_;
737 bool verbose_ = false;
738 };
739
Compile(IAaptContext * context,io::IFileCollection * inputs,IArchiveWriter * output_writer,CompileOptions & options)740 int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
741 CompileOptions& options) {
742 TRACE_CALL();
743 bool error = false;
744
745 // Iterate over the input files in a stable, platform-independent manner
746 auto file_iterator = inputs->Iterator();
747 while (file_iterator->HasNext()) {
748 auto file = file_iterator->Next();
749 std::string path = file->GetSource().path;
750
751 // Skip hidden input files
752 if (file::IsHidden(path)) {
753 continue;
754 }
755
756 if (!options.res_zip && !IsValidFile(context, path)) {
757 error = true;
758 continue;
759 }
760
761 // Extract resource type information from the full path
762 std::string err_str;
763 ResourcePathData path_data;
764 if (auto maybe_path_data = ExtractResourcePathData(
765 path, inputs->GetDirSeparator(), &err_str, options)) {
766 path_data = maybe_path_data.value();
767 } else {
768 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
769 error = true;
770 continue;
771 }
772
773 // Determine how to compile the file based on its type.
774 auto compile_func = &CompileFile;
775 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
776 compile_func = &CompileTable;
777 // We use a different extension (not necessary anymore, but avoids altering the existing
778 // build system logic).
779 path_data.extension = "arsc";
780
781 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
782 if (*type != ResourceType::kRaw) {
783 if (*type == ResourceType::kXml || path_data.extension == "xml") {
784 compile_func = &CompileXml;
785 } else if ((!options.no_png_crunch && path_data.extension == "png")
786 || path_data.extension == "9.png") {
787 compile_func = &CompilePng;
788 }
789 }
790 } else {
791 context->GetDiagnostics()->Error(android::DiagMessage()
792 << "invalid file path '" << path_data.source << "'");
793 error = true;
794 continue;
795 }
796
797 // Treat periods as a reserved character that should not be present in a file name
798 // Legacy support for AAPT which did not reserve periods
799 if (compile_func != &CompileFile && !options.legacy_mode
800 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
801 error = true;
802 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
803 << "file name cannot contain '.' other than for"
804 << " specifying the extension");
805 continue;
806 }
807
808 const std::string out_path = BuildIntermediateContainerFilename(path_data);
809 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
810 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
811 << "file failed to compile");
812 error = true;
813 }
814 }
815
816 return error ? 1 : 0;
817 }
818
Action(const std::vector<std::string> & args)819 int CompileCommand::Action(const std::vector<std::string>& args) {
820 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
821 CompileContext context(diagnostic_);
822 context.SetVerbose(options_.verbose);
823
824 if (visibility_) {
825 if (visibility_.value() == "public") {
826 options_.visibility = Visibility::Level::kPublic;
827 } else if (visibility_.value() == "private") {
828 options_.visibility = Visibility::Level::kPrivate;
829 } else if (visibility_.value() == "default") {
830 options_.visibility = Visibility::Level::kUndefined;
831 } else {
832 context.GetDiagnostics()->Error(android::DiagMessage()
833 << "Unrecognized visibility level passes to --visibility: '"
834 << visibility_.value()
835 << "'. Accepted levels: public, private, default");
836 return 1;
837 }
838 }
839
840 std::unique_ptr<io::IFileCollection> file_collection;
841
842 // Collect the resources files to compile
843 if (options_.res_dir && options_.res_zip) {
844 context.GetDiagnostics()->Error(android::DiagMessage()
845 << "only one of --dir and --zip can be specified");
846 return 1;
847 } else if ((options_.res_dir || options_.res_zip) &&
848 options_.source_path && args.size() > 1) {
849 context.GetDiagnostics()->Error(android::DiagMessage(kPath)
850 << "Cannot use an overriding source path with multiple files.");
851 return 1;
852 } else if (options_.res_dir) {
853 if (!args.empty()) {
854 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
855 Usage(&std::cerr);
856 return 1;
857 }
858
859 // Load the files from the res directory
860 std::string err;
861 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
862 if (!file_collection) {
863 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
864 return 1;
865 }
866 } else if (options_.res_zip) {
867 if (!args.empty()) {
868 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
869 Usage(&std::cerr);
870 return 1;
871 }
872
873 // Load a zip file containing a res directory
874 std::string err;
875 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
876 if (!file_collection) {
877 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
878 return 1;
879 }
880 } else {
881 auto collection = util::make_unique<io::FileCollection>();
882
883 // Collect data from the path for each input file.
884 std::vector<std::string> sorted_args = args;
885 std::sort(sorted_args.begin(), sorted_args.end());
886
887 for (const std::string& arg : sorted_args) {
888 collection->InsertFile(arg);
889 }
890
891 file_collection = std::move(collection);
892 }
893
894 std::unique_ptr<IArchiveWriter> archive_writer;
895 file::FileType output_file_type = file::GetFileType(options_.output_path);
896 if (output_file_type == file::FileType::kDirectory) {
897 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
898 } else {
899 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
900 }
901
902 if (!archive_writer) {
903 return 1;
904 }
905
906 // Parse the feature flag values. An argument that starts with '@' points to a file to read flag
907 // values from.
908 std::vector<std::string> all_feature_flags_args;
909 for (const std::string& arg : feature_flags_args_) {
910 if (util::StartsWith(arg, "@")) {
911 const std::string path = arg.substr(1, arg.size() - 1);
912 std::string error;
913 if (!file::AppendArgsFromFile(path, &all_feature_flags_args, &error)) {
914 context.GetDiagnostics()->Error(android::DiagMessage(path) << error);
915 return 1;
916 }
917 } else {
918 all_feature_flags_args.push_back(arg);
919 }
920 }
921
922 for (const std::string& arg : all_feature_flags_args) {
923 if (!ParseFeatureFlagsParameter(arg, context.GetDiagnostics(), &options_.feature_flag_values)) {
924 return 1;
925 }
926 }
927
928 if (!options_.png_compression_level) {
929 options_.png_compression_level_int = 9;
930 } else {
931 if (options_.png_compression_level->size() != 1 ||
932 options_.png_compression_level->front() < '0' ||
933 options_.png_compression_level->front() > '9') {
934 context.GetDiagnostics()->Error(
935 android::DiagMessage() << "PNG compression level should be a number in [0..9] range");
936 return 1;
937 }
938 options_.png_compression_level_int = options_.png_compression_level->front() - '0';
939 }
940
941 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
942 }
943
944 } // namespace aapt
945