blob: 52372fa385251d0354e01aef172f35fea3251077 [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;
Jeremy Meyere08e0372024-09-17 12:45:26 -0700199 parser_options.flag = ParseFlag(path_data.flag_name);
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100200
Jeremy Meyere08e0372024-09-17 12:45:26 -0700201 if (parser_options.flag) {
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700202 std::string error;
Jeremy Meyere08e0372024-09-17 12:45:26 -0700203 auto flag_status = GetFlagStatus(parser_options.flag, options.feature_flag_values, &error);
Jeremy Meyer988be5d2024-09-03 16:31:54 -0700204 if (flag_status) {
205 parser_options.flag_status = std::move(flag_status.value());
206 } else {
207 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error);
208 return false;
209 }
210 }
211
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700212 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700213 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800216 }
Inseob Kim5fe521e2023-09-15 16:38:50 +0900217
218 if (options.product_.has_value()) {
219 if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true)
220 .Consume(context, &table)) {
221 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
222 << "failed to filter product");
223 return false;
224 }
225 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800227
Mihai Nitad1a65212019-03-26 13:47:45 -0700228 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 // Generate pseudo-localized strings (en-XA and ar-XB).
230 // These are created as weak symbols, and are only generated from default
231 // configuration
232 // strings and plurals.
Brandon Liu5274e062023-05-25 22:41:10 +0000233 std::string grammatical_gender_values;
234 std::string grammatical_gender_ratio;
235 if (options.pseudo_localize_gender_values) {
236 grammatical_gender_values = options.pseudo_localize_gender_values.value();
237 } else {
238 grammatical_gender_values = "f,m,n";
239 }
240 if (options.pseudo_localize_gender_ratio) {
241 grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value();
242 } else {
243 grammatical_gender_ratio = "1.0";
244 }
245 PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values,
246 grammatical_gender_ratio);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700249 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700251
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000254 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 return false;
256 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800257
Adam Lesinski00451162017-10-03 07:44:08 -0700258 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 {
Adam Lesinski00451162017-10-03 07:44:08 -0700260 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700262 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700264 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700265 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700266 if (!container_writer.AddResTableEntry(pb_table)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000267 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000273 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 return false;
275 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000276
277 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000278 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000279
280 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000281 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000282 << "failed writing to'"
283 << options.generate_text_symbols_path.value()
284 << "': " << fout_text.GetError());
285 return false;
286 }
287
288 Printer r_txt_printer(&fout_text);
289 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100290 // Only print resources defined locally, e.g. don't write android attributes.
291 if (package->name.empty()) {
292 for (const auto& type : package->types) {
293 for (const auto& entry : type->entries) {
294 // Check access modifiers.
295 switch (entry->visibility.level) {
296 case Visibility::Level::kUndefined :
297 r_txt_printer.Print("default ");
298 break;
299 case Visibility::Level::kPublic :
300 r_txt_printer.Print("public ");
301 break;
302 case Visibility::Level::kPrivate :
303 r_txt_printer.Print("private ");
304 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000305
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000306 if (type->named_type.type != ResourceType::kStyleable) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100307 r_txt_printer.Print("int ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000308 r_txt_printer.Print(type->named_type.to_string());
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100309 r_txt_printer.Print(" ");
310 r_txt_printer.Println(entry->name);
311 } else {
312 r_txt_printer.Print("int[] styleable ");
313 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000314
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100315 if (!entry->values.empty()) {
316 auto styleable =
317 static_cast<const Styleable*>(entry->values.front()->value.get());
318 for (const auto& attr : styleable->entries) {
319 // The visibility of the children under the styleable does not matter as they are
320 // nested under their parent and use its visibility.
321 r_txt_printer.Print("default int styleable ");
322 r_txt_printer.Print(entry->name);
323 // If the package name is present, also include it in the mangled name (e.g.
324 // "android")
325 if (!attr.name.value().package.empty()) {
326 r_txt_printer.Print("_");
327 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
328 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100329 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100330 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100331 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000332 }
333 }
334 }
335 }
336 }
337 }
338 }
339
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800341}
342
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700343static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file,
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000344 android::KnownSizeInputStream* in, IArchiveWriter* writer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000345 android::IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800346 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000349 diag->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return false;
351 }
352
Adam Lesinski00451162017-10-03 07:44:08 -0700353 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 {
Adam Lesinski00451162017-10-03 07:44:08 -0700355 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700357 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700359 pb::internal::CompiledFile pb_compiled_file;
360 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361
Adam Lesinski00451162017-10-03 07:44:08 -0700362 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000363 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800365 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000369 diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 return false;
371 }
372 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700373}
374
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700375static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000376 ContainerWriter* container_writer, android::IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700377 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700378 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700379
Adam Lesinski00451162017-10-03 07:44:08 -0700380 pb::XmlNode pb_xml_node;
381 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
382
383 std::string serialized_xml = pb_xml_node.SerializeAsString();
384 io::StringInputStream serialized_in(serialized_xml);
385
386 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000387 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 return false;
389 }
390 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700391}
392
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700393static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700394 const file::FileType file_type = file::GetFileType(input_path);
395 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
396 if (file_type == file::FileType::kDirectory) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000397 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700398 << "resource file cannot be a directory");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700399 } else if (file_type == file::FileType::kNonExistant) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000400 context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700401 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000402 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700403 << "not a valid resource file");
404 }
405 return false;
406 }
407 return true;
408}
409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700411 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700412 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800413 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700414 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000415 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 }
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700420 auto fin = file->OpenInputStream();
421 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000422 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700423 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700425 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700426
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700427 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
428 if (!xmlres) {
429 return false;
430 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431 }
432
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700433 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 xmlres->file.config = path_data.config;
435 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700436 xmlres->file.type = ResourceFile::Type::kProtoXml;
Jeremy Meyere08e0372024-09-17 12:45:26 -0700437 xmlres->file.flag = ParseFlag(path_data.flag_name);
438
439 if (xmlres->file.flag) {
440 std::string error;
441 auto flag_status = GetFlagStatus(xmlres->file.flag, options.feature_flag_values, &error);
442 if (flag_status) {
443 xmlres->file.flag_status = flag_status.value();
444 } else {
445 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error);
446 return false;
447 }
448 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449
450 // Collect IDs that are defined here.
451 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700452 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 return false;
454 }
455
456 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700457 InlineXmlFormatParser inline_xml_format_parser;
458 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700459 return false;
460 }
461
462 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700463 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000464 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 return false;
466 }
467
Adam Lesinski00451162017-10-03 07:44:08 -0700468 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
469 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
470
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700471 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700473 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700474 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700475 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476
Adam Lesinski00451162017-10-03 07:44:08 -0700477 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
478 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700479 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700480 }
481
Adam Lesinski00451162017-10-03 07:44:08 -0700482 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
483 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
484 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700485 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700487 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700488 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700489
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000491 context->GetDiagnostics()->Error(android::DiagMessage(output_path)
492 << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 return false;
494 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000495
496 if (options.generate_text_symbols_path) {
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000497 android::FileOutputStream fout_text(options.generate_text_symbols_path.value());
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000498
499 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000500 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000501 << "failed writing to'"
502 << options.generate_text_symbols_path.value()
503 << "': " << fout_text.GetError());
504 return false;
505 }
506
507 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800508 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000509 r_txt_printer.Print("default int id ");
510 r_txt_printer.Println(res.name.entry);
511 }
512
513 // And print ourselves.
514 r_txt_printer.Print("default int ");
515 r_txt_printer.Print(path_data.resource_dir);
516 r_txt_printer.Print(" ");
517 r_txt_printer.Println(path_data.name);
518 }
519
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700521}
522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700524 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700525 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800526 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700527 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000528 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529 }
530
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000531 android::BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700532 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700533 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 res_file.config = path_data.config;
535 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700536 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537
Jeremy Meyere08e0372024-09-17 12:45:26 -0700538 if (!path_data.flag_name.empty()) {
539 FeatureFlagAttribute flag;
540 auto name = path_data.flag_name;
541 if (name.starts_with('!')) {
542 flag.negated = true;
543 flag.name = name.substr(1);
544 } else {
545 flag.name = name;
546 }
547 res_file.flag = flag;
548
549 std::string error;
550 auto flag_status = GetFlagStatus(flag, options.feature_flag_values, &error);
551 if (flag_status) {
552 res_file.flag_status = flag_status.value();
553 } else {
554 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error);
555 return false;
556 }
557 }
558
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700560 auto data = file->OpenAsData();
561 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000562 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
563 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700564 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700565 }
566
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000567 android::BigBuffer crunched_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000568 android::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700569
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 // Ensure that we only keep the chunks we care about if we end up
571 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800572 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000573 android::PngChunkFilter png_chunk_filter(content);
574 android::SourcePathDiagnostics source_diag(path_data.source, context->GetDiagnostics());
575 auto image = android::ReadPng(&png_chunk_filter, &source_diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 if (!image) {
577 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700578 }
579
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000580 std::unique_ptr<android::NinePatch> nine_patch;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 std::string err;
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000583 nine_patch = android::NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 if (!nine_patch) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000585 context->GetDiagnostics()->Error(android::DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700586 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700587 }
588
589 // Remove the 1px border around the NinePatch.
590 // Basically the row array is shifted up by 1, and the length is treated
591 // as height - 2.
592 // For each row, shift the array to the left by 1, and treat the length as
593 // width - 2.
594 image->width -= 2;
595 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700596 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700597 for (int32_t h = 0; h < image->height; h++) {
598 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
599 }
600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000602 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
603 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700605 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700606
607 // Write the crunched PNG.
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000608 if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out, {},
609 &source_diag, context->IsVerbose())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 return false;
611 }
612
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700613 if (nine_patch != nullptr ||
614 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700615 // No matter what, we must use the re-encoded PNG, even if it is larger.
616 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700617 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700618 } else {
619 // The re-encoded PNG is larger than the original, and there is
620 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000622 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700623 << "original PNG is smaller than crunched PNG"
624 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625 }
626
Adam Lesinski06460ef2017-03-14 18:52:13 -0700627 png_chunk_filter.Rewind();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000628 android::BigBuffer filtered_png_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000629 android::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700630 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700631 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 }
633
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700635 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
636 // This will help catch exotic cases where the new code may generate larger PNGs.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700637 std::stringstream legacy_stream{std::string(content)};
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000638 android::BigBuffer legacy_buffer(4096);
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000639 android::Png png(context->GetDiagnostics());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 return false;
642 }
643
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000644 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700645 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 << " new=" << buffer.size());
647 }
648 }
649
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000650 android::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700651 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
652 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700653}
654
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700655static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700656 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800658 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700659 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000660 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700661 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700662
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700663 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700664 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 res_file.config = path_data.config;
666 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700667 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700668
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700669 auto data = file->OpenAsData();
670 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000671 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
672 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700673 return false;
674 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700675
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700676 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
677 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700678}
679
680class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700681 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000682 explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100683 }
684
Adam Lesinskib522f042017-04-21 16:57:59 -0700685 PackageType GetPackageType() override {
686 // Every compilation unit starts as an app and then gets linked as potentially something else.
687 return PackageType::kApp;
688 }
689
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700690 void SetVerbose(bool val) {
691 verbose_ = val;
Brandon Liu48d229d2023-05-04 23:54:03 +0000692 diagnostics_->SetVerbose(val);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700693 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800694
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700695 bool IsVerbose() override {
696 return verbose_;
697 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800698
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000699 android::IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100700 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700701 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700702
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700703 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700704 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705 return nullptr;
706 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700707
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700708 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700709 static std::string empty;
710 return empty;
711 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700712
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700713 uint8_t GetPackageId() override {
714 return 0x0;
715 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700716
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700717 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700718 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700719 return nullptr;
720 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800721
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700722 int GetMinSdkVersion() override {
723 return 0;
724 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700725
Udam Sainib228df32019-06-18 16:50:34 -0700726 const std::set<std::string>& GetSplitNameDependencies() override {
727 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
728 static std::set<std::string> empty;
729 return empty;
730 }
731
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700732 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700733 DISALLOW_COPY_AND_ASSIGN(CompileContext);
734
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000735 android::IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700736 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700737};
738
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700739int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
740 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800741 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700742 bool error = false;
743
744 // Iterate over the input files in a stable, platform-independent manner
745 auto file_iterator = inputs->Iterator();
746 while (file_iterator->HasNext()) {
747 auto file = file_iterator->Next();
748 std::string path = file->GetSource().path;
749
750 // Skip hidden input files
751 if (file::IsHidden(path)) {
752 continue;
753 }
754
755 if (!options.res_zip && !IsValidFile(context, path)) {
756 error = true;
757 continue;
758 }
759
760 // Extract resource type information from the full path
761 std::string err_str;
762 ResourcePathData path_data;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100763 if (auto maybe_path_data = ExtractResourcePathData(
764 path, inputs->GetDirSeparator(), &err_str, options)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700765 path_data = maybe_path_data.value();
766 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000767 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700768 error = true;
769 continue;
770 }
771
772 // Determine how to compile the file based on its type.
773 auto compile_func = &CompileFile;
774 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
775 compile_func = &CompileTable;
776 // We use a different extension (not necessary anymore, but avoids altering the existing
777 // build system logic).
778 path_data.extension = "arsc";
779
780 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
781 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700782 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700783 compile_func = &CompileXml;
784 } else if ((!options.no_png_crunch && path_data.extension == "png")
785 || path_data.extension == "9.png") {
786 compile_func = &CompilePng;
787 }
788 }
789 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000790 context->GetDiagnostics()->Error(android::DiagMessage()
791 << "invalid file path '" << path_data.source << "'");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700792 error = true;
793 continue;
794 }
795
796 // Treat periods as a reserved character that should not be present in a file name
797 // Legacy support for AAPT which did not reserve periods
798 if (compile_func != &CompileFile && !options.legacy_mode
799 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
800 error = true;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000801 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
802 << "file name cannot contain '.' other than for"
803 << " specifying the extension");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700804 continue;
805 }
806
807 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000808 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000809 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
810 << "file failed to compile");
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000811 error = true;
812 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700813 }
814
815 return error ? 1 : 0;
816}
817
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700818int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800819 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700820 CompileContext context(diagnostic_);
821 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700822
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700823 if (visibility_) {
824 if (visibility_.value() == "public") {
825 options_.visibility = Visibility::Level::kPublic;
826 } else if (visibility_.value() == "private") {
827 options_.visibility = Visibility::Level::kPrivate;
828 } else if (visibility_.value() == "default") {
829 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100830 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000831 context.GetDiagnostics()->Error(android::DiagMessage()
832 << "Unrecognized visibility level passes to --visibility: '"
833 << visibility_.value()
834 << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100835 return 1;
836 }
837 }
838
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700839 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700840
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700841 // Collect the resources files to compile
842 if (options_.res_dir && options_.res_zip) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000843 context.GetDiagnostics()->Error(android::DiagMessage()
844 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700845 return 1;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100846 } else if ((options_.res_dir || options_.res_zip) &&
847 options_.source_path && args.size() > 1) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000848 context.GetDiagnostics()->Error(android::DiagMessage(kPath)
849 << "Cannot use an overriding source path with multiple files.");
850 return 1;
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700851 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700852 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000853 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700854 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700855 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700856 }
857
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700858 // Load the files from the res directory
859 std::string err;
860 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
861 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000862 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700863 return 1;
864 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700865 } else if (options_.res_zip) {
866 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000867 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700868 Usage(&std::cerr);
869 return 1;
870 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700871
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700872 // Load a zip file containing a res directory
873 std::string err;
874 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
875 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000876 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700877 return 1;
878 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700879 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700880 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800881
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700882 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800883 std::vector<std::string> sorted_args = args;
884 std::sort(sorted_args.begin(), sorted_args.end());
885
886 for (const std::string& arg : sorted_args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700887 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700888 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800889
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700890 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700891 }
892
893 std::unique_ptr<IArchiveWriter> archive_writer;
894 file::FileType output_file_type = file::GetFileType(options_.output_path);
895 if (output_file_type == file::FileType::kDirectory) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700896 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700897 } else {
898 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700899 }
900
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700901 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700902 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700903 }
904
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -0700905 // Parse the feature flag values. An argument that starts with '@' points to a file to read flag
906 // values from.
907 std::vector<std::string> all_feature_flags_args;
908 for (const std::string& arg : feature_flags_args_) {
909 if (util::StartsWith(arg, "@")) {
910 const std::string path = arg.substr(1, arg.size() - 1);
911 std::string error;
912 if (!file::AppendArgsFromFile(path, &all_feature_flags_args, &error)) {
913 context.GetDiagnostics()->Error(android::DiagMessage(path) << error);
914 return 1;
915 }
916 } else {
917 all_feature_flags_args.push_back(arg);
918 }
919 }
920
921 for (const std::string& arg : all_feature_flags_args) {
922 if (!ParseFeatureFlagsParameter(arg, context.GetDiagnostics(), &options_.feature_flag_values)) {
923 return 1;
924 }
925 }
926
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700927 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700928}
929
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700930} // namespace aapt