blob: 53910afff59380a68dedc552dbd68db5a179f13f [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"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25#include "google/protobuf/io/coded_stream.h"
26#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
27
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include "ConfigDescription.h"
29#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;
52using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070053using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070054using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070055
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056namespace aapt {
57
58struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 std::string name;
62 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070064 // Original config str. We keep this because when we parse the config, we may add on
65 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069};
70
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070071// Resource file paths are expected to look like: [--/res/]type[-config]/name
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path,
73 std::string* out_error) {
74 std::vector<std::string> parts = util::Split(path, file::sDirSep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 return {};
78 }
79
80 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 size_t dash_pos = dir.find('-');
86 if (dash_pos != std::string::npos) {
87 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
88 if (!ConfigDescription::Parse(config_str, &config)) {
89 if (out_error) {
90 std::stringstream err_str;
91 err_str << "invalid configuration '" << config_str << "'";
92 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
94 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 std::string& filename = parts[parts.size() - 1];
100 StringPiece name = filename;
101 StringPiece extension;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 size_t dot_pos = filename.find('.');
103 if (dot_pos != std::string::npos) {
104 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
105 name = name.substr(0, dot_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107
Adam Lesinskid5083f62017-01-16 15:07:21 -0800108 return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(),
109 extension.to_string(), config_str.to_string(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110}
111
112struct CompileOptions {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 std::string output_path;
114 Maybe<std::string> res_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 bool pseudolocalize = false;
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700116 bool no_png_crunch = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 bool legacy_mode = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 bool verbose = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119};
120
Adam Lesinski00451162017-10-03 07:44:08 -0700121static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 name << data.resource_dir;
124 if (!data.config_str.empty()) {
125 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 }
127 name << "_" << data.name;
128 if (!data.extension.empty()) {
129 name << "." << data.extension;
130 }
131 name << ".flat";
132 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800133}
134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135static bool IsHidden(const StringPiece& filename) {
136 return util::StartsWith(filename, ".");
Adam Lesinskia40e9722015-11-24 19:11:46 -0800137}
138
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700139// Walks the res directory structure, looking for resource files.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700140static bool LoadInputFilesFromDir(IAaptContext* context, const CompileOptions& options,
141 std::vector<ResourcePathData>* out_path_data) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 const std::string& root_dir = options.res_dir.value();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700143 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 if (!d) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700145 context->GetDiagnostics()->Error(DiagMessage(root_dir) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700146 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 return false;
148 }
149
150 while (struct dirent* entry = readdir(d.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 if (IsHidden(entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 continue;
153 }
154
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 std::string prefix_path = root_dir;
156 file::AppendPath(&prefix_path, entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 if (file::GetFileType(prefix_path) != file::FileType::kDirectory) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 continue;
160 }
161
Adam Lesinski06460ef2017-03-14 18:52:13 -0700162 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 if (!subdir) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700164 context->GetDiagnostics()->Error(DiagMessage(prefix_path) << "failed to open directory: "
Adam Lesinski00451162017-10-03 07:44:08 -0700165 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 return false;
167 }
168
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 while (struct dirent* leaf_entry = readdir(subdir.get())) {
170 if (IsHidden(leaf_entry->d_name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 continue;
172 }
173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 std::string full_path = prefix_path;
175 file::AppendPath(&full_path, leaf_entry->d_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 std::string err_str;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700178 Maybe<ResourcePathData> path_data = ExtractResourcePathData(full_path, &err_str);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 if (!path_data) {
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700180 context->GetDiagnostics()->Error(DiagMessage(full_path) << err_str);
Adam Lesinskia40e9722015-11-24 19:11:46 -0800181 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 out_path_data->push_back(std::move(path_data.value()));
Adam Lesinskia40e9722015-11-24 19:11:46 -0800185 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
187 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188}
189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700191 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 const std::string& output_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 ResourceTable table;
194 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700195 FileInputStream fin(path_data.source.path);
196 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700198 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200 }
201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 // Parse the values file from XML.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700203 xml::XmlPullParser xml_parser(&fin);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 ResourceParserOptions parser_options;
206 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700208 // If the filename includes donottranslate, then the default translatable is false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700209 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700211 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 parser_options);
213 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800215 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800217
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 if (options.pseudolocalize) {
219 // Generate pseudo-localized strings (en-XA and ar-XB).
220 // These are created as weak symbols, and are only generated from default
221 // configuration
222 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 PseudolocaleGenerator pseudolocale_generator;
224 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700225 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700226 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231
232 // Assign an ID to any package that has resources.
233 for (auto& pkg : table.packages) {
234 if (!pkg->id) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700235 // If no package ID was set while parsing (public identifiers), auto assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700241 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700242 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 return false;
244 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800245
Adam Lesinski00451162017-10-03 07:44:08 -0700246 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 {
Adam Lesinski00451162017-10-03 07:44:08 -0700248 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700250 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700252 pb::ResourceTable pb_table;
253 SerializeTableToPb(table, &pb_table);
Adam Lesinski00451162017-10-03 07:44:08 -0700254 if (!container_writer.AddResTableEntry(pb_table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700255 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700257 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700261 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 return false;
263 }
264 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800265}
266
Adam Lesinski00451162017-10-03 07:44:08 -0700267static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
268 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800269 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 if (!writer->StartEntry(output_path, 0)) {
272 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 return false;
274 }
275
Adam Lesinski00451162017-10-03 07:44:08 -0700276 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 {
Adam Lesinski00451162017-10-03 07:44:08 -0700278 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700280 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700282 pb::internal::CompiledFile pb_compiled_file;
283 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284
Adam Lesinski00451162017-10-03 07:44:08 -0700285 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
286 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800288 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800290
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 if (!writer->FinishEntry()) {
292 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 return false;
294 }
295 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700296}
297
Adam Lesinski00451162017-10-03 07:44:08 -0700298static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
299 ContainerWriter* container_writer, IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700300 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700301 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700302
Adam Lesinski00451162017-10-03 07:44:08 -0700303 pb::XmlNode pb_xml_node;
304 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
305
306 std::string serialized_xml = pb_xml_node.SerializeAsString();
307 io::StringInputStream serialized_in(serialized_xml);
308
309 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
310 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 return false;
312 }
313 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700314}
315
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700316static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700317 const file::FileType file_type = file::GetFileType(input_path);
318 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
319 if (file_type == file::FileType::kDirectory) {
320 context->GetDiagnostics()->Error(DiagMessage(input_path)
321 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700322 } else if (file_type == file::FileType::kNonexistant) {
323 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700324 } else {
325 context->GetDiagnostics()->Error(DiagMessage(input_path)
326 << "not a valid resource file");
327 }
328 return false;
329 }
330 return true;
331}
332
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700334 const ResourcePathData& path_data, IArchiveWriter* writer,
335 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700336 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700337 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700338 }
339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700341 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700342 FileInputStream fin(path_data.source.path);
343 if (fin.HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700344 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700345 << "failed to open file: " << fin.GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700347 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700349 xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700351
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352 if (!xmlres) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700353 return false;
354 }
355
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700356 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 xmlres->file.config = path_data.config;
358 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700359 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360
361 // Collect IDs that are defined here.
362 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700364 return false;
365 }
366
367 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368 InlineXmlFormatParser inline_xml_format_parser;
369 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700370 return false;
371 }
372
373 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700375 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700376 return false;
377 }
378
Adam Lesinski00451162017-10-03 07:44:08 -0700379 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
380 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
381
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700382 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700383 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700384 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700385 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700386 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700387
Adam Lesinski00451162017-10-03 07:44:08 -0700388 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
389 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700390 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700391 }
392
Adam Lesinski00451162017-10-03 07:44:08 -0700393 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
394 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
395 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700396 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700397 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700398 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700399 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700400
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700401 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700402 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700403 return false;
404 }
405 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700406}
407
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700408static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700409 const ResourcePathData& path_data, IArchiveWriter* writer,
410 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700411 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700412 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 }
414
415 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700416 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700417 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418 res_file.config = path_data.config;
419 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700420 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421
422 {
423 std::string content;
Adam Lesinski2354b562017-05-26 16:31:38 -0700424 if (!android::base::ReadFileToString(path_data.source.path, &content,
425 true /*follow_symlinks*/)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700426 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Adam Lesinski60d9c2f2017-05-17 16:07:45 -0700427 << "failed to open file: "
Adam Lesinski00451162017-10-03 07:44:08 -0700428 << SystemErrorCodeToString(errno));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700430 }
431
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700432 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700433 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700434
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 // Ensure that we only keep the chunks we care about if we end up
436 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700437 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700438 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 if (!image) {
440 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700441 }
442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443 std::unique_ptr<NinePatch> nine_patch;
444 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700445 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700446 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700447 if (!nine_patch) {
448 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700449 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700450 }
451
452 // Remove the 1px border around the NinePatch.
453 // Basically the row array is shifted up by 1, and the length is treated
454 // as height - 2.
455 // For each row, shift the array to the left by 1, and treat the length as
456 // width - 2.
457 image->width -= 2;
458 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700459 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700460 for (int32_t h = 0; h < image->height; h++) {
461 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
462 }
463
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700464 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700465 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
466 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700468 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469
470 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700471 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 return false;
473 }
474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475 if (nine_patch != nullptr ||
476 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 // No matter what, we must use the re-encoded PNG, even if it is larger.
478 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 } else {
481 // The re-encoded PNG is larger than the original, and there is
482 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700483 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700484 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
485 << "original PNG is smaller than crunched PNG"
486 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700487 }
488
Adam Lesinski06460ef2017-03-14 18:52:13 -0700489 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700491 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
492 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700493 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 }
495
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700496 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700497 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
498 // This will help catch exotic cases where the new code may generate larger PNGs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700499 std::stringstream legacy_stream(content);
500 BigBuffer legacy_buffer(4096);
501 Png png(context->GetDiagnostics());
502 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 return false;
504 }
505
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700506 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
507 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700508 << " new=" << buffer.size());
509 }
510 }
511
Adam Lesinski00451162017-10-03 07:44:08 -0700512 io::BigBufferInputStream buffer_in(&buffer);
513 if (!WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
514 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 return false;
516 }
517 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700518}
519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700521 const ResourcePathData& path_data, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700522 const std::string& output_path) {
523 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700524 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700526
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527 BigBuffer buffer(256);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700529 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700530 res_file.config = path_data.config;
531 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700532 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700533
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700534 std::string error_str;
535 Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536 if (!f) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700537 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: "
538 << error_str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 return false;
540 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700541
Adam Lesinski00451162017-10-03 07:44:08 -0700542 io::MmappedData mmapped_in(std::move(f.value()));
543 if (!WriteHeaderAndDataToWriter(output_path, res_file, &mmapped_in, writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700544 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 return false;
546 }
547 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700548}
549
550class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100552 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
553 }
554
Adam Lesinskib522f042017-04-21 16:57:59 -0700555 PackageType GetPackageType() override {
556 // Every compilation unit starts as an app and then gets linked as potentially something else.
557 return PackageType::kApp;
558 }
559
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700560 void SetVerbose(bool val) {
561 verbose_ = val;
562 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800563
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700564 bool IsVerbose() override {
565 return verbose_;
566 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800567
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700568 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100569 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700570 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700571
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700572 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700573 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700574 return nullptr;
575 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700576
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 static std::string empty;
579 return empty;
580 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700581
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700582 uint8_t GetPackageId() override {
583 return 0x0;
584 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700585
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700586 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700587 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700588 return nullptr;
589 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800590
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700591 int GetMinSdkVersion() override {
592 return 0;
593 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700594
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700595 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700596 DISALLOW_COPY_AND_ASSIGN(CompileContext);
597
Chris Warrington820d72a2017-04-27 15:27:01 +0100598 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700600};
601
Adam Lesinski00451162017-10-03 07:44:08 -0700602// Entry point for compilation phase. Parses arguments and dispatches to the correct steps.
Chris Warrington820d72a2017-04-27 15:27:01 +0100603int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
604 CompileContext context(diagnostics);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700605 CompileOptions options;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 bool verbose = false;
608 Flags flags =
609 Flags()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 .RequiredFlag("-o", "Output path", &options.output_path)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700611 .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700612 .OptionalSwitch("--pseudo-localize",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 "Generate resources for pseudo-locales "
614 "(en-XA and ar-XB)",
615 &options.pseudolocalize)
Adam Lesinski28e6c0b2017-05-10 14:56:36 -0700616 .OptionalSwitch("--no-crunch", "Disables PNG processing", &options.no_png_crunch)
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700617 .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings",
618 &options.legacy_mode)
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700619 .OptionalSwitch("-v", "Enables verbose logging", &verbose);
620 if (!flags.Parse("aapt2 compile", args, &std::cerr)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700621 return 1;
622 }
623
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700624 context.SetVerbose(verbose);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700625
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700626 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700627
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700628 std::vector<ResourcePathData> input_data;
629 if (options.res_dir) {
630 if (!flags.GetArgs().empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700631 // Can't have both files and a resource directory.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700632 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700633 flags.Usage("aapt2 compile", &std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700634 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700635 }
636
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700637 if (!LoadInputFilesFromDir(&context, options, &input_data)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700638 return 1;
639 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800640
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700641 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700642
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700643 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700644 input_data.reserve(flags.GetArgs().size());
Adam Lesinskia40e9722015-11-24 19:11:46 -0800645
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700646 // Collect data from the path for each input file.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700647 for (const std::string& arg : flags.GetArgs()) {
648 std::string error_str;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700649 if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700650 input_data.push_back(std::move(path_data.value()));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700651 } else {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700652 context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700653 return 1;
654 }
655 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800656
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700657 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700658 }
659
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700660 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700661 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700662 }
663
664 bool error = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700665 for (ResourcePathData& path_data : input_data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700666 if (options.verbose) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700667 context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700668 }
669
Adam Lesinski776aa952017-04-24 15:09:32 -0700670 if (!IsValidFile(&context, path_data.source.path)) {
671 error = true;
672 continue;
673 }
674
Adam Lesinski00451162017-10-03 07:44:08 -0700675 // Determine how to compile the file based on its type.
676 auto compile_func = &CompileFile;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700677 if (path_data.resource_dir == "values") {
Adam Lesinski00451162017-10-03 07:44:08 -0700678 compile_func = &CompileTable;
679 // We use a different extension (not necessary anymore, but avoids altering the existing
680 // build system logic).
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700681 path_data.extension = "arsc";
Adam Lesinski00451162017-10-03 07:44:08 -0700682 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
683 if (*type != ResourceType::kRaw) {
684 if (path_data.extension == "xml") {
685 compile_func = &CompileXml;
686 } else if (!options.no_png_crunch &&
687 (path_data.extension == "png" || path_data.extension == "9.png")) {
688 compile_func = &CompilePng;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700689 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700690 }
Adam Lesinski00451162017-10-03 07:44:08 -0700691 } else {
692 context.GetDiagnostics()->Error(DiagMessage()
693 << "invalid file path '" << path_data.source << "'");
694 error = true;
695 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700696 }
697
Adam Lesinski00451162017-10-03 07:44:08 -0700698 // Compile the file.
699 const std::string out_path = BuildIntermediateContainerFilename(path_data);
700 error |= !compile_func(&context, options, path_data, archive_writer.get(), out_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700701 }
Adam Lesinski00451162017-10-03 07:44:08 -0700702 return error ? 1 : 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700703}
704
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700705} // namespace aapt