blob: fc9514a691d28e53dad0e91f0729d72454cfb3c3 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ryan Mitchell833a1a62018-07-10 13:51:36 -070017#include "Compile.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Ryan Mitchell833a1a62018-07-10 13:51:36 -070019#include <dirent.h>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <string>
21
Adam Lesinskid5083f62017-01-16 15:07:21 -080022#include "android-base/errors.h"
23#include "android-base/file.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070024#include "android-base/utf8.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
27#include "google/protobuf/io/coded_stream.h"
28#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "Diagnostics.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "ResourceParser.h"
32#include "ResourceTable.h"
Izabela Orlowska10560192018-04-13 11:56:35 +010033#include "cmd/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034#include "compile/IdAssigner.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070035#include "compile/InlineXmlFormatParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036#include "compile/Png.h"
Adam Lesinski393b5f02015-12-17 13:03:11 -080037#include "compile/PseudolocaleGenerator.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038#include "compile/XmlIdCollector.h"
Adam Lesinski46708052017-09-29 14:49:15 -070039#include "format/Archive.h"
Adam Lesinski00451162017-10-03 07:44:08 -070040#include "format/Container.h"
Adam Lesinski46708052017-09-29 14:49:15 -070041#include "format/proto/ProtoSerialize.h"
Adam Lesinski00451162017-10-03 07:44:08 -070042#include "io/BigBufferStream.h"
43#include "io/FileStream.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070044#include "io/FileSystem.h"
Adam Lesinski00451162017-10-03 07:44:08 -070045#include "io/StringStream.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070046#include "io/Util.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070047#include "io/ZipArchive.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048#include "util/Files.h"
49#include "util/Maybe.h"
50#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080051#include "xml/XmlDom.h"
52#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070054using ::aapt::io::FileInputStream;
Izabela Orlowskac81d9f32017-12-05 12:07:28 +000055using ::aapt::text::Printer;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020056using ::android::ConfigDescription;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070057using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070058using ::android::base::SystemErrorCodeToString;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070059using ::google::protobuf::io::CopyingOutputStreamAdaptor;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070060
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061namespace aapt {
62
63struct ResourcePathData {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 Source source;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 std::string resource_dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 std::string name;
67 std::string extension;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070069 // Original config str. We keep this because when we parse the config, we may add on
70 // version qualifiers. We want to preserve the original input so the output is easily
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 // computed before hand.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 std::string config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074};
75
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076// Resource file paths are expected to look like: [--/res/]type[-config]/name
Ryan Mitchell0ce89732018-10-03 09:20:57 -070077static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path, const char dir_sep,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::string* out_error) {
Ryan Mitchell0ce89732018-10-03 09:20:57 -070079 std::vector<std::string> parts = util::Split(path, dir_sep);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 if (parts.size() < 2) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 if (out_error) *out_error = "bad resource path";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 return {};
83 }
84
85 std::string& dir = parts[parts.size() - 2];
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 StringPiece dir_str = dir;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 StringPiece config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 ConfigDescription config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 size_t dash_pos = dir.find('-');
91 if (dash_pos != std::string::npos) {
92 config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1));
93 if (!ConfigDescription::Parse(config_str, &config)) {
94 if (out_error) {
95 std::stringstream err_str;
96 err_str << "invalid configuration '" << config_str << "'";
97 *out_error = err_str.str();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 }
99 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 dir_str = dir_str.substr(0, dash_pos);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 std::string& filename = parts[parts.size() - 1];
105 StringPiece name = filename;
106 StringPiece extension;
yd6b83292018-04-11 09:54:56 -0700107
108 const std::string kNinePng = ".9.png";
109 if (filename.size() > kNinePng.size()
110 && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) {
111 // Split on .9.png if this extension is present at the end of the file path
112 name = name.substr(0, filename.size() - kNinePng.size());
113 extension = "9.png";
114 } else {
115 // Split on the last period occurrence
116 size_t dot_pos = filename.rfind('.');
117 if (dot_pos != std::string::npos) {
118 extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1));
119 name = name.substr(0, dot_pos);
120 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122
Adam Lesinskid5083f62017-01-16 15:07:21 -0800123 return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(),
124 extension.to_string(), config_str.to_string(), config};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125}
126
Adam Lesinski00451162017-10-03 07:44:08 -0700127static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 std::stringstream name;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 name << data.resource_dir;
130 if (!data.config_str.empty()) {
131 name << "-" << data.config_str;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 }
133 name << "_" << data.name;
134 if (!data.extension.empty()) {
135 name << "." << data.extension;
136 }
137 name << ".flat";
138 return name.str();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800139}
140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141static bool CompileTable(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700142 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143 const std::string& output_path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 ResourceTable table;
145 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700146 auto fin = file->OpenInputStream();
147 if (fin->HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700149 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 }
152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 // Parse the values file from XML.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700154 xml::XmlPullParser xml_parser(fin.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 ResourceParserOptions parser_options;
157 parser_options.error_on_positional_arguments = !options.legacy_mode;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700159 // If the filename includes donottranslate, then the default translatable is false.
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700160 parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100162 // If visibility was forced, we need to use it when creating a new resource and also error if
163 // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
164 parser_options.visibility = options.visibility;
165
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700166 ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700167 parser_options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 if (!res_parser.Parse(&xml_parser)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 return false;
Adam Lesinski393b5f02015-12-17 13:03:11 -0800170 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 }
Adam Lesinski83f22552015-11-07 11:51:23 -0800172
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 if (options.pseudolocalize) {
174 // Generate pseudo-localized strings (en-XA and ar-XB).
175 // These are created as weak symbols, and are only generated from default
176 // configuration
177 // strings and plurals.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 PseudolocaleGenerator pseudolocale_generator;
179 if (!pseudolocale_generator.Consume(context, &table)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 return false;
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700181 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 // Ensure we have the compilation package at least.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 table.CreatePackage(context->GetCompilationPackage());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186
187 // Assign an ID to any package that has resources.
188 for (auto& pkg : table.packages) {
189 if (!pkg->id) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700190 // If no package ID was set while parsing (public identifiers), auto assign an ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 pkg->id = context->GetPackageId();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 // Create the file/zip entry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700197 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 return false;
199 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800200
Adam Lesinski00451162017-10-03 07:44:08 -0700201 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 {
Adam Lesinski00451162017-10-03 07:44:08 -0700203 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700205 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700207 pb::ResourceTable pb_table;
Ryan Mitchella15c2a82018-03-26 11:05:31 -0700208 SerializeTableToPb(table, &pb_table, context->GetDiagnostics());
Adam Lesinski00451162017-10-03 07:44:08 -0700209 if (!container_writer.AddResTableEntry(pb_table)) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700210 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700212 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800214
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 if (!writer->FinishEntry()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700216 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 return false;
218 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000219
220 if (options.generate_text_symbols_path) {
221 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
222
223 if (fout_text.HadError()) {
224 context->GetDiagnostics()->Error(DiagMessage()
225 << "failed writing to'"
226 << options.generate_text_symbols_path.value()
227 << "': " << fout_text.GetError());
228 return false;
229 }
230
231 Printer r_txt_printer(&fout_text);
232 for (const auto& package : table.packages) {
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100233 // Only print resources defined locally, e.g. don't write android attributes.
234 if (package->name.empty()) {
235 for (const auto& type : package->types) {
236 for (const auto& entry : type->entries) {
237 // Check access modifiers.
238 switch (entry->visibility.level) {
239 case Visibility::Level::kUndefined :
240 r_txt_printer.Print("default ");
241 break;
242 case Visibility::Level::kPublic :
243 r_txt_printer.Print("public ");
244 break;
245 case Visibility::Level::kPrivate :
246 r_txt_printer.Print("private ");
247 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000248
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100249 if (type->type != ResourceType::kStyleable) {
250 r_txt_printer.Print("int ");
251 r_txt_printer.Print(to_string(type->type));
252 r_txt_printer.Print(" ");
253 r_txt_printer.Println(entry->name);
254 } else {
255 r_txt_printer.Print("int[] styleable ");
256 r_txt_printer.Println(entry->name);
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000257
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100258 if (!entry->values.empty()) {
259 auto styleable =
260 static_cast<const Styleable*>(entry->values.front()->value.get());
261 for (const auto& attr : styleable->entries) {
262 // The visibility of the children under the styleable does not matter as they are
263 // nested under their parent and use its visibility.
264 r_txt_printer.Print("default int styleable ");
265 r_txt_printer.Print(entry->name);
266 // If the package name is present, also include it in the mangled name (e.g.
267 // "android")
268 if (!attr.name.value().package.empty()) {
269 r_txt_printer.Print("_");
270 r_txt_printer.Print(MakePackageSafeName(attr.name.value().package));
271 }
Izabela Orlowska10560192018-04-13 11:56:35 +0100272 r_txt_printer.Print("_");
Izabela Orlowskaf67d4862018-07-24 11:54:32 +0100273 r_txt_printer.Println(attr.name.value().entry);
Izabela Orlowska10560192018-04-13 11:56:35 +0100274 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000275 }
276 }
277 }
278 }
279 }
280 }
281 }
282
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 return true;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800284}
285
Adam Lesinski00451162017-10-03 07:44:08 -0700286static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file,
287 io::KnownSizeInputStream* in, IArchiveWriter* writer,
Adam Lesinski59e04c62016-02-04 15:59:23 -0800288 IDiagnostics* diag) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 if (!writer->StartEntry(output_path, 0)) {
291 diag->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 return false;
293 }
294
Adam Lesinski00451162017-10-03 07:44:08 -0700295 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 {
Adam Lesinski00451162017-10-03 07:44:08 -0700297 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700299 ContainerWriter container_writer(&copying_adaptor, 1u);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700301 pb::internal::CompiledFile pb_compiled_file;
302 SerializeCompiledFileToPb(file, &pb_compiled_file);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303
Adam Lesinski00451162017-10-03 07:44:08 -0700304 if (!container_writer.AddResFileEntry(pb_compiled_file, in)) {
305 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 return false;
Adam Lesinski59e04c62016-02-04 15:59:23 -0800307 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 }
Adam Lesinski59e04c62016-02-04 15:59:23 -0800309
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700310 if (!writer->FinishEntry()) {
311 diag->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 return false;
313 }
314 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315}
316
Adam Lesinski00451162017-10-03 07:44:08 -0700317static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres,
318 ContainerWriter* container_writer, IDiagnostics* diag) {
Adam Lesinski8cdca1b2017-09-28 15:50:03 -0700319 pb::internal::CompiledFile pb_compiled_file;
Adam Lesinski00451162017-10-03 07:44:08 -0700320 SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700321
Adam Lesinski00451162017-10-03 07:44:08 -0700322 pb::XmlNode pb_xml_node;
323 SerializeXmlToPb(*xmlres.root, &pb_xml_node);
324
325 std::string serialized_xml = pb_xml_node.SerializeAsString();
326 io::StringInputStream serialized_in(serialized_xml);
327
328 if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) {
329 diag->Error(DiagMessage(output_path) << "failed to write entry data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330 return false;
331 }
332 return true;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700333}
334
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700335static bool IsValidFile(IAaptContext* context, const std::string& input_path) {
Adam Lesinski776aa952017-04-24 15:09:32 -0700336 const file::FileType file_type = file::GetFileType(input_path);
337 if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) {
338 if (file_type == file::FileType::kDirectory) {
339 context->GetDiagnostics()->Error(DiagMessage(input_path)
340 << "resource file cannot be a directory");
Adam Lesinskicc73e992017-05-12 18:16:44 -0700341 } else if (file_type == file::FileType::kNonexistant) {
342 context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
Adam Lesinski776aa952017-04-24 15:09:32 -0700343 } else {
344 context->GetDiagnostics()->Error(DiagMessage(input_path)
345 << "not a valid resource file");
346 }
347 return false;
348 }
349 return true;
350}
351
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700352static bool CompileXml(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700353 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700354 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700355 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700356 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 }
358
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 std::unique_ptr<xml::XmlResource> xmlres;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700360 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700361 auto fin = file->OpenInputStream();
362 if (fin->HadError()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 context->GetDiagnostics()->Error(DiagMessage(path_data.source)
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700364 << "failed to open file: " << fin->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700366 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700367
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700368 xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source);
369 if (!xmlres) {
370 return false;
371 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700372 }
373
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700374 xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700375 xmlres->file.config = path_data.config;
376 xmlres->file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700377 xmlres->file.type = ResourceFile::Type::kProtoXml;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378
379 // Collect IDs that are defined here.
380 XmlIdCollector collector;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700381 if (!collector.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382 return false;
383 }
384
385 // Look for and process any <aapt:attr> tags and create sub-documents.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700386 InlineXmlFormatParser inline_xml_format_parser;
387 if (!inline_xml_format_parser.Consume(context, xmlres.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700388 return false;
389 }
390
391 // Start the entry so we can write the header.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700392 if (!writer->StartEntry(output_path, 0)) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700393 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700394 return false;
395 }
396
Adam Lesinski00451162017-10-03 07:44:08 -0700397 std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents =
398 inline_xml_format_parser.GetExtractedInlineXmlDocuments();
399
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700400 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700401 {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700402 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700403 CopyingOutputStreamAdaptor copying_adaptor(writer);
Adam Lesinski00451162017-10-03 07:44:08 -0700404 ContainerWriter container_writer(&copying_adaptor, 1u + inline_documents.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405
Adam Lesinski00451162017-10-03 07:44:08 -0700406 if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer,
407 context->GetDiagnostics())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700408 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700409 }
410
Adam Lesinski00451162017-10-03 07:44:08 -0700411 for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) {
412 if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer,
413 context->GetDiagnostics())) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700414 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700415 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700417 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700418
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700419 if (!writer->FinishEntry()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700420 context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700421 return false;
422 }
Izabela Orlowskac81d9f32017-12-05 12:07:28 +0000423
424 if (options.generate_text_symbols_path) {
425 io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
426
427 if (fout_text.HadError()) {
428 context->GetDiagnostics()->Error(DiagMessage()
429 << "failed writing to'"
430 << options.generate_text_symbols_path.value()
431 << "': " << fout_text.GetError());
432 return false;
433 }
434
435 Printer r_txt_printer(&fout_text);
436 for (const auto res : xmlres->file.exported_symbols) {
437 r_txt_printer.Print("default int id ");
438 r_txt_printer.Println(res.name.entry);
439 }
440
441 // And print ourselves.
442 r_txt_printer.Print("default int ");
443 r_txt_printer.Print(path_data.resource_dir);
444 r_txt_printer.Print(" ");
445 r_txt_printer.Println(path_data.name);
446 }
447
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700449}
450
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700451static bool CompilePng(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700452 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700453 const std::string& output_path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700454 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700455 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 }
457
458 BigBuffer buffer(4096);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700459 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700460 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700461 res_file.config = path_data.config;
462 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700463 res_file.type = ResourceFile::Type::kPng;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700464
465 {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700466 auto data = file->OpenAsData();
467 if (!data) {
468 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 return false;
Adam Lesinski21efb682016-09-14 17:35:43 -0700470 }
471
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700472 // Read the file as a string
473 char buffer_2[data->size()];
474 memcpy(&buffer_2, data->data(), data->size());
475 StringPiece content(buffer_2, data->size());
476
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700477 BigBuffer crunched_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700478 io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700479
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 // Ensure that we only keep the chunks we care about if we end up
481 // using the original PNG instead of the crunched one.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700482 PngChunkFilter png_chunk_filter(content);
Adam Lesinskicc73e992017-05-12 18:16:44 -0700483 std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 if (!image) {
485 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700486 }
487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488 std::unique_ptr<NinePatch> nine_patch;
489 if (path_data.extension == "9.png") {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700490 std::string err;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700491 nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492 if (!nine_patch) {
493 context->GetDiagnostics()->Error(DiagMessage() << err);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700494 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 }
496
497 // Remove the 1px border around the NinePatch.
498 // Basically the row array is shifted up by 1, and the length is treated
499 // as height - 2.
500 // For each row, shift the array to the left by 1, and treat the length as
501 // width - 2.
502 image->width -= 2;
503 image->height -= 2;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700504 memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700505 for (int32_t h = 0; h < image->height; h++) {
506 memmove(image->rows[h], image->rows[h] + 4, image->width * 4);
507 }
508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700510 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: "
511 << *nine_patch);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700512 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700513 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514
515 // Write the crunched PNG.
Adam Lesinski06460ef2017-03-14 18:52:13 -0700516 if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700517 return false;
518 }
519
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700520 if (nine_patch != nullptr ||
521 crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 // No matter what, we must use the re-encoded PNG, even if it is larger.
523 // 9-patch images must be re-encoded since their borders are stripped.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700524 buffer.AppendBuffer(std::move(crunched_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 } else {
526 // The re-encoded PNG is larger than the original, and there is
527 // no mandatory transformation. Use the original.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700528 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700529 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
530 << "original PNG is smaller than crunched PNG"
531 << ", using original");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700532 }
533
Adam Lesinski06460ef2017-03-14 18:52:13 -0700534 png_chunk_filter.Rewind();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700535 BigBuffer filtered_png_buffer(4096);
Adam Lesinski06460ef2017-03-14 18:52:13 -0700536 io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer);
537 io::Copy(&filtered_png_buffer_out, &png_chunk_filter);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700538 buffer.AppendBuffer(std::move(filtered_png_buffer));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700539 }
540
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700541 if (context->IsVerbose()) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700542 // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes.
543 // This will help catch exotic cases where the new code may generate larger PNGs.
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700544 std::stringstream legacy_stream(content.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700545 BigBuffer legacy_buffer(4096);
546 Png png(context->GetDiagnostics());
547 if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548 return false;
549 }
550
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700551 context->GetDiagnostics()->Note(DiagMessage(path_data.source)
552 << "legacy=" << legacy_buffer.size()
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553 << " new=" << buffer.size());
554 }
555 }
556
Adam Lesinski00451162017-10-03 07:44:08 -0700557 io::BigBufferInputStream buffer_in(&buffer);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700558 return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer,
559 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700560}
561
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700562static bool CompileFile(IAaptContext* context, const CompileOptions& options,
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700563 const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700564 const std::string& output_path) {
565 if (context->IsVerbose()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700566 context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700567 }
Adam Lesinski21efb682016-09-14 17:35:43 -0700568
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700569 ResourceFile res_file;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700570 res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700571 res_file.config = path_data.config;
572 res_file.source = path_data.source;
Adam Lesinski00451162017-10-03 07:44:08 -0700573 res_file.type = ResourceFile::Type::kUnknown;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700574
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700575 auto data = file->OpenAsData();
576 if (!data) {
577 context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file ");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700578 return false;
579 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700580
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700581 return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer,
582 context->GetDiagnostics());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700583}
584
585class CompileContext : public IAaptContext {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700586 public:
Chris Warrington820d72a2017-04-27 15:27:01 +0100587 CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) {
588 }
589
Adam Lesinskib522f042017-04-21 16:57:59 -0700590 PackageType GetPackageType() override {
591 // Every compilation unit starts as an app and then gets linked as potentially something else.
592 return PackageType::kApp;
593 }
594
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700595 void SetVerbose(bool val) {
596 verbose_ = val;
597 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800598
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700599 bool IsVerbose() override {
600 return verbose_;
601 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800602
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700603 IDiagnostics* GetDiagnostics() override {
Chris Warrington820d72a2017-04-27 15:27:01 +0100604 return diagnostics_;
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700605 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700606
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700607 NameMangler* GetNameMangler() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700608 UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700609 return nullptr;
610 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700611
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700612 const std::string& GetCompilationPackage() override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700613 static std::string empty;
614 return empty;
615 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700616
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700617 uint8_t GetPackageId() override {
618 return 0x0;
619 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700620
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700621 SymbolTable* GetExternalSymbols() override {
Adam Lesinski00451162017-10-03 07:44:08 -0700622 UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700623 return nullptr;
624 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800625
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700626 int GetMinSdkVersion() override {
627 return 0;
628 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700629
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700630 private:
Adam Lesinski00451162017-10-03 07:44:08 -0700631 DISALLOW_COPY_AND_ASSIGN(CompileContext);
632
Chris Warrington820d72a2017-04-27 15:27:01 +0100633 IDiagnostics* diagnostics_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700634 bool verbose_ = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700635};
636
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700637int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
638 CompileOptions& options) {
639 bool error = false;
640
641 // Iterate over the input files in a stable, platform-independent manner
642 auto file_iterator = inputs->Iterator();
643 while (file_iterator->HasNext()) {
644 auto file = file_iterator->Next();
645 std::string path = file->GetSource().path;
646
647 // Skip hidden input files
648 if (file::IsHidden(path)) {
649 continue;
650 }
651
652 if (!options.res_zip && !IsValidFile(context, path)) {
653 error = true;
654 continue;
655 }
656
657 // Extract resource type information from the full path
658 std::string err_str;
659 ResourcePathData path_data;
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700660 if (auto maybe_path_data = ExtractResourcePathData(path, inputs->GetDirSeparator(), &err_str)) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700661 path_data = maybe_path_data.value();
662 } else {
663 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << err_str);
664 error = true;
665 continue;
666 }
667
668 // Determine how to compile the file based on its type.
669 auto compile_func = &CompileFile;
670 if (path_data.resource_dir == "values" && path_data.extension == "xml") {
671 compile_func = &CompileTable;
672 // We use a different extension (not necessary anymore, but avoids altering the existing
673 // build system logic).
674 path_data.extension = "arsc";
675
676 } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) {
677 if (*type != ResourceType::kRaw) {
678 if (path_data.extension == "xml") {
679 compile_func = &CompileXml;
680 } else if ((!options.no_png_crunch && path_data.extension == "png")
681 || path_data.extension == "9.png") {
682 compile_func = &CompilePng;
683 }
684 }
685 } else {
686 context->GetDiagnostics()->Error(DiagMessage()
687 << "invalid file path '" << path_data.source << "'");
688 error = true;
689 continue;
690 }
691
692 // Treat periods as a reserved character that should not be present in a file name
693 // Legacy support for AAPT which did not reserve periods
694 if (compile_func != &CompileFile && !options.legacy_mode
695 && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) {
696 error = true;
697 context->GetDiagnostics()->Error(DiagMessage(file->GetSource())
698 << "file name cannot contain '.' other than for"
699 << " specifying the extension");
700 continue;
701 }
702
703 const std::string out_path = BuildIntermediateContainerFilename(path_data);
704 error |= !compile_func(context, options, path_data, file, output_writer, out_path);
705 }
706
707 return error ? 1 : 0;
708}
709
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700710int CompileCommand::Action(const std::vector<std::string>& args) {
711 CompileContext context(diagnostic_);
712 context.SetVerbose(options_.verbose);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700713
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700714 if (visibility_) {
715 if (visibility_.value() == "public") {
716 options_.visibility = Visibility::Level::kPublic;
717 } else if (visibility_.value() == "private") {
718 options_.visibility = Visibility::Level::kPrivate;
719 } else if (visibility_.value() == "default") {
720 options_.visibility = Visibility::Level::kUndefined;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100721 } else {
722 context.GetDiagnostics()->Error(
723 DiagMessage() << "Unrecognized visibility level passes to --visibility: '"
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700724 << visibility_.value() << "'. Accepted levels: public, private, default");
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +0100725 return 1;
726 }
727 }
728
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700729 std::unique_ptr<io::IFileCollection> file_collection;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700730 std::unique_ptr<IArchiveWriter> archive_writer;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700732 // Collect the resources files to compile
733 if (options_.res_dir && options_.res_zip) {
734 context.GetDiagnostics()->Error(DiagMessage()
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700735 << "only one of --dir and --zip can be specified");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700736 return 1;
737 } else if (options_.res_dir) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700738 if (!args.empty()) {
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700739 context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified");
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700740 Usage(&std::cerr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700741 return 1;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700742 }
743
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700744 // Load the files from the res directory
745 std::string err;
746 file_collection = io::FileCollection::Create(options_.res_dir.value(), &err);
747 if (!file_collection) {
748 context.GetDiagnostics()->Error(DiagMessage(options_.res_dir.value()) << err);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700749 return 1;
750 }
Adam Lesinski355f2852016-02-13 20:26:45 -0800751
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700752 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700753 } else if (options_.res_zip) {
754 if (!args.empty()) {
755 context.GetDiagnostics()->Error(DiagMessage() << "files given but --zip specified");
756 Usage(&std::cerr);
757 return 1;
758 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700759
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700760 // Load a zip file containing a res directory
761 std::string err;
762 file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err);
763 if (!file_collection) {
764 context.GetDiagnostics()->Error(DiagMessage(options_.res_zip.value()) << err);
765 return 1;
766 }
767
768 archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700769 } else {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700770 auto collection = util::make_unique<io::FileCollection>();
Adam Lesinskia40e9722015-11-24 19:11:46 -0800771
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700772 // Collect data from the path for each input file.
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700773 for (const std::string& arg : args) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700774 collection->InsertFile(arg);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700775 }
Adam Lesinskia40e9722015-11-24 19:11:46 -0800776
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700777 file_collection = std::move(collection);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700778 archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700779 }
780
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700781 if (!archive_writer) {
Adam Lesinskidfaecaf2016-10-20 17:08:51 -0700782 return 1;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700783 }
784
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700785 return Compile(&context, file_collection.get(), archive_writer.get(), options_);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700786}
787
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700788} // namespace aapt