blob: 411ad747faa3b3101a51f5483b2abf8f8eaa31f8 [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <dirent.h>
18
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include <string>
20
Adam Lesinskid5083f62017-01-16 15:07:21 -080021#include "android-base/errors.h"
22#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070023#include "android-base/utf8.h"
Mårten Kongstad5c541f62018-06-20 08:46:41 +020024#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
26#include "google/protobuf/io/coded_stream.h"
27#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include "Diagnostics.h"
30#include "Flags.h"
31#include "ResourceParser.h"
32#include "ResourceTable.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070034#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080036#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070037#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070038#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070039#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070040#include "format/proto/ProtoSerialize.h"
Adam Lesinski00451162017-10-03 07:44:08 -070041#include "io/BigBufferStream.h"
42#include "io/FileStream.h"
43#include "io/StringStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070044#include "io/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045#include "util/Files.h"
46#include "util/Maybe.h"
47#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080048#include "xml/XmlDom.h"
49#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070051using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000052using ::aapt::text::Printer;
Mårten Kongstad5c541f62018-06-20 08:46:41 +020053using ::android::ConfigDescription;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070054using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070055using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070056using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070057
Adam Lesinski1ab598f2015-08-14 14:26:04 -070058namespace aapt {
59
60struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 std::string name;
64 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070066 // Original config str. We keep this because when we parse the config, we may add on
67 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071};
72
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070073// Resource file paths are expected to look like: [--/res/]type[-config]/name
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path,
75 std::string* out_error) {
76 std::vector<std::string> parts = util::Split(path, file::sDirSep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 return {};
80 }
81
82 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 size_t dash_pos = dir.find('-');
88 if (dash_pos != std::string::npos) {
89 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
90 if (!ConfigDescription::Parse(config_str, &config)) {
91 if (out_error) {
92 std::stringstream err_str;
93 err_str << "invalid configuration '" << config_str << "'";
94 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 }
96 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 std::string& filename = parts[parts.size() - 1];
102 StringPiece name = filename;
103 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700104
105 const std::string kNinePng = ".9.png";
106 if (filename.size() > kNinePng.size()
107 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
108 // Split on .9.png if this extension is present at the end of the file path
109 name = name.substr(0, filename.size() - kNinePng.size());
110 extension = "9.png";
111 } else {
112 // Split on the last period occurrence
113 size_t dot_pos = filename.rfind('.');
114 if (dot_pos != std::string::npos) {
115 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
116 name = name.substr(0, dot_pos);
117 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119
Adam Lesinskid5083f62017-01-16 15:07:21 -0800120 return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(),
121 extension.to_string(), config_str.to_string(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122}
123
124struct CompileOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 std::string output_path;
126 Maybe<std::string> res_dir;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000127 Maybe<std::string> generate_text_symbols_path;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 bool pseudolocalize = false;
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700129 bool no_png_crunch = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 bool legacy_mode = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 bool verbose = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132};
133
Adam Lesinski00451162017-10-03 07:44:08 -0700134static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 name << data.resource_dir;
137 if (!data.config_str.empty()) {
138 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 }
140 name << "_" << data.name;
141 if (!data.extension.empty()) {
142 name << "." << data.extension;
143 }
144 name << ".flat";
145 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800146}
147
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148static bool IsHidden(const StringPiece& filename) {
149 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800150}
151
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700152// Walks the res directory structure, looking for resource files.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700153static bool LoadInputFilesFromDir(IAaptContext* context, const CompileOptions& options,
154 std::vector<ResourcePathData>* out_path_data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 const std::string& root_dir = options.res_dir.value();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700156 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 if (!d) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700158 context->GetDiagnostics()->Error(DiagMessage(root_dir) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700159 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 return false;
161 }
162
163 while (struct dirent* entry = readdir(d.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 if (IsHidden(entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 continue;
166 }
167
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 std::string prefix_path = root_dir;
169 file::AppendPath(&prefix_path, entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (file::GetFileType(prefix_path) != file::FileType::kDirectory) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 continue;
173 }
174
Adam Lesinski06460ef2017-03-14 18:52:13 -0700175 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 if (!subdir) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700177 context->GetDiagnostics()->Error(DiagMessage(prefix_path) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700178 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 return false;
180 }
181
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 while (struct dirent* leaf_entry = readdir(subdir.get())) {
183 if (IsHidden(leaf_entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 continue;
185 }
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 std::string full_path = prefix_path;
188 file::AppendPath(&full_path, leaf_entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 std::string err_str;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700191 Maybe<ResourcePathData> path_data = ExtractResourcePathData(full_path, &err_str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 if (!path_data) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700193 context->GetDiagnostics()->Error(DiagMessage(full_path) << err_str);
Adam Lesinskia40e9722015-11-24 19:11:46 -0800194 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 }
196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 out_path_data->push_back(std::move(path_data.value()));
Adam Lesinskia40e9722015-11-24 19:11:46 -0800198 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 }
Adam Lesinskie6aa6d12017-12-20 14:01:14 -0800200
201 // File-system directory enumeration order is platform-dependent. Sort the result to remove any
202 // inconsistencies between platforms.
203 std::sort(
204 out_path_data->begin(), out_path_data->end(),
205 [](const ResourcePathData& a, const ResourcePathData& b) { return a.source < b.source; });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207}
208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700210 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700211 const std::string& output_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 ResourceTable table;
213 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700214 FileInputStream fin(path_data.source.path);
215 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700217 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219 }
220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 // Parse the values file from XML.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700222 xml::XmlPullParser xml_parser(&fin);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 ResourceParserOptions parser_options;
225 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700227 // If the filename includes donottranslate, then the default translatable is false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700228 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700230 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 parser_options);
232 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800234 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 if (options.pseudolocalize) {
238 // Generate pseudo-localized strings (en-XA and ar-XB).
239 // These are created as weak symbols, and are only generated from default
240 // configuration
241 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 PseudolocaleGenerator pseudolocale_generator;
243 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700245 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700247
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250
251 // Assign an ID to any package that has resources.
252 for (auto& pkg : table.packages) {
253 if (!pkg->id) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700254 // If no package ID was set while parsing (public identifiers), auto assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700261 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return false;
263 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800264
Adam Lesinski00451162017-10-03 07:44:08 -0700265 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 {
Adam Lesinski00451162017-10-03 07:44:08 -0700267 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700268 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700269 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700271 pb::ResourceTable pb_table;
Ryan Mitchell70414f22018-03-26 11:05:31 -0700272 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700273 if (!container_writer.AddResTableEntry(pb_table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700274 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700276 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700280 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 return false;
282 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000283
284 if (options.generate_text_symbols_path) {
285 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
286
287 if (fout_text.HadError()) {
288 context->GetDiagnostics()->Error(DiagMessage()
289 << "failed writing to'"
290 << options.generate_text_symbols_path.value()
291 << "': " << fout_text.GetError());
292 return false;
293 }
294
295 Printer r_txt_printer(&fout_text);
296 for (const auto& package : table.packages) {
297 for (const auto& type : package->types) {
298 for (const auto& entry : type->entries) {
299 // Check access modifiers.
300 switch(entry->visibility.level) {
301 case Visibility::Level::kUndefined :
302 r_txt_printer.Print("default ");
303 break;
304 case Visibility::Level::kPublic :
305 r_txt_printer.Print("public ");
306 break;
307 case Visibility::Level::kPrivate :
308 r_txt_printer.Print("private ");
309 }
310
311 if (type->type != ResourceType::kStyleable) {
312 r_txt_printer.Print("int ");
313 r_txt_printer.Print(to_string(type->type));
314 r_txt_printer.Print(" ");
315 r_txt_printer.Println(entry->name);
316 } else {
317 r_txt_printer.Print("int[] styleable ");
318 r_txt_printer.Println(entry->name);
319
320 if (!entry->values.empty()) {
321 auto styleable = static_cast<const Styleable*>(entry->values.front()->value.get());
322 for (const auto& attr : styleable->entries) {
323 r_txt_printer.Print("default int styleable ");
324 r_txt_printer.Print(entry->name);
325 r_txt_printer.Print("_");
326 r_txt_printer.Println(attr.name.value().entry);
327 }
328 }
329 }
330 }
331 }
332 }
333 }
334
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800336}
337
Adam Lesinski00451162017-10-03 07:44:08 -0700338static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
339 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800340 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700342 if (!writer->StartEntry(output_path, 0)) {
343 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 return false;
345 }
346
Adam Lesinski00451162017-10-03 07:44:08 -0700347 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348 {
Adam Lesinski00451162017-10-03 07:44:08 -0700349 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700351 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700353 pb::internal::CompiledFile pb_compiled_file;
354 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355
Adam Lesinski00451162017-10-03 07:44:08 -0700356 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
357 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800359 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800361
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700362 if (!writer->FinishEntry()) {
363 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
365 }
366 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700367}
368
Adam Lesinski00451162017-10-03 07:44:08 -0700369static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
370 ContainerWriter* container_writer, IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700371 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700372 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700373
Adam Lesinski00451162017-10-03 07:44:08 -0700374 pb::XmlNode pb_xml_node;
375 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
376
377 std::string serialized_xml = pb_xml_node.SerializeAsString();
378 io::StringInputStream serialized_in(serialized_xml);
379
380 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
381 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 return false;
383 }
384 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700385}
386
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700387static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700388 const file::FileType file_type = file::GetFileType(input_path);
389 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
390 if (file_type == file::FileType::kDirectory) {
391 context->GetDiagnostics()->Error(DiagMessage(input_path)
392 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700393 } else if (file_type == file::FileType::kNonexistant) {
394 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700395 } else {
396 context->GetDiagnostics()->Error(DiagMessage(input_path)
397 << "not a valid resource file");
398 }
399 return false;
400 }
401 return true;
402}
403
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700404static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700405 const ResourcePathData& path_data, IArchiveWriter* writer,
406 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700407 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700408 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 }
410
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700412 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700413 FileInputStream fin(path_data.source.path);
414 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700415 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700416 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700418 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700419
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700420 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700422
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700423 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700424 return false;
425 }
426
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700427 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700428 xmlres->file.config = path_data.config;
429 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700430 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700431
432 // Collect IDs that are defined here.
433 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 return false;
436 }
437
438 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700439 InlineXmlFormatParser inline_xml_format_parser;
440 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 return false;
442 }
443
444 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700446 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 return false;
448 }
449
Adam Lesinski00451162017-10-03 07:44:08 -0700450 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
451 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
452
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700453 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700455 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700456 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700457 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458
Adam Lesinski00451162017-10-03 07:44:08 -0700459 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
460 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700462 }
463
Adam Lesinski00451162017-10-03 07:44:08 -0700464 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
465 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
466 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700467 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700469 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700471
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700472 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700473 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 return false;
475 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000476
477 if (options.generate_text_symbols_path) {
478 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
479
480 if (fout_text.HadError()) {
481 context->GetDiagnostics()->Error(DiagMessage()
482 << "failed writing to'"
483 << options.generate_text_symbols_path.value()
484 << "': " << fout_text.GetError());
485 return false;
486 }
487
488 Printer r_txt_printer(&fout_text);
489 for (const auto res : xmlres->file.exported_symbols) {
490 r_txt_printer.Print("default int id ");
491 r_txt_printer.Println(res.name.entry);
492 }
493
494 // And print ourselves.
495 r_txt_printer.Print("default int ");
496 r_txt_printer.Print(path_data.resource_dir);
497 r_txt_printer.Print(" ");
498 r_txt_printer.Println(path_data.name);
499 }
500
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700502}
503
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700504static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700505 const ResourcePathData& path_data, IArchiveWriter* writer,
506 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700507 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700508 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 }
510
511 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700512 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700513 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700514 res_file.config = path_data.config;
515 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700516 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517
518 {
519 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700520 if (!android::base::ReadFileToString(path_data.source.path, &content,
521 true /*follow_symlinks*/)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700522 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700523 << "failed to open file: "
Adam Lesinski00451162017-10-03 07:44:08 -0700524 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700526 }
527
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700529 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700530
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 // Ensure that we only keep the chunks we care about if we end up
532 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700533 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700534 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700535 if (!image) {
536 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700537 }
538
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 std::unique_ptr<NinePatch> nine_patch;
540 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700542 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700543 if (!nine_patch) {
544 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700545 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 }
547
548 // Remove the 1px border around the NinePatch.
549 // Basically the row array is shifted up by 1, and the length is treated
550 // as height - 2.
551 // For each row, shift the array to the left by 1, and treat the length as
552 // width - 2.
553 image->width -= 2;
554 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700555 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700556 for (int32_t h = 0; h < image->height; h++) {
557 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
558 }
559
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700560 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700561 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
562 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700563 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700564 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700565
566 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700567 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700568 return false;
569 }
570
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 if (nine_patch != nullptr ||
572 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700573 // No matter what, we must use the re-encoded PNG, even if it is larger.
574 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700575 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 } else {
577 // The re-encoded PNG is larger than the original, and there is
578 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700579 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700580 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
581 << "original PNG is smaller than crunched PNG"
582 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700583 }
584
Adam Lesinski06460ef2017-03-14 18:52:13 -0700585 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700587 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
588 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700590 }
591
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700593 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
594 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 std::stringstream legacy_stream(content);
596 BigBuffer legacy_buffer(4096);
597 Png png(context->GetDiagnostics());
598 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700599 return false;
600 }
601
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700602 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
603 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700604 << " new=" << buffer.size());
605 }
606 }
607
Adam Lesinski00451162017-10-03 07:44:08 -0700608 io::BigBufferInputStream buffer_in(&buffer);
609 if (!WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
610 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700611 return false;
612 }
613 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700614}
615
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700616static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700617 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700618 const std::string& output_path) {
619 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700620 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700622
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700625 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 res_file.config = path_data.config;
627 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700628 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700629
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 std::string error_str;
631 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700632 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700633 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
634 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700635 return false;
636 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700637
Adam Lesinski00451162017-10-03 07:44:08 -0700638 io::MmappedData mmapped_in(std::move(f.value()));
639 if (!WriteHeaderAndDataToWriter(output_path, res_file, &mmapped_in, writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700640 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 return false;
642 }
643 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700644}
645
646class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100648 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
649 }
650
Adam Lesinskib522f042017-04-21 16:57:59 -0700651 PackageType GetPackageType() override {
652 // Every compilation unit starts as an app and then gets linked as potentially something else.
653 return PackageType::kApp;
654 }
655
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700656 void SetVerbose(bool val) {
657 verbose_ = val;
658 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800659
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700660 bool IsVerbose() override {
661 return verbose_;
662 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800663
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700664 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100665 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700666 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700667
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700668 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700669 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700670 return nullptr;
671 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700672
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700673 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700674 static std::string empty;
675 return empty;
676 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700677
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700678 uint8_t GetPackageId() override {
679 return 0x0;
680 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700681
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700682 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700683 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700684 return nullptr;
685 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800686
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700687 int GetMinSdkVersion() override {
688 return 0;
689 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700690
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700691 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700692 DISALLOW_COPY_AND_ASSIGN(CompileContext);
693
Chris Warrington820d72a2017-04-27 15:27:01 +0100694 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700695 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700696};
697
Adam Lesinski00451162017-10-03 07:44:08 -0700698// Entry point for compilation phase. Parses arguments and dispatches to the correct steps.
Chris Warrington820d72a2017-04-27 15:27:01 +0100699int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
700 CompileContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700702
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700703 bool verbose = false;
704 Flags flags =
705 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700706 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700707 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000708 .OptionalFlag("--output-text-symbols",
709 "Generates a text file containing the resource symbols in the\n"
710 "specified file",
711 &options.generate_text_symbols_path)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700712 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700713 "Generate resources for pseudo-locales "
714 "(en-XA and ar-XB)",
715 &options.pseudolocalize)
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700716 .OptionalSwitch("--no-crunch", "Disables PNG processing", &options.no_png_crunch)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700717 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
718 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700719 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
720 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700721 return 1;
722 }
723
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700724 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700725
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700726 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700727
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700728 std::vector<ResourcePathData> input_data;
729 if (options.res_dir) {
730 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700732 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700733 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700734 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700735 }
736
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700737 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700738 return 1;
739 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800740
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700741 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700742
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700744 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800745
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700746 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 for (const std::string& arg : flags.GetArgs()) {
748 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700749 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700750 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700751 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700752 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700753 return 1;
754 }
755 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800756
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700757 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700758 }
759
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700760 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700761 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700762 }
763
764 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700765 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700766 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700767 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700768 }
769
Adam Lesinski776aa952017-04-24 15:09:32 -0700770 if (!IsValidFile(&context, path_data.source.path)) {
771 error = true;
772 continue;
773 }
774
Adam Lesinski00451162017-10-03 07:44:08 -0700775 // Determine how to compile the file based on its type.
776 auto compile_func = &CompileFile;
Adam Lesinski3a725dc2017-11-02 16:14:59 -0700777 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
Adam Lesinski00451162017-10-03 07:44:08 -0700778 compile_func = &CompileTable;
779 // We use a different extension (not necessary anymore, but avoids altering the existing
780 // build system logic).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 path_data.extension = "arsc";
yd6b83292018-04-11 09:54:56 -0700782
Adam Lesinski00451162017-10-03 07:44:08 -0700783 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
784 if (*type != ResourceType::kRaw) {
785 if (path_data.extension == "xml") {
786 compile_func = &CompileXml;
yd6b83292018-04-11 09:54:56 -0700787 } else if ((!options.no_png_crunch && path_data.extension == "png")
788 || path_data.extension == "9.png") {
Adam Lesinski00451162017-10-03 07:44:08 -0700789 compile_func = &CompilePng;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700790 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700791 }
Adam Lesinski00451162017-10-03 07:44:08 -0700792 } else {
793 context.GetDiagnostics()->Error(DiagMessage()
794 << "invalid file path '" << path_data.source << "'");
795 error = true;
796 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700797 }
798
yd6b83292018-04-11 09:54:56 -0700799 // Treat periods as a reserved character that should not be present in a file name
800 // Legacy support for AAPT which did not reserve periods
801 if (compile_func != &CompileFile && !options.legacy_mode
802 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
803 error = true;
804 context.GetDiagnostics()->Error(DiagMessage() << "resource file '" << path_data.source.path
805 << "' name cannot contain '.' other than for"
806 << "specifying the extension");
807 continue;
808 }
809
Adam Lesinski00451162017-10-03 07:44:08 -0700810 // Compile the file.
811 const std::string out_path = BuildIntermediateContainerFilename(path_data);
812 error |= !compile_func(&context, options, path_data, archive_writer.get(), out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700813 }
Adam Lesinski00451162017-10-03 07:44:08 -0700814 return error ? 1 : 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700815}
816
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700817} // namespace aapt