blob: b5c290ec8dadcfe823ee1d67571210d13e1e6715 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ryan Mitchell833a1a62018-07-10 13:51:36 -070017#include "Compile.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Ryan Mitchell833a1a62018-07-10 13:51:36 -070019#include <dirent.h>
Jeremy Meyer56f36e82022-05-20 20:35:42 +000020
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include <string>
22
Jeremy Meyer56f36e82022-05-20 20:35:42 +000023#include "ResourceParser.h"
24#include "ResourceTable.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "android-base/errors.h"
26#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070027#include "android-base/utf8.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020028#include "androidfw/ConfigDescription.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000029#include "androidfw/IDiagnostics.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080030#include "androidfw/StringPiece.h"
Izabela Orlowska10560192018-04-13 11:56:35 +010031#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070033#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080035#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070037#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070038#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070039#include "format/proto/ProtoSerialize.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000040#include "google/protobuf/io/coded_stream.h"
41#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
Adam Lesinski00451162017-10-03 07:44:08 -070042#include "io/BigBufferStream.h"
43#include "io/FileStream.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070044#include "io/FileSystem.h"
Adam Lesinski00451162017-10-03 07:44:08 -070045#include "io/StringStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070046#include "io/Util.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070047#include "io/ZipArchive.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080048#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049#include "util/Files.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080051#include "xml/XmlDom.h"
52#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070054using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000055using ::aapt::text::Printer;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020056using ::android::ConfigDescription;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070057using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070058using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070059using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070060
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061namespace aapt {
62
63struct ResourcePathData {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000064 android::Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 std::string name;
67 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070069 // Original config str. We keep this because when we parse the config, we may add on
70 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074};
75
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076// Resource file paths are expected to look like: [--/res/]type[-config]/name
Ryan Mitchell4382e442021-07-14 12:53:01 -070077static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path,
78 const char dir_sep,
79 std::string* out_error,
80 const CompileOptions& options) {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070081 std::vector<std::string> parts = util::Split(path, dir_sep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return {};
85 }
86
87 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 size_t dash_pos = dir.find('-');
93 if (dash_pos != std::string::npos) {
94 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
95 if (!ConfigDescription::Parse(config_str, &config)) {
96 if (out_error) {
97 std::stringstream err_str;
98 err_str << "invalid configuration '" << config_str << "'";
99 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 }
101 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 std::string& filename = parts[parts.size() - 1];
107 StringPiece name = filename;
108 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700109
110 const std::string kNinePng = ".9.png";
111 if (filename.size() > kNinePng.size()
112 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
113 // Split on .9.png if this extension is present at the end of the file path
114 name = name.substr(0, filename.size() - kNinePng.size());
115 extension = "9.png";
116 } else {
117 // Split on the last period occurrence
118 size_t dot_pos = filename.rfind('.');
119 if (dot_pos != std::string::npos) {
120 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
121 name = name.substr(0, dot_pos);
122 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000125 const android::Source res_path =
126 options.source_path ? StringPiece(options.source_path.value()) : StringPiece(path);
lukeedgar19b8ebf2020-06-23 10:43:42 +0100127
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700128 return ResourcePathData{res_path,
129 std::string(dir_str),
130 std::string(name),
131 std::string(extension),
132 std::string(config_str),
133 config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134}
135
Adam Lesinski00451162017-10-03 07:44:08 -0700136static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 name << data.resource_dir;
139 if (!data.config_str.empty()) {
140 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 }
142 name << "_" << data.name;
143 if (!data.extension.empty()) {
144 name << "." << data.extension;
145 }
146 name << ".flat";
147 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800148}
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700151 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800153 TRACE_CALL();
Mihai Nitad1a65212019-03-26 13:47:45 -0700154 // Filenames starting with "donottranslate" are not localizable
155 bool translatable_file = path_data.name.find("donottranslate") != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 ResourceTable table;
157 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700158 auto fin = file->OpenInputStream();
159 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000160 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
161 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163 }
164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 // Parse the values file from XML.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700166 xml::XmlPullParser xml_parser(fin.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 ResourceParserOptions parser_options;
169 parser_options.error_on_positional_arguments = !options.legacy_mode;
Donald Chai94e4a012020-01-06 13:52:41 -0800170 parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables;
Mihai Nitad1a65212019-03-26 13:47:45 -0700171 parser_options.translatable = translatable_file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100173 // If visibility was forced, we need to use it when creating a new resource and also error if
174 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
175 parser_options.visibility = options.visibility;
176
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700177 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700178 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800181 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800183
Mihai Nitad1a65212019-03-26 13:47:45 -0700184 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 // Generate pseudo-localized strings (en-XA and ar-XB).
186 // These are created as weak symbols, and are only generated from default
187 // configuration
188 // strings and plurals.
Brandon Liu5274e062023-05-25 22:41:10 +0000189 std::string grammatical_gender_values;
190 std::string grammatical_gender_ratio;
191 if (options.pseudo_localize_gender_values) {
192 grammatical_gender_values = options.pseudo_localize_gender_values.value();
193 } else {
194 grammatical_gender_values = "f,m,n";
195 }
196 if (options.pseudo_localize_gender_ratio) {
197 grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value();
198 } else {
199 grammatical_gender_ratio = "1.0";
200 }
201 PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values,
202 grammatical_gender_ratio);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700205 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700207
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000210 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 return false;
212 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800213
Adam Lesinski00451162017-10-03 07:44:08 -0700214 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 {
Adam Lesinski00451162017-10-03 07:44:08 -0700216 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700218 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700220 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700221 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700222 if (!container_writer.AddResTableEntry(pb_table)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000223 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800227
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000229 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 return false;
231 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000232
233 if (options.generate_text_symbols_path) {
234 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
235
236 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000237 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000238 << "failed writing to'"
239 << options.generate_text_symbols_path.value()
240 << "': " << fout_text.GetError());
241 return false;
242 }
243
244 Printer r_txt_printer(&fout_text);
245 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100246 // Only print resources defined locally, e.g. don't write android attributes.
247 if (package->name.empty()) {
248 for (const auto& type : package->types) {
249 for (const auto& entry : type->entries) {
250 // Check access modifiers.
251 switch (entry->visibility.level) {
252 case Visibility::Level::kUndefined :
253 r_txt_printer.Print("default ");
254 break;
255 case Visibility::Level::kPublic :
256 r_txt_printer.Print("public ");
257 break;
258 case Visibility::Level::kPrivate :
259 r_txt_printer.Print("private ");
260 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000261
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000262 if (type->named_type.type != ResourceType::kStyleable) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100263 r_txt_printer.Print("int ");
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000264 r_txt_printer.Print(type->named_type.to_string());
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100265 r_txt_printer.Print(" ");
266 r_txt_printer.Println(entry->name);
267 } else {
268 r_txt_printer.Print("int[] styleable ");
269 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000270
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100271 if (!entry->values.empty()) {
272 auto styleable =
273 static_cast<const Styleable*>(entry->values.front()->value.get());
274 for (const auto& attr : styleable->entries) {
275 // The visibility of the children under the styleable does not matter as they are
276 // nested under their parent and use its visibility.
277 r_txt_printer.Print("default int styleable ");
278 r_txt_printer.Print(entry->name);
279 // If the package name is present, also include it in the mangled name (e.g.
280 // "android")
281 if (!attr.name.value().package.empty()) {
282 r_txt_printer.Print("_");
283 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
284 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100285 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100286 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100287 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000288 }
289 }
290 }
291 }
292 }
293 }
294 }
295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800297}
298
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700299static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file,
Adam Lesinski00451162017-10-03 07:44:08 -0700300 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000301 android::IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800302 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000305 diag->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return false;
307 }
308
Adam Lesinski00451162017-10-03 07:44:08 -0700309 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 {
Adam Lesinski00451162017-10-03 07:44:08 -0700311 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700312 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700313 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700315 pb::internal::CompiledFile pb_compiled_file;
316 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317
Adam Lesinski00451162017-10-03 07:44:08 -0700318 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000319 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800321 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800323
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000325 diag->Error(android::DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 return false;
327 }
328 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700329}
330
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700331static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000332 ContainerWriter* container_writer, android::IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700333 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700334 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700335
Adam Lesinski00451162017-10-03 07:44:08 -0700336 pb::XmlNode pb_xml_node;
337 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
338
339 std::string serialized_xml = pb_xml_node.SerializeAsString();
340 io::StringInputStream serialized_in(serialized_xml);
341
342 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000343 diag->Error(android::DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return false;
345 }
346 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700347}
348
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700349static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700350 const file::FileType file_type = file::GetFileType(input_path);
351 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
352 if (file_type == file::FileType::kDirectory) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000353 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700354 << "resource file cannot be a directory");
Ryan Mitchell4382e442021-07-14 12:53:01 -0700355 } else if (file_type == file::FileType::kNonExistant) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000356 context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700357 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000358 context->GetDiagnostics()->Error(android::DiagMessage(input_path)
Adam Lesinski776aa952017-04-24 15:09:32 -0700359 << "not a valid resource file");
360 }
361 return false;
362 }
363 return true;
364}
365
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700366static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700367 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700368 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800369 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000371 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 }
373
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700375 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700376 auto fin = file->OpenInputStream();
377 if (fin->HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000378 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700379 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700381 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700382
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700383 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
384 if (!xmlres) {
385 return false;
386 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 }
388
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700389 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700390 xmlres->file.config = path_data.config;
391 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700392 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393
394 // Collect IDs that are defined here.
395 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700396 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 return false;
398 }
399
400 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 InlineXmlFormatParser inline_xml_format_parser;
402 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 return false;
404 }
405
406 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 if (!writer->StartEntry(output_path, 0)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000408 context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 return false;
410 }
411
Adam Lesinski00451162017-10-03 07:44:08 -0700412 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
413 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
414
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700415 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700416 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700417 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700419 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420
Adam Lesinski00451162017-10-03 07:44:08 -0700421 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
422 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700424 }
425
Adam Lesinski00451162017-10-03 07:44:08 -0700426 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
427 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
428 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700429 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700431 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700432 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (!writer->FinishEntry()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000435 context->GetDiagnostics()->Error(android::DiagMessage(output_path)
436 << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437 return false;
438 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000439
440 if (options.generate_text_symbols_path) {
441 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
442
443 if (fout_text.HadError()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000444 context->GetDiagnostics()->Error(android::DiagMessage()
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000445 << "failed writing to'"
446 << options.generate_text_symbols_path.value()
447 << "': " << fout_text.GetError());
448 return false;
449 }
450
451 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800452 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000453 r_txt_printer.Print("default int id ");
454 r_txt_printer.Println(res.name.entry);
455 }
456
457 // And print ourselves.
458 r_txt_printer.Print("default int ");
459 r_txt_printer.Print(path_data.resource_dir);
460 r_txt_printer.Print(" ");
461 r_txt_printer.Println(path_data.name);
462 }
463
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700465}
466
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700468 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700469 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800470 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700471 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000472 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700473 }
474
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000475 android::BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700476 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700477 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 res_file.config = path_data.config;
479 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700480 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481
482 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700483 auto data = file->OpenAsData();
484 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000485 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
486 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700488 }
489
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000490 android::BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700491 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700492
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 // Ensure that we only keep the chunks we care about if we end up
494 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800495 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700497 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700498 if (!image) {
499 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700500 }
501
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700502 std::unique_ptr<NinePatch> nine_patch;
503 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700505 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 if (!nine_patch) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000507 context->GetDiagnostics()->Error(android::DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700508 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 }
510
511 // Remove the 1px border around the NinePatch.
512 // Basically the row array is shifted up by 1, and the length is treated
513 // as height - 2.
514 // For each row, shift the array to the left by 1, and treat the length as
515 // width - 2.
516 image->width -= 2;
517 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700518 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 for (int32_t h = 0; h < image->height; h++) {
520 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
521 }
522
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700523 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000524 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
525 << "9-patch: " << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700527 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528
529 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700530 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 return false;
532 }
533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 if (nine_patch != nullptr ||
535 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 // No matter what, we must use the re-encoded PNG, even if it is larger.
537 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 } else {
540 // The re-encoded PNG is larger than the original, and there is
541 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700542 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000543 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinski06460ef2017-03-14 18:52:13 -0700544 << "original PNG is smaller than crunched PNG"
545 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 }
547
Adam Lesinski06460ef2017-03-14 18:52:13 -0700548 png_chunk_filter.Rewind();
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000549 android::BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700550 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
551 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700552 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 }
554
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700555 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700556 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
557 // This will help catch exotic cases where the new code may generate larger PNGs.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700558 std::stringstream legacy_stream{std::string(content)};
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000559 android::BigBuffer legacy_buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 Png png(context->GetDiagnostics());
561 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 return false;
563 }
564
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000565 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 << " new=" << buffer.size());
568 }
569 }
570
Adam Lesinski00451162017-10-03 07:44:08 -0700571 io::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700572 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
573 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700574}
575
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700576static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700577 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700578 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800579 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700580 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000581 context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700583
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700584 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700585 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 res_file.config = path_data.config;
587 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700588 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700589
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700590 auto data = file->OpenAsData();
591 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000592 context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
593 << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700594 return false;
595 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700596
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700597 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
598 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700599}
600
601class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000603 explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100604 }
605
Adam Lesinskib522f042017-04-21 16:57:59 -0700606 PackageType GetPackageType() override {
607 // Every compilation unit starts as an app and then gets linked as potentially something else.
608 return PackageType::kApp;
609 }
610
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700611 void SetVerbose(bool val) {
612 verbose_ = val;
Brandon Liu48d229d2023-05-04 23:54:03 +0000613 diagnostics_->SetVerbose(val);
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700614 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800615
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700616 bool IsVerbose() override {
617 return verbose_;
618 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800619
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000620 android::IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100621 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700622 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700625 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 return nullptr;
627 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700628
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700629 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 static std::string empty;
631 return empty;
632 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700633
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700634 uint8_t GetPackageId() override {
635 return 0x0;
636 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700637
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700638 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700639 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700640 return nullptr;
641 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800642
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700643 int GetMinSdkVersion() override {
644 return 0;
645 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700646
Udam Sainib228df32019-06-18 16:50:34 -0700647 const std::set<std::string>& GetSplitNameDependencies() override {
648 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
649 static std::set<std::string> empty;
650 return empty;
651 }
652
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700654 DISALLOW_COPY_AND_ASSIGN(CompileContext);
655
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000656 android::IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700657 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700658};
659
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700660int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
661 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800662 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700663 bool error = false;
664
665 // Iterate over the input files in a stable, platform-independent manner
666 auto file_iterator = inputs->Iterator();
667 while (file_iterator->HasNext()) {
668 auto file = file_iterator->Next();
669 std::string path = file->GetSource().path;
670
671 // Skip hidden input files
672 if (file::IsHidden(path)) {
673 continue;
674 }
675
676 if (!options.res_zip && !IsValidFile(context, path)) {
677 error = true;
678 continue;
679 }
680
681 // Extract resource type information from the full path
682 std::string err_str;
683 ResourcePathData path_data;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100684 if (auto maybe_path_data = ExtractResourcePathData(
685 path, inputs->GetDirSeparator(), &err_str, options)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700686 path_data = maybe_path_data.value();
687 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000688 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700689 error = true;
690 continue;
691 }
692
693 // Determine how to compile the file based on its type.
694 auto compile_func = &CompileFile;
695 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
696 compile_func = &CompileTable;
697 // We use a different extension (not necessary anymore, but avoids altering the existing
698 // build system logic).
699 path_data.extension = "arsc";
700
701 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
702 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700703 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700704 compile_func = &CompileXml;
705 } else if ((!options.no_png_crunch && path_data.extension == "png")
706 || path_data.extension == "9.png") {
707 compile_func = &CompilePng;
708 }
709 }
710 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000711 context->GetDiagnostics()->Error(android::DiagMessage()
712 << "invalid file path '" << path_data.source << "'");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700713 error = true;
714 continue;
715 }
716
717 // Treat periods as a reserved character that should not be present in a file name
718 // Legacy support for AAPT which did not reserve periods
719 if (compile_func != &CompileFile && !options.legacy_mode
720 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
721 error = true;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000722 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
723 << "file name cannot contain '.' other than for"
724 << " specifying the extension");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700725 continue;
726 }
727
728 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000729 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000730 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
731 << "file failed to compile");
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000732 error = true;
733 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700734 }
735
736 return error ? 1 : 0;
737}
738
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700739int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800740 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700741 CompileContext context(diagnostic_);
742 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700743
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700744 if (visibility_) {
745 if (visibility_.value() == "public") {
746 options_.visibility = Visibility::Level::kPublic;
747 } else if (visibility_.value() == "private") {
748 options_.visibility = Visibility::Level::kPrivate;
749 } else if (visibility_.value() == "default") {
750 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100751 } else {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000752 context.GetDiagnostics()->Error(android::DiagMessage()
753 << "Unrecognized visibility level passes to --visibility: '"
754 << visibility_.value()
755 << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100756 return 1;
757 }
758 }
759
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700760 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700761
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700762 // Collect the resources files to compile
763 if (options_.res_dir && options_.res_zip) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000764 context.GetDiagnostics()->Error(android::DiagMessage()
765 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700766 return 1;
lukeedgar19b8ebf2020-06-23 10:43:42 +0100767 } else if ((options_.res_dir || options_.res_zip) &&
768 options_.source_path && args.size() > 1) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000769 context.GetDiagnostics()->Error(android::DiagMessage(kPath)
770 << "Cannot use an overriding source path with multiple files.");
771 return 1;
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700772 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700773 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000774 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700775 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700776 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700777 }
778
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700779 // Load the files from the res directory
780 std::string err;
781 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
782 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000783 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700784 return 1;
785 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700786 } else if (options_.res_zip) {
787 if (!args.empty()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000788 context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700789 Usage(&std::cerr);
790 return 1;
791 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700792
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700793 // Load a zip file containing a res directory
794 std::string err;
795 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
796 if (!file_collection) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000797 context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700798 return 1;
799 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700800 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700801 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800802
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700803 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800804 std::vector<std::string> sorted_args = args;
805 std::sort(sorted_args.begin(), sorted_args.end());
806
807 for (const std::string& arg : sorted_args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700808 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700809 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800810
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700811 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700812 }
813
814 std::unique_ptr<IArchiveWriter> archive_writer;
815 file::FileType output_file_type = file::GetFileType(options_.output_path);
816 if (output_file_type == file::FileType::kDirectory) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700817 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700818 } else {
819 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700820 }
821
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700822 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700823 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700824 }
825
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700826 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700827}
828
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700829} // namespace aapt