Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 17 | #include "Compile.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 20 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 23 | #include "ResourceParser.h" |
| 24 | #include "ResourceTable.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "android-base/errors.h" |
| 26 | #include "android-base/file.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 27 | #include "android-base/utf8.h" |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 28 | #include "androidfw/BigBufferStream.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 29 | #include "androidfw/ConfigDescription.h" |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 30 | #include "androidfw/FileStream.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 31 | #include "androidfw/IDiagnostics.h" |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 32 | #include "androidfw/Image.h" |
| 33 | #include "androidfw/Png.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 34 | #include "androidfw/StringPiece.h" |
Izabela Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 35 | #include "cmd/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | #include "compile/IdAssigner.h" |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 37 | #include "compile/InlineXmlFormatParser.h" |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 38 | #include "compile/PseudolocaleGenerator.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | #include "compile/XmlIdCollector.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 40 | #include "format/Archive.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 41 | #include "format/Container.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 42 | #include "format/proto/ProtoSerialize.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 43 | #include "google/protobuf/io/coded_stream.h" |
| 44 | #include "google/protobuf/io/zero_copy_stream_impl_lite.h" |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 45 | #include "io/FileSystem.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 46 | #include "io/StringStream.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 47 | #include "io/Util.h" |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 48 | #include "io/ZipArchive.h" |
Inseob Kim | 5fe521e | 2023-09-15 16:38:50 +0900 | [diff] [blame] | 49 | #include "process/ProductFilter.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 50 | #include "trace/TraceBuffer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | #include "util/Files.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 53 | #include "xml/XmlDom.h" |
| 54 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 56 | using ::aapt::text::Printer; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 57 | using ::android::ConfigDescription; |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 58 | using ::android::FileInputStream; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 59 | using ::android::StringPiece; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 60 | using ::android::base::SystemErrorCodeToString; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 61 | using ::google::protobuf::io::CopyingOutputStreamAdaptor; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | namespace aapt { |
| 64 | |
| 65 | struct ResourcePathData { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 66 | android::Source source; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | std::string resource_dir; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | std::string name; |
| 69 | std::string extension; |
Jeremy Meyer | 988be5d | 2024-09-03 16:31:54 -0700 | [diff] [blame] | 70 | std::string flag_name; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 72 | // Original config str. We keep this because when we parse the config, we may add on |
| 73 | // 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] | 74 | // computed before hand. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | std::string config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | ConfigDescription config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 79 | // Resource file paths are expected to look like: [--/res/]type[-config]/name |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 80 | static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path, |
| 81 | const char dir_sep, |
| 82 | std::string* out_error, |
| 83 | const CompileOptions& options) { |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 84 | std::vector<std::string> parts = util::Split(path, dir_sep); |
Jeremy Meyer | 988be5d | 2024-09-03 16:31:54 -0700 | [diff] [blame] | 85 | |
| 86 | std::string flag_name; |
| 87 | // Check for a flag |
| 88 | for (auto iter = parts.begin(); iter != parts.end();) { |
| 89 | if (iter->starts_with("flag(") && iter->ends_with(")")) { |
| 90 | if (!flag_name.empty()) { |
| 91 | if (out_error) *out_error = "resource path cannot contain more than one flag directory"; |
| 92 | return {}; |
| 93 | } |
| 94 | flag_name = iter->substr(5, iter->size() - 6); |
| 95 | iter = parts.erase(iter); |
| 96 | } else { |
| 97 | ++iter; |
| 98 | } |
| 99 | } |
| 100 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | if (parts.size() < 2) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | if (out_error) *out_error = "bad resource path"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | return {}; |
| 104 | } |
| 105 | |
| 106 | std::string& dir = parts[parts.size() - 2]; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | StringPiece dir_str = dir; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | StringPiece config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | ConfigDescription config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | size_t dash_pos = dir.find('-'); |
| 112 | if (dash_pos != std::string::npos) { |
| 113 | config_str = dir_str.substr(dash_pos + 1, dir.size() - (dash_pos + 1)); |
| 114 | if (!ConfigDescription::Parse(config_str, &config)) { |
| 115 | if (out_error) { |
| 116 | std::stringstream err_str; |
| 117 | err_str << "invalid configuration '" << config_str << "'"; |
| 118 | *out_error = err_str.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | } |
| 120 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 121 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | dir_str = dir_str.substr(0, dash_pos); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | std::string& filename = parts[parts.size() - 1]; |
| 126 | StringPiece name = filename; |
| 127 | StringPiece extension; |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 128 | |
| 129 | const std::string kNinePng = ".9.png"; |
| 130 | if (filename.size() > kNinePng.size() |
| 131 | && std::equal(kNinePng.rbegin(), kNinePng.rend(), filename.rbegin())) { |
| 132 | // Split on .9.png if this extension is present at the end of the file path |
| 133 | name = name.substr(0, filename.size() - kNinePng.size()); |
| 134 | extension = "9.png"; |
| 135 | } else { |
| 136 | // Split on the last period occurrence |
| 137 | size_t dot_pos = filename.rfind('.'); |
| 138 | if (dot_pos != std::string::npos) { |
| 139 | extension = name.substr(dot_pos + 1, filename.size() - (dot_pos + 1)); |
| 140 | name = name.substr(0, dot_pos); |
| 141 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 144 | const android::Source res_path = |
| 145 | options.source_path ? StringPiece(options.source_path.value()) : StringPiece(path); |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 146 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 147 | return ResourcePathData{res_path, |
| 148 | std::string(dir_str), |
| 149 | std::string(name), |
| 150 | std::string(extension), |
Jeremy Meyer | 988be5d | 2024-09-03 16:31:54 -0700 | [diff] [blame] | 151 | std::move(flag_name), |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 152 | std::string(config_str), |
| 153 | config}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 156 | static std::string BuildIntermediateContainerFilename(const ResourcePathData& data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | std::stringstream name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | name << data.resource_dir; |
| 159 | if (!data.config_str.empty()) { |
| 160 | name << "-" << data.config_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | } |
| 162 | name << "_" << data.name; |
Jeremy Meyer | 988be5d | 2024-09-03 16:31:54 -0700 | [diff] [blame] | 163 | if (!data.flag_name.empty()) { |
| 164 | name << ".(" << data.flag_name << ")"; |
| 165 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | if (!data.extension.empty()) { |
| 167 | name << "." << data.extension; |
| 168 | } |
| 169 | name << ".flat"; |
| 170 | return name.str(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 173 | static bool CompileTable(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 174 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | const std::string& output_path) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 176 | TRACE_CALL(); |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 177 | // Filenames starting with "donottranslate" are not localizable |
| 178 | bool translatable_file = path_data.name.find("donottranslate") != 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | ResourceTable table; |
| 180 | { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 181 | auto fin = file->OpenInputStream(); |
| 182 | if (fin->HadError()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 183 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) |
| 184 | << "failed to open file: " << fin->GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 187 | // Parse the values file from XML. |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 188 | xml::XmlPullParser xml_parser(fin.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 190 | ResourceParserOptions parser_options; |
| 191 | parser_options.error_on_positional_arguments = !options.legacy_mode; |
Donald Chai | 94e4a01 | 2020-01-06 13:52:41 -0800 | [diff] [blame] | 192 | parser_options.preserve_visibility_of_styleables = options.preserve_visibility_of_styleables; |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 193 | parser_options.translatable = translatable_file; |
Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 194 | parser_options.feature_flag_values = options.feature_flag_values; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 196 | // If visibility was forced, we need to use it when creating a new resource and also error if |
| 197 | // we try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags. |
| 198 | parser_options.visibility = options.visibility; |
Jeremy Meyer | e08e037 | 2024-09-17 12:45:26 -0700 | [diff] [blame^] | 199 | parser_options.flag = ParseFlag(path_data.flag_name); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 200 | |
Jeremy Meyer | e08e037 | 2024-09-17 12:45:26 -0700 | [diff] [blame^] | 201 | if (parser_options.flag) { |
Jeremy Meyer | 988be5d | 2024-09-03 16:31:54 -0700 | [diff] [blame] | 202 | std::string error; |
Jeremy Meyer | e08e037 | 2024-09-17 12:45:26 -0700 | [diff] [blame^] | 203 | auto flag_status = GetFlagStatus(parser_options.flag, options.feature_flag_values, &error); |
Jeremy Meyer | 988be5d | 2024-09-03 16:31:54 -0700 | [diff] [blame] | 204 | if (flag_status) { |
| 205 | parser_options.flag_status = std::move(flag_status.value()); |
| 206 | } else { |
| 207 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error); |
| 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 212 | ResourceParser res_parser(context->GetDiagnostics(), &table, path_data.source, path_data.config, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 213 | parser_options); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | if (!res_parser.Parse(&xml_parser)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | return false; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 216 | } |
Inseob Kim | 5fe521e | 2023-09-15 16:38:50 +0900 | [diff] [blame] | 217 | |
| 218 | if (options.product_.has_value()) { |
| 219 | if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true) |
| 220 | .Consume(context, &table)) { |
| 221 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) |
| 222 | << "failed to filter product"); |
| 223 | return false; |
| 224 | } |
| 225 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 226 | } |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 227 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 228 | if (options.pseudolocalize && translatable_file) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | // Generate pseudo-localized strings (en-XA and ar-XB). |
| 230 | // These are created as weak symbols, and are only generated from default |
| 231 | // configuration |
| 232 | // strings and plurals. |
Brandon Liu | 5274e06 | 2023-05-25 22:41:10 +0000 | [diff] [blame] | 233 | std::string grammatical_gender_values; |
| 234 | std::string grammatical_gender_ratio; |
| 235 | if (options.pseudo_localize_gender_values) { |
| 236 | grammatical_gender_values = options.pseudo_localize_gender_values.value(); |
| 237 | } else { |
| 238 | grammatical_gender_values = "f,m,n"; |
| 239 | } |
| 240 | if (options.pseudo_localize_gender_ratio) { |
| 241 | grammatical_gender_ratio = options.pseudo_localize_gender_ratio.value(); |
| 242 | } else { |
| 243 | grammatical_gender_ratio = "1.0"; |
| 244 | } |
| 245 | PseudolocaleGenerator pseudolocale_generator(grammatical_gender_values, |
| 246 | grammatical_gender_ratio); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | if (!pseudolocale_generator.Consume(context, &table)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | return false; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 249 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 251 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | // Create the file/zip entry. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | if (!writer->StartEntry(output_path, 0)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 254 | context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 257 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 258 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 260 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 262 | ContainerWriter container_writer(©ing_adaptor, 1u); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 264 | pb::ResourceTable pb_table; |
Ryan Mitchell | a15c2a8 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 265 | SerializeTableToPb(table, &pb_table, context->GetDiagnostics()); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 266 | if (!container_writer.AddResTableEntry(pb_table)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 267 | context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to write"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 268 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 269 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 271 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | if (!writer->FinishEntry()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 273 | context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to finish entry"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 274 | return false; |
| 275 | } |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 276 | |
| 277 | if (options.generate_text_symbols_path) { |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 278 | android::FileOutputStream fout_text(options.generate_text_symbols_path.value()); |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 279 | |
| 280 | if (fout_text.HadError()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 281 | context->GetDiagnostics()->Error(android::DiagMessage() |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 282 | << "failed writing to'" |
| 283 | << options.generate_text_symbols_path.value() |
| 284 | << "': " << fout_text.GetError()); |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | Printer r_txt_printer(&fout_text); |
| 289 | for (const auto& package : table.packages) { |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 290 | // Only print resources defined locally, e.g. don't write android attributes. |
| 291 | if (package->name.empty()) { |
| 292 | for (const auto& type : package->types) { |
| 293 | for (const auto& entry : type->entries) { |
| 294 | // Check access modifiers. |
| 295 | switch (entry->visibility.level) { |
| 296 | case Visibility::Level::kUndefined : |
| 297 | r_txt_printer.Print("default "); |
| 298 | break; |
| 299 | case Visibility::Level::kPublic : |
| 300 | r_txt_printer.Print("public "); |
| 301 | break; |
| 302 | case Visibility::Level::kPrivate : |
| 303 | r_txt_printer.Print("private "); |
| 304 | } |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 305 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 306 | if (type->named_type.type != ResourceType::kStyleable) { |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 307 | r_txt_printer.Print("int "); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 308 | r_txt_printer.Print(type->named_type.to_string()); |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 309 | r_txt_printer.Print(" "); |
| 310 | r_txt_printer.Println(entry->name); |
| 311 | } else { |
| 312 | r_txt_printer.Print("int[] styleable "); |
| 313 | r_txt_printer.Println(entry->name); |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 314 | |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 315 | if (!entry->values.empty()) { |
| 316 | auto styleable = |
| 317 | static_cast<const Styleable*>(entry->values.front()->value.get()); |
| 318 | for (const auto& attr : styleable->entries) { |
| 319 | // The visibility of the children under the styleable does not matter as they are |
| 320 | // nested under their parent and use its visibility. |
| 321 | r_txt_printer.Print("default int styleable "); |
| 322 | r_txt_printer.Print(entry->name); |
| 323 | // If the package name is present, also include it in the mangled name (e.g. |
| 324 | // "android") |
| 325 | if (!attr.name.value().package.empty()) { |
| 326 | r_txt_printer.Print("_"); |
| 327 | r_txt_printer.Print(MakePackageSafeName(attr.name.value().package)); |
| 328 | } |
Izabela Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 329 | r_txt_printer.Print("_"); |
Izabela Orlowska | f67d486 | 2018-07-24 11:54:32 +0100 | [diff] [blame] | 330 | r_txt_printer.Println(attr.name.value().entry); |
Izabela Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 331 | } |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 340 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 343 | static bool WriteHeaderAndDataToWriter(StringPiece output_path, const ResourceFile& file, |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 344 | android::KnownSizeInputStream* in, IArchiveWriter* writer, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 345 | android::IDiagnostics* diag) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 346 | TRACE_CALL(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 347 | // Start the entry so we can write the header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 348 | if (!writer->StartEntry(output_path, 0)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 349 | diag->Error(android::DiagMessage(output_path) << "failed to open file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 353 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
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 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 356 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 357 | ContainerWriter container_writer(©ing_adaptor, 1u); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 359 | pb::internal::CompiledFile pb_compiled_file; |
| 360 | SerializeCompiledFileToPb(file, &pb_compiled_file); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 361 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 362 | if (!container_writer.AddResFileEntry(pb_compiled_file, in)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 363 | diag->Error(android::DiagMessage(output_path) << "failed to write entry data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 364 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 365 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 366 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 367 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 368 | if (!writer->FinishEntry()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 369 | diag->Error(android::DiagMessage(output_path) << "failed to finish writing data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 375 | static bool FlattenXmlToOutStream(StringPiece output_path, const xml::XmlResource& xmlres, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 376 | ContainerWriter* container_writer, android::IDiagnostics* diag) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 377 | pb::internal::CompiledFile pb_compiled_file; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 378 | SerializeCompiledFileToPb(xmlres.file, &pb_compiled_file); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 379 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 380 | pb::XmlNode pb_xml_node; |
| 381 | SerializeXmlToPb(*xmlres.root, &pb_xml_node); |
| 382 | |
| 383 | std::string serialized_xml = pb_xml_node.SerializeAsString(); |
| 384 | io::StringInputStream serialized_in(serialized_xml); |
| 385 | |
| 386 | if (!container_writer->AddResFileEntry(pb_compiled_file, &serialized_in)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 387 | diag->Error(android::DiagMessage(output_path) << "failed to write entry data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | return true; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 393 | static bool IsValidFile(IAaptContext* context, const std::string& input_path) { |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 394 | const file::FileType file_type = file::GetFileType(input_path); |
| 395 | if (file_type != file::FileType::kRegular && file_type != file::FileType::kSymlink) { |
| 396 | if (file_type == file::FileType::kDirectory) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 397 | context->GetDiagnostics()->Error(android::DiagMessage(input_path) |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 398 | << "resource file cannot be a directory"); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 399 | } else if (file_type == file::FileType::kNonExistant) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 400 | context->GetDiagnostics()->Error(android::DiagMessage(input_path) << "file not found"); |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 401 | } else { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 402 | context->GetDiagnostics()->Error(android::DiagMessage(input_path) |
Adam Lesinski | 776aa95 | 2017-04-24 15:09:32 -0700 | [diff] [blame] | 403 | << "not a valid resource file"); |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | return true; |
| 408 | } |
| 409 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 410 | static bool CompileXml(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 411 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 412 | const std::string& output_path) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 413 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 414 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 415 | context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling XML"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 418 | std::unique_ptr<xml::XmlResource> xmlres; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 419 | { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 420 | auto fin = file->OpenInputStream(); |
| 421 | if (fin->HadError()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 422 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 423 | << "failed to open file: " << fin->GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 424 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 425 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 426 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 427 | xmlres = xml::Inflate(fin.get(), context->GetDiagnostics(), path_data.source); |
| 428 | if (!xmlres) { |
| 429 | return false; |
| 430 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 433 | xmlres->file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 434 | xmlres->file.config = path_data.config; |
| 435 | xmlres->file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 436 | xmlres->file.type = ResourceFile::Type::kProtoXml; |
Jeremy Meyer | e08e037 | 2024-09-17 12:45:26 -0700 | [diff] [blame^] | 437 | xmlres->file.flag = ParseFlag(path_data.flag_name); |
| 438 | |
| 439 | if (xmlres->file.flag) { |
| 440 | std::string error; |
| 441 | auto flag_status = GetFlagStatus(xmlres->file.flag, options.feature_flag_values, &error); |
| 442 | if (flag_status) { |
| 443 | xmlres->file.flag_status = flag_status.value(); |
| 444 | } else { |
| 445 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error); |
| 446 | return false; |
| 447 | } |
| 448 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 449 | |
| 450 | // Collect IDs that are defined here. |
| 451 | XmlIdCollector collector; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 452 | if (!collector.Consume(context, xmlres.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 453 | return false; |
| 454 | } |
| 455 | |
| 456 | // Look for and process any <aapt:attr> tags and create sub-documents. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 457 | InlineXmlFormatParser inline_xml_format_parser; |
| 458 | if (!inline_xml_format_parser.Consume(context, xmlres.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 459 | return false; |
| 460 | } |
| 461 | |
| 462 | // Start the entry so we can write the header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 463 | if (!writer->StartEntry(output_path, 0)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 464 | context->GetDiagnostics()->Error(android::DiagMessage(output_path) << "failed to open file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 465 | return false; |
| 466 | } |
| 467 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 468 | std::vector<std::unique_ptr<xml::XmlResource>>& inline_documents = |
| 469 | inline_xml_format_parser.GetExtractedInlineXmlDocuments(); |
| 470 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 471 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry(). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 472 | { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 473 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 474 | CopyingOutputStreamAdaptor copying_adaptor(writer); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 475 | ContainerWriter container_writer(©ing_adaptor, 1u + inline_documents.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 476 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 477 | if (!FlattenXmlToOutStream(output_path, *xmlres, &container_writer, |
| 478 | context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 479 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 482 | for (const std::unique_ptr<xml::XmlResource>& inline_xml_doc : inline_documents) { |
| 483 | if (!FlattenXmlToOutStream(output_path, *inline_xml_doc, &container_writer, |
| 484 | context->GetDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 485 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 486 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 487 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 488 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 489 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 490 | if (!writer->FinishEntry()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 491 | context->GetDiagnostics()->Error(android::DiagMessage(output_path) |
| 492 | << "failed to finish writing data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 493 | return false; |
| 494 | } |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 495 | |
| 496 | if (options.generate_text_symbols_path) { |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 497 | android::FileOutputStream fout_text(options.generate_text_symbols_path.value()); |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 498 | |
| 499 | if (fout_text.HadError()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 500 | context->GetDiagnostics()->Error(android::DiagMessage() |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 501 | << "failed writing to'" |
| 502 | << options.generate_text_symbols_path.value() |
| 503 | << "': " << fout_text.GetError()); |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | Printer r_txt_printer(&fout_text); |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 508 | for (const auto& res : xmlres->file.exported_symbols) { |
Izabela Orlowska | c81d9f3 | 2017-12-05 12:07:28 +0000 | [diff] [blame] | 509 | r_txt_printer.Print("default int id "); |
| 510 | r_txt_printer.Println(res.name.entry); |
| 511 | } |
| 512 | |
| 513 | // And print ourselves. |
| 514 | r_txt_printer.Print("default int "); |
| 515 | r_txt_printer.Print(path_data.resource_dir); |
| 516 | r_txt_printer.Print(" "); |
| 517 | r_txt_printer.Println(path_data.name); |
| 518 | } |
| 519 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 520 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 523 | static bool CompilePng(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 524 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 525 | const std::string& output_path) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 526 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 527 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 528 | context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling PNG"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 531 | android::BigBuffer buffer(4096); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 532 | ResourceFile res_file; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 533 | res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 534 | res_file.config = path_data.config; |
| 535 | res_file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 536 | res_file.type = ResourceFile::Type::kPng; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 537 | |
Jeremy Meyer | e08e037 | 2024-09-17 12:45:26 -0700 | [diff] [blame^] | 538 | if (!path_data.flag_name.empty()) { |
| 539 | FeatureFlagAttribute flag; |
| 540 | auto name = path_data.flag_name; |
| 541 | if (name.starts_with('!')) { |
| 542 | flag.negated = true; |
| 543 | flag.name = name.substr(1); |
| 544 | } else { |
| 545 | flag.name = name; |
| 546 | } |
| 547 | res_file.flag = flag; |
| 548 | |
| 549 | std::string error; |
| 550 | auto flag_status = GetFlagStatus(flag, options.feature_flag_values, &error); |
| 551 | if (flag_status) { |
| 552 | res_file.flag_status = flag_status.value(); |
| 553 | } else { |
| 554 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) << error); |
| 555 | return false; |
| 556 | } |
| 557 | } |
| 558 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 559 | { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 560 | auto data = file->OpenAsData(); |
| 561 | if (!data) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 562 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) |
| 563 | << "failed to open file "); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 564 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 567 | android::BigBuffer crunched_png_buffer(4096); |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 568 | android::BigBufferOutputStream crunched_png_buffer_out(&crunched_png_buffer); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 569 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 570 | // Ensure that we only keep the chunks we care about if we end up |
| 571 | // using the original PNG instead of the crunched one. |
Ryan Mitchell | f67808b | 2019-01-22 16:16:13 -0800 | [diff] [blame] | 572 | const StringPiece content(reinterpret_cast<const char*>(data->data()), data->size()); |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 573 | android::PngChunkFilter png_chunk_filter(content); |
| 574 | android::SourcePathDiagnostics source_diag(path_data.source, context->GetDiagnostics()); |
| 575 | auto image = android::ReadPng(&png_chunk_filter, &source_diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 576 | if (!image) { |
| 577 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 580 | std::unique_ptr<android::NinePatch> nine_patch; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 581 | if (path_data.extension == "9.png") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 582 | std::string err; |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 583 | nine_patch = android::NinePatch::Create(image->rows.get(), image->width, image->height, &err); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 584 | if (!nine_patch) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 585 | context->GetDiagnostics()->Error(android::DiagMessage() << err); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 586 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Remove the 1px border around the NinePatch. |
| 590 | // Basically the row array is shifted up by 1, and the length is treated |
| 591 | // as height - 2. |
| 592 | // For each row, shift the array to the left by 1, and treat the length as |
| 593 | // width - 2. |
| 594 | image->width -= 2; |
| 595 | image->height -= 2; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 596 | 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] | 597 | for (int32_t h = 0; h < image->height; h++) { |
| 598 | memmove(image->rows[h], image->rows[h] + 4, image->width * 4); |
| 599 | } |
| 600 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 601 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 602 | context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) |
| 603 | << "9-patch: " << *nine_patch); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 604 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 605 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 606 | |
| 607 | // Write the crunched PNG. |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 608 | if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out, {}, |
| 609 | &source_diag, context->IsVerbose())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 610 | return false; |
| 611 | } |
| 612 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 613 | if (nine_patch != nullptr || |
| 614 | crunched_png_buffer_out.ByteCount() <= png_chunk_filter.ByteCount()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 615 | // No matter what, we must use the re-encoded PNG, even if it is larger. |
| 616 | // 9-patch images must be re-encoded since their borders are stripped. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 617 | buffer.AppendBuffer(std::move(crunched_png_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 618 | } else { |
| 619 | // The re-encoded PNG is larger than the original, and there is |
| 620 | // no mandatory transformation. Use the original. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 621 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 622 | context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 623 | << "original PNG is smaller than crunched PNG" |
| 624 | << ", using original"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 627 | png_chunk_filter.Rewind(); |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 628 | android::BigBuffer filtered_png_buffer(4096); |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 629 | android::BigBufferOutputStream filtered_png_buffer_out(&filtered_png_buffer); |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 630 | io::Copy(&filtered_png_buffer_out, &png_chunk_filter); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 631 | buffer.AppendBuffer(std::move(filtered_png_buffer)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 634 | if (context->IsVerbose()) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 635 | // For debugging only, use the legacy PNG cruncher and compare the resulting file sizes. |
| 636 | // This will help catch exotic cases where the new code may generate larger PNGs. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 637 | std::stringstream legacy_stream{std::string(content)}; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 638 | android::BigBuffer legacy_buffer(4096); |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 639 | android::Png png(context->GetDiagnostics()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 640 | if (!png.process(path_data.source, &legacy_stream, &legacy_buffer, {})) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 641 | return false; |
| 642 | } |
| 643 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 644 | context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 645 | << "legacy=" << legacy_buffer.size() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 646 | << " new=" << buffer.size()); |
| 647 | } |
| 648 | } |
| 649 | |
Jeremy Meyer | b4f83ff | 2023-11-30 19:29:50 +0000 | [diff] [blame] | 650 | android::BigBufferInputStream buffer_in(&buffer); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 651 | return WriteHeaderAndDataToWriter(output_path, res_file, &buffer_in, writer, |
| 652 | context->GetDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 655 | static bool CompileFile(IAaptContext* context, const CompileOptions& options, |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 656 | const ResourcePathData& path_data, io::IFile* file, IArchiveWriter* writer, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 657 | const std::string& output_path) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 658 | TRACE_CALL(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 659 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 660 | context->GetDiagnostics()->Note(android::DiagMessage(path_data.source) << "compiling file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 661 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 662 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 663 | ResourceFile res_file; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 664 | res_file.name = ResourceName({}, *ParseResourceType(path_data.resource_dir), path_data.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 665 | res_file.config = path_data.config; |
| 666 | res_file.source = path_data.source; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 667 | res_file.type = ResourceFile::Type::kUnknown; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 668 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 669 | auto data = file->OpenAsData(); |
| 670 | if (!data) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 671 | context->GetDiagnostics()->Error(android::DiagMessage(path_data.source) |
| 672 | << "failed to open file "); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 673 | return false; |
| 674 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 675 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 676 | return WriteHeaderAndDataToWriter(output_path, res_file, data.get(), writer, |
| 677 | context->GetDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | class CompileContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 681 | public: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 682 | explicit CompileContext(android::IDiagnostics* diagnostics) : diagnostics_(diagnostics) { |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 685 | PackageType GetPackageType() override { |
| 686 | // Every compilation unit starts as an app and then gets linked as potentially something else. |
| 687 | return PackageType::kApp; |
| 688 | } |
| 689 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 690 | void SetVerbose(bool val) { |
| 691 | verbose_ = val; |
Brandon Liu | 48d229d | 2023-05-04 23:54:03 +0000 | [diff] [blame] | 692 | diagnostics_->SetVerbose(val); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 693 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 694 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 695 | bool IsVerbose() override { |
| 696 | return verbose_; |
| 697 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 698 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 699 | android::IDiagnostics* GetDiagnostics() override { |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 700 | return diagnostics_; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 701 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 702 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 703 | NameMangler* GetNameMangler() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 704 | UNIMPLEMENTED(FATAL) << "No name mangling should be needed in compile phase"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 705 | return nullptr; |
| 706 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 707 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 708 | const std::string& GetCompilationPackage() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 709 | static std::string empty; |
| 710 | return empty; |
| 711 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 712 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 713 | uint8_t GetPackageId() override { |
| 714 | return 0x0; |
| 715 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 716 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 717 | SymbolTable* GetExternalSymbols() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 718 | UNIMPLEMENTED(FATAL) << "No symbols should be needed in compile phase"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 719 | return nullptr; |
| 720 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 721 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 722 | int GetMinSdkVersion() override { |
| 723 | return 0; |
| 724 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 725 | |
Udam Saini | b228df3 | 2019-06-18 16:50:34 -0700 | [diff] [blame] | 726 | const std::set<std::string>& GetSplitNameDependencies() override { |
| 727 | UNIMPLEMENTED(FATAL) << "No Split Name Dependencies be needed in compile phase"; |
| 728 | static std::set<std::string> empty; |
| 729 | return empty; |
| 730 | } |
| 731 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 732 | private: |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 733 | DISALLOW_COPY_AND_ASSIGN(CompileContext); |
| 734 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 735 | android::IDiagnostics* diagnostics_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 736 | bool verbose_ = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 737 | }; |
| 738 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 739 | int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer, |
| 740 | CompileOptions& options) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 741 | TRACE_CALL(); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 742 | bool error = false; |
| 743 | |
| 744 | // Iterate over the input files in a stable, platform-independent manner |
| 745 | auto file_iterator = inputs->Iterator(); |
| 746 | while (file_iterator->HasNext()) { |
| 747 | auto file = file_iterator->Next(); |
| 748 | std::string path = file->GetSource().path; |
| 749 | |
| 750 | // Skip hidden input files |
| 751 | if (file::IsHidden(path)) { |
| 752 | continue; |
| 753 | } |
| 754 | |
| 755 | if (!options.res_zip && !IsValidFile(context, path)) { |
| 756 | error = true; |
| 757 | continue; |
| 758 | } |
| 759 | |
| 760 | // Extract resource type information from the full path |
| 761 | std::string err_str; |
| 762 | ResourcePathData path_data; |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 763 | if (auto maybe_path_data = ExtractResourcePathData( |
| 764 | path, inputs->GetDirSeparator(), &err_str, options)) { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 765 | path_data = maybe_path_data.value(); |
| 766 | } else { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 767 | context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) << err_str); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 768 | error = true; |
| 769 | continue; |
| 770 | } |
| 771 | |
| 772 | // Determine how to compile the file based on its type. |
| 773 | auto compile_func = &CompileFile; |
| 774 | if (path_data.resource_dir == "values" && path_data.extension == "xml") { |
| 775 | compile_func = &CompileTable; |
| 776 | // We use a different extension (not necessary anymore, but avoids altering the existing |
| 777 | // build system logic). |
| 778 | path_data.extension = "arsc"; |
| 779 | |
| 780 | } else if (const ResourceType* type = ParseResourceType(path_data.resource_dir)) { |
| 781 | if (*type != ResourceType::kRaw) { |
Ryan Mitchell | 62b6834 | 2019-03-11 13:28:02 -0700 | [diff] [blame] | 782 | if (*type == ResourceType::kXml || path_data.extension == "xml") { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 783 | compile_func = &CompileXml; |
| 784 | } else if ((!options.no_png_crunch && path_data.extension == "png") |
| 785 | || path_data.extension == "9.png") { |
| 786 | compile_func = &CompilePng; |
| 787 | } |
| 788 | } |
| 789 | } else { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 790 | context->GetDiagnostics()->Error(android::DiagMessage() |
| 791 | << "invalid file path '" << path_data.source << "'"); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 792 | error = true; |
| 793 | continue; |
| 794 | } |
| 795 | |
| 796 | // Treat periods as a reserved character that should not be present in a file name |
| 797 | // Legacy support for AAPT which did not reserve periods |
| 798 | if (compile_func != &CompileFile && !options.legacy_mode |
| 799 | && std::count(path_data.name.begin(), path_data.name.end(), '.') != 0) { |
| 800 | error = true; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 801 | context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) |
| 802 | << "file name cannot contain '.' other than for" |
| 803 | << " specifying the extension"); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 804 | continue; |
| 805 | } |
| 806 | |
| 807 | const std::string out_path = BuildIntermediateContainerFilename(path_data); |
Izabela Orlowska | a3ab21f | 2019-01-17 12:09:59 +0000 | [diff] [blame] | 808 | if (!compile_func(context, options, path_data, file, output_writer, out_path)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 809 | context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) |
| 810 | << "file failed to compile"); |
Izabela Orlowska | a3ab21f | 2019-01-17 12:09:59 +0000 | [diff] [blame] | 811 | error = true; |
| 812 | } |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | return error ? 1 : 0; |
| 816 | } |
| 817 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 818 | int CompileCommand::Action(const std::vector<std::string>& args) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 819 | TRACE_FLUSH(trace_folder_? trace_folder_.value() : "", "CompileCommand::Action"); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 820 | CompileContext context(diagnostic_); |
| 821 | context.SetVerbose(options_.verbose); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 822 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 823 | if (visibility_) { |
| 824 | if (visibility_.value() == "public") { |
| 825 | options_.visibility = Visibility::Level::kPublic; |
| 826 | } else if (visibility_.value() == "private") { |
| 827 | options_.visibility = Visibility::Level::kPrivate; |
| 828 | } else if (visibility_.value() == "default") { |
| 829 | options_.visibility = Visibility::Level::kUndefined; |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 830 | } else { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 831 | context.GetDiagnostics()->Error(android::DiagMessage() |
| 832 | << "Unrecognized visibility level passes to --visibility: '" |
| 833 | << visibility_.value() |
| 834 | << "'. Accepted levels: public, private, default"); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 835 | return 1; |
| 836 | } |
| 837 | } |
| 838 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 839 | std::unique_ptr<io::IFileCollection> file_collection; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 840 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 841 | // Collect the resources files to compile |
| 842 | if (options_.res_dir && options_.res_zip) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 843 | context.GetDiagnostics()->Error(android::DiagMessage() |
| 844 | << "only one of --dir and --zip can be specified"); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 845 | return 1; |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 846 | } else if ((options_.res_dir || options_.res_zip) && |
| 847 | options_.source_path && args.size() > 1) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 848 | context.GetDiagnostics()->Error(android::DiagMessage(kPath) |
| 849 | << "Cannot use an overriding source path with multiple files."); |
| 850 | return 1; |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 851 | } else if (options_.res_dir) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 852 | if (!args.empty()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 853 | context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --dir specified"); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 854 | Usage(&std::cerr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 855 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 858 | // Load the files from the res directory |
| 859 | std::string err; |
| 860 | file_collection = io::FileCollection::Create(options_.res_dir.value(), &err); |
| 861 | if (!file_collection) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 862 | context.GetDiagnostics()->Error(android::DiagMessage(options_.res_dir.value()) << err); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 863 | return 1; |
| 864 | } |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 865 | } else if (options_.res_zip) { |
| 866 | if (!args.empty()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 867 | context.GetDiagnostics()->Error(android::DiagMessage() << "files given but --zip specified"); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 868 | Usage(&std::cerr); |
| 869 | return 1; |
| 870 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 871 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 872 | // Load a zip file containing a res directory |
| 873 | std::string err; |
| 874 | file_collection = io::ZipFileCollection::Create(options_.res_zip.value(), &err); |
| 875 | if (!file_collection) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 876 | context.GetDiagnostics()->Error(android::DiagMessage(options_.res_zip.value()) << err); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 877 | return 1; |
| 878 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 879 | } else { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 880 | auto collection = util::make_unique<io::FileCollection>(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 881 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 882 | // Collect data from the path for each input file. |
Ryan Mitchell | f22ed8d | 2019-02-20 08:05:31 -0800 | [diff] [blame] | 883 | std::vector<std::string> sorted_args = args; |
| 884 | std::sort(sorted_args.begin(), sorted_args.end()); |
| 885 | |
| 886 | for (const std::string& arg : sorted_args) { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 887 | collection->InsertFile(arg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 888 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 889 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 890 | file_collection = std::move(collection); |
Ryan Mitchell | c7db244 | 2019-08-28 11:21:47 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | std::unique_ptr<IArchiveWriter> archive_writer; |
| 894 | file::FileType output_file_type = file::GetFileType(options_.output_path); |
| 895 | if (output_file_type == file::FileType::kDirectory) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 896 | archive_writer = CreateDirectoryArchiveWriter(context.GetDiagnostics(), options_.output_path); |
Ryan Mitchell | c7db244 | 2019-08-28 11:21:47 -0700 | [diff] [blame] | 897 | } else { |
| 898 | archive_writer = CreateZipFileArchiveWriter(context.GetDiagnostics(), options_.output_path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 901 | if (!archive_writer) { |
Adam Lesinski | dfaecaf | 2016-10-20 17:08:51 -0700 | [diff] [blame] | 902 | return 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Jeremy Meyer | b5c0ef0 | 2024-06-12 10:42:55 -0700 | [diff] [blame] | 905 | // Parse the feature flag values. An argument that starts with '@' points to a file to read flag |
| 906 | // values from. |
| 907 | std::vector<std::string> all_feature_flags_args; |
| 908 | for (const std::string& arg : feature_flags_args_) { |
| 909 | if (util::StartsWith(arg, "@")) { |
| 910 | const std::string path = arg.substr(1, arg.size() - 1); |
| 911 | std::string error; |
| 912 | if (!file::AppendArgsFromFile(path, &all_feature_flags_args, &error)) { |
| 913 | context.GetDiagnostics()->Error(android::DiagMessage(path) << error); |
| 914 | return 1; |
| 915 | } |
| 916 | } else { |
| 917 | all_feature_flags_args.push_back(arg); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | for (const std::string& arg : all_feature_flags_args) { |
| 922 | if (!ParseFeatureFlagsParameter(arg, context.GetDiagnostics(), &options_.feature_flag_values)) { |
| 923 | return 1; |
| 924 | } |
| 925 | } |
| 926 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 927 | return Compile(&context, file_collection.get(), archive_writer.get(), options_); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 930 | } // namespace aapt |