blob: 2a978a5153cace857436062f4e3017ae65d19039 [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;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700174 parser_options.feature_flag_values = options.feature_flag_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100176 // If visibility was forced, we need to use it when creating a new resource and also error if
177 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
178 parser_options.visibility = options.visibility;
179
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700180 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700181 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800184 }
Inseob Kim5fe521e2023-09-15 16:38:50 +0900185
186 if (options.product_.has_value()) {
187 if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true)
188 .Consume(context, &table)) {
189 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
190 << "failed to filter product");
191 return false;
192 }
193 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800195
Mihai Nitad1a65212019-03-26 13:47:45 -0700196 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 // Generate pseudo-localized strings (en-XA and ar-XB).
198 // These are created as weak symbols, and are only generated from default
199 // configuration
200 // strings and plurals.
Brandon Liu5274e062023-05-25 22:41:10 +0000201 std::string grammatical_gender_values;
202 std::string grammatical_gender_ratio;
203 if (options.pseudo_localize_gender_values) {
204 grammatical_gender_values = options.pseudo_localize_gender_values.value();
205 } else {
206 grammatical_gender_values = "f,m,n";
207 }
208 if (options.pseudo_localize_gender_ratio) {
209 grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value();
210 } else {
211 grammatical_gender_ratio = "1.0";
212 }
213 PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values,
214 grammatical_gender_ratio);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700217 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000222 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 return false;
224 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800225
Adam Lesinski00451162017-10-03 07:44:08 -0700226 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 {
Adam Lesinski00451162017-10-03 07:44:08 -0700228 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700230 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700232 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700233 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700234 if (!container_writer.AddResTableEntry(pb_table)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000235 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800239
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000241 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 return false;
243 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000244
245 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000246 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000247
248 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000249 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000250 << "failed writing to'"
251 << options.generate_text_symbols_path.value()
252 << "': " << fout_text.GetError());
253 return false;
254 }
255
256 Printer r_txt_printer(&fout_text);
257 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100258 // Only print resources defined locally, e.g. don't write android attributes.
259 if (package->name.empty()) {
260 for (const auto& type : package->types) {
261 for (const auto& entry : type->entries) {
262 // Check access modifiers.
263 switch (entry->visibility.level) {
264 case Visibility::Level::kUndefined :
265 r_txt_printer.Print("default ");
266 break;
267 case Visibility::Level::kPublic :
268 r_txt_printer.Print("public ");
269 break;
270 case Visibility::Level::kPrivate :
271 r_txt_printer.Print("private ");
272 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000273
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000274 if (type->named_type.type != ResourceType::kStyleable) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100275 r_txt_printer.Print("int ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000276 r_txt_printer.Print(type->named_type.to_string());
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100277 r_txt_printer.Print(" ");
278 r_txt_printer.Println(entry->name);
279 } else {
280 r_txt_printer.Print("int[] styleable ");
281 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000282
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100283 if (!entry->values.empty()) {
284 auto styleable =
285 static_cast<const Styleable*>(entry->values.front()->value.get());
286 for (const auto& attr : styleable->entries) {
287 // The visibility of the children under the styleable does not matter as they are
288 // nested under their parent and use its visibility.
289 r_txt_printer.Print("default int styleable ");
290 r_txt_printer.Print(entry->name);
291 // If the package name is present, also include it in the mangled name (e.g.
292 // "android")
293 if (!attr.name.value().package.empty()) {
294 r_txt_printer.Print("_");
295 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
296 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100297 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100298 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100299 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000300 }
301 }
302 }
303 }
304 }
305 }
306 }
307
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800309}
310
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700311static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file,
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000312 android::KnownSizeInputStream* in, IArchiveWriter* writer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000313 android::IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800314 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000317 diag->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 return false;
319 }
320
Adam Lesinski00451162017-10-03 07:44:08 -0700321 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 {
Adam Lesinski00451162017-10-03 07:44:08 -0700323 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700325 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700327 pb::internal::CompiledFile pb_compiled_file;
328 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329
Adam Lesinski00451162017-10-03 07:44:08 -0700330 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000331 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800333 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800335
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000337 diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 return false;
339 }
340 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700341}
342
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700343static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000344 ContainerWriter* container_writer, android::IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700345 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700346 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700347
Adam Lesinski00451162017-10-03 07:44:08 -0700348 pb::XmlNode pb_xml_node;
349 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
350
351 std::string serialized_xml = pb_xml_node.SerializeAsString();
352 io::StringInputStream serialized_in(serialized_xml);
353
354 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000355 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 return false;
357 }
358 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700359}
360
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700361static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700362 const file::FileType file_type = file::GetFileType(input_path);
363 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
364 if (file_type == file::FileType::kDirectory) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000365 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700366 << "resource file cannot be a directory");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700367 } else if (file_type == file::FileType::kNonExistant) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000368 context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700369 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000370 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700371 << "not a valid resource file");
372 }
373 return false;
374 }
375 return true;
376}
377
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700378static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700379 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700380 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800381 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700382 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000383 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700384 }
385
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700388 auto fin = file->OpenInputStream();
389 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000390 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700391 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700393 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700394
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700395 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
396 if (!xmlres) {
397 return false;
398 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 }
400
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700401 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700402 xmlres->file.config = path_data.config;
403 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700404 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405
406 // Collect IDs that are defined here.
407 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 return false;
410 }
411
412 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 InlineXmlFormatParser inline_xml_format_parser;
414 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 return false;
416 }
417
418 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000420 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 return false;
422 }
423
Adam Lesinski00451162017-10-03 07:44:08 -0700424 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
425 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
426
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700427 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700429 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700430 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700431 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432
Adam Lesinski00451162017-10-03 07:44:08 -0700433 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
434 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700436 }
437
Adam Lesinski00451162017-10-03 07:44:08 -0700438 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
439 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
440 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700441 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700443 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700445
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700446 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000447 context->GetDiagnostics()->Error(android::DiagMessage(output_path)
448 << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 return false;
450 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000451
452 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000453 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000454
455 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000456 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000457 << "failed writing to'"
458 << options.generate_text_symbols_path.value()
459 << "': " << fout_text.GetError());
460 return false;
461 }
462
463 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800464 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000465 r_txt_printer.Print("default int id ");
466 r_txt_printer.Println(res.name.entry);
467 }
468
469 // And print ourselves.
470 r_txt_printer.Print("default int ");
471 r_txt_printer.Print(path_data.resource_dir);
472 r_txt_printer.Print(" ");
473 r_txt_printer.Println(path_data.name);
474 }
475
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700477}
478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700480 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700481 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800482 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000484 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 }
486
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000487 android::BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700489 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 res_file.config = path_data.config;
491 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700492 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493
494 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700495 auto data = file->OpenAsData();
496 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000497 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
498 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700500 }
501
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000502 android::BigBuffer crunched_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000503 android::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700504
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 // Ensure that we only keep the chunks we care about if we end up
506 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800507 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000508 android::PngChunkFilter png_chunk_filter(content);
509 android::SourcePathDiagnostics source_diag(path_data.source, context->GetDiagnostics());
510 auto image = android::ReadPng(&png_chunk_filter, &source_diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 if (!image) {
512 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700513 }
514
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000515 std::unique_ptr<android::NinePatch> nine_patch;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700516 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 std::string err;
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000518 nine_patch = android::NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700519 if (!nine_patch) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000520 context->GetDiagnostics()->Error(android::DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 }
523
524 // Remove the 1px border around the NinePatch.
525 // Basically the row array is shifted up by 1, and the length is treated
526 // as height - 2.
527 // For each row, shift the array to the left by 1, and treat the length as
528 // width - 2.
529 image->width -= 2;
530 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700531 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 for (int32_t h = 0; h < image->height; h++) {
533 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
534 }
535
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700536 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000537 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
538 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700540 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541
542 // Write the crunched PNG.
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000543 if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out, {},
544 &source_diag, context->IsVerbose())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 return false;
546 }
547
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700548 if (nine_patch != nullptr ||
549 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 // No matter what, we must use the re-encoded PNG, even if it is larger.
551 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 } else {
554 // The re-encoded PNG is larger than the original, and there is
555 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700556 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000557 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700558 << "original PNG is smaller than crunched PNG"
559 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700560 }
561
Adam Lesinski06460ef2017-03-14 18:52:13 -0700562 png_chunk_filter.Rewind();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000563 android::BigBuffer filtered_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000564 android::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700565 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 }
568
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700570 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
571 // This will help catch exotic cases where the new code may generate larger PNGs.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700572 std::stringstream legacy_stream{std::string(content)};
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000573 android::BigBuffer legacy_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000574 android::Png png(context->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 return false;
577 }
578
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000579 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 << " new=" << buffer.size());
582 }
583 }
584
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000585 android::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700586 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
587 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700588}
589
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700590static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700591 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800593 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000595 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700597
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700599 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 res_file.config = path_data.config;
601 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700602 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700603
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700604 auto data = file->OpenAsData();
605 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000606 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
607 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 return false;
609 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700610
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700611 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
612 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700613}
614
615class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000617 explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100618 }
619
Adam Lesinskib522f042017-04-21 16:57:59 -0700620 PackageType GetPackageType() override {
621 // Every compilation unit starts as an app and then gets linked as potentially something else.
622 return PackageType::kApp;
623 }
624
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700625 void SetVerbose(bool val) {
626 verbose_ = val;
Brandon Liu48d229d2023-05-04 23:54:03 +0000627 diagnostics_->SetVerbose(val);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700628 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800629
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700630 bool IsVerbose() override {
631 return verbose_;
632 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800633
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000634 android::IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100635 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700636 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700637
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700639 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 return nullptr;
641 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700642
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700644 static std::string empty;
645 return empty;
646 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700647
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700648 uint8_t GetPackageId() override {
649 return 0x0;
650 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700652 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700653 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700654 return nullptr;
655 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800656
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700657 int GetMinSdkVersion() override {
658 return 0;
659 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700660
Udam Sainib228df32019-06-18 16:50:34 -0700661 const std::set<std::string>& GetSplitNameDependencies() override {
662 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
663 static std::set<std::string> empty;
664 return empty;
665 }
666
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700667 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700668 DISALLOW_COPY_AND_ASSIGN(CompileContext);
669
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000670 android::IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700671 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700672};
673
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700674int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
675 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800676 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700677 bool error = false;
678
679 // Iterate over the input files in a stable, platform-independent manner
680 auto file_iterator = inputs->Iterator();
681 while (file_iterator->HasNext()) {
682 auto file = file_iterator->Next();
683 std::string path = file->GetSource().path;
684
685 // Skip hidden input files
686 if (file::IsHidden(path)) {
687 continue;
688 }
689
690 if (!options.res_zip && !IsValidFile(context, path)) {
691 error = true;
692 continue;
693 }
694
695 // Extract resource type information from the full path
696 std::string err_str;
697 ResourcePathData path_data;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100698 if (auto maybe_path_data = ExtractResourcePathData(
699 path, inputs->GetDirSeparator(), &err_str, options)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700700 path_data = maybe_path_data.value();
701 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000702 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700703 error = true;
704 continue;
705 }
706
707 // Determine how to compile the file based on its type.
708 auto compile_func = &CompileFile;
709 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
710 compile_func = &CompileTable;
711 // We use a different extension (not necessary anymore, but avoids altering the existing
712 // build system logic).
713 path_data.extension = "arsc";
714
715 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
716 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700717 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700718 compile_func = &CompileXml;
719 } else if ((!options.no_png_crunch && path_data.extension == "png")
720 || path_data.extension == "9.png") {
721 compile_func = &CompilePng;
722 }
723 }
724 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000725 context->GetDiagnostics()->Error(android::DiagMessage()
726 << "invalid file path '" << path_data.source << "'");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700727 error = true;
728 continue;
729 }
730
731 // Treat periods as a reserved character that should not be present in a file name
732 // Legacy support for AAPT which did not reserve periods
733 if (compile_func != &CompileFile && !options.legacy_mode
734 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
735 error = true;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000736 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
737 << "file name cannot contain '.' other than for"
738 << " specifying the extension");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700739 continue;
740 }
741
742 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000743 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000744 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
745 << "file failed to compile");
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000746 error = true;
747 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700748 }
749
750 return error ? 1 : 0;
751}
752
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700753int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800754 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700755 CompileContext context(diagnostic_);
756 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700757
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700758 if (visibility_) {
759 if (visibility_.value() == "public") {
760 options_.visibility = Visibility::Level::kPublic;
761 } else if (visibility_.value() == "private") {
762 options_.visibility = Visibility::Level::kPrivate;
763 } else if (visibility_.value() == "default") {
764 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100765 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000766 context.GetDiagnostics()->Error(android::DiagMessage()
767 << "Unrecognized visibility level passes to --visibility: '"
768 << visibility_.value()
769 << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100770 return 1;
771 }
772 }
773
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700774 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700776 // Collect the resources files to compile
777 if (options_.res_dir && options_.res_zip) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000778 context.GetDiagnostics()->Error(android::DiagMessage()
779 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700780 return 1;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100781 } else if ((options_.res_dir || options_.res_zip) &&
782 options_.source_path && args.size() > 1) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000783 context.GetDiagnostics()->Error(android::DiagMessage(kPath)
784 << "Cannot use an overriding source path with multiple files.");
785 return 1;
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700786 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700787 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000788 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700789 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700790 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700791 }
792
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700793 // Load the files from the res directory
794 std::string err;
795 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
796 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000797 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 return 1;
799 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700800 } else if (options_.res_zip) {
801 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000802 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700803 Usage(&std::cerr);
804 return 1;
805 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700806
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700807 // Load a zip file containing a res directory
808 std::string err;
809 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
810 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000811 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700812 return 1;
813 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700815 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800816
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800818 std::vector<std::string> sorted_args = args;
819 std::sort(sorted_args.begin(), sorted_args.end());
820
821 for (const std::string& arg : sorted_args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700822 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700823 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800824
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700825 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700826 }
827
828 std::unique_ptr<IArchiveWriter> archive_writer;
829 file::FileType output_file_type = file::GetFileType(options_.output_path);
830 if (output_file_type == file::FileType::kDirectory) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700831 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700832 } else {
833 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700834 }
835
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700836 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700837 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700838 }
839
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -0700840 // Parse the feature flag values. An argument that starts with '@' points to a file to read flag
841 // values from.
842 std::vector<std::string> all_feature_flags_args;
843 for (const std::string& arg : feature_flags_args_) {
844 if (util::StartsWith(arg, "@")) {
845 const std::string path = arg.substr(1, arg.size() - 1);
846 std::string error;
847 if (!file::AppendArgsFromFile(path, &all_feature_flags_args, &error)) {
848 context.GetDiagnostics()->Error(android::DiagMessage(path) << error);
849 return 1;
850 }
851 } else {
852 all_feature_flags_args.push_back(arg);
853 }
854 }
855
856 for (const std::string& arg : all_feature_flags_args) {
857 if (!ParseFeatureFlagsParameter(arg, context.GetDiagnostics(), &options_.feature_flag_values)) {
858 return 1;
859 }
860 }
861
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700862 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700863}
864
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700865} // namespace aapt