blob: 728ba8aa4fdddf6f65f3f1393c49cf782489921f [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>
Jeremy Meyer56f36e82022-05-20 20:35:42 +000020
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <string>
22
Jeremy Meyer56f36e82022-05-20 20:35:42 +000023#include "ResourceParser.h"
24#include "ResourceTable.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "android-base/errors.h"
26#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070027#include "android-base/utf8.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020028#include "androidfw/ConfigDescription.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000029#include "androidfw/IDiagnostics.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080030#include "androidfw/StringPiece.h"
Izabela Orlowska10560192018-04-13 11:56:35 +010031#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070033#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080035#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070037#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070038#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070039#include "format/proto/ProtoSerialize.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000040#include "google/protobuf/io/coded_stream.h"
41#include "google/protobuf/io/zero_copy_stream_impl_lite.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"
Inseob Kim5fe521e2023-09-15 16:38:50 +090048#include "process/ProductFilter.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080049#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080052#include "xml/XmlDom.h"
53#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070055using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000056using ::aapt::text::Printer;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020057using ::android::ConfigDescription;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070058using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070059using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070060using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070061
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062namespace aapt {
63
64struct ResourcePathData {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000065 android::Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 std::string name;
68 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070070 // 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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075};
76
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070077// Resource file paths are expected to look like: [--/res/]type[-config]/name
Ryan Mitchell4382e442021-07-14 12:53:01 -070078static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path,
79 const char dir_sep,
80 std::string* out_error,
81 const CompileOptions& options) {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070082 std::vector<std::string> parts = util::Split(path, dir_sep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 return {};
86 }
87
88 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 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();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 }
102 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 std::string& filename = parts[parts.size() - 1];
108 StringPiece name = filename;
109 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700110
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 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000126 const android::Source res_path =
127 options.source_path ? StringPiece(options.source_path.value()) : StringPiece(path);
lukeedgar19b8ebf2020-06-23 10:43:42 +0100128
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700129 return ResourcePathData{res_path,
130 std::string(dir_str),
131 std::string(name),
132 std::string(extension),
133 std::string(config_str),
134 config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135}
136
Adam Lesinski00451162017-10-03 07:44:08 -0700137static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 name << data.resource_dir;
140 if (!data.config_str.empty()) {
141 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 }
143 name << "_" << data.name;
144 if (!data.extension.empty()) {
145 name << "." << data.extension;
146 }
147 name << ".flat";
148 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800149}
150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700152 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800154 TRACE_CALL();
Mihai Nitad1a65212019-03-26 13:47:45 -0700155 // Filenames starting with "donottranslate" are not localizable
156 bool translatable_file = path_data.name.find("donottranslate") != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 ResourceTable table;
158 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700159 auto fin = file->OpenInputStream();
160 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000161 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
162 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164 }
165
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 // Parse the values file from XML.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700167 xml::XmlPullParser xml_parser(fin.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 ResourceParserOptions parser_options;
170 parser_options.error_on_positional_arguments = !options.legacy_mode;
Donald Chai94e4a012020-01-06 13:52:41 -0800171 parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables;
Mihai Nitad1a65212019-03-26 13:47:45 -0700172 parser_options.translatable = translatable_file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100174 // If visibility was forced, we need to use it when creating a new resource and also error if
175 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
176 parser_options.visibility = options.visibility;
177
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700178 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700179 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800182 }
Inseob Kim5fe521e2023-09-15 16:38:50 +0900183
184 if (options.product_.has_value()) {
185 if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true)
186 .Consume(context, &table)) {
187 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
188 << "failed to filter product");
189 return false;
190 }
191 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800193
Mihai Nitad1a65212019-03-26 13:47:45 -0700194 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 // Generate pseudo-localized strings (en-XA and ar-XB).
196 // These are created as weak symbols, and are only generated from default
197 // configuration
198 // strings and plurals.
Brandon Liu5274e062023-05-25 22:41:10 +0000199 std::string grammatical_gender_values;
200 std::string grammatical_gender_ratio;
201 if (options.pseudo_localize_gender_values) {
202 grammatical_gender_values = options.pseudo_localize_gender_values.value();
203 } else {
204 grammatical_gender_values = "f,m,n";
205 }
206 if (options.pseudo_localize_gender_ratio) {
207 grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value();
208 } else {
209 grammatical_gender_ratio = "1.0";
210 }
211 PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values,
212 grammatical_gender_ratio);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700215 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700217
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000220 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 return false;
222 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800223
Adam Lesinski00451162017-10-03 07:44:08 -0700224 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 {
Adam Lesinski00451162017-10-03 07:44:08 -0700226 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700228 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700230 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700231 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700232 if (!container_writer.AddResTableEntry(pb_table)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000233 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000239 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 return false;
241 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000242
243 if (options.generate_text_symbols_path) {
244 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
245
246 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000247 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000248 << "failed writing to'"
249 << options.generate_text_symbols_path.value()
250 << "': " << fout_text.GetError());
251 return false;
252 }
253
254 Printer r_txt_printer(&fout_text);
255 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100256 // Only print resources defined locally, e.g. don't write android attributes.
257 if (package->name.empty()) {
258 for (const auto& type : package->types) {
259 for (const auto& entry : type->entries) {
260 // Check access modifiers.
261 switch (entry->visibility.level) {
262 case Visibility::Level::kUndefined :
263 r_txt_printer.Print("default ");
264 break;
265 case Visibility::Level::kPublic :
266 r_txt_printer.Print("public ");
267 break;
268 case Visibility::Level::kPrivate :
269 r_txt_printer.Print("private ");
270 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000271
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000272 if (type->named_type.type != ResourceType::kStyleable) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100273 r_txt_printer.Print("int ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000274 r_txt_printer.Print(type->named_type.to_string());
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100275 r_txt_printer.Print(" ");
276 r_txt_printer.Println(entry->name);
277 } else {
278 r_txt_printer.Print("int[] styleable ");
279 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000280
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100281 if (!entry->values.empty()) {
282 auto styleable =
283 static_cast<const Styleable*>(entry->values.front()->value.get());
284 for (const auto& attr : styleable->entries) {
285 // The visibility of the children under the styleable does not matter as they are
286 // nested under their parent and use its visibility.
287 r_txt_printer.Print("default int styleable ");
288 r_txt_printer.Print(entry->name);
289 // If the package name is present, also include it in the mangled name (e.g.
290 // "android")
291 if (!attr.name.value().package.empty()) {
292 r_txt_printer.Print("_");
293 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
294 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100295 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100296 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100297 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000298 }
299 }
300 }
301 }
302 }
303 }
304 }
305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800307}
308
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700309static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file,
Adam Lesinski00451162017-10-03 07:44:08 -0700310 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000311 android::IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800312 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000315 diag->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return false;
317 }
318
Adam Lesinski00451162017-10-03 07:44:08 -0700319 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 {
Adam Lesinski00451162017-10-03 07:44:08 -0700321 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700323 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700325 pb::internal::CompiledFile pb_compiled_file;
326 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327
Adam Lesinski00451162017-10-03 07:44:08 -0700328 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000329 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800331 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000335 diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700336 return false;
337 }
338 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700339}
340
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700341static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000342 ContainerWriter* container_writer, android::IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700343 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700344 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700345
Adam Lesinski00451162017-10-03 07:44:08 -0700346 pb::XmlNode pb_xml_node;
347 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
348
349 std::string serialized_xml = pb_xml_node.SerializeAsString();
350 io::StringInputStream serialized_in(serialized_xml);
351
352 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000353 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 return false;
355 }
356 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700357}
358
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700359static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700360 const file::FileType file_type = file::GetFileType(input_path);
361 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
362 if (file_type == file::FileType::kDirectory) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000363 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700364 << "resource file cannot be a directory");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700365 } else if (file_type == file::FileType::kNonExistant) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000366 context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700367 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000368 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700369 << "not a valid resource file");
370 }
371 return false;
372 }
373 return true;
374}
375
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700376static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700377 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700378 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800379 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000381 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 }
383
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700384 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700385 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700386 auto fin = file->OpenInputStream();
387 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000388 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700389 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700391 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700392
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700393 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
394 if (!xmlres) {
395 return false;
396 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 }
398
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700399 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700400 xmlres->file.config = path_data.config;
401 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700402 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403
404 // Collect IDs that are defined here.
405 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700406 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700407 return false;
408 }
409
410 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 InlineXmlFormatParser inline_xml_format_parser;
412 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 return false;
414 }
415
416 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000418 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 return false;
420 }
421
Adam Lesinski00451162017-10-03 07:44:08 -0700422 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
423 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
424
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700425 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700427 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700429 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430
Adam Lesinski00451162017-10-03 07:44:08 -0700431 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
432 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700434 }
435
Adam Lesinski00451162017-10-03 07:44:08 -0700436 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
437 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
438 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700439 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700441 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700443
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700444 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000445 context->GetDiagnostics()->Error(android::DiagMessage(output_path)
446 << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 return false;
448 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000449
450 if (options.generate_text_symbols_path) {
451 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
452
453 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000454 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000455 << "failed writing to'"
456 << options.generate_text_symbols_path.value()
457 << "': " << fout_text.GetError());
458 return false;
459 }
460
461 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800462 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000463 r_txt_printer.Print("default int id ");
464 r_txt_printer.Println(res.name.entry);
465 }
466
467 // And print ourselves.
468 r_txt_printer.Print("default int ");
469 r_txt_printer.Print(path_data.resource_dir);
470 r_txt_printer.Print(" ");
471 r_txt_printer.Println(path_data.name);
472 }
473
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700475}
476
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700478 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700479 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800480 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700481 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000482 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 }
484
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000485 android::BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700486 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700487 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 res_file.config = path_data.config;
489 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700490 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491
492 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700493 auto data = file->OpenAsData();
494 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000495 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
496 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700498 }
499
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000500 android::BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700501 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 // Ensure that we only keep the chunks we care about if we end up
504 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800505 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700507 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 if (!image) {
509 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700510 }
511
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 std::unique_ptr<NinePatch> nine_patch;
513 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700515 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 if (!nine_patch) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000517 context->GetDiagnostics()->Error(android::DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700518 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 }
520
521 // Remove the 1px border around the NinePatch.
522 // Basically the row array is shifted up by 1, and the length is treated
523 // as height - 2.
524 // For each row, shift the array to the left by 1, and treat the length as
525 // width - 2.
526 image->width -= 2;
527 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700528 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 for (int32_t h = 0; h < image->height; h++) {
530 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
531 }
532
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000534 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
535 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538
539 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700540 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 return false;
542 }
543
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 if (nine_patch != nullptr ||
545 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 // No matter what, we must use the re-encoded PNG, even if it is larger.
547 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 } else {
550 // The re-encoded PNG is larger than the original, and there is
551 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000553 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700554 << "original PNG is smaller than crunched PNG"
555 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 }
557
Adam Lesinski06460ef2017-03-14 18:52:13 -0700558 png_chunk_filter.Rewind();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000559 android::BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700560 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
561 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 }
564
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700566 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
567 // This will help catch exotic cases where the new code may generate larger PNGs.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700568 std::stringstream legacy_stream{std::string(content)};
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000569 android::BigBuffer legacy_buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700570 Png png(context->GetDiagnostics());
571 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572 return false;
573 }
574
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000575 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700577 << " new=" << buffer.size());
578 }
579 }
580
Adam Lesinski00451162017-10-03 07:44:08 -0700581 io::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700582 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
583 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700584}
585
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700587 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700588 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800589 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000591 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700592 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700595 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 res_file.config = path_data.config;
597 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700598 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700600 auto data = file->OpenAsData();
601 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000602 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
603 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 return false;
605 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700607 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
608 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609}
610
611class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000613 explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100614 }
615
Adam Lesinskib522f042017-04-21 16:57:59 -0700616 PackageType GetPackageType() override {
617 // Every compilation unit starts as an app and then gets linked as potentially something else.
618 return PackageType::kApp;
619 }
620
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700621 void SetVerbose(bool val) {
622 verbose_ = val;
Brandon Liu48d229d2023-05-04 23:54:03 +0000623 diagnostics_->SetVerbose(val);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700624 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800625
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700626 bool IsVerbose() override {
627 return verbose_;
628 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800629
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000630 android::IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100631 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700632 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700635 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700636 return nullptr;
637 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700638
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 static std::string empty;
641 return empty;
642 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700643
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700644 uint8_t GetPackageId() override {
645 return 0x0;
646 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700647
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700648 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700649 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700650 return nullptr;
651 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800652
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700653 int GetMinSdkVersion() override {
654 return 0;
655 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700656
Udam Sainib228df32019-06-18 16:50:34 -0700657 const std::set<std::string>& GetSplitNameDependencies() override {
658 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
659 static std::set<std::string> empty;
660 return empty;
661 }
662
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700663 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700664 DISALLOW_COPY_AND_ASSIGN(CompileContext);
665
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000666 android::IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700667 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700668};
669
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700670int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
671 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800672 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700673 bool error = false;
674
675 // Iterate over the input files in a stable, platform-independent manner
676 auto file_iterator = inputs->Iterator();
677 while (file_iterator->HasNext()) {
678 auto file = file_iterator->Next();
679 std::string path = file->GetSource().path;
680
681 // Skip hidden input files
682 if (file::IsHidden(path)) {
683 continue;
684 }
685
686 if (!options.res_zip && !IsValidFile(context, path)) {
687 error = true;
688 continue;
689 }
690
691 // Extract resource type information from the full path
692 std::string err_str;
693 ResourcePathData path_data;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100694 if (auto maybe_path_data = ExtractResourcePathData(
695 path, inputs->GetDirSeparator(), &err_str, options)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700696 path_data = maybe_path_data.value();
697 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000698 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700699 error = true;
700 continue;
701 }
702
703 // Determine how to compile the file based on its type.
704 auto compile_func = &CompileFile;
705 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
706 compile_func = &CompileTable;
707 // We use a different extension (not necessary anymore, but avoids altering the existing
708 // build system logic).
709 path_data.extension = "arsc";
710
711 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
712 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700713 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700714 compile_func = &CompileXml;
715 } else if ((!options.no_png_crunch && path_data.extension == "png")
716 || path_data.extension == "9.png") {
717 compile_func = &CompilePng;
718 }
719 }
720 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000721 context->GetDiagnostics()->Error(android::DiagMessage()
722 << "invalid file path '" << path_data.source << "'");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700723 error = true;
724 continue;
725 }
726
727 // Treat periods as a reserved character that should not be present in a file name
728 // Legacy support for AAPT which did not reserve periods
729 if (compile_func != &CompileFile && !options.legacy_mode
730 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
731 error = true;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000732 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
733 << "file name cannot contain '.' other than for"
734 << " specifying the extension");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700735 continue;
736 }
737
738 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000739 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000740 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
741 << "file failed to compile");
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000742 error = true;
743 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700744 }
745
746 return error ? 1 : 0;
747}
748
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700749int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800750 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700751 CompileContext context(diagnostic_);
752 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700753
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700754 if (visibility_) {
755 if (visibility_.value() == "public") {
756 options_.visibility = Visibility::Level::kPublic;
757 } else if (visibility_.value() == "private") {
758 options_.visibility = Visibility::Level::kPrivate;
759 } else if (visibility_.value() == "default") {
760 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100761 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000762 context.GetDiagnostics()->Error(android::DiagMessage()
763 << "Unrecognized visibility level passes to --visibility: '"
764 << visibility_.value()
765 << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100766 return 1;
767 }
768 }
769
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700770 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700771
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700772 // Collect the resources files to compile
773 if (options_.res_dir && options_.res_zip) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000774 context.GetDiagnostics()->Error(android::DiagMessage()
775 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700776 return 1;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100777 } else if ((options_.res_dir || options_.res_zip) &&
778 options_.source_path && args.size() > 1) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000779 context.GetDiagnostics()->Error(android::DiagMessage(kPath)
780 << "Cannot use an overriding source path with multiple files.");
781 return 1;
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700782 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700783 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000784 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700785 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700786 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700787 }
788
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700789 // Load the files from the res directory
790 std::string err;
791 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
792 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000793 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700794 return 1;
795 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700796 } else if (options_.res_zip) {
797 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000798 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700799 Usage(&std::cerr);
800 return 1;
801 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700802
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700803 // Load a zip file containing a res directory
804 std::string err;
805 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
806 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000807 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700808 return 1;
809 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700811 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800812
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800814 std::vector<std::string> sorted_args = args;
815 std::sort(sorted_args.begin(), sorted_args.end());
816
817 for (const std::string& arg : sorted_args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700818 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700819 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800820
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700821 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700822 }
823
824 std::unique_ptr<IArchiveWriter> archive_writer;
825 file::FileType output_file_type = file::GetFileType(options_.output_path);
826 if (output_file_type == file::FileType::kDirectory) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700827 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700828 } else {
829 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700830 }
831
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700832 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700833 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834 }
835
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700836 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700837}
838
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839} // namespace aapt