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