blob: fe560180bd48f7fab8e0ba2dcc96106625c6f515 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ryan Mitchell833a1a62018-07-10 13:51:36 -070017#include "Compile.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Ryan Mitchell833a1a62018-07-10 13:51:36 -070019#include <dirent.h>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <string>
21
Adam Lesinskid5083f62017-01-16 15:07:21 -080022#include "android-base/errors.h"
23#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070024#include "android-base/utf8.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
27#include "google/protobuf/io/coded_stream.h"
28#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "Diagnostics.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "ResourceParser.h"
32#include "ResourceTable.h"
Izabela Orlowska10560192018-04-13 11:56:35 +010033#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070035#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080037#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070039#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070040#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070041#include "format/proto/ProtoSerialize.h"
Adam Lesinski00451162017-10-03 07:44:08 -070042#include "io/BigBufferStream.h"
43#include "io/FileStream.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070044#include "io/FileSystem.h"
Adam Lesinski00451162017-10-03 07:44:08 -070045#include "io/StringStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070046#include "io/Util.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070047#include "io/ZipArchive.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080048#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080051#include "xml/XmlDom.h"
52#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070054using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000055using ::aapt::text::Printer;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020056using ::android::ConfigDescription;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070057using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070058using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070059using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070060
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061namespace aapt {
62
63struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 std::string name;
67 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070069 // Original config str. We keep this because when we parse the config, we may add on
70 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074};
75
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076// Resource file paths are expected to look like: [--/res/]type[-config]/name
Ryan Mitchell4382e442021-07-14 12:53:01 -070077static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path,
78 const char dir_sep,
79 std::string* out_error,
80 const CompileOptions& options) {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070081 std::vector<std::string> parts = util::Split(path, dir_sep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return {};
85 }
86
87 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 size_t dash_pos = dir.find('-');
93 if (dash_pos != std::string::npos) {
94 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
95 if (!ConfigDescription::Parse(config_str, &config)) {
96 if (out_error) {
97 std::stringstream err_str;
98 err_str << "invalid configuration '" << config_str << "'";
99 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 }
101 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 std::string& filename = parts[parts.size() - 1];
107 StringPiece name = filename;
108 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700109
110 const std::string kNinePng = ".9.png";
111 if (filename.size() > kNinePng.size()
112 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
113 // Split on .9.png if this extension is present at the end of the file path
114 name = name.substr(0, filename.size() - kNinePng.size());
115 extension = "9.png";
116 } else {
117 // Split on the last period occurrence
118 size_t dot_pos = filename.rfind('.');
119 if (dot_pos != std::string::npos) {
120 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
121 name = name.substr(0, dot_pos);
122 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
lukeedgar19b8ebf2020-06-23 10:43:42 +0100125 const Source res_path = options.source_path
126 ? StringPiece(options.source_path.value())
127 : StringPiece(path);
128
129 return ResourcePathData{res_path, dir_str.to_string(), name.to_string(),
Adam Lesinskid5083f62017-01-16 15:07:21 -0800130 extension.to_string(), config_str.to_string(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131}
132
Adam Lesinski00451162017-10-03 07:44:08 -0700133static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 name << data.resource_dir;
136 if (!data.config_str.empty()) {
137 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 }
139 name << "_" << data.name;
140 if (!data.extension.empty()) {
141 name << "." << data.extension;
142 }
143 name << ".flat";
144 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800145}
146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700148 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800150 TRACE_CALL();
Mihai Nitad1a65212019-03-26 13:47:45 -0700151 // Filenames starting with "donottranslate" are not localizable
152 bool translatable_file = path_data.name.find("donottranslate") != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 ResourceTable table;
154 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700155 auto fin = file->OpenInputStream();
156 if (fin->HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700158 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160 }
161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 // Parse the values file from XML.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700163 xml::XmlPullParser xml_parser(fin.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 ResourceParserOptions parser_options;
166 parser_options.error_on_positional_arguments = !options.legacy_mode;
Donald Chai94e4a012020-01-06 13:52:41 -0800167 parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables;
Mihai Nitad1a65212019-03-26 13:47:45 -0700168 parser_options.translatable = translatable_file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100170 // If visibility was forced, we need to use it when creating a new resource and also error if
171 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
172 parser_options.visibility = options.visibility;
173
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700174 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700175 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800178 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800180
Mihai Nitad1a65212019-03-26 13:47:45 -0700181 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 // Generate pseudo-localized strings (en-XA and ar-XB).
183 // These are created as weak symbols, and are only generated from default
184 // configuration
185 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 PseudolocaleGenerator pseudolocale_generator;
187 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700189 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700191
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700194 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 return false;
196 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800197
Adam Lesinski00451162017-10-03 07:44:08 -0700198 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 {
Adam Lesinski00451162017-10-03 07:44:08 -0700200 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700202 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700204 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700205 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700206 if (!container_writer.AddResTableEntry(pb_table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700207 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800211
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700213 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return false;
215 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000216
217 if (options.generate_text_symbols_path) {
218 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
219
220 if (fout_text.HadError()) {
221 context->GetDiagnostics()->Error(DiagMessage()
222 << "failed writing to'"
223 << options.generate_text_symbols_path.value()
224 << "': " << fout_text.GetError());
225 return false;
226 }
227
228 Printer r_txt_printer(&fout_text);
229 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100230 // Only print resources defined locally, e.g. don't write android attributes.
231 if (package->name.empty()) {
232 for (const auto& type : package->types) {
233 for (const auto& entry : type->entries) {
234 // Check access modifiers.
235 switch (entry->visibility.level) {
236 case Visibility::Level::kUndefined :
237 r_txt_printer.Print("default ");
238 break;
239 case Visibility::Level::kPublic :
240 r_txt_printer.Print("public ");
241 break;
242 case Visibility::Level::kPrivate :
243 r_txt_printer.Print("private ");
244 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000245
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100246 if (type->type != ResourceType::kStyleable) {
247 r_txt_printer.Print("int ");
248 r_txt_printer.Print(to_string(type->type));
249 r_txt_printer.Print(" ");
250 r_txt_printer.Println(entry->name);
251 } else {
252 r_txt_printer.Print("int[] styleable ");
253 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000254
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100255 if (!entry->values.empty()) {
256 auto styleable =
257 static_cast<const Styleable*>(entry->values.front()->value.get());
258 for (const auto& attr : styleable->entries) {
259 // The visibility of the children under the styleable does not matter as they are
260 // nested under their parent and use its visibility.
261 r_txt_printer.Print("default int styleable ");
262 r_txt_printer.Print(entry->name);
263 // If the package name is present, also include it in the mangled name (e.g.
264 // "android")
265 if (!attr.name.value().package.empty()) {
266 r_txt_printer.Print("_");
267 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
268 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100269 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100270 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100271 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000272 }
273 }
274 }
275 }
276 }
277 }
278 }
279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800281}
282
Adam Lesinski00451162017-10-03 07:44:08 -0700283static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
284 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800285 IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800286 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 if (!writer->StartEntry(output_path, 0)) {
289 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 return false;
291 }
292
Adam Lesinski00451162017-10-03 07:44:08 -0700293 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 {
Adam Lesinski00451162017-10-03 07:44:08 -0700295 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700297 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700299 pb::internal::CompiledFile pb_compiled_file;
300 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301
Adam Lesinski00451162017-10-03 07:44:08 -0700302 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
303 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800305 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 if (!writer->FinishEntry()) {
309 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 return false;
311 }
312 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700313}
314
Adam Lesinski00451162017-10-03 07:44:08 -0700315static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
316 ContainerWriter* container_writer, IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700317 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700318 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700319
Adam Lesinski00451162017-10-03 07:44:08 -0700320 pb::XmlNode pb_xml_node;
321 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
322
323 std::string serialized_xml = pb_xml_node.SerializeAsString();
324 io::StringInputStream serialized_in(serialized_xml);
325
326 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
327 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 return false;
329 }
330 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700331}
332
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700333static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700334 const file::FileType file_type = file::GetFileType(input_path);
335 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
336 if (file_type == file::FileType::kDirectory) {
337 context->GetDiagnostics()->Error(DiagMessage(input_path)
338 << "resource file cannot be a directory");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700339 } else if (file_type == file::FileType::kNonExistant) {
Adam Lesinskicc73e992017-05-12 18:16:44 -0700340 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700341 } else {
342 context->GetDiagnostics()->Error(DiagMessage(input_path)
343 << "not a valid resource file");
344 }
345 return false;
346 }
347 return true;
348}
349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700351 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700352 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800353 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700355 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 }
357
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700358 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700360 auto fin = file->OpenInputStream();
361 if (fin->HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700363 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700365 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700366
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700367 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
368 if (!xmlres) {
369 return false;
370 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 }
372
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700373 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 xmlres->file.config = path_data.config;
375 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700376 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377
378 // Collect IDs that are defined here.
379 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700381 return false;
382 }
383
384 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 InlineXmlFormatParser inline_xml_format_parser;
386 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 return false;
388 }
389
390 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700392 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 return false;
394 }
395
Adam Lesinski00451162017-10-03 07:44:08 -0700396 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
397 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
398
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700399 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700400 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700401 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700403 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404
Adam Lesinski00451162017-10-03 07:44:08 -0700405 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
406 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700408 }
409
Adam Lesinski00451162017-10-03 07:44:08 -0700410 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
411 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
412 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700413 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700415 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700419 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 return false;
421 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000422
423 if (options.generate_text_symbols_path) {
424 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
425
426 if (fout_text.HadError()) {
427 context->GetDiagnostics()->Error(DiagMessage()
428 << "failed writing to'"
429 << options.generate_text_symbols_path.value()
430 << "': " << fout_text.GetError());
431 return false;
432 }
433
434 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800435 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000436 r_txt_printer.Print("default int id ");
437 r_txt_printer.Println(res.name.entry);
438 }
439
440 // And print ourselves.
441 r_txt_printer.Print("default int ");
442 r_txt_printer.Print(path_data.resource_dir);
443 r_txt_printer.Print(" ");
444 r_txt_printer.Println(path_data.name);
445 }
446
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700448}
449
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700450static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700451 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700452 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800453 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700455 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 }
457
458 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700460 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 res_file.config = path_data.config;
462 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700463 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464
465 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700466 auto data = file->OpenAsData();
467 if (!data) {
468 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700470 }
471
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700473 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700474
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 // Ensure that we only keep the chunks we care about if we end up
476 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800477 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700479 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 if (!image) {
481 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700482 }
483
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 std::unique_ptr<NinePatch> nine_patch;
485 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700487 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 if (!nine_patch) {
489 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700490 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 }
492
493 // Remove the 1px border around the NinePatch.
494 // Basically the row array is shifted up by 1, and the length is treated
495 // as height - 2.
496 // For each row, shift the array to the left by 1, and treat the length as
497 // width - 2.
498 image->width -= 2;
499 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700500 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 for (int32_t h = 0; h < image->height; h++) {
502 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
503 }
504
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700505 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700506 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
507 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700509 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510
511 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700512 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700513 return false;
514 }
515
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 if (nine_patch != nullptr ||
517 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 // No matter what, we must use the re-encoded PNG, even if it is larger.
519 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 } else {
522 // The re-encoded PNG is larger than the original, and there is
523 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700525 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
526 << "original PNG is smaller than crunched PNG"
527 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 }
529
Adam Lesinski06460ef2017-03-14 18:52:13 -0700530 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700532 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
533 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 }
536
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700538 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
539 // This will help catch exotic cases where the new code may generate larger PNGs.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700540 std::stringstream legacy_stream(content.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 BigBuffer legacy_buffer(4096);
542 Png png(context->GetDiagnostics());
543 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 return false;
545 }
546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
548 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 << " new=" << buffer.size());
550 }
551 }
552
Adam Lesinski00451162017-10-03 07:44:08 -0700553 io::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700554 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
555 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700556}
557
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700558static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700559 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800561 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700563 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700565
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700567 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 res_file.config = path_data.config;
569 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700570 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700571
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700572 auto data = file->OpenAsData();
573 if (!data) {
574 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 return false;
576 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700577
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700578 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
579 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700580}
581
582class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 public:
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800584 explicit CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100585 }
586
Adam Lesinskib522f042017-04-21 16:57:59 -0700587 PackageType GetPackageType() override {
588 // Every compilation unit starts as an app and then gets linked as potentially something else.
589 return PackageType::kApp;
590 }
591
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700592 void SetVerbose(bool val) {
593 verbose_ = val;
594 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800595
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700596 bool IsVerbose() override {
597 return verbose_;
598 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800599
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700600 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100601 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700602 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700604 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700605 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606 return nullptr;
607 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700608
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700609 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 static std::string empty;
611 return empty;
612 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700613
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700614 uint8_t GetPackageId() override {
615 return 0x0;
616 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700617
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700619 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700620 return nullptr;
621 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800622
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700623 int GetMinSdkVersion() override {
624 return 0;
625 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700626
Udam Sainib228df32019-06-18 16:50:34 -0700627 const std::set<std::string>& GetSplitNameDependencies() override {
628 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
629 static std::set<std::string> empty;
630 return empty;
631 }
632
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700633 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700634 DISALLOW_COPY_AND_ASSIGN(CompileContext);
635
Chris Warrington820d72a2017-04-27 15:27:01 +0100636 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638};
639
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700640int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
641 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800642 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700643 bool error = false;
644
645 // Iterate over the input files in a stable, platform-independent manner
646 auto file_iterator = inputs->Iterator();
647 while (file_iterator->HasNext()) {
648 auto file = file_iterator->Next();
649 std::string path = file->GetSource().path;
650
651 // Skip hidden input files
652 if (file::IsHidden(path)) {
653 continue;
654 }
655
656 if (!options.res_zip && !IsValidFile(context, path)) {
657 error = true;
658 continue;
659 }
660
661 // Extract resource type information from the full path
662 std::string err_str;
663 ResourcePathData path_data;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100664 if (auto maybe_path_data = ExtractResourcePathData(
665 path, inputs->GetDirSeparator(), &err_str, options)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700666 path_data = maybe_path_data.value();
667 } else {
668 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << err_str);
669 error = true;
670 continue;
671 }
672
673 // Determine how to compile the file based on its type.
674 auto compile_func = &CompileFile;
675 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
676 compile_func = &CompileTable;
677 // We use a different extension (not necessary anymore, but avoids altering the existing
678 // build system logic).
679 path_data.extension = "arsc";
680
681 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
682 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700683 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700684 compile_func = &CompileXml;
685 } else if ((!options.no_png_crunch && path_data.extension == "png")
686 || path_data.extension == "9.png") {
687 compile_func = &CompilePng;
688 }
689 }
690 } else {
691 context->GetDiagnostics()->Error(DiagMessage()
692 << "invalid file path '" << path_data.source << "'");
693 error = true;
694 continue;
695 }
696
697 // Treat periods as a reserved character that should not be present in a file name
698 // Legacy support for AAPT which did not reserve periods
699 if (compile_func != &CompileFile && !options.legacy_mode
700 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
701 error = true;
702 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
703 << "file name cannot contain '.' other than for"
704 << " specifying the extension");
705 continue;
706 }
707
708 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000709 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
710 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "file failed to compile");
711 error = true;
712 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700713 }
714
715 return error ? 1 : 0;
716}
717
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700718int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800719 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700720 CompileContext context(diagnostic_);
721 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700722
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700723 if (visibility_) {
724 if (visibility_.value() == "public") {
725 options_.visibility = Visibility::Level::kPublic;
726 } else if (visibility_.value() == "private") {
727 options_.visibility = Visibility::Level::kPrivate;
728 } else if (visibility_.value() == "default") {
729 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100730 } else {
731 context.GetDiagnostics()->Error(
732 DiagMessage() << "Unrecognized visibility level passes to --visibility: '"
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700733 << visibility_.value() << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100734 return 1;
735 }
736 }
737
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700738 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700739
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700740 // Collect the resources files to compile
741 if (options_.res_dir && options_.res_zip) {
742 context.GetDiagnostics()->Error(DiagMessage()
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700743 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700744 return 1;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100745 } else if ((options_.res_dir || options_.res_zip) &&
746 options_.source_path && args.size() > 1) {
747 context.GetDiagnostics()->Error(DiagMessage(kPath)
748 << "Cannot use an overriding source path with multiple files.");
749 return 1;
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700750 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700751 if (!args.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700752 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700753 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700755 }
756
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700757 // 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);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 return 1;
763 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700764 } 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 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700770
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700771 // 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 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700779 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800780
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800782 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) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700786 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800788
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700789 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700790 }
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) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700795 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700796 } else {
797 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 }
799
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700801 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 }
803
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700804 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700805}
806
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807} // namespace aapt