Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 17 | #include "Compile.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 22 | #include "android-base/errors.h" |
| 23 | #include "android-base/file.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 24 | #include "android-base/utf8.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 25 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 26 | #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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "Diagnostics.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | #include "ResourceParser.h" |
| 32 | #include "ResourceTable.h" |
Izabela Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 33 | #include "cmd/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | #include "compile/IdAssigner.h" |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 35 | #include "compile/InlineXmlFormatParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | #include "compile/Png.h" |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 37 | #include "compile/PseudolocaleGenerator.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | #include "compile/XmlIdCollector.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 39 | #include "format/Archive.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 40 | #include "format/Container.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 41 | #include "format/proto/ProtoSerialize.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 42 | #include "io/BigBufferStream.h" |
| 43 | #include "io/FileStream.h" |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 44 | #include "io/FileSystem.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 45 | #include "io/StringStream.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 46 | #include "io/Util.h" |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 47 | #include "io/ZipArchive.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | #include "util/Files.h" |
| 49 | #include "util/Maybe.h" |
| 50 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 51 | #include "xml/XmlDom.h" |
| 52 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 54 | using ::aapt::io::FileInputStream; |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 55 | using ::aapt::text::Printer; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 56 | using ::android::ConfigDescription; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 57 | using ::android::StringPiece; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 58 | using ::android::base::SystemErrorCodeToString; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 59 | using ::google::protobuf::io::CopyingOutputStreamAdaptor; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | namespace aapt { |
| 62 | |
| 63 | struct ResourcePathData { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | Source source; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | std::string resource_dir; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | std::string name; |
| 67 | std::string extension; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 69 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | // computed before hand. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | std::string config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | ConfigDescription config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 76 | // Resource file paths are expected to look like: [--/res/]type[-config]/name |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 77 | static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path, const char dir_sep, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | std::string* out_error) { |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 79 | std::vector<std::string> parts = util::Split(path, dir_sep); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | if (parts.size() < 2) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | if (out_error) *out_error = "bad resource path"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | return {}; |
| 83 | } |
| 84 | |
| 85 | std::string& dir = parts[parts.size() - 2]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | StringPiece dir_str = dir; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | StringPiece config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | ConfigDescription config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | } |
| 99 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | dir_str = dir_str.substr(0, dash_pos); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | std::string& filename = parts[parts.size() - 1]; |
| 105 | StringPiece name = filename; |
| 106 | StringPiece extension; |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 107 | |
| 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 123 | return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(), |
| 124 | extension.to_string(), config_str.to_string(), config}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 127 | static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | std::stringstream name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | name << data.resource_dir; |
| 130 | if (!data.config_str.empty()) { |
| 131 | name << "-" << data.config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | } |
| 133 | name << "_" << data.name; |
| 134 | if (!data.extension.empty()) { |
| 135 | name << "." << data.extension; |
| 136 | } |
| 137 | name << ".flat"; |
| 138 | return name.str(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | static bool CompileTable(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 142 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | const std::string& output_path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | ResourceTable table; |
| 145 | { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 146 | auto fin = file->OpenInputStream(); |
| 147 | if (fin->HadError()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 149 | << "failed to open file: " << fin->GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | // Parse the values file from XML. |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 154 | xml::XmlPullParser xml_parser(fin.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 156 | ResourceParserOptions parser_options; |
| 157 | parser_options.error_on_positional_arguments = !options.legacy_mode; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 159 | // If the filename includes donottranslate, then the default translatable is false. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 160 | parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 162 | // 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 Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 166 | ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 167 | parser_options); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | if (!res_parser.Parse(&xml_parser)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | return false; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 170 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | } |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 172 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | PseudolocaleGenerator pseudolocale_generator; |
| 179 | if (!pseudolocale_generator.Consume(context, &table)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | return false; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 181 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | // Ensure we have the compilation package at least. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | table.CreatePackage(context->GetCompilationPackage()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | |
| 187 | // Assign an ID to any package that has resources. |
| 188 | for (auto& pkg : table.packages) { |
| 189 | if (!pkg->id) { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 190 | // If no package ID was set while parsing (public identifiers), auto assign an ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | pkg->id = context->GetPackageId(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 192 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | // Create the file/zip entry. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | if (!writer->StartEntry(output_path, 0)) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 197 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | return false; |
| 199 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 200 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 201 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 203 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 205 | ContainerWriter container_writer(©ing_adaptor, 1u); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 207 | pb::ResourceTable pb_table; |
Ryan Mitchell | a15c2a8 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 208 | SerializeTableToPb(table, &pb_table, context->GetDiagnostics()); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 209 | if (!container_writer.AddResTableEntry(pb_table)) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 210 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | if (!writer->FinishEntry()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 216 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 217 | return false; |
| 218 | } |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 219 | |
| 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 Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 233 | // 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 Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 248 | |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 249 | 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 Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 257 | |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 258 | 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 Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 272 | r_txt_printer.Print("_"); |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 273 | r_txt_printer.Println(attr.name.value().entry); |
Izabela Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 274 | } |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 286 | static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file, |
| 287 | io::KnownSizeInputStream* in, IArchiveWriter* writer, |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 288 | IDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | // Start the entry so we can write the header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 290 | if (!writer->StartEntry(output_path, 0)) { |
| 291 | diag->Error(DiagMessage(output_path) << "failed to open file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 292 | return false; |
| 293 | } |
| 294 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 295 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 297 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 299 | ContainerWriter container_writer(©ing_adaptor, 1u); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 300 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 301 | pb::internal::CompiledFile pb_compiled_file; |
| 302 | SerializeCompiledFileToPb(file, &pb_compiled_file); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 303 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 304 | if (!container_writer.AddResFileEntry(pb_compiled_file, in)) { |
| 305 | diag->Error(DiagMessage(output_path) << "failed to write entry data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 307 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 308 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 309 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | if (!writer->FinishEntry()) { |
| 311 | diag->Error(DiagMessage(output_path) << "failed to finish writing data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 317 | static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres, |
| 318 | ContainerWriter* container_writer, IDiagnostics* diag) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 319 | pb::internal::CompiledFile pb_compiled_file; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 320 | SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 321 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 322 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 335 | static bool IsValidFile(IAaptContext* context, const std::string& input_path) { |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 336 | 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 Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 341 | } else if (file_type == file::FileType::kNonexistant) { |
| 342 | context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found"); |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 343 | } 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | static bool CompileXml(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 353 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 354 | const std::string& output_path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 355 | if (context->IsVerbose()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 356 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | std::unique_ptr<xml::XmlResource> xmlres; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 360 | { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 361 | auto fin = file->OpenInputStream(); |
| 362 | if (fin->HadError()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 363 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 364 | << "failed to open file: " << fin->GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 365 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 366 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 367 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 368 | xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source); |
| 369 | if (!xmlres) { |
| 370 | return false; |
| 371 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 374 | xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 375 | xmlres->file.config = path_data.config; |
| 376 | xmlres->file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 377 | xmlres->file.type = ResourceFile::Type::kProtoXml; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 378 | |
| 379 | // Collect IDs that are defined here. |
| 380 | XmlIdCollector collector; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | if (!collector.Consume(context, xmlres.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | |
| 385 | // Look for and process any <aapt:attr> tags and create sub-documents. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 386 | InlineXmlFormatParser inline_xml_format_parser; |
| 387 | if (!inline_xml_format_parser.Consume(context, xmlres.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
| 391 | // Start the entry so we can write the header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 392 | if (!writer->StartEntry(output_path, 0)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 393 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 397 | std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents = |
| 398 | inline_xml_format_parser.GetExtractedInlineXmlDocuments(); |
| 399 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 400 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 401 | { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 402 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 403 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 404 | ContainerWriter container_writer(©ing_adaptor, 1u + inline_documents.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 405 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 406 | if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer, |
| 407 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 408 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 411 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 414 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 415 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 416 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 417 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 418 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 419 | if (!writer->FinishEntry()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 420 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 421 | return false; |
| 422 | } |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 423 | |
| 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 448 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 451 | static bool CompilePng(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 452 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 453 | const std::string& output_path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 454 | if (context->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 455 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | BigBuffer buffer(4096); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 459 | ResourceFile res_file; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 460 | res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 461 | res_file.config = path_data.config; |
| 462 | res_file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 463 | res_file.type = ResourceFile::Type::kPng; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 464 | |
| 465 | { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 466 | auto data = file->OpenAsData(); |
| 467 | if (!data) { |
| 468 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file "); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 469 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 472 | // 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 477 | BigBuffer crunched_png_buffer(4096); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 478 | io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 479 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | // 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 482 | PngChunkFilter png_chunk_filter(content); |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 483 | std::unique_ptr<Image> image = ReadPng(context, path_data.source, &png_chunk_filter); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 484 | if (!image) { |
| 485 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 488 | std::unique_ptr<NinePatch> nine_patch; |
| 489 | if (path_data.extension == "9.png") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 490 | std::string err; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 491 | nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 492 | if (!nine_patch) { |
| 493 | context->GetDiagnostics()->Error(DiagMessage() << err); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 494 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 495 | } |
| 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 Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 504 | memmove(image->rows.get(), image->rows.get() + 1, image->height * sizeof(uint8_t**)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 505 | for (int32_t h = 0; h < image->height; h++) { |
| 506 | memmove(image->rows[h], image->rows[h] + 4, image->width * 4); |
| 507 | } |
| 508 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 509 | if (context->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 510 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: " |
| 511 | << *nine_patch); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 512 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 513 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 514 | |
| 515 | // Write the crunched PNG. |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 516 | if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 520 | if (nine_patch != nullptr || |
| 521 | crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 522 | // 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 524 | buffer.AppendBuffer(std::move(crunched_png_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 525 | } else { |
| 526 | // The re-encoded PNG is larger than the original, and there is |
| 527 | // no mandatory transformation. Use the original. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 528 | if (context->IsVerbose()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 529 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) |
| 530 | << "original PNG is smaller than crunched PNG" |
| 531 | << ", using original"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 534 | png_chunk_filter.Rewind(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 535 | BigBuffer filtered_png_buffer(4096); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 536 | io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer); |
| 537 | io::Copy(&filtered_png_buffer_out, &png_chunk_filter); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 538 | buffer.AppendBuffer(std::move(filtered_png_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 541 | if (context->IsVerbose()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 542 | // 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 Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 544 | std::stringstream legacy_stream(content.to_string()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 545 | BigBuffer legacy_buffer(4096); |
| 546 | Png png(context->GetDiagnostics()); |
| 547 | if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 551 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) |
| 552 | << "legacy=" << legacy_buffer.size() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 553 | << " new=" << buffer.size()); |
| 554 | } |
| 555 | } |
| 556 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 557 | io::BigBufferInputStream buffer_in(&buffer); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 558 | return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer, |
| 559 | context->GetDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 562 | static bool CompileFile(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 563 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 564 | const std::string& output_path) { |
| 565 | if (context->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 566 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 567 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 568 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 569 | ResourceFile res_file; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 570 | res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 571 | res_file.config = path_data.config; |
| 572 | res_file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 573 | res_file.type = ResourceFile::Type::kUnknown; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 574 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 575 | auto data = file->OpenAsData(); |
| 576 | if (!data) { |
| 577 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to open file "); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 578 | return false; |
| 579 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 580 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 581 | return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer, |
| 582 | context->GetDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | class CompileContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 586 | public: |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 587 | CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) { |
| 588 | } |
| 589 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 590 | 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 Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 595 | void SetVerbose(bool val) { |
| 596 | verbose_ = val; |
| 597 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 598 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 599 | bool IsVerbose() override { |
| 600 | return verbose_; |
| 601 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 602 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 603 | IDiagnostics* GetDiagnostics() override { |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 604 | return diagnostics_; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 605 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 606 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 607 | NameMangler* GetNameMangler() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 608 | UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 609 | return nullptr; |
| 610 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 611 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 612 | const std::string& GetCompilationPackage() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 613 | static std::string empty; |
| 614 | return empty; |
| 615 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 616 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 617 | uint8_t GetPackageId() override { |
| 618 | return 0x0; |
| 619 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 620 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 621 | SymbolTable* GetExternalSymbols() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 622 | UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 623 | return nullptr; |
| 624 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 625 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 626 | int GetMinSdkVersion() override { |
| 627 | return 0; |
| 628 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 629 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 630 | private: |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 631 | DISALLOW_COPY_AND_ASSIGN(CompileContext); |
| 632 | |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 633 | IDiagnostics* diagnostics_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 634 | bool verbose_ = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 635 | }; |
| 636 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 637 | int 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 Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 660 | if (auto maybe_path_data = ExtractResourcePathData(path, inputs->GetDirSeparator(), &err_str)) { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 661 | 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 Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 710 | int CompileCommand::Action(const std::vector<std::string>& args) { |
| 711 | CompileContext context(diagnostic_); |
| 712 | context.SetVerbose(options_.verbose); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 713 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 714 | 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 Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 721 | } else { |
| 722 | context.GetDiagnostics()->Error( |
| 723 | DiagMessage() << "Unrecognized visibility level passes to --visibility: '" |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 724 | << visibility_.value() << "'. Accepted levels: public, private, default"); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 725 | return 1; |
| 726 | } |
| 727 | } |
| 728 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 729 | std::unique_ptr<io::IFileCollection> file_collection; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 730 | std::unique_ptr<IArchiveWriter> archive_writer; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 731 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 732 | // Collect the resources files to compile |
| 733 | if (options_.res_dir && options_.res_zip) { |
| 734 | context.GetDiagnostics()->Error(DiagMessage() |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 735 | << "only one of --dir and --zip can be specified"); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 736 | return 1; |
| 737 | } else if (options_.res_dir) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 738 | if (!args.empty()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 739 | context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified"); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 740 | Usage(&std::cerr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 741 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 744 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 749 | return 1; |
| 750 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 751 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 752 | archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 753 | } 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 759 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 760 | // 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 769 | } else { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 770 | auto collection = util::make_unique<io::FileCollection>(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 771 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 772 | // Collect data from the path for each input file. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 773 | for (const std::string& arg : args) { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 774 | collection->InsertFile(arg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 775 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 776 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 777 | file_collection = std::move(collection); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 778 | archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 781 | if (!archive_writer) { |
Adam Lesinski | dfaecaf | 2016-10-20 17:08:51 -0700 | [diff] [blame] | 782 | return 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 785 | return Compile(&context, file_collection.get(), archive_writer.get(), options_); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 788 | } // namespace aapt |