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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 21 | #include "android-base/errors.h" |
| 22 | #include "android-base/file.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 23 | #include "android-base/utf8.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
| 25 | #include "google/protobuf/io/coded_stream.h" |
| 26 | #include "google/protobuf/io/zero_copy_stream_impl_lite.h" |
| 27 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "ConfigDescription.h" |
| 29 | #include "Diagnostics.h" |
| 30 | #include "Flags.h" |
| 31 | #include "ResourceParser.h" |
| 32 | #include "ResourceTable.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | #include "compile/IdAssigner.h" |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 34 | #include "compile/InlineXmlFormatParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | #include "compile/Png.h" |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 36 | #include "compile/PseudolocaleGenerator.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | #include "compile/XmlIdCollector.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 38 | #include "format/Archive.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 39 | #include "format/Container.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 40 | #include "format/proto/ProtoSerialize.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 41 | #include "io/BigBufferStream.h" |
| 42 | #include "io/FileStream.h" |
| 43 | #include "io/StringStream.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 44 | #include "io/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 45 | #include "util/Files.h" |
| 46 | #include "util/Maybe.h" |
| 47 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 48 | #include "xml/XmlDom.h" |
| 49 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 50 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 51 | using ::aapt::io::FileInputStream; |
| 52 | using ::android::StringPiece; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 53 | using ::android::base::SystemErrorCodeToString; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 54 | using ::google::protobuf::io::CopyingOutputStreamAdaptor; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | namespace aapt { |
| 57 | |
| 58 | struct ResourcePathData { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | Source source; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | std::string resource_dir; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | std::string name; |
| 62 | std::string extension; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 64 | // Original config str. We keep this because when we parse the config, we may add on |
| 65 | // version qualifiers. We want to preserve the original input so the output is easily |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | // computed before hand. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | std::string config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | ConfigDescription config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 71 | // Resource file paths are expected to look like: [--/res/]type[-config]/name |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path, |
| 73 | std::string* out_error) { |
| 74 | std::vector<std::string> parts = util::Split(path, file::sDirSep); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | if (parts.size() < 2) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | if (out_error) *out_error = "bad resource path"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | return {}; |
| 78 | } |
| 79 | |
| 80 | std::string& dir = parts[parts.size() - 2]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | StringPiece dir_str = dir; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | StringPiece config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | ConfigDescription config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | size_t dash_pos = dir.find('-'); |
| 86 | if (dash_pos != std::string::npos) { |
| 87 | config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1)); |
| 88 | if (!ConfigDescription::Parse(config_str, &config)) { |
| 89 | if (out_error) { |
| 90 | std::stringstream err_str; |
| 91 | err_str << "invalid configuration '" << config_str << "'"; |
| 92 | *out_error = err_str.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | } |
| 94 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | dir_str = dir_str.substr(0, dash_pos); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 98 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | std::string& filename = parts[parts.size() - 1]; |
| 100 | StringPiece name = filename; |
| 101 | StringPiece extension; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | size_t dot_pos = filename.find('.'); |
| 103 | if (dot_pos != std::string::npos) { |
| 104 | extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1)); |
| 105 | name = name.substr(0, dot_pos); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 108 | return ResourcePathData{Source(path), dir_str.to_string(), name.to_string(), |
| 109 | extension.to_string(), config_str.to_string(), config}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | struct CompileOptions { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | std::string output_path; |
| 114 | Maybe<std::string> res_dir; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | bool pseudolocalize = false; |
Adam Lesinski | 28e6c0b | 2017-05-10 14:56:36 -0700 | [diff] [blame] | 116 | bool no_png_crunch = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | bool legacy_mode = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | bool verbose = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 121 | static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | std::stringstream name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | name << data.resource_dir; |
| 124 | if (!data.config_str.empty()) { |
| 125 | name << "-" << data.config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | } |
| 127 | name << "_" << data.name; |
| 128 | if (!data.extension.empty()) { |
| 129 | name << "." << data.extension; |
| 130 | } |
| 131 | name << ".flat"; |
| 132 | return name.str(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 135 | static bool IsHidden(const StringPiece& filename) { |
| 136 | return util::StartsWith(filename, "."); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 139 | // Walks the res directory structure, looking for resource files. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 140 | static bool LoadInputFilesFromDir(IAaptContext* context, const CompileOptions& options, |
| 141 | std::vector<ResourcePathData>* out_path_data) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | const std::string& root_dir = options.res_dir.value(); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 143 | std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | if (!d) { |
Adam Lesinski | 60d9c2f | 2017-05-17 16:07:45 -0700 | [diff] [blame] | 145 | context->GetDiagnostics()->Error(DiagMessage(root_dir) << "failed to open directory: " |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 146 | << SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
| 150 | while (struct dirent* entry = readdir(d.get())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | if (IsHidden(entry->d_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | continue; |
| 153 | } |
| 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | std::string prefix_path = root_dir; |
| 156 | file::AppendPath(&prefix_path, entry->d_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | if (file::GetFileType(prefix_path) != file::FileType::kDirectory) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | continue; |
| 160 | } |
| 161 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 162 | std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | if (!subdir) { |
Adam Lesinski | 60d9c2f | 2017-05-17 16:07:45 -0700 | [diff] [blame] | 164 | context->GetDiagnostics()->Error(DiagMessage(prefix_path) << "failed to open directory: " |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 165 | << SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | while (struct dirent* leaf_entry = readdir(subdir.get())) { |
| 170 | if (IsHidden(leaf_entry->d_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | continue; |
| 172 | } |
| 173 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | std::string full_path = prefix_path; |
| 175 | file::AppendPath(&full_path, leaf_entry->d_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | std::string err_str; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 178 | Maybe<ResourcePathData> path_data = ExtractResourcePathData(full_path, &err_str); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | if (!path_data) { |
Adam Lesinski | 60d9c2f | 2017-05-17 16:07:45 -0700 | [diff] [blame] | 180 | context->GetDiagnostics()->Error(DiagMessage(full_path) << err_str); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 181 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | out_path_data->push_back(std::move(path_data.value())); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 185 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | } |
| 187 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | static bool CompileTable(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 191 | const ResourcePathData& path_data, IArchiveWriter* writer, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 192 | const std::string& output_path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | ResourceTable table; |
| 194 | { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 195 | FileInputStream fin(path_data.source.path); |
| 196 | if (fin.HadError()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 198 | << "failed to open file: " << fin.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | // Parse the values file from XML. |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 203 | xml::XmlPullParser xml_parser(&fin); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | ResourceParserOptions parser_options; |
| 206 | parser_options.error_on_positional_arguments = !options.legacy_mode; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 208 | // If the filename includes donottranslate, then the default translatable is false. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 209 | parser_options.translatable = path_data.name.find("donottranslate") == std::string::npos; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 211 | ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 212 | parser_options); |
| 213 | if (!res_parser.Parse(&xml_parser)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | return false; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 215 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | } |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 217 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | if (options.pseudolocalize) { |
| 219 | // Generate pseudo-localized strings (en-XA and ar-XB). |
| 220 | // These are created as weak symbols, and are only generated from default |
| 221 | // configuration |
| 222 | // strings and plurals. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | PseudolocaleGenerator pseudolocale_generator; |
| 224 | if (!pseudolocale_generator.Consume(context, &table)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | return false; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 226 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 228 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | // Ensure we have the compilation package at least. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | table.CreatePackage(context->GetCompilationPackage()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | |
| 232 | // Assign an ID to any package that has resources. |
| 233 | for (auto& pkg : table.packages) { |
| 234 | if (!pkg->id) { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 235 | // 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] | 236 | pkg->id = context->GetPackageId(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 237 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 239 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 240 | // Create the file/zip entry. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 241 | if (!writer->StartEntry(output_path, 0)) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 242 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 245 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 246 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 248 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 250 | ContainerWriter container_writer(©ing_adaptor, 1u); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 252 | pb::ResourceTable pb_table; |
| 253 | SerializeTableToPb(table, &pb_table); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 254 | if (!container_writer.AddResTableEntry(pb_table)) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 255 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to write"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 257 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 259 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | if (!writer->FinishEntry()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 261 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 267 | static bool WriteHeaderAndDataToWriter(const StringPiece& output_path, const ResourceFile& file, |
| 268 | io::KnownSizeInputStream* in, IArchiveWriter* writer, |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 269 | IDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | // Start the entry so we can write the header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 271 | if (!writer->StartEntry(output_path, 0)) { |
| 272 | diag->Error(DiagMessage(output_path) << "failed to open file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 276 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 277 | { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 278 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 280 | ContainerWriter container_writer(©ing_adaptor, 1u); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 282 | pb::internal::CompiledFile pb_compiled_file; |
| 283 | SerializeCompiledFileToPb(file, &pb_compiled_file); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 284 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 285 | if (!container_writer.AddResFileEntry(pb_compiled_file, in)) { |
| 286 | diag->Error(DiagMessage(output_path) << "failed to write entry data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 287 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 288 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 290 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | if (!writer->FinishEntry()) { |
| 292 | diag->Error(DiagMessage(output_path) << "failed to finish writing data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 298 | static bool FlattenXmlToOutStream(const StringPiece& output_path, const xml::XmlResource& xmlres, |
| 299 | ContainerWriter* container_writer, IDiagnostics* diag) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 300 | pb::internal::CompiledFile pb_compiled_file; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 301 | SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 302 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 303 | pb::XmlNode pb_xml_node; |
| 304 | SerializeXmlToPb(*xmlres.root, &pb_xml_node); |
| 305 | |
| 306 | std::string serialized_xml = pb_xml_node.SerializeAsString(); |
| 307 | io::StringInputStream serialized_in(serialized_xml); |
| 308 | |
| 309 | if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) { |
| 310 | diag->Error(DiagMessage(output_path) << "failed to write entry data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 316 | static bool IsValidFile(IAaptContext* context, const std::string& input_path) { |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 317 | const file::FileType file_type = file::GetFileType(input_path); |
| 318 | if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) { |
| 319 | if (file_type == file::FileType::kDirectory) { |
| 320 | context->GetDiagnostics()->Error(DiagMessage(input_path) |
| 321 | << "resource file cannot be a directory"); |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 322 | } else if (file_type == file::FileType::kNonexistant) { |
| 323 | context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found"); |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 324 | } else { |
| 325 | context->GetDiagnostics()->Error(DiagMessage(input_path) |
| 326 | << "not a valid resource file"); |
| 327 | } |
| 328 | return false; |
| 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | static bool CompileXml(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 334 | const ResourcePathData& path_data, IArchiveWriter* writer, |
| 335 | const std::string& output_path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 336 | if (context->IsVerbose()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 337 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling XML"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 340 | std::unique_ptr<xml::XmlResource> xmlres; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 342 | FileInputStream fin(path_data.source.path); |
| 343 | if (fin.HadError()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 344 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 345 | << "failed to open file: " << fin.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 346 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 347 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 348 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 349 | xmlres = xml::Inflate(&fin, context->GetDiagnostics(), path_data.source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 351 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 352 | if (!xmlres) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 356 | xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 357 | xmlres->file.config = path_data.config; |
| 358 | xmlres->file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 359 | xmlres->file.type = ResourceFile::Type::kProtoXml; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 360 | |
| 361 | // Collect IDs that are defined here. |
| 362 | XmlIdCollector collector; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 363 | if (!collector.Consume(context, xmlres.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 364 | return false; |
| 365 | } |
| 366 | |
| 367 | // Look for and process any <aapt:attr> tags and create sub-documents. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | InlineXmlFormatParser inline_xml_format_parser; |
| 369 | if (!inline_xml_format_parser.Consume(context, xmlres.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // Start the entry so we can write the header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 374 | if (!writer->StartEntry(output_path, 0)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 375 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to open file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 376 | return false; |
| 377 | } |
| 378 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 379 | std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents = |
| 380 | inline_xml_format_parser.GetExtractedInlineXmlDocuments(); |
| 381 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 382 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 383 | { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 384 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 385 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 386 | ContainerWriter container_writer(©ing_adaptor, 1u + inline_documents.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 387 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 388 | if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer, |
| 389 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 393 | for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) { |
| 394 | if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer, |
| 395 | context->GetDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 396 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 397 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 398 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 399 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 400 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 401 | if (!writer->FinishEntry()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 402 | context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 408 | static bool CompilePng(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 409 | const ResourcePathData& path_data, IArchiveWriter* writer, |
| 410 | const std::string& output_path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 411 | if (context->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 412 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling PNG"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | BigBuffer buffer(4096); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 416 | ResourceFile res_file; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 417 | res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 418 | res_file.config = path_data.config; |
| 419 | res_file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 420 | res_file.type = ResourceFile::Type::kPng; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 421 | |
| 422 | { |
| 423 | std::string content; |
Adam Lesinski | 2354b56 | 2017-05-26 16:31:38 -0700 | [diff] [blame] | 424 | if (!android::base::ReadFileToString(path_data.source.path, &content, |
| 425 | true /*follow_symlinks*/)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 426 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) |
Adam Lesinski | 60d9c2f | 2017-05-17 16:07:45 -0700 | [diff] [blame] | 427 | << "failed to open file: " |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 428 | << SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 429 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 432 | BigBuffer crunched_png_buffer(4096); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 433 | io::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 434 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 435 | // Ensure that we only keep the chunks we care about if we end up |
| 436 | // using the original PNG instead of the crunched one. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 437 | PngChunkFilter png_chunk_filter(content); |
Adam Lesinski | cc73e99 | 2017-05-12 18:16:44 -0700 | [diff] [blame] | 438 | 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] | 439 | if (!image) { |
| 440 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 443 | std::unique_ptr<NinePatch> nine_patch; |
| 444 | if (path_data.extension == "9.png") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 445 | std::string err; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 446 | nine_patch = NinePatch::Create(image->rows.get(), image->width, image->height, &err); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 447 | if (!nine_patch) { |
| 448 | context->GetDiagnostics()->Error(DiagMessage() << err); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 449 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Remove the 1px border around the NinePatch. |
| 453 | // Basically the row array is shifted up by 1, and the length is treated |
| 454 | // as height - 2. |
| 455 | // For each row, shift the array to the left by 1, and treat the length as |
| 456 | // width - 2. |
| 457 | image->width -= 2; |
| 458 | image->height -= 2; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 459 | 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] | 460 | for (int32_t h = 0; h < image->height; h++) { |
| 461 | memmove(image->rows[h], image->rows[h] + 4, image->width * 4); |
| 462 | } |
| 463 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 464 | if (context->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 465 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "9-patch: " |
| 466 | << *nine_patch); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 467 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 468 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 469 | |
| 470 | // Write the crunched PNG. |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 471 | if (!WritePng(context, image.get(), nine_patch.get(), &crunched_png_buffer_out, {})) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 472 | return false; |
| 473 | } |
| 474 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 475 | if (nine_patch != nullptr || |
| 476 | crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 477 | // No matter what, we must use the re-encoded PNG, even if it is larger. |
| 478 | // 9-patch images must be re-encoded since their borders are stripped. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 479 | buffer.AppendBuffer(std::move(crunched_png_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | } else { |
| 481 | // The re-encoded PNG is larger than the original, and there is |
| 482 | // no mandatory transformation. Use the original. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 483 | if (context->IsVerbose()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 484 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) |
| 485 | << "original PNG is smaller than crunched PNG" |
| 486 | << ", using original"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 489 | png_chunk_filter.Rewind(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 490 | BigBuffer filtered_png_buffer(4096); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 491 | io::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer); |
| 492 | io::Copy(&filtered_png_buffer_out, &png_chunk_filter); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 493 | buffer.AppendBuffer(std::move(filtered_png_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 496 | if (context->IsVerbose()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 497 | // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes. |
| 498 | // This will help catch exotic cases where the new code may generate larger PNGs. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 499 | std::stringstream legacy_stream(content); |
| 500 | BigBuffer legacy_buffer(4096); |
| 501 | Png png(context->GetDiagnostics()); |
| 502 | if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } |
| 505 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 506 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) |
| 507 | << "legacy=" << legacy_buffer.size() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 508 | << " new=" << buffer.size()); |
| 509 | } |
| 510 | } |
| 511 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 512 | io::BigBufferInputStream buffer_in(&buffer); |
| 513 | if (!WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer, |
| 514 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 520 | static bool CompileFile(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 521 | const ResourcePathData& path_data, IArchiveWriter* writer, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 522 | const std::string& output_path) { |
| 523 | if (context->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 524 | context->GetDiagnostics()->Note(DiagMessage(path_data.source) << "compiling file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 525 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 526 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 527 | BigBuffer buffer(256); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 528 | ResourceFile res_file; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 529 | res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | res_file.config = path_data.config; |
| 531 | res_file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 532 | res_file.type = ResourceFile::Type::kUnknown; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 533 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 534 | std::string error_str; |
| 535 | Maybe<android::FileMap> f = file::MmapPath(path_data.source.path, &error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 536 | if (!f) { |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 537 | context->GetDiagnostics()->Error(DiagMessage(path_data.source) << "failed to mmap file: " |
| 538 | << error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 539 | return false; |
| 540 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 541 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 542 | io::MmappedData mmapped_in(std::move(f.value())); |
| 543 | if (!WriteHeaderAndDataToWriter(output_path, res_file, &mmapped_in, writer, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 544 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 545 | return false; |
| 546 | } |
| 547 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | class CompileContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 551 | public: |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 552 | CompileContext(IDiagnostics* diagnostics) : diagnostics_(diagnostics) { |
| 553 | } |
| 554 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 555 | PackageType GetPackageType() override { |
| 556 | // Every compilation unit starts as an app and then gets linked as potentially something else. |
| 557 | return PackageType::kApp; |
| 558 | } |
| 559 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 560 | void SetVerbose(bool val) { |
| 561 | verbose_ = val; |
| 562 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 563 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 564 | bool IsVerbose() override { |
| 565 | return verbose_; |
| 566 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 567 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 568 | IDiagnostics* GetDiagnostics() override { |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 569 | return diagnostics_; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 570 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 571 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 572 | NameMangler* GetNameMangler() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 573 | UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 574 | return nullptr; |
| 575 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 576 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 577 | const std::string& GetCompilationPackage() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 578 | static std::string empty; |
| 579 | return empty; |
| 580 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 581 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 582 | uint8_t GetPackageId() override { |
| 583 | return 0x0; |
| 584 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 585 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 586 | SymbolTable* GetExternalSymbols() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 587 | UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 588 | return nullptr; |
| 589 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 590 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 591 | int GetMinSdkVersion() override { |
| 592 | return 0; |
| 593 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 594 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 595 | private: |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 596 | DISALLOW_COPY_AND_ASSIGN(CompileContext); |
| 597 | |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 598 | IDiagnostics* diagnostics_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 599 | bool verbose_ = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 600 | }; |
| 601 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 602 | // Entry point for compilation phase. Parses arguments and dispatches to the correct steps. |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 603 | int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) { |
| 604 | CompileContext context(diagnostics); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 605 | CompileOptions options; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 606 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 607 | bool verbose = false; |
| 608 | Flags flags = |
| 609 | Flags() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 610 | .RequiredFlag("-o", "Output path", &options.output_path) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 611 | .OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 612 | .OptionalSwitch("--pseudo-localize", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 613 | "Generate resources for pseudo-locales " |
| 614 | "(en-XA and ar-XB)", |
| 615 | &options.pseudolocalize) |
Adam Lesinski | 28e6c0b | 2017-05-10 14:56:36 -0700 | [diff] [blame] | 616 | .OptionalSwitch("--no-crunch", "Disables PNG processing", &options.no_png_crunch) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 617 | .OptionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings", |
| 618 | &options.legacy_mode) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 619 | .OptionalSwitch("-v", "Enables verbose logging", &verbose); |
| 620 | if (!flags.Parse("aapt2 compile", args, &std::cerr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 621 | return 1; |
| 622 | } |
| 623 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 624 | context.SetVerbose(verbose); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 625 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 626 | std::unique_ptr<IArchiveWriter> archive_writer; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 627 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 628 | std::vector<ResourcePathData> input_data; |
| 629 | if (options.res_dir) { |
| 630 | if (!flags.GetArgs().empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 631 | // Can't have both files and a resource directory. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 632 | context.GetDiagnostics()->Error(DiagMessage() << "files given but --dir specified"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 633 | flags.Usage("aapt2 compile", &std::cerr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 634 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 637 | if (!LoadInputFilesFromDir(&context, options, &input_data)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 638 | return 1; |
| 639 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 640 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 641 | archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options.output_path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 642 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 643 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 644 | input_data.reserve(flags.GetArgs().size()); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 645 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 646 | // Collect data from the path for each input file. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 647 | for (const std::string& arg : flags.GetArgs()) { |
| 648 | std::string error_str; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 649 | if (Maybe<ResourcePathData> path_data = ExtractResourcePathData(arg, &error_str)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 650 | input_data.push_back(std::move(path_data.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 651 | } else { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 652 | context.GetDiagnostics()->Error(DiagMessage() << error_str << " (" << arg << ")"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 653 | return 1; |
| 654 | } |
| 655 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 656 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 657 | archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options.output_path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 660 | if (!archive_writer) { |
Adam Lesinski | dfaecaf | 2016-10-20 17:08:51 -0700 | [diff] [blame] | 661 | return 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 665 | for (ResourcePathData& path_data : input_data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 666 | if (options.verbose) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 667 | context.GetDiagnostics()->Note(DiagMessage(path_data.source) << "processing"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 670 | if (!IsValidFile(&context, path_data.source.path)) { |
| 671 | error = true; |
| 672 | continue; |
| 673 | } |
| 674 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 675 | // Determine how to compile the file based on its type. |
| 676 | auto compile_func = &CompileFile; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 677 | if (path_data.resource_dir == "values") { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 678 | compile_func = &CompileTable; |
| 679 | // We use a different extension (not necessary anymore, but avoids altering the existing |
| 680 | // build system logic). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 681 | path_data.extension = "arsc"; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 682 | } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) { |
| 683 | if (*type != ResourceType::kRaw) { |
| 684 | if (path_data.extension == "xml") { |
| 685 | compile_func = &CompileXml; |
| 686 | } else if (!options.no_png_crunch && |
| 687 | (path_data.extension == "png" || path_data.extension == "9.png")) { |
| 688 | compile_func = &CompilePng; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 689 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 690 | } |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 691 | } else { |
| 692 | context.GetDiagnostics()->Error(DiagMessage() |
| 693 | << "invalid file path '" << path_data.source << "'"); |
| 694 | error = true; |
| 695 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 698 | // Compile the file. |
| 699 | const std::string out_path = BuildIntermediateContainerFilename(path_data); |
| 700 | error |= !compile_func(&context, options, path_data, archive_writer.get(), out_path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 701 | } |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame^] | 702 | return error ? 1 : 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 705 | } // namespace aapt |