blob: 9b8c3b3d549c40880af7bb0f8c7e3e757c5d5f39 [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"
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000028#include "androidfw/BigBufferStream.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020029#include "androidfw/ConfigDescription.h"
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000030#include "androidfw/FileStream.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031#include "androidfw/IDiagnostics.h"
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000032#include "androidfw/Image.h"
33#include "androidfw/Png.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080034#include "androidfw/StringPiece.h"
Izabela Orlowska10560192018-04-13 11:56:35 +010035#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070037#include "compile/InlineXmlFormatParser.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080038#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070040#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070041#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070042#include "format/proto/ProtoSerialize.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000043#include "google/protobuf/io/coded_stream.h"
44#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070045#include "io/FileSystem.h"
Adam Lesinski00451162017-10-03 07:44:08 -070046#include "io/StringStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070047#include "io/Util.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070048#include "io/ZipArchive.h"
Inseob Kim5fe521e2023-09-15 16:38:50 +090049#include "process/ProductFilter.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080050#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080053#include "xml/XmlDom.h"
54#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000056using ::aapt::text::Printer;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020057using ::android::ConfigDescription;
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000058using ::android::FileInputStream;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070059using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070060using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070061using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070062
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063namespace aapt {
64
65struct ResourcePathData {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000066 android::Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 std::string name;
69 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070071 // Original config str. We keep this because when we parse the config, we may add on
72 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076};
77
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070078// Resource file paths are expected to look like: [--/res/]type[-config]/name
Ryan Mitchell4382e442021-07-14 12:53:01 -070079static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path,
80 const char dir_sep,
81 std::string* out_error,
82 const CompileOptions& options) {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070083 std::vector<std::string> parts = util::Split(path, dir_sep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 return {};
87 }
88
89 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 size_t dash_pos = dir.find('-');
95 if (dash_pos != std::string::npos) {
96 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
97 if (!ConfigDescription::Parse(config_str, &config)) {
98 if (out_error) {
99 std::stringstream err_str;
100 err_str << "invalid configuration '" << config_str << "'";
101 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 }
103 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 std::string& filename = parts[parts.size() - 1];
109 StringPiece name = filename;
110 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700111
112 const std::string kNinePng = ".9.png";
113 if (filename.size() > kNinePng.size()
114 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
115 // Split on .9.png if this extension is present at the end of the file path
116 name = name.substr(0, filename.size() - kNinePng.size());
117 extension = "9.png";
118 } else {
119 // Split on the last period occurrence
120 size_t dot_pos = filename.rfind('.');
121 if (dot_pos != std::string::npos) {
122 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
123 name = name.substr(0, dot_pos);
124 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000127 const android::Source res_path =
128 options.source_path ? StringPiece(options.source_path.value()) : StringPiece(path);
lukeedgar19b8ebf2020-06-23 10:43:42 +0100129
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700130 return ResourcePathData{res_path,
131 std::string(dir_str),
132 std::string(name),
133 std::string(extension),
134 std::string(config_str),
135 config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136}
137
Adam Lesinski00451162017-10-03 07:44:08 -0700138static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 name << data.resource_dir;
141 if (!data.config_str.empty()) {
142 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 }
144 name << "_" << data.name;
145 if (!data.extension.empty()) {
146 name << "." << data.extension;
147 }
148 name << ".flat";
149 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800150}
151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700153 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800155 TRACE_CALL();
Mihai Nitad1a65212019-03-26 13:47:45 -0700156 // Filenames starting with "donottranslate" are not localizable
157 bool translatable_file = path_data.name.find("donottranslate") != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 ResourceTable table;
159 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700160 auto fin = file->OpenInputStream();
161 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000162 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
163 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165 }
166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 // Parse the values file from XML.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700168 xml::XmlPullParser xml_parser(fin.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 ResourceParserOptions parser_options;
171 parser_options.error_on_positional_arguments = !options.legacy_mode;
Donald Chai94e4a012020-01-06 13:52:41 -0800172 parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables;
Mihai Nitad1a65212019-03-26 13:47:45 -0700173 parser_options.translatable = translatable_file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100175 // If visibility was forced, we need to use it when creating a new resource and also error if
176 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
177 parser_options.visibility = options.visibility;
178
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700179 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700180 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800183 }
Inseob Kim5fe521e2023-09-15 16:38:50 +0900184
185 if (options.product_.has_value()) {
186 if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true)
187 .Consume(context, &table)) {
188 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
189 << "failed to filter product");
190 return false;
191 }
192 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800194
Mihai Nitad1a65212019-03-26 13:47:45 -0700195 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 // Generate pseudo-localized strings (en-XA and ar-XB).
197 // These are created as weak symbols, and are only generated from default
198 // configuration
199 // strings and plurals.
Brandon Liu5274e062023-05-25 22:41:10 +0000200 std::string grammatical_gender_values;
201 std::string grammatical_gender_ratio;
202 if (options.pseudo_localize_gender_values) {
203 grammatical_gender_values = options.pseudo_localize_gender_values.value();
204 } else {
205 grammatical_gender_values = "f,m,n";
206 }
207 if (options.pseudo_localize_gender_ratio) {
208 grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value();
209 } else {
210 grammatical_gender_ratio = "1.0";
211 }
212 PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values,
213 grammatical_gender_ratio);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700216 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700218
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000221 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 return false;
223 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800224
Adam Lesinski00451162017-10-03 07:44:08 -0700225 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 {
Adam Lesinski00451162017-10-03 07:44:08 -0700227 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700229 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700231 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700232 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700233 if (!container_writer.AddResTableEntry(pb_table)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000234 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800238
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700239 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000240 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 return false;
242 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000243
244 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000245 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000246
247 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000248 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000249 << "failed writing to'"
250 << options.generate_text_symbols_path.value()
251 << "': " << fout_text.GetError());
252 return false;
253 }
254
255 Printer r_txt_printer(&fout_text);
256 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100257 // Only print resources defined locally, e.g. don't write android attributes.
258 if (package->name.empty()) {
259 for (const auto& type : package->types) {
260 for (const auto& entry : type->entries) {
261 // Check access modifiers.
262 switch (entry->visibility.level) {
263 case Visibility::Level::kUndefined :
264 r_txt_printer.Print("default ");
265 break;
266 case Visibility::Level::kPublic :
267 r_txt_printer.Print("public ");
268 break;
269 case Visibility::Level::kPrivate :
270 r_txt_printer.Print("private ");
271 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000272
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000273 if (type->named_type.type != ResourceType::kStyleable) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100274 r_txt_printer.Print("int ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000275 r_txt_printer.Print(type->named_type.to_string());
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100276 r_txt_printer.Print(" ");
277 r_txt_printer.Println(entry->name);
278 } else {
279 r_txt_printer.Print("int[] styleable ");
280 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000281
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100282 if (!entry->values.empty()) {
283 auto styleable =
284 static_cast<const Styleable*>(entry->values.front()->value.get());
285 for (const auto& attr : styleable->entries) {
286 // The visibility of the children under the styleable does not matter as they are
287 // nested under their parent and use its visibility.
288 r_txt_printer.Print("default int styleable ");
289 r_txt_printer.Print(entry->name);
290 // If the package name is present, also include it in the mangled name (e.g.
291 // "android")
292 if (!attr.name.value().package.empty()) {
293 r_txt_printer.Print("_");
294 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
295 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100296 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100297 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100298 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000299 }
300 }
301 }
302 }
303 }
304 }
305 }
306
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800308}
309
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700310static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file,
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000311 android::KnownSizeInputStream* in, IArchiveWriter* writer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000312 android::IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800313 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000316 diag->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 return false;
318 }
319
Adam Lesinski00451162017-10-03 07:44:08 -0700320 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 {
Adam Lesinski00451162017-10-03 07:44:08 -0700322 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700324 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700325
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700326 pb::internal::CompiledFile pb_compiled_file;
327 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328
Adam Lesinski00451162017-10-03 07:44:08 -0700329 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000330 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800332 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800334
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700335 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000336 diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 return false;
338 }
339 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700340}
341
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700342static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000343 ContainerWriter* container_writer, android::IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700344 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700345 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700346
Adam Lesinski00451162017-10-03 07:44:08 -0700347 pb::XmlNode pb_xml_node;
348 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
349
350 std::string serialized_xml = pb_xml_node.SerializeAsString();
351 io::StringInputStream serialized_in(serialized_xml);
352
353 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000354 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 return false;
356 }
357 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700358}
359
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700360static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700361 const file::FileType file_type = file::GetFileType(input_path);
362 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
363 if (file_type == file::FileType::kDirectory) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000364 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700365 << "resource file cannot be a directory");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700366 } else if (file_type == file::FileType::kNonExistant) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000367 context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700368 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000369 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700370 << "not a valid resource file");
371 }
372 return false;
373 }
374 return true;
375}
376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700378 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700379 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800380 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000382 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 }
384
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700386 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700387 auto fin = file->OpenInputStream();
388 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000389 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700390 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700391 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700392 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700393
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700394 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
395 if (!xmlres) {
396 return false;
397 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700398 }
399
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700400 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 xmlres->file.config = path_data.config;
402 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700403 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700404
405 // Collect IDs that are defined here.
406 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 return false;
409 }
410
411 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700412 InlineXmlFormatParser inline_xml_format_parser;
413 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700414 return false;
415 }
416
417 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000419 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 return false;
421 }
422
Adam Lesinski00451162017-10-03 07:44:08 -0700423 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
424 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
425
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700426 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700428 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700430 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431
Adam Lesinski00451162017-10-03 07:44:08 -0700432 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
433 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700434 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700435 }
436
Adam Lesinski00451162017-10-03 07:44:08 -0700437 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
438 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
439 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700440 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700442 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700443 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700444
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000446 context->GetDiagnostics()->Error(android::DiagMessage(output_path)
447 << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 return false;
449 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000450
451 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000452 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000453
454 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000455 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000456 << "failed writing to'"
457 << options.generate_text_symbols_path.value()
458 << "': " << fout_text.GetError());
459 return false;
460 }
461
462 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800463 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000464 r_txt_printer.Print("default int id ");
465 r_txt_printer.Println(res.name.entry);
466 }
467
468 // And print ourselves.
469 r_txt_printer.Print("default int ");
470 r_txt_printer.Print(path_data.resource_dir);
471 r_txt_printer.Print(" ");
472 r_txt_printer.Println(path_data.name);
473 }
474
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700476}
477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700479 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700480 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800481 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000483 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 }
485
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000486 android::BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700488 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700489 res_file.config = path_data.config;
490 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700491 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492
493 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700494 auto data = file->OpenAsData();
495 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000496 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
497 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700499 }
500
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000501 android::BigBuffer crunched_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000502 android::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700503
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 // Ensure that we only keep the chunks we care about if we end up
505 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800506 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000507 android::PngChunkFilter png_chunk_filter(content);
508 android::SourcePathDiagnostics source_diag(path_data.source, context->GetDiagnostics());
509 auto image = android::ReadPng(&png_chunk_filter, &source_diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 if (!image) {
511 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700512 }
513
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000514 std::unique_ptr<android::NinePatch> nine_patch;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700515 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 std::string err;
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000517 nine_patch = android::NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 if (!nine_patch) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000519 context->GetDiagnostics()->Error(android::DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700520 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700521 }
522
523 // Remove the 1px border around the NinePatch.
524 // Basically the row array is shifted up by 1, and the length is treated
525 // as height - 2.
526 // For each row, shift the array to the left by 1, and treat the length as
527 // width - 2.
528 image->width -= 2;
529 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700530 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 for (int32_t h = 0; h < image->height; h++) {
532 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
533 }
534
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000536 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
537 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700539 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540
541 // Write the crunched PNG.
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000542 if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out, {},
543 &source_diag, context->IsVerbose())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700544 return false;
545 }
546
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 if (nine_patch != nullptr ||
548 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549 // No matter what, we must use the re-encoded PNG, even if it is larger.
550 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 } else {
553 // The re-encoded PNG is larger than the original, and there is
554 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000556 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700557 << "original PNG is smaller than crunched PNG"
558 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 }
560
Adam Lesinski06460ef2017-03-14 18:52:13 -0700561 png_chunk_filter.Rewind();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000562 android::BigBuffer filtered_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000563 android::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700564 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700565 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700566 }
567
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700569 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
570 // This will help catch exotic cases where the new code may generate larger PNGs.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700571 std::stringstream legacy_stream{std::string(content)};
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000572 android::BigBuffer legacy_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000573 android::Png png(context->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700575 return false;
576 }
577
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000578 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 << " new=" << buffer.size());
581 }
582 }
583
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000584 android::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700585 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
586 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700587}
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700590 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800592 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000594 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700596
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700598 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 res_file.config = path_data.config;
600 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700601 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700602
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700603 auto data = file->OpenAsData();
604 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000605 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
606 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 return false;
608 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700610 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
611 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700612}
613
614class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000616 explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100617 }
618
Adam Lesinskib522f042017-04-21 16:57:59 -0700619 PackageType GetPackageType() override {
620 // Every compilation unit starts as an app and then gets linked as potentially something else.
621 return PackageType::kApp;
622 }
623
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700624 void SetVerbose(bool val) {
625 verbose_ = val;
Brandon Liu48d229d2023-05-04 23:54:03 +0000626 diagnostics_->SetVerbose(val);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700627 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800628
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700629 bool IsVerbose() override {
630 return verbose_;
631 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800632
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000633 android::IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100634 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700635 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700636
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700638 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 return nullptr;
640 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700641
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700642 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 static std::string empty;
644 return empty;
645 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700646
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700647 uint8_t GetPackageId() override {
648 return 0x0;
649 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700650
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700651 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700652 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 return nullptr;
654 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800655
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700656 int GetMinSdkVersion() override {
657 return 0;
658 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700659
Udam Sainib228df32019-06-18 16:50:34 -0700660 const std::set<std::string>& GetSplitNameDependencies() override {
661 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
662 static std::set<std::string> empty;
663 return empty;
664 }
665
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700667 DISALLOW_COPY_AND_ASSIGN(CompileContext);
668
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000669 android::IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700670 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700671};
672
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700673int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
674 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800675 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700676 bool error = false;
677
678 // Iterate over the input files in a stable, platform-independent manner
679 auto file_iterator = inputs->Iterator();
680 while (file_iterator->HasNext()) {
681 auto file = file_iterator->Next();
682 std::string path = file->GetSource().path;
683
684 // Skip hidden input files
685 if (file::IsHidden(path)) {
686 continue;
687 }
688
689 if (!options.res_zip && !IsValidFile(context, path)) {
690 error = true;
691 continue;
692 }
693
694 // Extract resource type information from the full path
695 std::string err_str;
696 ResourcePathData path_data;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100697 if (auto maybe_path_data = ExtractResourcePathData(
698 path, inputs->GetDirSeparator(), &err_str, options)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700699 path_data = maybe_path_data.value();
700 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000701 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700702 error = true;
703 continue;
704 }
705
706 // Determine how to compile the file based on its type.
707 auto compile_func = &CompileFile;
708 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
709 compile_func = &CompileTable;
710 // We use a different extension (not necessary anymore, but avoids altering the existing
711 // build system logic).
712 path_data.extension = "arsc";
713
714 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
715 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700716 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700717 compile_func = &CompileXml;
718 } else if ((!options.no_png_crunch && path_data.extension == "png")
719 || path_data.extension == "9.png") {
720 compile_func = &CompilePng;
721 }
722 }
723 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000724 context->GetDiagnostics()->Error(android::DiagMessage()
725 << "invalid file path '" << path_data.source << "'");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700726 error = true;
727 continue;
728 }
729
730 // Treat periods as a reserved character that should not be present in a file name
731 // Legacy support for AAPT which did not reserve periods
732 if (compile_func != &CompileFile && !options.legacy_mode
733 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
734 error = true;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000735 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
736 << "file name cannot contain '.' other than for"
737 << " specifying the extension");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700738 continue;
739 }
740
741 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000742 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000743 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
744 << "file failed to compile");
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000745 error = true;
746 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700747 }
748
749 return error ? 1 : 0;
750}
751
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700752int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800753 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700754 CompileContext context(diagnostic_);
755 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700756
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700757 if (visibility_) {
758 if (visibility_.value() == "public") {
759 options_.visibility = Visibility::Level::kPublic;
760 } else if (visibility_.value() == "private") {
761 options_.visibility = Visibility::Level::kPrivate;
762 } else if (visibility_.value() == "default") {
763 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100764 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000765 context.GetDiagnostics()->Error(android::DiagMessage()
766 << "Unrecognized visibility level passes to --visibility: '"
767 << visibility_.value()
768 << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100769 return 1;
770 }
771 }
772
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700773 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700774
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700775 // Collect the resources files to compile
776 if (options_.res_dir && options_.res_zip) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000777 context.GetDiagnostics()->Error(android::DiagMessage()
778 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700779 return 1;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100780 } else if ((options_.res_dir || options_.res_zip) &&
781 options_.source_path && args.size() > 1) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000782 context.GetDiagnostics()->Error(android::DiagMessage(kPath)
783 << "Cannot use an overriding source path with multiple files.");
784 return 1;
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700785 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700786 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000787 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700788 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700789 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700790 }
791
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700792 // Load the files from the res directory
793 std::string err;
794 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
795 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000796 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700797 return 1;
798 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700799 } else if (options_.res_zip) {
800 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000801 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700802 Usage(&std::cerr);
803 return 1;
804 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700805
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700806 // Load a zip file containing a res directory
807 std::string err;
808 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
809 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000810 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700811 return 1;
812 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700814 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800815
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800817 std::vector<std::string> sorted_args = args;
818 std::sort(sorted_args.begin(), sorted_args.end());
819
820 for (const std::string& arg : sorted_args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700821 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700822 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800823
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700824 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700825 }
826
827 std::unique_ptr<IArchiveWriter> archive_writer;
828 file::FileType output_file_type = file::GetFileType(options_.output_path);
829 if (output_file_type == file::FileType::kDirectory) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700830 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700831 } else {
832 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700833 }
834
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700835 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700836 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700837 }
838
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -0700839 // Parse the feature flag values. An argument that starts with '@' points to a file to read flag
840 // values from.
841 std::vector<std::string> all_feature_flags_args;
842 for (const std::string& arg : feature_flags_args_) {
843 if (util::StartsWith(arg, "@")) {
844 const std::string path = arg.substr(1, arg.size() - 1);
845 std::string error;
846 if (!file::AppendArgsFromFile(path, &all_feature_flags_args, &error)) {
847 context.GetDiagnostics()->Error(android::DiagMessage(path) << error);
848 return 1;
849 }
850 } else {
851 all_feature_flags_args.push_back(arg);
852 }
853 }
854
855 for (const std::string& arg : all_feature_flags_args) {
856 if (!ParseFeatureFlagsParameter(arg, context.GetDiagnostics(), &options_.feature_flag_values)) {
857 return 1;
858 }
859 }
860
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700861 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700862}
863
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864} // namespace aapt