blob: 2ba2cf7926b0297a0408e4dc3b4600bcdab33c09 [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"
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 "ConfigDescription.h"
30#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"
44#include "io/StringStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070045#include "io/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046#include "util/Files.h"
47#include "util/Maybe.h"
48#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080049#include "xml/XmlDom.h"
50#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070052using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000053using ::aapt::text::Printer;
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
Adam Lesinski00451162017-10-03 07:44:08 -0700124static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 name << data.resource_dir;
127 if (!data.config_str.empty()) {
128 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 }
130 name << "_" << data.name;
131 if (!data.extension.empty()) {
132 name << "." << data.extension;
133 }
134 name << ".flat";
135 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800136}
137
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138static bool IsHidden(const StringPiece& filename) {
139 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800140}
141
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700142// Walks the res directory structure, looking for resource files.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700143static bool LoadInputFilesFromDir(IAaptContext* context, const CompileOptions& options,
144 std::vector<ResourcePathData>* out_path_data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 const std::string& root_dir = options.res_dir.value();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700146 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 if (!d) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700148 context->GetDiagnostics()->Error(DiagMessage(root_dir) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700149 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 return false;
151 }
152
153 while (struct dirent* entry = readdir(d.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 if (IsHidden(entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 continue;
156 }
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 std::string prefix_path = root_dir;
159 file::AppendPath(&prefix_path, entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 if (file::GetFileType(prefix_path) != file::FileType::kDirectory) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 continue;
163 }
164
Adam Lesinski06460ef2017-03-14 18:52:13 -0700165 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 if (!subdir) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700167 context->GetDiagnostics()->Error(DiagMessage(prefix_path) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700168 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 return false;
170 }
171
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 while (struct dirent* leaf_entry = readdir(subdir.get())) {
173 if (IsHidden(leaf_entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 continue;
175 }
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 std::string full_path = prefix_path;
178 file::AppendPath(&full_path, leaf_entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 std::string err_str;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700181 Maybe<ResourcePathData> path_data = ExtractResourcePathData(full_path, &err_str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 if (!path_data) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700183 context->GetDiagnostics()->Error(DiagMessage(full_path) << err_str);
Adam Lesinskia40e9722015-11-24 19:11:46 -0800184 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 }
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 out_path_data->push_back(std::move(path_data.value()));
Adam Lesinskia40e9722015-11-24 19:11:46 -0800188 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 }
Adam Lesinskie6aa6d12017-12-20 14:01:14 -0800190
191 // File-system directory enumeration order is platform-dependent. Sort the result to remove any
192 // inconsistencies between platforms.
193 std::sort(
194 out_path_data->begin(), out_path_data->end(),
195 [](const ResourcePathData& a, const ResourcePathData& b) { return a.source < b.source; });
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700197}
198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700200 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 const std::string& output_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 ResourceTable table;
203 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700204 FileInputStream fin(path_data.source.path);
205 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700207 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209 }
210
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 // Parse the values file from XML.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700212 xml::XmlPullParser xml_parser(&fin);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 ResourceParserOptions parser_options;
215 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700217 // If the filename includes donottranslate, then the default translatable is false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700218 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100220 // If visibility was forced, we need to use it when creating a new resource and also error if
221 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
222 parser_options.visibility = options.visibility;
223
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700224 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 parser_options);
226 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800228 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800230
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 if (options.pseudolocalize) {
232 // Generate pseudo-localized strings (en-XA and ar-XB).
233 // These are created as weak symbols, and are only generated from default
234 // configuration
235 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 PseudolocaleGenerator pseudolocale_generator;
237 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700239 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244
245 // Assign an ID to any package that has resources.
246 for (auto& pkg : table.packages) {
247 if (!pkg->id) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700248 // If no package ID was set while parsing (public identifiers), auto assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700254 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700255 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 return false;
257 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800258
Adam Lesinski00451162017-10-03 07:44:08 -0700259 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 {
Adam Lesinski00451162017-10-03 07:44:08 -0700261 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700263 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700265 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700266 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700267 if (!container_writer.AddResTableEntry(pb_table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700268 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700270 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700274 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return false;
276 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000277
278 if (options.generate_text_symbols_path) {
279 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
280
281 if (fout_text.HadError()) {
282 context->GetDiagnostics()->Error(DiagMessage()
283 << "failed writing to'"
284 << options.generate_text_symbols_path.value()
285 << "': " << fout_text.GetError());
286 return false;
287 }
288
289 Printer r_txt_printer(&fout_text);
290 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100291 // Only print resources defined locally, e.g. don't write android attributes.
292 if (package->name.empty()) {
293 for (const auto& type : package->types) {
294 for (const auto& entry : type->entries) {
295 // Check access modifiers.
296 switch (entry->visibility.level) {
297 case Visibility::Level::kUndefined :
298 r_txt_printer.Print("default ");
299 break;
300 case Visibility::Level::kPublic :
301 r_txt_printer.Print("public ");
302 break;
303 case Visibility::Level::kPrivate :
304 r_txt_printer.Print("private ");
305 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000306
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100307 if (type->type != ResourceType::kStyleable) {
308 r_txt_printer.Print("int ");
309 r_txt_printer.Print(to_string(type->type));
310 r_txt_printer.Print(" ");
311 r_txt_printer.Println(entry->name);
312 } else {
313 r_txt_printer.Print("int[] styleable ");
314 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000315
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100316 if (!entry->values.empty()) {
317 auto styleable =
318 static_cast<const Styleable*>(entry->values.front()->value.get());
319 for (const auto& attr : styleable->entries) {
320 // The visibility of the children under the styleable does not matter as they are
321 // nested under their parent and use its visibility.
322 r_txt_printer.Print("default int styleable ");
323 r_txt_printer.Print(entry->name);
324 // If the package name is present, also include it in the mangled name (e.g.
325 // "android")
326 if (!attr.name.value().package.empty()) {
327 r_txt_printer.Print("_");
328 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
329 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100330 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100331 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100332 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000333 }
334 }
335 }
336 }
337 }
338 }
339 }
340
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800342}
343
Adam Lesinski00451162017-10-03 07:44:08 -0700344static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
345 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800346 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700348 if (!writer->StartEntry(output_path, 0)) {
349 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 return false;
351 }
352
Adam Lesinski00451162017-10-03 07:44:08 -0700353 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354 {
Adam Lesinski00451162017-10-03 07:44:08 -0700355 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700357 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700359 pb::internal::CompiledFile pb_compiled_file;
360 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361
Adam Lesinski00451162017-10-03 07:44:08 -0700362 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
363 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800365 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700366 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 if (!writer->FinishEntry()) {
369 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 return false;
371 }
372 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700373}
374
Adam Lesinski00451162017-10-03 07:44:08 -0700375static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
376 ContainerWriter* container_writer, IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700377 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700378 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700379
Adam Lesinski00451162017-10-03 07:44:08 -0700380 pb::XmlNode pb_xml_node;
381 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
382
383 std::string serialized_xml = pb_xml_node.SerializeAsString();
384 io::StringInputStream serialized_in(serialized_xml);
385
386 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
387 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 return false;
389 }
390 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700391}
392
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700393static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700394 const file::FileType file_type = file::GetFileType(input_path);
395 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
396 if (file_type == file::FileType::kDirectory) {
397 context->GetDiagnostics()->Error(DiagMessage(input_path)
398 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700399 } else if (file_type == file::FileType::kNonexistant) {
400 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700401 } else {
402 context->GetDiagnostics()->Error(DiagMessage(input_path)
403 << "not a valid resource file");
404 }
405 return false;
406 }
407 return true;
408}
409
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700410static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700411 const ResourcePathData& path_data, IArchiveWriter* writer,
412 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700413 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700414 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 }
416
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700417 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700419 FileInputStream fin(path_data.source.path);
420 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700421 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700422 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700424 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700425
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700426 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700427 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700428
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700429 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700430 return false;
431 }
432
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700433 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434 xmlres->file.config = path_data.config;
435 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700436 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700437
438 // Collect IDs that are defined here.
439 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700440 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700441 return false;
442 }
443
444 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700445 InlineXmlFormatParser inline_xml_format_parser;
446 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700447 return false;
448 }
449
450 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700452 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 return false;
454 }
455
Adam Lesinski00451162017-10-03 07:44:08 -0700456 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
457 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
458
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700459 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700461 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700463 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464
Adam Lesinski00451162017-10-03 07:44:08 -0700465 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
466 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700468 }
469
Adam Lesinski00451162017-10-03 07:44:08 -0700470 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
471 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
472 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700473 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700474 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700475 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700477
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700478 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700479 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 return false;
481 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000482
483 if (options.generate_text_symbols_path) {
484 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
485
486 if (fout_text.HadError()) {
487 context->GetDiagnostics()->Error(DiagMessage()
488 << "failed writing to'"
489 << options.generate_text_symbols_path.value()
490 << "': " << fout_text.GetError());
491 return false;
492 }
493
494 Printer r_txt_printer(&fout_text);
495 for (const auto res : xmlres->file.exported_symbols) {
496 r_txt_printer.Print("default int id ");
497 r_txt_printer.Println(res.name.entry);
498 }
499
500 // And print ourselves.
501 r_txt_printer.Print("default int ");
502 r_txt_printer.Print(path_data.resource_dir);
503 r_txt_printer.Print(" ");
504 r_txt_printer.Println(path_data.name);
505 }
506
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700508}
509
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700510static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700511 const ResourcePathData& path_data, IArchiveWriter* writer,
512 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700514 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 }
516
517 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700518 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700519 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 res_file.config = path_data.config;
521 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700522 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523
524 {
525 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700526 if (!android::base::ReadFileToString(path_data.source.path, &content,
527 true /*follow_symlinks*/)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700528 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700529 << "failed to open file: "
Adam Lesinski00451162017-10-03 07:44:08 -0700530 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700532 }
533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700535 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700536
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 // Ensure that we only keep the chunks we care about if we end up
538 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700539 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700540 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 if (!image) {
542 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700543 }
544
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 std::unique_ptr<NinePatch> nine_patch;
546 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700547 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700548 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700549 if (!nine_patch) {
550 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700551 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552 }
553
554 // Remove the 1px border around the NinePatch.
555 // Basically the row array is shifted up by 1, and the length is treated
556 // as height - 2.
557 // For each row, shift the array to the left by 1, and treat the length as
558 // width - 2.
559 image->width -= 2;
560 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700561 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700562 for (int32_t h = 0; h < image->height; h++) {
563 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
564 }
565
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700566 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700567 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
568 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700569 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700570 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700571
572 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700573 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 return false;
575 }
576
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 if (nine_patch != nullptr ||
578 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700579 // No matter what, we must use the re-encoded PNG, even if it is larger.
580 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700581 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700582 } else {
583 // The re-encoded PNG is larger than the original, and there is
584 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700586 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
587 << "original PNG is smaller than crunched PNG"
588 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700589 }
590
Adam Lesinski06460ef2017-03-14 18:52:13 -0700591 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700592 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700593 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
594 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700595 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 }
597
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700598 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700599 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
600 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 std::stringstream legacy_stream(content);
602 BigBuffer legacy_buffer(4096);
603 Png png(context->GetDiagnostics());
604 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 return false;
606 }
607
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700608 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
609 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700610 << " new=" << buffer.size());
611 }
612 }
613
Adam Lesinski00451162017-10-03 07:44:08 -0700614 io::BigBufferInputStream buffer_in(&buffer);
615 if (!WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
616 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700617 return false;
618 }
619 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700620}
621
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700622static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700623 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 const std::string& output_path) {
625 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700626 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700628
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700629 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700630 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700631 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700632 res_file.config = path_data.config;
633 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700634 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700635
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700636 std::string error_str;
637 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700639 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
640 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700641 return false;
642 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700643
Adam Lesinski00451162017-10-03 07:44:08 -0700644 io::MmappedData mmapped_in(std::move(f.value()));
645 if (!WriteHeaderAndDataToWriter(output_path, res_file, &mmapped_in, writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700646 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700647 return false;
648 }
649 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700650}
651
652class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100654 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
655 }
656
Adam Lesinskib522f042017-04-21 16:57:59 -0700657 PackageType GetPackageType() override {
658 // Every compilation unit starts as an app and then gets linked as potentially something else.
659 return PackageType::kApp;
660 }
661
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700662 void SetVerbose(bool val) {
663 verbose_ = val;
664 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800665
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700666 bool IsVerbose() override {
667 return verbose_;
668 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800669
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700670 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100671 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700672 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700673
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700674 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700675 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700676 return nullptr;
677 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700678
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700679 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700680 static std::string empty;
681 return empty;
682 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700683
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700684 uint8_t GetPackageId() override {
685 return 0x0;
686 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700687
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700688 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700689 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 return nullptr;
691 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800692
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700693 int GetMinSdkVersion() override {
694 return 0;
695 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700696
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700697 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700698 DISALLOW_COPY_AND_ASSIGN(CompileContext);
699
Chris Warrington820d72a2017-04-27 15:27:01 +0100700 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700701 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700702};
703
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700704int CompileCommand::Action(const std::vector<std::string>& args) {
705 CompileContext context(diagnostic_);
706 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700707
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700708 if (visibility_) {
709 if (visibility_.value() == "public") {
710 options_.visibility = Visibility::Level::kPublic;
711 } else if (visibility_.value() == "private") {
712 options_.visibility = Visibility::Level::kPrivate;
713 } else if (visibility_.value() == "default") {
714 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100715 } else {
716 context.GetDiagnostics()->Error(
717 DiagMessage() << "Unrecognized visibility level passes to --visibility: '"
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700718 << visibility_.value() << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100719 return 1;
720 }
721 }
722
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700723 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700724
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700725 std::vector<ResourcePathData> input_data;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700726 if (options_.res_dir) {
727 if (!args.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700728 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700729 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700730 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700732 }
733
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700734 if (!LoadInputFilesFromDir(&context, options_, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700735 return 1;
736 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800737
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700738 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700739
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740 } else {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700741 input_data.reserve(args.size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800742
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700743 // Collect data from the path for each input file.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700744 for (const std::string& arg : args) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700745 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700746 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700747 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700748 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700749 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700750 return 1;
751 }
752 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800753
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700754 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700755 }
756
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700757 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700758 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700759 }
760
761 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700762 for (ResourcePathData& path_data : input_data) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700763 if (options_.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700764 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700765 }
766
Adam Lesinski776aa952017-04-24 15:09:32 -0700767 if (!IsValidFile(&context, path_data.source.path)) {
768 error = true;
769 continue;
770 }
771
Adam Lesinski00451162017-10-03 07:44:08 -0700772 // Determine how to compile the file based on its type.
773 auto compile_func = &CompileFile;
Adam Lesinski3a725dc2017-11-02 16:14:59 -0700774 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
Adam Lesinski00451162017-10-03 07:44:08 -0700775 compile_func = &CompileTable;
776 // We use a different extension (not necessary anymore, but avoids altering the existing
777 // build system logic).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700778 path_data.extension = "arsc";
yd6b83292018-04-11 09:54:56 -0700779
Adam Lesinski00451162017-10-03 07:44:08 -0700780 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
781 if (*type != ResourceType::kRaw) {
782 if (path_data.extension == "xml") {
783 compile_func = &CompileXml;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700784 } else if ((!options_.no_png_crunch && path_data.extension == "png")
yd6b83292018-04-11 09:54:56 -0700785 || path_data.extension == "9.png") {
Adam Lesinski00451162017-10-03 07:44:08 -0700786 compile_func = &CompilePng;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700787 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788 }
Adam Lesinski00451162017-10-03 07:44:08 -0700789 } else {
790 context.GetDiagnostics()->Error(DiagMessage()
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700791 << "invalid file path '" << path_data.source << "'");
Adam Lesinski00451162017-10-03 07:44:08 -0700792 error = true;
793 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700794 }
795
yd6b83292018-04-11 09:54:56 -0700796 // Treat periods as a reserved character that should not be present in a file name
797 // Legacy support for AAPT which did not reserve periods
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700798 if (compile_func != &CompileFile && !options_.legacy_mode
yd6b83292018-04-11 09:54:56 -0700799 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
800 error = true;
801 context.GetDiagnostics()->Error(DiagMessage() << "resource file '" << path_data.source.path
802 << "' name cannot contain '.' other than for"
803 << "specifying the extension");
804 continue;
805 }
806
Adam Lesinski00451162017-10-03 07:44:08 -0700807 // Compile the file.
808 const std::string out_path = BuildIntermediateContainerFilename(path_data);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700809 error |= !compile_func(&context, options_, path_data, archive_writer.get(), out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700810 }
Adam Lesinski00451162017-10-03 07:44:08 -0700811 return error ? 1 : 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700812}
813
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700814} // namespace aapt