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