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