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