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