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