blob: cd8c187c1e99408ef57b9040ec7aa73e4b36501b [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;
Jeremy Meyer988be5d2024-09-03 16:31:54 -070070 std::string flag_name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070072 // Original config str. We keep this because when we parse the config, we may add on
73 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077};
78
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070079// Resource file paths are expected to look like: [--/res/]type[-config]/name
Ryan Mitchell4382e442021-07-14 12:53:01 -070080static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path,
81 const char dir_sep,
82 std::string* out_error,
83 const CompileOptions& options) {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070084 std::vector<std::string> parts = util::Split(path, dir_sep);
Jeremy Meyer988be5d2024-09-03 16:31:54 -070085
86 std::string flag_name;
87 // Check for a flag
88 for (auto iter = parts.begin(); iter != parts.end();) {
89 if (iter->starts_with("flag(") && iter->ends_with(")")) {
90 if (!flag_name.empty()) {
91 if (out_error) *out_error = "resource path cannot contain more than one flag directory";
92 return {};
93 }
94 flag_name = iter->substr(5, iter->size() - 6);
95 iter = parts.erase(iter);
96 } else {
97 ++iter;
98 }
99 }
100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 return {};
104 }
105
106 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 size_t dash_pos = dir.find('-');
112 if (dash_pos != std::string::npos) {
113 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
114 if (!ConfigDescription::Parse(config_str, &config)) {
115 if (out_error) {
116 std::stringstream err_str;
117 err_str << "invalid configuration '" << config_str << "'";
118 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 }
120 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 std::string& filename = parts[parts.size() - 1];
126 StringPiece name = filename;
127 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700128
129 const std::string kNinePng = ".9.png";
130 if (filename.size() > kNinePng.size()
131 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
132 // Split on .9.png if this extension is present at the end of the file path
133 name = name.substr(0, filename.size() - kNinePng.size());
134 extension = "9.png";
135 } else {
136 // Split on the last period occurrence
137 size_t dot_pos = filename.rfind('.');
138 if (dot_pos != std::string::npos) {
139 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
140 name = name.substr(0, dot_pos);
141 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000144 const android::Source res_path =
145 options.source_path ? StringPiece(options.source_path.value()) : StringPiece(path);
lukeedgar19b8ebf2020-06-23 10:43:42 +0100146
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700147 return ResourcePathData{res_path,
148 std::string(dir_str),
149 std::string(name),
150 std::string(extension),
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700151 std::move(flag_name),
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700152 std::string(config_str),
153 config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154}
155
Adam Lesinski00451162017-10-03 07:44:08 -0700156static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 name << data.resource_dir;
159 if (!data.config_str.empty()) {
160 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
162 name << "_" << data.name;
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700163 if (!data.flag_name.empty()) {
164 name << ".(" << data.flag_name << ")";
165 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 if (!data.extension.empty()) {
167 name << "." << data.extension;
168 }
169 name << ".flat";
170 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800171}
172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700174 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800176 TRACE_CALL();
Mihai Nitad1a65212019-03-26 13:47:45 -0700177 // Filenames starting with "donottranslate" are not localizable
178 bool translatable_file = path_data.name.find("donottranslate") != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 ResourceTable table;
180 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700181 auto fin = file->OpenInputStream();
182 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000183 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
184 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 // Parse the values file from XML.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700188 xml::XmlPullParser xml_parser(fin.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 ResourceParserOptions parser_options;
191 parser_options.error_on_positional_arguments = !options.legacy_mode;
Donald Chai94e4a012020-01-06 13:52:41 -0800192 parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables;
Mihai Nitad1a65212019-03-26 13:47:45 -0700193 parser_options.translatable = translatable_file;
Jeremy Meyer211bec22024-06-04 14:22:03 -0700194 parser_options.feature_flag_values = options.feature_flag_values;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100196 // If visibility was forced, we need to use it when creating a new resource and also error if
197 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
198 parser_options.visibility = options.visibility;
199
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700200 if (!path_data.flag_name.empty()) {
201 FeatureFlagAttribute flag;
202 const auto& name = path_data.flag_name;
203 if (name.starts_with('!')) {
204 flag.negated = true;
205 flag.name = name.substr(1);
206 } else {
207 flag.name = name;
208 }
209 parser_options.flag = flag;
210
211 std::string error;
212 auto flag_status = GetFlagStatus(flag, options.feature_flag_values, &error);
213 if (flag_status) {
214 parser_options.flag_status = std::move(flag_status.value());
215 } else {
216 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error);
217 return false;
218 }
219 }
220
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700221 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700222 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800225 }
Inseob Kim5fe521e2023-09-15 16:38:50 +0900226
227 if (options.product_.has_value()) {
228 if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true)
229 .Consume(context, &table)) {
230 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
231 << "failed to filter product");
232 return false;
233 }
234 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800236
Mihai Nitad1a65212019-03-26 13:47:45 -0700237 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 // Generate pseudo-localized strings (en-XA and ar-XB).
239 // These are created as weak symbols, and are only generated from default
240 // configuration
241 // strings and plurals.
Brandon Liu5274e062023-05-25 22:41:10 +0000242 std::string grammatical_gender_values;
243 std::string grammatical_gender_ratio;
244 if (options.pseudo_localize_gender_values) {
245 grammatical_gender_values = options.pseudo_localize_gender_values.value();
246 } else {
247 grammatical_gender_values = "f,m,n";
248 }
249 if (options.pseudo_localize_gender_ratio) {
250 grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value();
251 } else {
252 grammatical_gender_ratio = "1.0";
253 }
254 PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values,
255 grammatical_gender_ratio);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700258 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000263 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 return false;
265 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800266
Adam Lesinski00451162017-10-03 07:44:08 -0700267 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 {
Adam Lesinski00451162017-10-03 07:44:08 -0700269 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700271 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700273 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700274 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700275 if (!container_writer.AddResTableEntry(pb_table)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000276 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000282 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 return false;
284 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000285
286 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000287 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000288
289 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000290 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000291 << "failed writing to'"
292 << options.generate_text_symbols_path.value()
293 << "': " << fout_text.GetError());
294 return false;
295 }
296
297 Printer r_txt_printer(&fout_text);
298 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100299 // Only print resources defined locally, e.g. don't write android attributes.
300 if (package->name.empty()) {
301 for (const auto& type : package->types) {
302 for (const auto& entry : type->entries) {
303 // Check access modifiers.
304 switch (entry->visibility.level) {
305 case Visibility::Level::kUndefined :
306 r_txt_printer.Print("default ");
307 break;
308 case Visibility::Level::kPublic :
309 r_txt_printer.Print("public ");
310 break;
311 case Visibility::Level::kPrivate :
312 r_txt_printer.Print("private ");
313 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000314
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000315 if (type->named_type.type != ResourceType::kStyleable) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100316 r_txt_printer.Print("int ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000317 r_txt_printer.Print(type->named_type.to_string());
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100318 r_txt_printer.Print(" ");
319 r_txt_printer.Println(entry->name);
320 } else {
321 r_txt_printer.Print("int[] styleable ");
322 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000323
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100324 if (!entry->values.empty()) {
325 auto styleable =
326 static_cast<const Styleable*>(entry->values.front()->value.get());
327 for (const auto& attr : styleable->entries) {
328 // The visibility of the children under the styleable does not matter as they are
329 // nested under their parent and use its visibility.
330 r_txt_printer.Print("default int styleable ");
331 r_txt_printer.Print(entry->name);
332 // If the package name is present, also include it in the mangled name (e.g.
333 // "android")
334 if (!attr.name.value().package.empty()) {
335 r_txt_printer.Print("_");
336 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
337 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100338 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100339 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100340 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000341 }
342 }
343 }
344 }
345 }
346 }
347 }
348
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800350}
351
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700352static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file,
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000353 android::KnownSizeInputStream* in, IArchiveWriter* writer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000354 android::IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800355 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700356 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000358 diag->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700359 return false;
360 }
361
Adam Lesinski00451162017-10-03 07:44:08 -0700362 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 {
Adam Lesinski00451162017-10-03 07:44:08 -0700364 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700366 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700367
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700368 pb::internal::CompiledFile pb_compiled_file;
369 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370
Adam Lesinski00451162017-10-03 07:44:08 -0700371 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000372 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700373 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800374 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000378 diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700379 return false;
380 }
381 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700382}
383
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700384static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000385 ContainerWriter* container_writer, android::IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700386 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700387 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700388
Adam Lesinski00451162017-10-03 07:44:08 -0700389 pb::XmlNode pb_xml_node;
390 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
391
392 std::string serialized_xml = pb_xml_node.SerializeAsString();
393 io::StringInputStream serialized_in(serialized_xml);
394
395 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000396 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 return false;
398 }
399 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700400}
401
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700402static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700403 const file::FileType file_type = file::GetFileType(input_path);
404 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
405 if (file_type == file::FileType::kDirectory) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000406 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700407 << "resource file cannot be a directory");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700408 } else if (file_type == file::FileType::kNonExistant) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000409 context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700410 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000411 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700412 << "not a valid resource file");
413 }
414 return false;
415 }
416 return true;
417}
418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700420 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700421 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800422 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000424 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 }
426
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700427 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700428 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700429 auto fin = file->OpenInputStream();
430 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000431 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700432 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700434 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700435
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700436 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
437 if (!xmlres) {
438 return false;
439 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700440 }
441
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700442 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 xmlres->file.config = path_data.config;
444 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700445 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446
447 // Collect IDs that are defined here.
448 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700449 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 return false;
451 }
452
453 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 InlineXmlFormatParser inline_xml_format_parser;
455 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 return false;
457 }
458
459 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000461 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 return false;
463 }
464
Adam Lesinski00451162017-10-03 07:44:08 -0700465 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
466 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
467
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700468 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700470 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700472 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473
Adam Lesinski00451162017-10-03 07:44:08 -0700474 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
475 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700477 }
478
Adam Lesinski00451162017-10-03 07:44:08 -0700479 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
480 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
481 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700482 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700483 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700484 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700485 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700487 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000488 context->GetDiagnostics()->Error(android::DiagMessage(output_path)
489 << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 return false;
491 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000492
493 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000494 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000495
496 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000497 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000498 << "failed writing to'"
499 << options.generate_text_symbols_path.value()
500 << "': " << fout_text.GetError());
501 return false;
502 }
503
504 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800505 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000506 r_txt_printer.Print("default int id ");
507 r_txt_printer.Println(res.name.entry);
508 }
509
510 // And print ourselves.
511 r_txt_printer.Print("default int ");
512 r_txt_printer.Print(path_data.resource_dir);
513 r_txt_printer.Print(" ");
514 r_txt_printer.Println(path_data.name);
515 }
516
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700518}
519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700521 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700522 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800523 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000525 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 }
527
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000528 android::BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700529 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700530 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700531 res_file.config = path_data.config;
532 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700533 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534
535 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700536 auto data = file->OpenAsData();
537 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000538 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
539 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700541 }
542
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000543 android::BigBuffer crunched_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000544 android::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700545
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 // Ensure that we only keep the chunks we care about if we end up
547 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800548 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000549 android::PngChunkFilter png_chunk_filter(content);
550 android::SourcePathDiagnostics source_diag(path_data.source, context->GetDiagnostics());
551 auto image = android::ReadPng(&png_chunk_filter, &source_diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 if (!image) {
553 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700554 }
555
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000556 std::unique_ptr<android::NinePatch> nine_patch;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700557 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700558 std::string err;
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000559 nine_patch = android::NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 if (!nine_patch) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000561 context->GetDiagnostics()->Error(android::DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700562 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 }
564
565 // Remove the 1px border around the NinePatch.
566 // Basically the row array is shifted up by 1, and the length is treated
567 // as height - 2.
568 // For each row, shift the array to the left by 1, and treat the length as
569 // width - 2.
570 image->width -= 2;
571 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700572 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 for (int32_t h = 0; h < image->height; h++) {
574 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
575 }
576
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000578 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
579 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700580 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700581 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582
583 // Write the crunched PNG.
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000584 if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out, {},
585 &source_diag, context->IsVerbose())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 return false;
587 }
588
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 if (nine_patch != nullptr ||
590 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 // No matter what, we must use the re-encoded PNG, even if it is larger.
592 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700593 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 } else {
595 // The re-encoded PNG is larger than the original, and there is
596 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700597 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000598 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700599 << "original PNG is smaller than crunched PNG"
600 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 }
602
Adam Lesinski06460ef2017-03-14 18:52:13 -0700603 png_chunk_filter.Rewind();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000604 android::BigBuffer filtered_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000605 android::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700606 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700608 }
609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700611 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
612 // This will help catch exotic cases where the new code may generate larger PNGs.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700613 std::stringstream legacy_stream{std::string(content)};
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000614 android::BigBuffer legacy_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000615 android::Png png(context->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700616 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 return false;
618 }
619
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000620 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700622 << " new=" << buffer.size());
623 }
624 }
625
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000626 android::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700627 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
628 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700629}
630
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700632 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800634 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700635 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000636 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700637 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700638
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700639 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700640 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700641 res_file.config = path_data.config;
642 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700643 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700645 auto data = file->OpenAsData();
646 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000647 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
648 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700649 return false;
650 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700651
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700652 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
653 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700654}
655
656class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700657 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000658 explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100659 }
660
Adam Lesinskib522f042017-04-21 16:57:59 -0700661 PackageType GetPackageType() override {
662 // Every compilation unit starts as an app and then gets linked as potentially something else.
663 return PackageType::kApp;
664 }
665
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700666 void SetVerbose(bool val) {
667 verbose_ = val;
Brandon Liu48d229d2023-05-04 23:54:03 +0000668 diagnostics_->SetVerbose(val);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700669 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800670
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700671 bool IsVerbose() override {
672 return verbose_;
673 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800674
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000675 android::IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100676 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700677 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700680 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 return nullptr;
682 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700683
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700684 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700685 static std::string empty;
686 return empty;
687 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700688
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700689 uint8_t GetPackageId() override {
690 return 0x0;
691 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700692
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700693 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700694 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700695 return nullptr;
696 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800697
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700698 int GetMinSdkVersion() override {
699 return 0;
700 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700701
Udam Sainib228df32019-06-18 16:50:34 -0700702 const std::set<std::string>& GetSplitNameDependencies() override {
703 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
704 static std::set<std::string> empty;
705 return empty;
706 }
707
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700708 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700709 DISALLOW_COPY_AND_ASSIGN(CompileContext);
710
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000711 android::IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700713};
714
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700715int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
716 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800717 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700718 bool error = false;
719
720 // Iterate over the input files in a stable, platform-independent manner
721 auto file_iterator = inputs->Iterator();
722 while (file_iterator->HasNext()) {
723 auto file = file_iterator->Next();
724 std::string path = file->GetSource().path;
725
726 // Skip hidden input files
727 if (file::IsHidden(path)) {
728 continue;
729 }
730
731 if (!options.res_zip && !IsValidFile(context, path)) {
732 error = true;
733 continue;
734 }
735
736 // Extract resource type information from the full path
737 std::string err_str;
738 ResourcePathData path_data;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100739 if (auto maybe_path_data = ExtractResourcePathData(
740 path, inputs->GetDirSeparator(), &err_str, options)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700741 path_data = maybe_path_data.value();
742 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000743 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700744 error = true;
745 continue;
746 }
747
748 // Determine how to compile the file based on its type.
749 auto compile_func = &CompileFile;
750 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
751 compile_func = &CompileTable;
752 // We use a different extension (not necessary anymore, but avoids altering the existing
753 // build system logic).
754 path_data.extension = "arsc";
755
756 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
757 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700758 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700759 compile_func = &CompileXml;
760 } else if ((!options.no_png_crunch && path_data.extension == "png")
761 || path_data.extension == "9.png") {
762 compile_func = &CompilePng;
763 }
764 }
765 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000766 context->GetDiagnostics()->Error(android::DiagMessage()
767 << "invalid file path '" << path_data.source << "'");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700768 error = true;
769 continue;
770 }
771
772 // Treat periods as a reserved character that should not be present in a file name
773 // Legacy support for AAPT which did not reserve periods
774 if (compile_func != &CompileFile && !options.legacy_mode
775 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
776 error = true;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000777 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
778 << "file name cannot contain '.' other than for"
779 << " specifying the extension");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700780 continue;
781 }
782
783 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000784 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000785 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
786 << "file failed to compile");
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000787 error = true;
788 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700789 }
790
791 return error ? 1 : 0;
792}
793
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700794int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800795 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700796 CompileContext context(diagnostic_);
797 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700798
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700799 if (visibility_) {
800 if (visibility_.value() == "public") {
801 options_.visibility = Visibility::Level::kPublic;
802 } else if (visibility_.value() == "private") {
803 options_.visibility = Visibility::Level::kPrivate;
804 } else if (visibility_.value() == "default") {
805 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100806 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000807 context.GetDiagnostics()->Error(android::DiagMessage()
808 << "Unrecognized visibility level passes to --visibility: '"
809 << visibility_.value()
810 << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100811 return 1;
812 }
813 }
814
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700815 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700816
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700817 // Collect the resources files to compile
818 if (options_.res_dir && options_.res_zip) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000819 context.GetDiagnostics()->Error(android::DiagMessage()
820 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700821 return 1;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100822 } else if ((options_.res_dir || options_.res_zip) &&
823 options_.source_path && args.size() > 1) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000824 context.GetDiagnostics()->Error(android::DiagMessage(kPath)
825 << "Cannot use an overriding source path with multiple files.");
826 return 1;
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700827 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700828 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000829 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700830 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700831 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700832 }
833
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700834 // Load the files from the res directory
835 std::string err;
836 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
837 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000838 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700839 return 1;
840 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700841 } else if (options_.res_zip) {
842 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000843 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700844 Usage(&std::cerr);
845 return 1;
846 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700847
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700848 // Load a zip file containing a res directory
849 std::string err;
850 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
851 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000852 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700853 return 1;
854 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700856 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800857
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700858 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800859 std::vector<std::string> sorted_args = args;
860 std::sort(sorted_args.begin(), sorted_args.end());
861
862 for (const std::string& arg : sorted_args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700863 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700864 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800865
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700866 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700867 }
868
869 std::unique_ptr<IArchiveWriter> archive_writer;
870 file::FileType output_file_type = file::GetFileType(options_.output_path);
871 if (output_file_type == file::FileType::kDirectory) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700872 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700873 } else {
874 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700875 }
876
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700877 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700878 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 }
880
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -0700881 // Parse the feature flag values. An argument that starts with '@' points to a file to read flag
882 // values from.
883 std::vector<std::string> all_feature_flags_args;
884 for (const std::string& arg : feature_flags_args_) {
885 if (util::StartsWith(arg, "@")) {
886 const std::string path = arg.substr(1, arg.size() - 1);
887 std::string error;
888 if (!file::AppendArgsFromFile(path, &all_feature_flags_args, &error)) {
889 context.GetDiagnostics()->Error(android::DiagMessage(path) << error);
890 return 1;
891 }
892 } else {
893 all_feature_flags_args.push_back(arg);
894 }
895 }
896
897 for (const std::string& arg : all_feature_flags_args) {
898 if (!ParseFeatureFlagsParameter(arg, context.GetDiagnostics(), &options_.feature_flag_values)) {
899 return 1;
900 }
901 }
902
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700903 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700904}
905
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700906} // namespace aapt