blob: 32686538c10d5c6e5ff95fd259ec7577677fc1f1 [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>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <string>
21
Adam Lesinskid5083f62017-01-16 15:07:21 -080022#include "android-base/errors.h"
23#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070024#include "android-base/utf8.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
27#include "google/protobuf/io/coded_stream.h"
28#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "Diagnostics.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "ResourceParser.h"
32#include "ResourceTable.h"
Izabela Orlowska10560192018-04-13 11:56:35 +010033#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070035#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080037#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070039#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070040#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070041#include "format/proto/ProtoSerialize.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"
50#include "util/Maybe.h"
51#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080052#include "xml/XmlDom.h"
53#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070055using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000056using ::aapt::text::Printer;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020057using ::android::ConfigDescription;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070058using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070059using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070060using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070061
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062namespace aapt {
63
64struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 std::string name;
68 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070070 // Original config str. We keep this because when we parse the config, we may add on
71 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075};
76
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070077// Resource file paths are expected to look like: [--/res/]type[-config]/name
Ryan Mitchell0ce89732018-10-03 09:20:57 -070078static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path, const char dir_sep,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 std::string* out_error) {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070080 std::vector<std::string> parts = util::Split(path, dir_sep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 return {};
84 }
85
86 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 size_t dash_pos = dir.find('-');
92 if (dash_pos != std::string::npos) {
93 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
94 if (!ConfigDescription::Parse(config_str, &config)) {
95 if (out_error) {
96 std::stringstream err_str;
97 err_str << "invalid configuration '" << config_str << "'";
98 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 }
100 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 std::string& filename = parts[parts.size() - 1];
106 StringPiece name = filename;
107 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700108
109 const std::string kNinePng = ".9.png";
110 if (filename.size() > kNinePng.size()
111 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
112 // Split on .9.png if this extension is present at the end of the file path
113 name = name.substr(0, filename.size() - kNinePng.size());
114 extension = "9.png";
115 } else {
116 // Split on the last period occurrence
117 size_t dot_pos = filename.rfind('.');
118 if (dot_pos != std::string::npos) {
119 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
120 name = name.substr(0, dot_pos);
121 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123
Adam Lesinskid5083f62017-01-16 15:07:21 -0800124 return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(),
125 extension.to_string(), config_str.to_string(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126}
127
Adam Lesinski00451162017-10-03 07:44:08 -0700128static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 name << data.resource_dir;
131 if (!data.config_str.empty()) {
132 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 }
134 name << "_" << data.name;
135 if (!data.extension.empty()) {
136 name << "." << data.extension;
137 }
138 name << ".flat";
139 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800140}
141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700143 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800145 TRACE_CALL();
Mihai Nitad1a65212019-03-26 13:47:45 -0700146 // Filenames starting with "donottranslate" are not localizable
147 bool translatable_file = path_data.name.find("donottranslate") != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 ResourceTable table;
149 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700150 auto fin = file->OpenInputStream();
151 if (fin->HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700153 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155 }
156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 // Parse the values file from XML.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700158 xml::XmlPullParser xml_parser(fin.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 ResourceParserOptions parser_options;
161 parser_options.error_on_positional_arguments = !options.legacy_mode;
Donald Chai94e4a012020-01-06 13:52:41 -0800162 parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables;
Mihai Nitad1a65212019-03-26 13:47:45 -0700163 parser_options.translatable = translatable_file;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100165 // If visibility was forced, we need to use it when creating a new resource and also error if
166 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
167 parser_options.visibility = options.visibility;
168
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700169 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700170 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800173 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800175
Mihai Nitad1a65212019-03-26 13:47:45 -0700176 if (options.pseudolocalize && translatable_file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 // Generate pseudo-localized strings (en-XA and ar-XB).
178 // These are created as weak symbols, and are only generated from default
179 // configuration
180 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 PseudolocaleGenerator pseudolocale_generator;
182 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700184 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189
190 // Assign an ID to any package that has resources.
191 for (auto& pkg : table.packages) {
192 if (!pkg->id) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700193 // If no package ID was set while parsing (public identifiers), auto assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700197
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700200 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 return false;
202 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800203
Adam Lesinski00451162017-10-03 07:44:08 -0700204 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 {
Adam Lesinski00451162017-10-03 07:44:08 -0700206 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700208 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700210 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700211 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700212 if (!container_writer.AddResTableEntry(pb_table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700213 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800217
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700219 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 return false;
221 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000222
223 if (options.generate_text_symbols_path) {
224 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
225
226 if (fout_text.HadError()) {
227 context->GetDiagnostics()->Error(DiagMessage()
228 << "failed writing to'"
229 << options.generate_text_symbols_path.value()
230 << "': " << fout_text.GetError());
231 return false;
232 }
233
234 Printer r_txt_printer(&fout_text);
235 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100236 // Only print resources defined locally, e.g. don't write android attributes.
237 if (package->name.empty()) {
238 for (const auto& type : package->types) {
239 for (const auto& entry : type->entries) {
240 // Check access modifiers.
241 switch (entry->visibility.level) {
242 case Visibility::Level::kUndefined :
243 r_txt_printer.Print("default ");
244 break;
245 case Visibility::Level::kPublic :
246 r_txt_printer.Print("public ");
247 break;
248 case Visibility::Level::kPrivate :
249 r_txt_printer.Print("private ");
250 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000251
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100252 if (type->type != ResourceType::kStyleable) {
253 r_txt_printer.Print("int ");
254 r_txt_printer.Print(to_string(type->type));
255 r_txt_printer.Print(" ");
256 r_txt_printer.Println(entry->name);
257 } else {
258 r_txt_printer.Print("int[] styleable ");
259 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000260
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100261 if (!entry->values.empty()) {
262 auto styleable =
263 static_cast<const Styleable*>(entry->values.front()->value.get());
264 for (const auto& attr : styleable->entries) {
265 // The visibility of the children under the styleable does not matter as they are
266 // nested under their parent and use its visibility.
267 r_txt_printer.Print("default int styleable ");
268 r_txt_printer.Print(entry->name);
269 // If the package name is present, also include it in the mangled name (e.g.
270 // "android")
271 if (!attr.name.value().package.empty()) {
272 r_txt_printer.Print("_");
273 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
274 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100275 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100276 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100277 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000278 }
279 }
280 }
281 }
282 }
283 }
284 }
285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800287}
288
Adam Lesinski00451162017-10-03 07:44:08 -0700289static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
290 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800291 IDiagnostics* diag) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800292 TRACE_CALL();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 if (!writer->StartEntry(output_path, 0)) {
295 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return false;
297 }
298
Adam Lesinski00451162017-10-03 07:44:08 -0700299 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 {
Adam Lesinski00451162017-10-03 07:44:08 -0700301 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700303 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700305 pb::internal::CompiledFile pb_compiled_file;
306 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307
Adam Lesinski00451162017-10-03 07:44:08 -0700308 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
309 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800311 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (!writer->FinishEntry()) {
315 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return false;
317 }
318 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700319}
320
Adam Lesinski00451162017-10-03 07:44:08 -0700321static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
322 ContainerWriter* container_writer, IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700323 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700324 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700325
Adam Lesinski00451162017-10-03 07:44:08 -0700326 pb::XmlNode pb_xml_node;
327 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
328
329 std::string serialized_xml = pb_xml_node.SerializeAsString();
330 io::StringInputStream serialized_in(serialized_xml);
331
332 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
333 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 return false;
335 }
336 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700337}
338
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700339static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700340 const file::FileType file_type = file::GetFileType(input_path);
341 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
342 if (file_type == file::FileType::kDirectory) {
343 context->GetDiagnostics()->Error(DiagMessage(input_path)
344 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700345 } else if (file_type == file::FileType::kNonexistant) {
346 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700347 } else {
348 context->GetDiagnostics()->Error(DiagMessage(input_path)
349 << "not a valid resource file");
350 }
351 return false;
352 }
353 return true;
354}
355
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700357 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700358 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800359 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700361 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 }
363
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700364 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700366 auto fin = file->OpenInputStream();
367 if (fin->HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700369 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700371 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700372
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700373 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
374 if (!xmlres) {
375 return false;
376 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700377 }
378
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700379 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700380 xmlres->file.config = path_data.config;
381 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700382 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383
384 // Collect IDs that are defined here.
385 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387 return false;
388 }
389
390 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 InlineXmlFormatParser inline_xml_format_parser;
392 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 return false;
394 }
395
396 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700397 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700398 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 return false;
400 }
401
Adam Lesinski00451162017-10-03 07:44:08 -0700402 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
403 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
404
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700405 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700406 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700407 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700409 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410
Adam Lesinski00451162017-10-03 07:44:08 -0700411 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
412 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700414 }
415
Adam Lesinski00451162017-10-03 07:44:08 -0700416 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
417 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
418 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700419 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700420 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700421 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700422 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700423
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700425 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 return false;
427 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000428
429 if (options.generate_text_symbols_path) {
430 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
431
432 if (fout_text.HadError()) {
433 context->GetDiagnostics()->Error(DiagMessage()
434 << "failed writing to'"
435 << options.generate_text_symbols_path.value()
436 << "': " << fout_text.GetError());
437 return false;
438 }
439
440 Printer r_txt_printer(&fout_text);
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800441 for (const auto& res : xmlres->file.exported_symbols) {
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000442 r_txt_printer.Print("default int id ");
443 r_txt_printer.Println(res.name.entry);
444 }
445
446 // And print ourselves.
447 r_txt_printer.Print("default int ");
448 r_txt_printer.Print(path_data.resource_dir);
449 r_txt_printer.Print(" ");
450 r_txt_printer.Println(path_data.name);
451 }
452
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700454}
455
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700457 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700458 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800459 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700460 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700461 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700462 }
463
464 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700465 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700466 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700467 res_file.config = path_data.config;
468 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700469 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470
471 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700472 auto data = file->OpenAsData();
473 if (!data) {
474 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700476 }
477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700479 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700480
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 // Ensure that we only keep the chunks we care about if we end up
482 // using the original PNG instead of the crunched one.
Ryan Mitchellf67808b2019-01-22 16:16:13 -0800483 const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700484 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700485 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700486 if (!image) {
487 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700488 }
489
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 std::unique_ptr<NinePatch> nine_patch;
491 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700492 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700493 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494 if (!nine_patch) {
495 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700496 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 }
498
499 // Remove the 1px border around the NinePatch.
500 // Basically the row array is shifted up by 1, and the length is treated
501 // as height - 2.
502 // For each row, shift the array to the left by 1, and treat the length as
503 // width - 2.
504 image->width -= 2;
505 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700506 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 for (int32_t h = 0; h < image->height; h++) {
508 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
509 }
510
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700511 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700512 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
513 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700515 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516
517 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700518 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700519 return false;
520 }
521
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 if (nine_patch != nullptr ||
523 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 // No matter what, we must use the re-encoded PNG, even if it is larger.
525 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700526 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 } else {
528 // The re-encoded PNG is larger than the original, and there is
529 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700531 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
532 << "original PNG is smaller than crunched PNG"
533 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700534 }
535
Adam Lesinski06460ef2017-03-14 18:52:13 -0700536 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700537 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700538 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
539 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700540 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
542
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700544 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
545 // This will help catch exotic cases where the new code may generate larger PNGs.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700546 std::stringstream legacy_stream(content.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700547 BigBuffer legacy_buffer(4096);
548 Png png(context->GetDiagnostics());
549 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700550 return false;
551 }
552
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700553 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
554 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 << " new=" << buffer.size());
556 }
557 }
558
Adam Lesinski00451162017-10-03 07:44:08 -0700559 io::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700560 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
561 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700562}
563
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700565 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 const std::string& output_path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800567 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700568 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700569 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700573 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700574 res_file.config = path_data.config;
575 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700576 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700577
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700578 auto data = file->OpenAsData();
579 if (!data) {
580 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700581 return false;
582 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700583
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700584 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
585 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700586}
587
588class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 public:
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800590 explicit CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
Chris Warrington820d72a2017-04-27 15:27:01 +0100591 }
592
Adam Lesinskib522f042017-04-21 16:57:59 -0700593 PackageType GetPackageType() override {
594 // Every compilation unit starts as an app and then gets linked as potentially something else.
595 return PackageType::kApp;
596 }
597
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700598 void SetVerbose(bool val) {
599 verbose_ = val;
600 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800601
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700602 bool IsVerbose() override {
603 return verbose_;
604 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800605
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700606 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100607 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700608 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700611 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 return nullptr;
613 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700614
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700615 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700616 static std::string empty;
617 return empty;
618 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700619
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700620 uint8_t GetPackageId() override {
621 return 0x0;
622 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700625 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700626 return nullptr;
627 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800628
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700629 int GetMinSdkVersion() override {
630 return 0;
631 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700632
Udam Sainib228df32019-06-18 16:50:34 -0700633 const std::set<std::string>& GetSplitNameDependencies() override {
634 UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase";
635 static std::set<std::string> empty;
636 return empty;
637 }
638
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700639 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700640 DISALLOW_COPY_AND_ASSIGN(CompileContext);
641
Chris Warrington820d72a2017-04-27 15:27:01 +0100642 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700643 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644};
645
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700646int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
647 CompileOptions& options) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800648 TRACE_CALL();
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700649 bool error = false;
650
651 // Iterate over the input files in a stable, platform-independent manner
652 auto file_iterator = inputs->Iterator();
653 while (file_iterator->HasNext()) {
654 auto file = file_iterator->Next();
655 std::string path = file->GetSource().path;
656
657 // Skip hidden input files
658 if (file::IsHidden(path)) {
659 continue;
660 }
661
662 if (!options.res_zip && !IsValidFile(context, path)) {
663 error = true;
664 continue;
665 }
666
667 // Extract resource type information from the full path
668 std::string err_str;
669 ResourcePathData path_data;
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700670 if (auto maybe_path_data = ExtractResourcePathData(path, inputs->GetDirSeparator(), &err_str)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700671 path_data = maybe_path_data.value();
672 } else {
673 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << err_str);
674 error = true;
675 continue;
676 }
677
678 // Determine how to compile the file based on its type.
679 auto compile_func = &CompileFile;
680 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
681 compile_func = &CompileTable;
682 // We use a different extension (not necessary anymore, but avoids altering the existing
683 // build system logic).
684 path_data.extension = "arsc";
685
686 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
687 if (*type != ResourceType::kRaw) {
Ryan Mitchell62b68342019-03-11 13:28:02 -0700688 if (*type == ResourceType::kXml || path_data.extension == "xml") {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700689 compile_func = &CompileXml;
690 } else if ((!options.no_png_crunch && path_data.extension == "png")
691 || path_data.extension == "9.png") {
692 compile_func = &CompilePng;
693 }
694 }
695 } else {
696 context->GetDiagnostics()->Error(DiagMessage()
697 << "invalid file path '" << path_data.source << "'");
698 error = true;
699 continue;
700 }
701
702 // Treat periods as a reserved character that should not be present in a file name
703 // Legacy support for AAPT which did not reserve periods
704 if (compile_func != &CompileFile && !options.legacy_mode
705 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
706 error = true;
707 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
708 << "file name cannot contain '.' other than for"
709 << " specifying the extension");
710 continue;
711 }
712
713 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Izabela Orlowskaa3ab21f2019-01-17 12:09:59 +0000714 if (!compile_func(context, options, path_data, file, output_writer, out_path)) {
715 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "file failed to compile");
716 error = true;
717 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700718 }
719
720 return error ? 1 : 0;
721}
722
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700723int CompileCommand::Action(const std::vector<std::string>& args) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800724 TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700725 CompileContext context(diagnostic_);
726 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700727
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700728 if (visibility_) {
729 if (visibility_.value() == "public") {
730 options_.visibility = Visibility::Level::kPublic;
731 } else if (visibility_.value() == "private") {
732 options_.visibility = Visibility::Level::kPrivate;
733 } else if (visibility_.value() == "default") {
734 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100735 } else {
736 context.GetDiagnostics()->Error(
737 DiagMessage() << "Unrecognized visibility level passes to --visibility: '"
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700738 << visibility_.value() << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100739 return 1;
740 }
741 }
742
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700743 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700744
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700745 // Collect the resources files to compile
746 if (options_.res_dir && options_.res_zip) {
747 context.GetDiagnostics()->Error(DiagMessage()
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700748 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700749 return 1;
750 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700751 if (!args.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700752 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700753 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700754 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700755 }
756
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700757 // Load the files from the res directory
758 std::string err;
759 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
760 if (!file_collection) {
761 context.GetDiagnostics()->Error(DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 return 1;
763 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700764 } else if (options_.res_zip) {
765 if (!args.empty()) {
766 context.GetDiagnostics()->Error(DiagMessage() << "files given but --zip specified");
767 Usage(&std::cerr);
768 return 1;
769 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700770
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700771 // Load a zip file containing a res directory
772 std::string err;
773 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
774 if (!file_collection) {
775 context.GetDiagnostics()->Error(DiagMessage(options_.res_zip.value()) << err);
776 return 1;
777 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700778 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700779 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800780
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700781 // Collect data from the path for each input file.
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800782 std::vector<std::string> sorted_args = args;
783 std::sort(sorted_args.begin(), sorted_args.end());
784
785 for (const std::string& arg : sorted_args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700786 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700787 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800788
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700789 file_collection = std::move(collection);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700790 }
791
792 std::unique_ptr<IArchiveWriter> archive_writer;
793 file::FileType output_file_type = file::GetFileType(options_.output_path);
794 if (output_file_type == file::FileType::kDirectory) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700795 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellc7db2442019-08-28 11:21:47 -0700796 } else {
797 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700798 }
799
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700800 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700801 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700802 }
803
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700804 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700805}
806
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700807} // namespace aapt