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