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