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 <sys/stat.h> |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <queue> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "android-base/errors.h" |
| 25 | #include "android-base/file.h" |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 26 | #include "android-base/stringprintf.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | #include "google/protobuf/io/coded_stream.h" |
| 29 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "AppInfo.h" |
| 31 | #include "Debug.h" |
| 32 | #include "Flags.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 33 | #include "Locale.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | #include "NameMangler.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 35 | #include "ResourceUtils.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 36 | #include "cmd/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | #include "compile/IdAssigner.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 38 | #include "filter/ConfigFilter.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | #include "flatten/Archive.h" |
| 40 | #include "flatten/TableFlattener.h" |
| 41 | #include "flatten/XmlFlattener.h" |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 42 | #include "io/BigBufferInputStream.h" |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 43 | #include "io/FileSystem.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 44 | #include "io/Util.h" |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 45 | #include "io/ZipArchive.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 46 | #include "java/JavaClassGenerator.h" |
| 47 | #include "java/ManifestClassGenerator.h" |
| 48 | #include "java/ProguardRules.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | #include "link/Linkers.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | #include "link/ManifestFixer.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 51 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | #include "link/TableMerger.h" |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 53 | #include "link/XmlCompatVersioner.h" |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 54 | #include "optimize/ResourceDeduper.h" |
| 55 | #include "optimize/VersionCollapser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | #include "process/IResourceTableConsumer.h" |
| 57 | #include "process/SymbolTable.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 58 | #include "proto/ProtoSerialize.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 59 | #include "split/TableSplitter.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | #include "unflatten/BinaryResourceParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | #include "util/Files.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 62 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 64 | using android::StringPiece; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 65 | using android::base::StringPrintf; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | namespace aapt { |
| 68 | |
| 69 | struct LinkOptions { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | std::string output_path; |
| 71 | std::string manifest_path; |
| 72 | std::vector<std::string> include_paths; |
| 73 | std::vector<std::string> overlay_files; |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 74 | std::vector<std::string> assets_dirs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | bool output_to_directory = false; |
| 76 | bool auto_add_overlay = false; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | // Java/Proguard options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | Maybe<std::string> generate_java_class_path; |
| 80 | Maybe<std::string> custom_java_package; |
| 81 | std::set<std::string> extra_java_packages; |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 82 | Maybe<std::string> generate_text_symbols_path; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | Maybe<std::string> generate_proguard_rules_path; |
| 84 | Maybe<std::string> generate_main_dex_proguard_rules_path; |
| 85 | bool generate_non_final_ids = false; |
| 86 | std::vector<std::string> javadoc_annotations; |
| 87 | Maybe<std::string> private_symbols; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 88 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | // Optimizations/features. |
| 90 | bool no_auto_version = false; |
| 91 | bool no_version_vectors = false; |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 92 | bool no_version_transitions = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | bool no_resource_deduping = false; |
| 94 | bool no_xml_namespaces = false; |
| 95 | bool do_not_compress_anything = false; |
| 96 | std::unordered_set<std::string> extensions_to_not_compress; |
| 97 | |
| 98 | // Static lib options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | bool no_static_lib_packages = false; |
| 100 | |
| 101 | // AndroidManifest.xml massaging options. |
| 102 | ManifestFixerOptions manifest_fixer_options; |
| 103 | |
| 104 | // Products to use/filter on. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | std::unordered_set<std::string> products; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 107 | // Flattening options. |
| 108 | TableFlattenerOptions table_flattener_options; |
| 109 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | // Split APK options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | TableSplitterOptions table_splitter_options; |
| 112 | std::vector<SplitConstraints> split_constraints; |
| 113 | std::vector<std::string> split_paths; |
Adam Lesinski | 36c73a5 | 2016-08-11 13:39:24 -0700 | [diff] [blame] | 114 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | // Stable ID options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | std::unordered_map<ResourceName, ResourceId> stable_id_map; |
| 117 | Maybe<std::string> resource_id_map_path; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 120 | class LinkContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | public: |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 122 | LinkContext(IDiagnostics* diagnostics) |
| 123 | : diagnostics_(diagnostics), name_mangler_({}), symbols_(&name_mangler_) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 124 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 126 | PackageType GetPackageType() override { |
| 127 | return package_type_; |
| 128 | } |
| 129 | |
| 130 | void SetPackageType(PackageType type) { |
| 131 | package_type_ = type; |
| 132 | } |
| 133 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 134 | IDiagnostics* GetDiagnostics() override { |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 135 | return diagnostics_; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 136 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 138 | NameMangler* GetNameMangler() override { |
| 139 | return &name_mangler_; |
| 140 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 141 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | void SetNameManglerPolicy(const NameManglerPolicy& policy) { |
| 143 | name_mangler_ = NameMangler(policy); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 145 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | const std::string& GetCompilationPackage() override { |
| 147 | return compilation_package_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | void SetCompilationPackage(const StringPiece& package_name) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 151 | compilation_package_ = package_name.to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 153 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 154 | uint8_t GetPackageId() override { |
| 155 | return package_id_; |
| 156 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 158 | void SetPackageId(uint8_t id) { |
| 159 | package_id_ = id; |
| 160 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 161 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 162 | SymbolTable* GetExternalSymbols() override { |
| 163 | return &symbols_; |
| 164 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 165 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 166 | bool IsVerbose() override { |
| 167 | return verbose_; |
| 168 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 169 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 170 | void SetVerbose(bool val) { |
| 171 | verbose_ = val; |
| 172 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 173 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 174 | int GetMinSdkVersion() override { |
| 175 | return min_sdk_version_; |
| 176 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 178 | void SetMinSdkVersion(int minSdk) { |
| 179 | min_sdk_version_ = minSdk; |
| 180 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 181 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 183 | DISALLOW_COPY_AND_ASSIGN(LinkContext); |
| 184 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 185 | PackageType package_type_ = PackageType::kApp; |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 186 | IDiagnostics* diagnostics_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | NameMangler name_mangler_; |
| 188 | std::string compilation_package_; |
| 189 | uint8_t package_id_ = 0x0; |
| 190 | SymbolTable symbols_; |
| 191 | bool verbose_ = false; |
| 192 | int min_sdk_version_ = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 195 | // A custom delegate that generates compatible pre-O IDs for use with feature splits. |
| 196 | // Feature splits use package IDs > 7f, which in Java (since Java doesn't have unsigned ints) |
| 197 | // is interpreted as a negative number. Some verification was wrongly assuming negative values |
| 198 | // were invalid. |
| 199 | // |
| 200 | // This delegate will attempt to masquerade any '@id/' references with ID 0xPPTTEEEE, |
| 201 | // where PP > 7f, as 0x7fPPEEEE. Any potential overlapping is verified and an error occurs if such |
| 202 | // an overlap exists. |
| 203 | class FeatureSplitSymbolTableDelegate : public DefaultSymbolTableDelegate { |
| 204 | public: |
| 205 | FeatureSplitSymbolTableDelegate(IAaptContext* context) : context_(context) { |
| 206 | } |
| 207 | |
| 208 | virtual ~FeatureSplitSymbolTableDelegate() = default; |
| 209 | |
| 210 | virtual std::unique_ptr<SymbolTable::Symbol> FindByName( |
| 211 | const ResourceName& name, |
| 212 | const std::vector<std::unique_ptr<ISymbolSource>>& sources) override { |
| 213 | std::unique_ptr<SymbolTable::Symbol> symbol = |
| 214 | DefaultSymbolTableDelegate::FindByName(name, sources); |
| 215 | if (symbol == nullptr) { |
| 216 | return {}; |
| 217 | } |
| 218 | |
| 219 | // Check to see if this is an 'id' with the target package. |
| 220 | if (name.type == ResourceType::kId && symbol->id) { |
| 221 | ResourceId* id = &symbol->id.value(); |
| 222 | if (id->package_id() > kAppPackageId) { |
| 223 | // Rewrite the resource ID to be compatible pre-O. |
| 224 | ResourceId rewritten_id(kAppPackageId, id->package_id(), id->entry_id()); |
| 225 | |
| 226 | // Check that this doesn't overlap another resource. |
| 227 | if (DefaultSymbolTableDelegate::FindById(rewritten_id, sources) != nullptr) { |
| 228 | // The ID overlaps, so log a message (since this is a weird failure) and fail. |
| 229 | context_->GetDiagnostics()->Error(DiagMessage() << "Failed to rewrite " << name |
| 230 | << " for pre-O feature split support"); |
| 231 | return {}; |
| 232 | } |
| 233 | |
| 234 | if (context_->IsVerbose()) { |
| 235 | context_->GetDiagnostics()->Note(DiagMessage() << "rewriting " << name << " (" << *id |
| 236 | << ") -> (" << rewritten_id << ")"); |
| 237 | } |
| 238 | |
| 239 | *id = rewritten_id; |
| 240 | } |
| 241 | } |
| 242 | return symbol; |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | DISALLOW_COPY_AND_ASSIGN(FeatureSplitSymbolTableDelegate); |
| 247 | |
| 248 | IAaptContext* context_; |
| 249 | }; |
| 250 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 251 | static bool FlattenXml(IAaptContext* context, xml::XmlResource* xml_res, const StringPiece& path, |
| 252 | bool keep_raw_values, IArchiveWriter* writer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | BigBuffer buffer(1024); |
| 254 | XmlFlattenerOptions options = {}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | options.keep_raw_values = keep_raw_values; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | XmlFlattener flattener(&buffer, options); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | if (!flattener.Consume(context, xml_res)) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 258 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 261 | if (context->IsVerbose()) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 262 | context->GetDiagnostics()->Note(DiagMessage(path) << "writing to archive (keep_raw_values=" |
| 263 | << (keep_raw_values ? "true" : "false") |
| 264 | << ")"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 267 | io::BigBufferInputStream input_stream(&buffer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 268 | return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(), |
| 269 | ArchiveEntry::kCompress, writer); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 272 | static std::unique_ptr<ResourceTable> LoadTableFromPb(const Source& source, const void* data, |
| 273 | size_t len, IDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 274 | pb::ResourceTable pb_table; |
| 275 | if (!pb_table.ParseFromArray(data, len)) { |
| 276 | diag->Error(DiagMessage(source) << "invalid compiled table"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 277 | return {}; |
| 278 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 279 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 280 | std::unique_ptr<ResourceTable> table = DeserializeTableFromPb(pb_table, source, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | if (!table) { |
| 282 | return {}; |
| 283 | } |
| 284 | return table; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Inflates an XML file from the source path. |
| 289 | */ |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 290 | static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 291 | std::ifstream fin(path, std::ifstream::binary); |
| 292 | if (!fin) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 293 | diag->Error(DiagMessage(path) << strerror(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 294 | return {}; |
| 295 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | return xml::Inflate(&fin, diag, Source(path)); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 299 | struct ResourceFileFlattenerOptions { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 300 | bool no_auto_version = false; |
| 301 | bool no_version_vectors = false; |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 302 | bool no_version_transitions = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 303 | bool no_xml_namespaces = false; |
| 304 | bool keep_raw_values = false; |
| 305 | bool do_not_compress_anything = false; |
| 306 | bool update_proguard_spec = false; |
| 307 | std::unordered_set<std::string> extensions_to_not_compress; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 310 | // A sampling of public framework resource IDs. |
| 311 | struct R { |
| 312 | struct attr { |
| 313 | enum : uint32_t { |
| 314 | paddingLeft = 0x010100d6u, |
| 315 | paddingRight = 0x010100d8u, |
| 316 | paddingHorizontal = 0x0101053du, |
| 317 | |
| 318 | paddingTop = 0x010100d7u, |
| 319 | paddingBottom = 0x010100d9u, |
| 320 | paddingVertical = 0x0101053eu, |
| 321 | |
| 322 | layout_marginLeft = 0x010100f7u, |
| 323 | layout_marginRight = 0x010100f9u, |
| 324 | layout_marginHorizontal = 0x0101053bu, |
| 325 | |
| 326 | layout_marginTop = 0x010100f8u, |
| 327 | layout_marginBottom = 0x010100fau, |
| 328 | layout_marginVertical = 0x0101053cu, |
| 329 | }; |
| 330 | }; |
| 331 | }; |
| 332 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 333 | class ResourceFileFlattener { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 334 | public: |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 335 | ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 336 | proguard::KeepSet* keep_set); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 337 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 338 | bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 339 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 340 | private: |
| 341 | struct FileOperation { |
| 342 | ConfigDescription config; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 343 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 344 | // The entry this file came from. |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 345 | ResourceEntry* entry; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 346 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 347 | // The file to copy as-is. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 348 | io::IFile* file_to_copy; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 349 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 350 | // The XML to process and flatten. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 351 | std::unique_ptr<xml::XmlResource> xml_to_flatten; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 352 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | // The destination to write this file to. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 354 | std::string dst_path; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 355 | }; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 356 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 357 | uint32_t GetCompressionFlags(const StringPiece& str); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 358 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 359 | std::vector<std::unique_ptr<xml::XmlResource>> LinkAndVersionXmlFile(ResourceTable* table, |
| 360 | FileOperation* file_op); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 361 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 362 | ResourceFileFlattenerOptions options_; |
| 363 | IAaptContext* context_; |
| 364 | proguard::KeepSet* keep_set_; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 365 | XmlCompatVersioner::Rules rules_; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 366 | }; |
| 367 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 368 | ResourceFileFlattener::ResourceFileFlattener(const ResourceFileFlattenerOptions& options, |
| 369 | IAaptContext* context, proguard::KeepSet* keep_set) |
| 370 | : options_(options), context_(context), keep_set_(keep_set) { |
| 371 | SymbolTable* symm = context_->GetExternalSymbols(); |
| 372 | |
| 373 | // Build up the rules for degrading newer attributes to older ones. |
| 374 | // NOTE(adamlesinski): These rules are hardcoded right now, but they should be |
| 375 | // generated from the attribute definitions themselves (b/62028956). |
| 376 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingHorizontal)) { |
| 377 | std::vector<ReplacementAttr> replacements{ |
| 378 | {"paddingLeft", R::attr::paddingLeft, |
| 379 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 380 | {"paddingRight", R::attr::paddingRight, |
| 381 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 382 | }; |
| 383 | rules_[R::attr::paddingHorizontal] = |
| 384 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 385 | } |
| 386 | |
| 387 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingVertical)) { |
| 388 | std::vector<ReplacementAttr> replacements{ |
| 389 | {"paddingTop", R::attr::paddingTop, |
| 390 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 391 | {"paddingBottom", R::attr::paddingBottom, |
| 392 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 393 | }; |
| 394 | rules_[R::attr::paddingVertical] = |
| 395 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 396 | } |
| 397 | |
| 398 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginHorizontal)) { |
| 399 | std::vector<ReplacementAttr> replacements{ |
| 400 | {"layout_marginLeft", R::attr::layout_marginLeft, |
| 401 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 402 | {"layout_marginRight", R::attr::layout_marginRight, |
| 403 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 404 | }; |
| 405 | rules_[R::attr::layout_marginHorizontal] = |
| 406 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 407 | } |
| 408 | |
| 409 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginVertical)) { |
| 410 | std::vector<ReplacementAttr> replacements{ |
| 411 | {"layout_marginTop", R::attr::layout_marginTop, |
| 412 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 413 | {"layout_marginBottom", R::attr::layout_marginBottom, |
| 414 | Attribute(false, android::ResTable_map::TYPE_DIMENSION)}, |
| 415 | }; |
| 416 | rules_[R::attr::layout_marginVertical] = |
| 417 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 418 | } |
| 419 | } |
| 420 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 421 | uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) { |
| 422 | if (options_.do_not_compress_anything) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 423 | return 0; |
| 424 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 425 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 426 | for (const std::string& extension : options_.extensions_to_not_compress) { |
| 427 | if (util::EndsWith(str, extension)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 | return 0; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 429 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 430 | } |
| 431 | return ArchiveEntry::kCompress; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 434 | static bool IsTransitionElement(const std::string& name) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 435 | return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" || |
| 436 | name == "changeImageTransform" || name == "changeTransform" || |
| 437 | name == "changeClipBounds" || name == "autoTransition" || name == "recolor" || |
| 438 | name == "changeScroll" || name == "transitionSet" || name == "transition" || |
| 439 | name == "transitionManager"; |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 440 | } |
| 441 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 442 | static bool IsVectorElement(const std::string& name) { |
| 443 | return name == "vector" || name == "animated-vector"; |
| 444 | } |
| 445 | |
| 446 | template <typename T> |
| 447 | std::vector<T> make_singleton_vec(T&& val) { |
| 448 | std::vector<T> vec; |
| 449 | vec.emplace_back(std::forward<T>(val)); |
| 450 | return vec; |
| 451 | } |
| 452 | |
| 453 | std::vector<std::unique_ptr<xml::XmlResource>> ResourceFileFlattener::LinkAndVersionXmlFile( |
| 454 | ResourceTable* table, FileOperation* file_op) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 455 | xml::XmlResource* doc = file_op->xml_to_flatten.get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 456 | const Source& src = doc->file.source; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 457 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 458 | if (context_->IsVerbose()) { |
| 459 | context_->GetDiagnostics()->Note(DiagMessage() << "linking " << src.path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 462 | XmlReferenceLinker xml_linker; |
| 463 | if (!xml_linker.Consume(context_, doc)) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 464 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 467 | if (options_.update_proguard_spec && !proguard::CollectProguardRules(src, doc, keep_set_)) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 468 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 471 | if (options_.no_xml_namespaces) { |
| 472 | XmlNamespaceRemover namespace_remover; |
| 473 | if (!namespace_remover.Consume(context_, doc)) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 474 | return {}; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 475 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 476 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 477 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 478 | if (options_.no_auto_version) { |
| 479 | return make_singleton_vec(std::move(file_op->xml_to_flatten)); |
| 480 | } |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 481 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 482 | if (options_.no_version_vectors || options_.no_version_transitions) { |
| 483 | // Skip this if it is a vector or animated-vector. |
| 484 | xml::Element* el = xml::FindRootElement(doc); |
| 485 | if (el && el->namespace_uri.empty()) { |
| 486 | if ((options_.no_version_vectors && IsVectorElement(el->name)) || |
| 487 | (options_.no_version_transitions && IsTransitionElement(el->name))) { |
| 488 | return make_singleton_vec(std::move(file_op->xml_to_flatten)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 489 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 490 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 491 | } |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 492 | |
| 493 | const ConfigDescription& config = file_op->config; |
| 494 | ResourceEntry* entry = file_op->entry; |
| 495 | |
| 496 | XmlCompatVersioner xml_compat_versioner(&rules_); |
| 497 | const util::Range<ApiVersion> api_range{config.sdkVersion, |
| 498 | FindNextApiVersionForConfig(entry, config)}; |
| 499 | return xml_compat_versioner.Process(context_, doc, api_range); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 502 | bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 503 | bool error = false; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 504 | std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 505 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 506 | for (auto& pkg : table->packages) { |
| 507 | for (auto& type : pkg->types) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 508 | // Sort by config and name, so that we get better locality in the zip file. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 509 | config_sorted_files.clear(); |
| 510 | std::queue<FileOperation> file_operations; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 511 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 512 | // Populate the queue with all files in the ResourceTable. |
| 513 | for (auto& entry : type->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 514 | for (auto& config_value : entry->values) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 515 | // WARNING! Do not insert or remove any resources while executing in this scope. It will |
| 516 | // corrupt the iteration order. |
| 517 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 518 | FileReference* file_ref = ValueCast<FileReference>(config_value->value.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 519 | if (!file_ref) { |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 520 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 521 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 522 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 523 | io::IFile* file = file_ref->file; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 524 | if (!file) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 525 | context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 526 | << "file not found"); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 527 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | FileOperation file_op; |
| 531 | file_op.entry = entry.get(); |
| 532 | file_op.dst_path = *file_ref->path; |
| 533 | file_op.config = config_value->config; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 534 | file_op.file_to_copy = file; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 535 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 536 | const StringPiece src_path = file->GetSource().path; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 537 | if (type->type != ResourceType::kRaw && |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 538 | (util::EndsWith(src_path, ".xml.flat") || util::EndsWith(src_path, ".xml"))) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 539 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 540 | if (!data) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 541 | context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 542 | << "failed to open file"); |
| 543 | return false; |
| 544 | } |
| 545 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 546 | file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(), |
| 547 | context_->GetDiagnostics(), file->GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 548 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 549 | if (!file_op.xml_to_flatten) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 550 | return false; |
| 551 | } |
| 552 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 553 | file_op.xml_to_flatten->file.config = config_value->config; |
| 554 | file_op.xml_to_flatten->file.source = file_ref->GetSource(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 555 | file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 556 | } |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 557 | |
| 558 | // NOTE(adamlesinski): Explicitly construct a StringPiece here, or |
| 559 | // else we end up copying the string in the std::make_pair() method, |
| 560 | // then creating a StringPiece from the copy, which would cause us |
| 561 | // to end up referencing garbage in the map. |
| 562 | const StringPiece entry_name(entry->name); |
| 563 | config_sorted_files[std::make_pair(config_value->config, entry_name)] = |
| 564 | std::move(file_op); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 568 | // Now flatten the sorted values. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 569 | for (auto& map_entry : config_sorted_files) { |
| 570 | const ConfigDescription& config = map_entry.first.first; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 571 | FileOperation& file_op = map_entry.second; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 572 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 573 | if (file_op.xml_to_flatten) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 574 | std::vector<std::unique_ptr<xml::XmlResource>> versioned_docs = |
| 575 | LinkAndVersionXmlFile(table, &file_op); |
| 576 | for (std::unique_ptr<xml::XmlResource>& doc : versioned_docs) { |
| 577 | std::string dst_path = file_op.dst_path; |
| 578 | if (doc->file.config != file_op.config) { |
| 579 | // Only add the new versioned configurations. |
| 580 | if (context_->IsVerbose()) { |
| 581 | context_->GetDiagnostics()->Note(DiagMessage(doc->file.source) |
| 582 | << "auto-versioning resource from config '" |
| 583 | << config << "' -> '" << doc->file.config << "'"); |
| 584 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 585 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 586 | dst_path = |
| 587 | ResourceUtils::BuildResourceFileName(doc->file, context_->GetNameMangler()); |
| 588 | bool result = table->AddFileReferenceAllowMangled(doc->file.name, doc->file.config, |
| 589 | doc->file.source, dst_path, nullptr, |
| 590 | context_->GetDiagnostics()); |
| 591 | if (!result) { |
| 592 | return false; |
| 593 | } |
| 594 | } |
| 595 | error |= !FlattenXml(context_, doc.get(), dst_path, options_.keep_raw_values, |
| 596 | archive_writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 597 | } |
| 598 | } else { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 599 | error |= !io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path, |
| 600 | GetCompressionFlags(file_op.dst_path), archive_writer); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 601 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 602 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 603 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 604 | } |
| 605 | return !error; |
| 606 | } |
| 607 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 608 | static bool WriteStableIdMapToPath(IDiagnostics* diag, |
| 609 | const std::unordered_map<ResourceName, ResourceId>& id_map, |
| 610 | const std::string& id_map_path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 611 | std::ofstream fout(id_map_path, std::ofstream::binary); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 612 | if (!fout) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 613 | diag->Error(DiagMessage(id_map_path) << strerror(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 614 | return false; |
| 615 | } |
| 616 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 617 | for (const auto& entry : id_map) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 618 | const ResourceName& name = entry.first; |
| 619 | const ResourceId& id = entry.second; |
| 620 | fout << name << " = " << id << "\n"; |
| 621 | } |
| 622 | |
| 623 | if (!fout) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 624 | diag->Error(DiagMessage(id_map_path) << "failed writing to file: " |
| 625 | << android::base::SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 626 | return false; |
| 627 | } |
| 628 | |
| 629 | return true; |
| 630 | } |
| 631 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 632 | static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path, |
| 633 | std::unordered_map<ResourceName, ResourceId>* out_id_map) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 634 | std::string content; |
| 635 | if (!android::base::ReadFileToString(path, &content)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 636 | diag->Error(DiagMessage(path) << "failed reading stable ID file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 637 | return false; |
| 638 | } |
| 639 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 640 | out_id_map->clear(); |
| 641 | size_t line_no = 0; |
| 642 | for (StringPiece line : util::Tokenize(content, '\n')) { |
| 643 | line_no++; |
| 644 | line = util::TrimWhitespace(line); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 645 | if (line.empty()) { |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | auto iter = std::find(line.begin(), line.end(), '='); |
| 650 | if (iter == line.end()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 651 | diag->Error(DiagMessage(Source(path, line_no)) << "missing '='"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 652 | return false; |
| 653 | } |
| 654 | |
| 655 | ResourceNameRef name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 656 | StringPiece res_name_str = |
| 657 | util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter))); |
| 658 | if (!ResourceUtils::ParseResourceName(res_name_str, &name)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 659 | diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str |
| 660 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 661 | return false; |
| 662 | } |
| 663 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 664 | const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1; |
| 665 | const size_t res_id_str_len = line.size() - res_id_start_idx; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 666 | StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 667 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 668 | Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str); |
| 669 | if (!maybe_id) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 670 | diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str |
| 671 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 675 | (*out_id_map)[name.ToResourceName()] = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 676 | } |
| 677 | return true; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 680 | class LinkCommand { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 681 | public: |
| 682 | LinkCommand(LinkContext* context, const LinkOptions& options) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 683 | : options_(options), |
| 684 | context_(context), |
| 685 | final_table_(), |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 686 | file_collection_(util::make_unique<io::FileCollection>()) { |
| 687 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 688 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 689 | /** |
| 690 | * Creates a SymbolTable that loads symbols from the various APKs and caches |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 691 | * the results for faster lookup. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 692 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 693 | bool LoadSymbolsFromIncludePaths() { |
| 694 | std::unique_ptr<AssetManagerSymbolSource> asset_source = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 695 | util::make_unique<AssetManagerSymbolSource>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 696 | for (const std::string& path : options_.include_paths) { |
| 697 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 698 | context_->GetDiagnostics()->Note(DiagMessage(path) << "loading include path"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 699 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 700 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 701 | // First try to load the file as a static lib. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 702 | std::string error_str; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 703 | std::unique_ptr<ResourceTable> include_static = LoadStaticLibrary(path, &error_str); |
| 704 | if (include_static) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 705 | if (context_->GetPackageType() != PackageType::kStaticLib) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 706 | // Can't include static libraries when not building a static library (they have no IDs |
| 707 | // assigned). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 708 | context_->GetDiagnostics()->Error( |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 709 | DiagMessage(path) << "can't include static library when not building a static lib"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 710 | return false; |
| 711 | } |
| 712 | |
| 713 | // If we are using --no-static-lib-packages, we need to rename the |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 714 | // package of this table to our compilation package. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 715 | if (options_.no_static_lib_packages) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 716 | // Since package names can differ, and multiple packages can exist in a ResourceTable, |
| 717 | // we place the requirement that all static libraries are built with the package |
| 718 | // ID 0x7f. So if one is not found, this is an error. |
| 719 | if (ResourceTablePackage* pkg = include_static->FindPackageById(kAppPackageId)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 720 | pkg->name = context_->GetCompilationPackage(); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 721 | } else { |
| 722 | context_->GetDiagnostics()->Error(DiagMessage(path) |
| 723 | << "no package with ID 0x7f found in static library"); |
| 724 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 728 | context_->GetExternalSymbols()->AppendSource( |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 729 | util::make_unique<ResourceTableSymbolSource>(include_static.get())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 730 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 731 | static_table_includes_.push_back(std::move(include_static)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 732 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 733 | } else if (!error_str.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 734 | // We had an error with reading, so fail. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 735 | context_->GetDiagnostics()->Error(DiagMessage(path) << error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 739 | if (!asset_source->AddAssetPath(path)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 740 | context_->GetDiagnostics()->Error(DiagMessage(path) << "failed to load include path"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | } |
| 744 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 745 | // Capture the shared libraries so that the final resource table can be properly flattened |
| 746 | // with support for shared libraries. |
| 747 | for (auto& entry : asset_source->GetAssignedPackageIds()) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 748 | if (entry.first > kFrameworkPackageId && entry.first < kAppPackageId) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 749 | final_table_.included_packages_[entry.first] = entry.second; |
| 750 | } |
| 751 | } |
| 752 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 753 | context_->GetExternalSymbols()->AppendSource(std::move(asset_source)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 754 | return true; |
| 755 | } |
| 756 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 757 | Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 758 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 759 | xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get()); |
| 760 | if (manifest_el == nullptr) { |
| 761 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 762 | } |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 763 | |
| 764 | AppInfo app_info; |
| 765 | |
| 766 | if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") { |
| 767 | diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>"); |
| 768 | return {}; |
| 769 | } |
| 770 | |
| 771 | xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package"); |
| 772 | if (!package_attr) { |
| 773 | diag->Error(DiagMessage(xml_res->file.source) |
| 774 | << "<manifest> must have a 'package' attribute"); |
| 775 | return {}; |
| 776 | } |
| 777 | app_info.package = package_attr->value; |
| 778 | |
| 779 | if (xml::Attribute* version_code_attr = |
| 780 | manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) { |
| 781 | Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value); |
| 782 | if (!maybe_code) { |
| 783 | diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number)) |
| 784 | << "invalid android:versionCode '" << version_code_attr->value << "'"); |
| 785 | return {}; |
| 786 | } |
| 787 | app_info.version_code = maybe_code.value(); |
| 788 | } |
| 789 | |
| 790 | if (xml::Attribute* revision_code_attr = |
| 791 | manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) { |
| 792 | Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value); |
| 793 | if (!maybe_code) { |
| 794 | diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number)) |
| 795 | << "invalid android:revisionCode '" << revision_code_attr->value << "'"); |
| 796 | return {}; |
| 797 | } |
| 798 | app_info.revision_code = maybe_code.value(); |
| 799 | } |
| 800 | |
| 801 | if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) { |
| 802 | if (!split_name_attr->value.empty()) { |
| 803 | app_info.split_name = split_name_attr->value; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) { |
| 808 | if (xml::Attribute* min_sdk = |
| 809 | uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 810 | app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value); |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | return app_info; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /** |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 817 | * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 818 | * Postcondition: ResourceTable has only one package left. All others are |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 819 | * stripped, or there is an error and false is returned. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 820 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 821 | bool VerifyNoExternalPackages() { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 822 | auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 823 | return context_->GetCompilationPackage() != pkg->name || !pkg->id || |
| 824 | pkg->id.value() != context_->GetPackageId(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 825 | }; |
| 826 | |
| 827 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 828 | for (const auto& package : final_table_.packages) { |
| 829 | if (is_ext_package_func(package)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 830 | // We have a package that is not related to the one we're building! |
| 831 | for (const auto& type : package->types) { |
| 832 | for (const auto& entry : type->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 833 | ResourceNameRef res_name(package->name, type->type, entry->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 834 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 835 | for (const auto& config_value : entry->values) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 836 | // Special case the occurrence of an ID that is being generated |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 837 | // for the 'android' package. This is due to legacy reasons. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 838 | if (ValueCast<Id>(config_value->value.get()) && package->name == "android") { |
| 839 | context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource()) |
| 840 | << "generated id '" << res_name |
| 841 | << "' for external package '" << package->name |
| 842 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 843 | } else { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 844 | context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource()) |
| 845 | << "defined resource '" << res_name |
| 846 | << "' for external package '" << package->name |
| 847 | << "'"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 848 | error = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 849 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 850 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 856 | auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(), |
| 857 | is_ext_package_func); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 858 | final_table_.packages.erase(new_end_iter, final_table_.packages.end()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 859 | return !error; |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * Returns true if no IDs have been set, false otherwise. |
| 864 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 865 | bool VerifyNoIdsSet() { |
| 866 | for (const auto& package : final_table_.packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 867 | for (const auto& type : package->types) { |
| 868 | if (type->id) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 869 | context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID " |
| 870 | << StringPrintf("%02x", type->id.value()) |
| 871 | << " assigned"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 872 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 875 | for (const auto& entry : type->entries) { |
| 876 | if (entry->id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 877 | ResourceNameRef res_name(package->name, type->type, entry->name); |
| 878 | context_->GetDiagnostics()->Error( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 879 | DiagMessage() << "entry " << res_name << " has ID " |
| 880 | << StringPrintf("%02x", entry->id.value()) << " assigned"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 889 | std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) { |
| 890 | if (options_.output_to_directory) { |
| 891 | return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 892 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 893 | return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 897 | bool FlattenTable(ResourceTable* table, IArchiveWriter* writer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 898 | BigBuffer buffer(1024); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 899 | TableFlattener flattener(options_.table_flattener_options, &buffer); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 900 | if (!flattener.Consume(context_, table)) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 901 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 902 | return false; |
| 903 | } |
| 904 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 905 | io::BigBufferInputStream input_stream(&buffer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 906 | return io::CopyInputStreamToArchive(context_, &input_stream, "resources.arsc", |
| 907 | ArchiveEntry::kAlign, writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 910 | bool FlattenTableToPb(ResourceTable* table, IArchiveWriter* writer) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 911 | std::unique_ptr<pb::ResourceTable> pb_table = SerializeTableToPb(table); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 912 | return io::CopyProtoToArchive(context_, pb_table.get(), "resources.arsc.flat", 0, writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 915 | bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 916 | const StringPiece& out_package, const JavaClassGeneratorOptions& java_options, |
| 917 | const Maybe<std::string> out_text_symbols_path = {}) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 918 | if (!options_.generate_java_class_path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 919 | return true; |
| 920 | } |
| 921 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 922 | std::string out_path = options_.generate_java_class_path.value(); |
| 923 | file::AppendPath(&out_path, file::PackageToPath(out_package)); |
| 924 | if (!file::mkdirs(out_path)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 925 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path |
| 926 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 927 | return false; |
| 928 | } |
| 929 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 930 | file::AppendPath(&out_path, "R.java"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 931 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 932 | std::ofstream fout(out_path, std::ofstream::binary); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 933 | if (!fout) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 934 | context_->GetDiagnostics()->Error(DiagMessage() |
| 935 | << "failed writing to '" << out_path |
| 936 | << "': " << android::base::SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 937 | return false; |
| 938 | } |
| 939 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 940 | std::unique_ptr<std::ofstream> fout_text; |
| 941 | if (out_text_symbols_path) { |
| 942 | fout_text = |
| 943 | util::make_unique<std::ofstream>(out_text_symbols_path.value(), std::ofstream::binary); |
| 944 | if (!*fout_text) { |
| 945 | context_->GetDiagnostics()->Error( |
| 946 | DiagMessage() << "failed writing to '" << out_text_symbols_path.value() |
| 947 | << "': " << android::base::SystemErrorCodeToString(errno)); |
| 948 | return false; |
| 949 | } |
| 950 | } |
| 951 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 952 | JavaClassGenerator generator(context_, table, java_options); |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 953 | if (!generator.Generate(package_name_to_generate, out_package, &fout, fout_text.get())) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 954 | context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.getError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 955 | return false; |
| 956 | } |
| 957 | |
| 958 | if (!fout) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 959 | context_->GetDiagnostics()->Error(DiagMessage() |
| 960 | << "failed writing to '" << out_path |
| 961 | << "': " << android::base::SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 962 | } |
| 963 | return true; |
| 964 | } |
| 965 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 966 | bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) { |
| 967 | if (!options_.generate_java_class_path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 968 | return true; |
| 969 | } |
| 970 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 971 | std::unique_ptr<ClassDefinition> manifest_class = |
| 972 | GenerateManifestClass(context_->GetDiagnostics(), manifest_xml); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 973 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 974 | if (!manifest_class) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 975 | // Something bad happened, but we already logged it, so exit. |
| 976 | return false; |
| 977 | } |
| 978 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 979 | if (manifest_class->empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 980 | // Empty Manifest class, no need to generate it. |
| 981 | return true; |
| 982 | } |
| 983 | |
| 984 | // Add any JavaDoc annotations to the generated class. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 985 | for (const std::string& annotation : options_.javadoc_annotations) { |
| 986 | std::string proper_annotation = "@"; |
| 987 | proper_annotation += annotation; |
| 988 | manifest_class->GetCommentBuilder()->AppendComment(proper_annotation); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 991 | const std::string& package_utf8 = context_->GetCompilationPackage(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 992 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 993 | std::string out_path = options_.generate_java_class_path.value(); |
| 994 | file::AppendPath(&out_path, file::PackageToPath(package_utf8)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 995 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 996 | if (!file::mkdirs(out_path)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 997 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path |
| 998 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 999 | return false; |
| 1000 | } |
| 1001 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1002 | file::AppendPath(&out_path, "Manifest.java"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1003 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1004 | std::ofstream fout(out_path, std::ofstream::binary); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1005 | if (!fout) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1006 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1007 | << "failed writing to '" << out_path |
| 1008 | << "': " << android::base::SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1009 | return false; |
| 1010 | } |
| 1011 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1012 | if (!ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout)) { |
| 1013 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1014 | << "failed writing to '" << out_path |
| 1015 | << "': " << android::base::SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1021 | bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1022 | if (!out) { |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1026 | const std::string& out_path = out.value(); |
| 1027 | std::ofstream fout(out_path, std::ofstream::binary); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1028 | if (!fout) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1029 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1030 | << "failed to open '" << out_path |
| 1031 | << "': " << android::base::SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1032 | return false; |
| 1033 | } |
| 1034 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1035 | proguard::WriteKeepSet(&fout, keep_set); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1036 | if (!fout) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1037 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1038 | << "failed writing to '" << out_path |
| 1039 | << "': " << android::base::SystemErrorCodeToString(errno)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1040 | return false; |
| 1041 | } |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1045 | std::unique_ptr<ResourceTable> LoadStaticLibrary(const std::string& input, |
| 1046 | std::string* out_error) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1047 | std::unique_ptr<io::ZipFileCollection> collection = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1048 | io::ZipFileCollection::Create(input, out_error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1049 | if (!collection) { |
| 1050 | return {}; |
| 1051 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1052 | return LoadTablePbFromCollection(collection.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1055 | std::unique_ptr<ResourceTable> LoadTablePbFromCollection(io::IFileCollection* collection) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1056 | io::IFile* file = collection->FindFile("resources.arsc.flat"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1057 | if (!file) { |
| 1058 | return {}; |
| 1059 | } |
| 1060 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1061 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 1062 | return LoadTableFromPb(file->GetSource(), data->data(), data->size(), |
| 1063 | context_->GetDiagnostics()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1066 | bool MergeStaticLibrary(const std::string& input, bool override) { |
| 1067 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1068 | context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1071 | std::string error_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1072 | std::unique_ptr<io::ZipFileCollection> collection = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1073 | io::ZipFileCollection::Create(input, &error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1074 | if (!collection) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1075 | context_->GetDiagnostics()->Error(DiagMessage(input) << error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1076 | return false; |
| 1077 | } |
| 1078 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1079 | std::unique_ptr<ResourceTable> table = LoadTablePbFromCollection(collection.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1080 | if (!table) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1081 | context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
| 1084 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1085 | ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1086 | if (!pkg) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1087 | context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | bool result; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1092 | if (options_.no_static_lib_packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1093 | // Merge all resources as if they were in the compilation package. This is |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1094 | // the old behavior of aapt. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1095 | |
| 1096 | // Add the package to the set of --extra-packages so we emit an R.java for |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1097 | // each library package. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1098 | if (!pkg->name.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1099 | options_.extra_java_packages.insert(pkg->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | pkg->name = ""; |
| 1103 | if (override) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1104 | result = table_merger_->MergeOverlay(Source(input), table.get(), collection.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1105 | } else { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1106 | result = table_merger_->Merge(Source(input), table.get(), collection.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | } else { |
| 1110 | // This is the proper way to merge libraries, where the package name is |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1111 | // preserved and resource names are mangled. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1112 | result = |
| 1113 | table_merger_->MergeAndMangle(Source(input), pkg->name, table.get(), collection.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | if (!result) { |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
| 1120 | // Make sure to move the collection into the set of IFileCollections. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1121 | collections_.push_back(std::move(collection)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1122 | return true; |
| 1123 | } |
| 1124 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1125 | bool MergeResourceTable(io::IFile* file, bool override) { |
| 1126 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1127 | context_->GetDiagnostics()->Note(DiagMessage() << "merging resource table " |
| 1128 | << file->GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1131 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1132 | if (!data) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1133 | context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | std::unique_ptr<ResourceTable> table = |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1138 | LoadTableFromPb(file->GetSource(), data->data(), data->size(), context_->GetDiagnostics()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1139 | if (!table) { |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | bool result = false; |
| 1144 | if (override) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1145 | result = table_merger_->MergeOverlay(file->GetSource(), table.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1146 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1147 | result = table_merger_->Merge(file->GetSource(), table.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1148 | } |
| 1149 | return result; |
| 1150 | } |
| 1151 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1152 | bool MergeCompiledFile(io::IFile* file, ResourceFile* file_desc, bool override) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1153 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1154 | context_->GetDiagnostics()->Note(DiagMessage() << "merging '" << file_desc->name |
| 1155 | << "' from compiled file " |
| 1156 | << file->GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | bool result = false; |
| 1160 | if (override) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1161 | result = table_merger_->MergeFileOverlay(*file_desc, file); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1162 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1163 | result = table_merger_->MergeFile(*file_desc, file); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | if (!result) { |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
| 1170 | // Add the exports of this file to the table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1171 | for (SourcedResourceName& exported_symbol : file_desc->exported_symbols) { |
| 1172 | if (exported_symbol.name.package.empty()) { |
| 1173 | exported_symbol.name.package = context_->GetCompilationPackage(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1176 | ResourceNameRef res_name = exported_symbol.name; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1178 | Maybe<ResourceName> mangled_name = |
| 1179 | context_->GetNameMangler()->MangleName(exported_symbol.name); |
| 1180 | if (mangled_name) { |
| 1181 | res_name = mangled_name.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1185 | id->SetSource(file_desc->source.WithLine(exported_symbol.line)); |
| 1186 | bool result = final_table_.AddResourceAllowMangled( |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1187 | res_name, ConfigDescription::DefaultConfig(), std::string(), std::move(id), |
| 1188 | context_->GetDiagnostics()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1189 | if (!result) { |
| 1190 | return false; |
| 1191 | } |
| 1192 | } |
| 1193 | return true; |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * Takes a path to load as a ZIP file and merges the files within into the |
| 1198 | * master ResourceTable. |
| 1199 | * If override is true, conflicting resources are allowed to override each |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1200 | * other, in order of last seen. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1201 | * |
| 1202 | * An io::IFileCollection is created from the ZIP file and added to the set of |
| 1203 | * io::IFileCollections that are open. |
| 1204 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1205 | bool MergeArchive(const std::string& input, bool override) { |
| 1206 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1207 | context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1210 | std::string error_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1211 | std::unique_ptr<io::ZipFileCollection> collection = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1212 | io::ZipFileCollection::Create(input, &error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1213 | if (!collection) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1214 | context_->GetDiagnostics()->Error(DiagMessage(input) << error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1219 | for (auto iter = collection->Iterator(); iter->HasNext();) { |
| 1220 | if (!MergeFile(iter->Next(), override)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1221 | error = true; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | // Make sure to move the collection into the set of IFileCollections. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1226 | collections_.push_back(std::move(collection)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1227 | return !error; |
| 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * Takes a path to load and merge into the master ResourceTable. If override |
| 1232 | * is true, |
| 1233 | * conflicting resources are allowed to override each other, in order of last |
| 1234 | * seen. |
| 1235 | * |
| 1236 | * If the file path ends with .flata, .jar, .jack, or .zip the file is treated |
| 1237 | * as ZIP archive |
| 1238 | * and the files within are merged individually. |
| 1239 | * |
| 1240 | * Otherwise the files is processed on its own. |
| 1241 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1242 | bool MergePath(const std::string& path, bool override) { |
| 1243 | if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") || |
| 1244 | util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) { |
| 1245 | return MergeArchive(path, override); |
| 1246 | } else if (util::EndsWith(path, ".apk")) { |
| 1247 | return MergeStaticLibrary(path, override); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1250 | io::IFile* file = file_collection_->InsertFile(path); |
| 1251 | return MergeFile(file, override); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * Takes a file to load and merge into the master ResourceTable. If override |
| 1256 | * is true, |
| 1257 | * conflicting resources are allowed to override each other, in order of last |
| 1258 | * seen. |
| 1259 | * |
| 1260 | * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and |
| 1261 | * merged into the |
| 1262 | * master ResourceTable. If the file ends with .flat, then it is treated like |
| 1263 | * a compiled file |
| 1264 | * and the header data is read and merged into the final ResourceTable. |
| 1265 | * |
| 1266 | * All other file types are ignored. This is because these files could be |
| 1267 | * coming from a zip, |
| 1268 | * where we could have other files like classes.dex. |
| 1269 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1270 | bool MergeFile(io::IFile* file, bool override) { |
| 1271 | const Source& src = file->GetSource(); |
| 1272 | if (util::EndsWith(src.path, ".arsc.flat")) { |
| 1273 | return MergeResourceTable(file, override); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1274 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1275 | } else if (util::EndsWith(src.path, ".flat")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1276 | // Try opening the file and looking for an Export header. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1277 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1278 | if (!data) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1279 | context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1280 | return false; |
| 1281 | } |
| 1282 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1283 | CompiledFileInputStream input_stream(data->data(), data->size()); |
| 1284 | uint32_t num_files = 0; |
| 1285 | if (!input_stream.ReadLittleEndian32(&num_files)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1286 | context_->GetDiagnostics()->Error(DiagMessage(src) << "failed read num files"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1287 | return false; |
| 1288 | } |
| 1289 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1290 | for (uint32_t i = 0; i < num_files; i++) { |
| 1291 | pb::CompiledFile compiled_file; |
| 1292 | if (!input_stream.ReadCompiledFile(&compiled_file)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1293 | context_->GetDiagnostics()->Error(DiagMessage(src) |
| 1294 | << "failed to read compiled file header"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1295 | return false; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1298 | uint64_t offset, len; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1299 | if (!input_stream.ReadDataMetaData(&offset, &len)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1300 | context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read data meta data"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1301 | return false; |
| 1302 | } |
| 1303 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1304 | std::unique_ptr<ResourceFile> resource_file = DeserializeCompiledFileFromPb( |
| 1305 | compiled_file, file->GetSource(), context_->GetDiagnostics()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1306 | if (!resource_file) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1307 | return false; |
| 1308 | } |
| 1309 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1310 | if (!MergeCompiledFile(file->CreateFileSegment(offset, len), resource_file.get(), |
| 1311 | override)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
| 1314 | } |
| 1315 | return true; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1316 | } else if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) { |
Adam Lesinski | 6a396c1 | 2016-10-20 14:38:23 -0700 | [diff] [blame] | 1317 | // Since AAPT compiles these file types and appends .flat to them, seeing |
| 1318 | // their raw extensions is a sign that they weren't compiled. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1319 | const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG"; |
| 1320 | context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type |
| 1321 | << " file passed as argument. Must be " |
| 1322 | "compiled first into .flat file."); |
Adam Lesinski | 6a396c1 | 2016-10-20 14:38:23 -0700 | [diff] [blame] | 1323 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | // Ignore non .flat files. This could be classes.dex or something else that |
| 1327 | // happens |
| 1328 | // to be in an archive. |
| 1329 | return true; |
| 1330 | } |
| 1331 | |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1332 | bool CopyAssetsDirsToApk(IArchiveWriter* writer) { |
| 1333 | std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets; |
| 1334 | for (const std::string& assets_dir : options_.assets_dirs) { |
| 1335 | Maybe<std::vector<std::string>> files = |
| 1336 | file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr); |
| 1337 | if (!files) { |
| 1338 | return false; |
| 1339 | } |
| 1340 | |
| 1341 | for (const std::string& file : files.value()) { |
| 1342 | std::string full_key = "assets/" + file; |
| 1343 | std::string full_path = assets_dir; |
| 1344 | file::AppendPath(&full_path, file); |
| 1345 | |
| 1346 | auto iter = merged_assets.find(full_key); |
| 1347 | if (iter == merged_assets.end()) { |
| 1348 | merged_assets.emplace(std::move(full_key), |
| 1349 | util::make_unique<io::RegularFile>(Source(std::move(full_path)))); |
| 1350 | } else if (context_->IsVerbose()) { |
| 1351 | context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource()) |
| 1352 | << "asset file overrides '" << full_path << "'"); |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | for (auto& entry : merged_assets) { |
| 1358 | uint32_t compression_flags = ArchiveEntry::kCompress; |
| 1359 | std::string extension = file::GetExtension(entry.first).to_string(); |
| 1360 | if (options_.extensions_to_not_compress.count(extension) > 0) { |
| 1361 | compression_flags = 0u; |
| 1362 | } |
| 1363 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1364 | if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags, |
| 1365 | writer)) { |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1366 | return false; |
| 1367 | } |
| 1368 | } |
| 1369 | return true; |
| 1370 | } |
| 1371 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1372 | /** |
| 1373 | * Writes the AndroidManifest, ResourceTable, and all XML files referenced by |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1374 | * the ResourceTable to the IArchiveWriter. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1375 | */ |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1376 | bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest, |
| 1377 | ResourceTable* table) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1378 | const bool keep_raw_values = context_->GetPackageType() == PackageType::kStaticLib; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame^] | 1379 | bool result = FlattenXml(context_, manifest, "AndroidManifest.xml", keep_raw_values, writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1380 | if (!result) { |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1384 | ResourceFileFlattenerOptions file_flattener_options; |
| 1385 | file_flattener_options.keep_raw_values = keep_raw_values; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1386 | file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything; |
| 1387 | file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1388 | file_flattener_options.no_auto_version = options_.no_auto_version; |
| 1389 | file_flattener_options.no_version_vectors = options_.no_version_vectors; |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 1390 | file_flattener_options.no_version_transitions = options_.no_version_transitions; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1391 | file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces; |
| 1392 | file_flattener_options.update_proguard_spec = |
| 1393 | static_cast<bool>(options_.generate_proguard_rules_path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1394 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1395 | ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1396 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1397 | if (!file_flattener.Flatten(table, writer)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1398 | context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1399 | return false; |
| 1400 | } |
| 1401 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1402 | if (context_->GetPackageType() == PackageType::kStaticLib) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1403 | if (!FlattenTableToPb(table, writer)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1404 | return false; |
| 1405 | } |
| 1406 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1407 | if (!FlattenTable(table, writer)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1408 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resources.arsc"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1409 | return false; |
| 1410 | } |
| 1411 | } |
| 1412 | return true; |
| 1413 | } |
| 1414 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1415 | int Run(const std::vector<std::string>& input_files) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1416 | // Load the AndroidManifest.xml |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1417 | std::unique_ptr<xml::XmlResource> manifest_xml = |
| 1418 | LoadXml(options_.manifest_path, context_->GetDiagnostics()); |
| 1419 | if (!manifest_xml) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1420 | return 1; |
| 1421 | } |
| 1422 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1423 | // First extract the Package name without modifying it (via --rename-manifest-package). |
| 1424 | if (Maybe<AppInfo> maybe_app_info = |
| 1425 | ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1426 | const AppInfo& app_info = maybe_app_info.value(); |
| 1427 | context_->SetCompilationPackage(app_info.package); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1430 | ManifestFixer manifest_fixer(options_.manifest_fixer_options); |
| 1431 | if (!manifest_fixer.Consume(context_, manifest_xml.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1432 | return 1; |
| 1433 | } |
| 1434 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1435 | Maybe<AppInfo> maybe_app_info = |
| 1436 | ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1437 | if (!maybe_app_info) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1438 | return 1; |
| 1439 | } |
| 1440 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1441 | const AppInfo& app_info = maybe_app_info.value(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1442 | context_->SetMinSdkVersion(app_info.min_sdk_version.value_or_default(0)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1443 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1444 | context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()}); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1445 | |
| 1446 | // Override the package ID when it is "android". |
| 1447 | if (context_->GetCompilationPackage() == "android") { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1448 | context_->SetPackageId(0x01); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1449 | |
| 1450 | // Verify we're building a regular app. |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1451 | if (context_->GetPackageType() != PackageType::kApp) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1452 | context_->GetDiagnostics()->Error( |
| 1453 | DiagMessage() << "package 'android' can only be built as a regular app"); |
| 1454 | return 1; |
| 1455 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1458 | if (!LoadSymbolsFromIncludePaths()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1459 | return 1; |
| 1460 | } |
| 1461 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1462 | TableMergerOptions table_merger_options; |
| 1463 | table_merger_options.auto_add_overlay = options_.auto_add_overlay; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1464 | table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1465 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1466 | if (context_->IsVerbose()) { |
| 1467 | context_->GetDiagnostics()->Note(DiagMessage() |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1468 | << StringPrintf("linking package '%s' using package ID %02x", |
| 1469 | context_->GetCompilationPackage().data(), |
| 1470 | context_->GetPackageId())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1473 | for (const std::string& input : input_files) { |
| 1474 | if (!MergePath(input, false)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1475 | context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1476 | return 1; |
| 1477 | } |
| 1478 | } |
| 1479 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1480 | for (const std::string& input : options_.overlay_files) { |
| 1481 | if (!MergePath(input, true)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1482 | context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1483 | return 1; |
| 1484 | } |
| 1485 | } |
| 1486 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1487 | if (!VerifyNoExternalPackages()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1488 | return 1; |
| 1489 | } |
| 1490 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1491 | if (context_->GetPackageType() != PackageType::kStaticLib) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1492 | PrivateAttributeMover mover; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1493 | if (!mover.Consume(context_, &final_table_)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1494 | context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1495 | return 1; |
| 1496 | } |
| 1497 | |
| 1498 | // Assign IDs if we are building a regular app. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1499 | IdAssigner id_assigner(&options_.stable_id_map); |
| 1500 | if (!id_assigner.Consume(context_, &final_table_)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1501 | context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1502 | return 1; |
| 1503 | } |
| 1504 | |
| 1505 | // Now grab each ID and emit it as a file. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1506 | if (options_.resource_id_map_path) { |
| 1507 | for (auto& package : final_table_.packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1508 | for (auto& type : package->types) { |
| 1509 | for (auto& entry : type->entries) { |
| 1510 | ResourceName name(package->name, type->type, entry->name); |
| 1511 | // The IDs are guaranteed to exist. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1512 | options_.stable_id_map[std::move(name)] = |
| 1513 | ResourceId(package->id.value(), type->id.value(), entry->id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1518 | if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1519 | options_.resource_id_map_path.value())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1520 | return 1; |
| 1521 | } |
| 1522 | } |
| 1523 | } else { |
| 1524 | // Static libs are merged with other apps, and ID collisions are bad, so |
| 1525 | // verify that |
| 1526 | // no IDs have been set. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1527 | if (!VerifyNoIdsSet()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1528 | return 1; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | // Add the names to mangle based on our source merge earlier. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1533 | context_->SetNameManglerPolicy( |
| 1534 | NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1535 | |
| 1536 | // Add our table to the symbol table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1537 | context_->GetExternalSymbols()->PrependSource( |
| 1538 | util::make_unique<ResourceTableSymbolSource>(&final_table_)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1539 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 1540 | // Workaround for pre-O runtime that would treat negative resource IDs |
| 1541 | // (any ID with a package ID > 7f) as invalid. Intercept any ID (PPTTEEEE) with PP > 0x7f |
| 1542 | // and type == 'id', and return the ID 0x7fPPEEEE. IDs don't need to be real resources, they |
| 1543 | // are just identifiers. |
| 1544 | if (context_->GetMinSdkVersion() < SDK_O && context_->GetPackageType() == PackageType::kApp) { |
| 1545 | if (context_->IsVerbose()) { |
| 1546 | context_->GetDiagnostics()->Note(DiagMessage() |
| 1547 | << "enabling pre-O feature split ID rewriting"); |
| 1548 | } |
| 1549 | context_->GetExternalSymbols()->SetDelegate( |
| 1550 | util::make_unique<FeatureSplitSymbolTableDelegate>(context_)); |
| 1551 | } |
| 1552 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1553 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1554 | if (!linker.Consume(context_, &final_table_)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1555 | context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1556 | return 1; |
| 1557 | } |
| 1558 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1559 | if (context_->GetPackageType() == PackageType::kStaticLib) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1560 | if (!options_.products.empty()) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1561 | context_->GetDiagnostics()->Warn(DiagMessage() |
| 1562 | << "can't select products when building static library"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1563 | } |
| 1564 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1565 | ProductFilter product_filter(options_.products); |
| 1566 | if (!product_filter.Consume(context_, &final_table_)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1567 | context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1568 | return 1; |
| 1569 | } |
| 1570 | } |
| 1571 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1572 | if (!options_.no_auto_version) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1573 | AutoVersioner versioner; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1574 | if (!versioner.Consume(context_, &final_table_)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1575 | context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1576 | return 1; |
| 1577 | } |
| 1578 | } |
| 1579 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1580 | if (context_->GetPackageType() != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1581 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1582 | context_->GetDiagnostics()->Note(DiagMessage() |
| 1583 | << "collapsing resource versions for minimum SDK " |
| 1584 | << context_->GetMinSdkVersion()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | VersionCollapser collapser; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1588 | if (!collapser.Consume(context_, &final_table_)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1589 | return 1; |
| 1590 | } |
| 1591 | } |
| 1592 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1593 | if (!options_.no_resource_deduping) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1594 | ResourceDeduper deduper; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1595 | if (!deduper.Consume(context_, &final_table_)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1596 | context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1597 | return 1; |
| 1598 | } |
| 1599 | } |
| 1600 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1601 | proguard::KeepSet proguard_keep_set; |
| 1602 | proguard::KeepSet proguard_main_dex_keep_set; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1603 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1604 | if (context_->GetPackageType() == PackageType::kStaticLib) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1605 | if (options_.table_splitter_options.config_filter != nullptr || |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 1606 | !options_.table_splitter_options.preferred_densities.empty()) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1607 | context_->GetDiagnostics()->Warn(DiagMessage() |
| 1608 | << "can't strip resources when building static library"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1609 | } |
| 1610 | } else { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1611 | // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or |
| 1612 | // equal to the minSdk. |
| 1613 | options_.split_constraints = |
| 1614 | AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1615 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1616 | TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1617 | if (!table_splitter.VerifySplitConstraints(context_)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1618 | return 1; |
| 1619 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1620 | table_splitter.SplitTable(&final_table_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1621 | |
| 1622 | // Now we need to write out the Split APKs. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1623 | auto path_iter = options_.split_paths.begin(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1624 | auto split_constraints_iter = options_.split_constraints.begin(); |
| 1625 | for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1626 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1627 | context_->GetDiagnostics()->Note(DiagMessage(*path_iter) |
| 1628 | << "generating split with configurations '" |
| 1629 | << util::Joiner(split_constraints_iter->configs, ", ") |
| 1630 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1633 | std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1634 | if (!archive_writer) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1635 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1636 | return 1; |
| 1637 | } |
| 1638 | |
| 1639 | // Generate an AndroidManifest.xml for each split. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1640 | std::unique_ptr<xml::XmlResource> split_manifest = |
| 1641 | GenerateSplitManifest(app_info, *split_constraints_iter); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1642 | |
| 1643 | XmlReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1644 | if (!linker.Consume(context_, split_manifest.get())) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1645 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1646 | << "failed to create Split AndroidManifest.xml"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1647 | return 1; |
| 1648 | } |
| 1649 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1650 | if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(), |
| 1651 | split_table.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1652 | return 1; |
| 1653 | } |
| 1654 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1655 | ++path_iter; |
| 1656 | ++split_constraints_iter; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | // Start writing the base APK. |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1661 | std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1662 | if (!archive_writer) { |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1663 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1664 | return 1; |
| 1665 | } |
| 1666 | |
| 1667 | bool error = false; |
| 1668 | { |
| 1669 | // AndroidManifest.xml has no resource name, but the CallSite is built |
| 1670 | // from the name |
| 1671 | // (aka, which package the AndroidManifest.xml is coming from). |
| 1672 | // So we give it a package name so it can see local resources. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1673 | manifest_xml->file.name.package = context_->GetCompilationPackage(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1674 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1675 | XmlReferenceLinker manifest_linker; |
| 1676 | if (manifest_linker.Consume(context_, manifest_xml.get())) { |
| 1677 | if (options_.generate_proguard_rules_path && |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1678 | !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path), |
| 1679 | manifest_xml.get(), &proguard_keep_set)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1680 | error = true; |
| 1681 | } |
| 1682 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1683 | if (options_.generate_main_dex_proguard_rules_path && |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1684 | !proguard::CollectProguardRulesForManifest(Source(options_.manifest_path), |
| 1685 | manifest_xml.get(), |
| 1686 | &proguard_main_dex_keep_set, true)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1687 | error = true; |
Alexandria Cornwall | 637b482 | 2016-08-11 09:53:16 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1690 | if (options_.generate_java_class_path) { |
| 1691 | if (!WriteManifestJavaFile(manifest_xml.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1692 | error = true; |
| 1693 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1696 | if (options_.no_xml_namespaces) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1697 | // PackageParser will fail if URIs are removed from |
| 1698 | // AndroidManifest.xml. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1699 | XmlNamespaceRemover namespace_remover(true /* keepUris */); |
| 1700 | if (!namespace_remover.Consume(context_, manifest_xml.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1701 | error = true; |
| 1702 | } |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 1703 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1704 | } else { |
| 1705 | error = true; |
| 1706 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1707 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1708 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1709 | if (error) { |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1710 | context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1711 | return 1; |
| 1712 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1713 | |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1714 | if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) { |
| 1715 | return 1; |
| 1716 | } |
| 1717 | |
| 1718 | if (!CopyAssetsDirsToApk(archive_writer.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1719 | return 1; |
| 1720 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1721 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1722 | if (options_.generate_java_class_path) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1723 | // The set of packages whose R class to call in the main classes |
| 1724 | // onResourcesLoaded callback. |
| 1725 | std::vector<std::string> packages_to_callback; |
| 1726 | |
| 1727 | JavaClassGeneratorOptions template_options; |
| 1728 | template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 1729 | template_options.javadoc_annotations = options_.javadoc_annotations; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1730 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1731 | if (context_->GetPackageType() == PackageType::kStaticLib || |
| 1732 | options_.generate_non_final_ids) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1733 | template_options.use_final = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1734 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1735 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1736 | if (context_->GetPackageType() == PackageType::kSharedLib) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1737 | template_options.use_final = false; |
| 1738 | template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{}; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1741 | const StringPiece actual_package = context_->GetCompilationPackage(); |
| 1742 | StringPiece output_package = context_->GetCompilationPackage(); |
| 1743 | if (options_.custom_java_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1744 | // Override the output java package to the custom one. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1745 | output_package = options_.custom_java_package.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1748 | // Generate the private symbols if required. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1749 | if (options_.private_symbols) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1750 | packages_to_callback.push_back(options_.private_symbols.value()); |
| 1751 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1752 | // If we defined a private symbols package, we only emit Public symbols |
| 1753 | // to the original package, and private and public symbols to the |
| 1754 | // private package. |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1755 | JavaClassGeneratorOptions options = template_options; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1756 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1757 | if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1758 | options)) { |
| 1759 | return 1; |
| 1760 | } |
| 1761 | } |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1762 | |
| 1763 | // Generate all the symbols for all extra packages. |
| 1764 | for (const std::string& extra_package : options_.extra_java_packages) { |
| 1765 | packages_to_callback.push_back(extra_package); |
| 1766 | |
| 1767 | JavaClassGeneratorOptions options = template_options; |
| 1768 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 1769 | if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) { |
| 1770 | return 1; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | // Generate the main public R class. |
| 1775 | JavaClassGeneratorOptions options = template_options; |
| 1776 | |
| 1777 | // Only generate public symbols if we have a private package. |
| 1778 | if (options_.private_symbols) { |
| 1779 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
| 1780 | } |
| 1781 | |
| 1782 | if (options.rewrite_callback_options) { |
| 1783 | options.rewrite_callback_options.value().packages_to_callback = |
| 1784 | std::move(packages_to_callback); |
| 1785 | } |
| 1786 | |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 1787 | if (!WriteJavaFile(&final_table_, actual_package, output_package, options, |
| 1788 | options_.generate_text_symbols_path)) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1789 | return 1; |
| 1790 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1793 | if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1794 | return 1; |
| 1795 | } |
| 1796 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1797 | if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path, |
| 1798 | proguard_main_dex_keep_set)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1799 | return 1; |
| 1800 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1801 | return 0; |
| 1802 | } |
| 1803 | |
| 1804 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1805 | LinkOptions options_; |
| 1806 | LinkContext* context_; |
| 1807 | ResourceTable final_table_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1808 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1809 | std::unique_ptr<TableMerger> table_merger_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1810 | |
| 1811 | // A pointer to the FileCollection representing the filesystem (not archives). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1812 | std::unique_ptr<io::FileCollection> file_collection_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1813 | |
| 1814 | // A vector of IFileCollections. This is mainly here to keep ownership of the |
| 1815 | // collections. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1816 | std::vector<std::unique_ptr<io::IFileCollection>> collections_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1817 | |
| 1818 | // A vector of ResourceTables. This is here to retain ownership, so that the |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1819 | // SymbolTable can use these. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1820 | std::vector<std::unique_ptr<ResourceTable>> static_table_includes_; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1821 | |
| 1822 | // The set of shared libraries being used, mapping their assigned package ID to package name. |
| 1823 | std::map<size_t, std::string> shared_libs_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1824 | }; |
| 1825 | |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 1826 | int Link(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) { |
| 1827 | LinkContext context(diagnostics); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1828 | LinkOptions options; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1829 | std::vector<std::string> overlay_arg_list; |
| 1830 | std::vector<std::string> extra_java_packages; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1831 | Maybe<std::string> package_id; |
Adam Lesinski | 113ee09 | 2017-04-03 19:38:25 -0700 | [diff] [blame] | 1832 | std::vector<std::string> configs; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1833 | Maybe<std::string> preferred_density; |
| 1834 | Maybe<std::string> product_list; |
| 1835 | bool legacy_x_flag = false; |
| 1836 | bool require_localization = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1837 | bool verbose = false; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1838 | bool shared_lib = false; |
| 1839 | bool static_lib = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1840 | Maybe<std::string> stable_id_file_path; |
| 1841 | std::vector<std::string> split_args; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1842 | Flags flags = |
| 1843 | Flags() |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1844 | .RequiredFlag("-o", "Output path.", &options.output_path) |
| 1845 | .RequiredFlag("--manifest", "Path to the Android manifest to build.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1846 | &options.manifest_path) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1847 | .OptionalFlagList("-I", "Adds an Android APK to link against.", &options.include_paths) |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1848 | .OptionalFlagList("-A", |
| 1849 | "An assets directory to include in the APK. These are unprocessed.", |
| 1850 | &options.assets_dirs) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1851 | .OptionalFlagList("-R", |
| 1852 | "Compilation unit to link, using `overlay` semantics.\n" |
| 1853 | "The last conflicting resource given takes precedence.", |
| 1854 | &overlay_arg_list) |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1855 | .OptionalFlag("--package-id", |
| 1856 | "Specify the package ID to use for this app. Must be greater or equal to\n" |
| 1857 | "0x7f and can't be used with --static-lib or --shared-lib.", |
| 1858 | &package_id) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1859 | .OptionalFlag("--java", "Directory in which to generate R.java.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1860 | &options.generate_java_class_path) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1861 | .OptionalFlag("--proguard", "Output file for generated Proguard rules.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1862 | &options.generate_proguard_rules_path) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1863 | .OptionalFlag("--proguard-main-dex", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1864 | "Output file for generated Proguard rules for the main dex.", |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1865 | &options.generate_main_dex_proguard_rules_path) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1866 | .OptionalSwitch("--no-auto-version", |
| 1867 | "Disables automatic style and layout SDK versioning.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1868 | &options.no_auto_version) |
| 1869 | .OptionalSwitch("--no-version-vectors", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1870 | "Disables automatic versioning of vector drawables. Use this only\n" |
| 1871 | "when building with vector drawable support library.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1872 | &options.no_version_vectors) |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 1873 | .OptionalSwitch("--no-version-transitions", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1874 | "Disables automatic versioning of transition resources. Use this only\n" |
| 1875 | "when building with transition support library.", |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 1876 | &options.no_version_transitions) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1877 | .OptionalSwitch("--no-resource-deduping", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1878 | "Disables automatic deduping of resources with\n" |
| 1879 | "identical values across compatible configurations.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1880 | &options.no_resource_deduping) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1881 | .OptionalSwitch("--enable-sparse-encoding", |
| 1882 | "Enables encoding sparse entries using a binary search tree.\n" |
| 1883 | "This decreases APK size at the cost of resource retrieval performance.", |
| 1884 | &options.table_flattener_options.use_sparse_entries) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1885 | .OptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.", |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1886 | &legacy_x_flag) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1887 | .OptionalSwitch("-z", "Require localization of strings marked 'suggested'.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1888 | &require_localization) |
Adam Lesinski | 113ee09 | 2017-04-03 19:38:25 -0700 | [diff] [blame] | 1889 | .OptionalFlagList("-c", |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 1890 | "Comma separated list of configurations to include. The default\n" |
| 1891 | "is all configurations.", |
| 1892 | &configs) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1893 | .OptionalFlag("--preferred-density", |
| 1894 | "Selects the closest matching density and strips out all others.", |
| 1895 | &preferred_density) |
| 1896 | .OptionalFlag("--product", "Comma separated list of product names to keep", &product_list) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1897 | .OptionalSwitch("--output-to-dir", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1898 | "Outputs the APK contents to a directory specified by -o.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1899 | &options.output_to_directory) |
| 1900 | .OptionalSwitch("--no-xml-namespaces", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1901 | "Removes XML namespace prefix and URI information from\n" |
| 1902 | "AndroidManifest.xml and XML binaries in res/*.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1903 | &options.no_xml_namespaces) |
| 1904 | .OptionalFlag("--min-sdk-version", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1905 | "Default minimum SDK version to use for AndroidManifest.xml.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1906 | &options.manifest_fixer_options.min_sdk_version_default) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1907 | .OptionalFlag("--target-sdk-version", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1908 | "Default target SDK version to use for AndroidManifest.xml.", |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1909 | &options.manifest_fixer_options.target_sdk_version_default) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1910 | .OptionalFlag("--version-code", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1911 | "Version code (integer) to inject into the AndroidManifest.xml if none is\n" |
| 1912 | "present.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1913 | &options.manifest_fixer_options.version_code_default) |
| 1914 | .OptionalFlag("--version-name", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1915 | "Version name to inject into the AndroidManifest.xml if none is present.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1916 | &options.manifest_fixer_options.version_name_default) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1917 | .OptionalSwitch("--shared-lib", "Generates a shared Android runtime library.", |
| 1918 | &shared_lib) |
| 1919 | .OptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1920 | .OptionalSwitch("--no-static-lib-packages", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1921 | "Merge all library resources under the app's package.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1922 | &options.no_static_lib_packages) |
| 1923 | .OptionalSwitch("--non-final-ids", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1924 | "Generates R.java without the final modifier. This is implied when\n" |
| 1925 | "--static-lib is specified.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1926 | &options.generate_non_final_ids) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1927 | .OptionalFlag("--stable-ids", "File containing a list of name to ID mapping.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1928 | &stable_id_file_path) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1929 | .OptionalFlag("--emit-ids", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1930 | "Emit a file at the given path with a list of name to ID mappings,\n" |
| 1931 | "suitable for use with --stable-ids.", |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1932 | &options.resource_id_map_path) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1933 | .OptionalFlag("--private-symbols", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1934 | "Package name to use when generating R.java for private symbols.\n" |
| 1935 | "If not specified, public and private symbols will use the application's\n" |
| 1936 | "package name.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1937 | &options.private_symbols) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1938 | .OptionalFlag("--custom-package", "Custom Java package under which to generate R.java.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1939 | &options.custom_java_package) |
| 1940 | .OptionalFlagList("--extra-packages", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1941 | "Generate the same R.java but with different package names.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1942 | &extra_java_packages) |
| 1943 | .OptionalFlagList("--add-javadoc-annotation", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1944 | "Adds a JavaDoc annotation to all generated Java classes.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1945 | &options.javadoc_annotations) |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 1946 | .OptionalFlag("--output-text-symbols", |
| 1947 | "Generates a text file containing the resource symbols of the R class in\n" |
| 1948 | "the specified folder.", |
| 1949 | &options.generate_text_symbols_path) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1950 | .OptionalSwitch("--auto-add-overlay", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1951 | "Allows the addition of new resources in overlays without\n" |
| 1952 | "<add-resource> tags.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1953 | &options.auto_add_overlay) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1954 | .OptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1955 | &options.manifest_fixer_options.rename_manifest_package) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1956 | .OptionalFlag("--rename-instrumentation-target-package", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1957 | "Changes the name of the target package for instrumentation. Most useful\n" |
| 1958 | "when used in conjunction with --rename-manifest-package.", |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1959 | &options.manifest_fixer_options.rename_instrumentation_target_package) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1960 | .OptionalFlagList("-0", "File extensions not to compress.", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1961 | &options.extensions_to_not_compress) |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1962 | .OptionalFlagList("--split", |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1963 | "Split resources matching a set of configs out to a Split APK.\n" |
Adam Lesinski | db09157 | 2017-04-13 12:48:56 -0700 | [diff] [blame] | 1964 | "Syntax: path/to/output.apk:<config>[,<config>[...]].\n" |
| 1965 | "On Windows, use a semicolon ';' separator instead.", |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 1966 | &split_args) |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1967 | .OptionalSwitch("-v", "Enables verbose logging.", &verbose); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1968 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1969 | if (!flags.Parse("aapt2 link", args, &std::cerr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1970 | return 1; |
| 1971 | } |
| 1972 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1973 | // Expand all argument-files passed into the command line. These start with '@'. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1974 | std::vector<std::string> arg_list; |
| 1975 | for (const std::string& arg : flags.GetArgs()) { |
| 1976 | if (util::StartsWith(arg, "@")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1977 | const std::string path = arg.substr(1, arg.size() - 1); |
| 1978 | std::string error; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1979 | if (!file::AppendArgsFromFile(path, &arg_list, &error)) { |
| 1980 | context.GetDiagnostics()->Error(DiagMessage(path) << error); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1981 | return 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1982 | } |
| 1983 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1984 | arg_list.push_back(arg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | // Expand all argument-files passed to -R. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1989 | for (const std::string& arg : overlay_arg_list) { |
| 1990 | if (util::StartsWith(arg, "@")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1991 | const std::string path = arg.substr(1, arg.size() - 1); |
| 1992 | std::string error; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1993 | if (!file::AppendArgsFromFile(path, &options.overlay_files, &error)) { |
| 1994 | context.GetDiagnostics()->Error(DiagMessage(path) << error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1995 | return 1; |
| 1996 | } |
| 1997 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1998 | options.overlay_files.push_back(arg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | if (verbose) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2003 | context.SetVerbose(verbose); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2006 | if (shared_lib && static_lib) { |
| 2007 | context.GetDiagnostics()->Error(DiagMessage() |
| 2008 | << "only one of --shared-lib and --static-lib can be defined"); |
| 2009 | return 1; |
| 2010 | } |
| 2011 | |
| 2012 | if (shared_lib) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2013 | context.SetPackageType(PackageType::kSharedLib); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2014 | context.SetPackageId(0x00); |
| 2015 | } else if (static_lib) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2016 | context.SetPackageType(PackageType::kStaticLib); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2017 | context.SetPackageId(kAppPackageId); |
| 2018 | } else { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2019 | context.SetPackageType(PackageType::kApp); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2020 | context.SetPackageId(kAppPackageId); |
| 2021 | } |
| 2022 | |
| 2023 | if (package_id) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2024 | if (context.GetPackageType() != PackageType::kApp) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2025 | context.GetDiagnostics()->Error( |
| 2026 | DiagMessage() << "can't specify --package-id when not building a regular app"); |
| 2027 | return 1; |
| 2028 | } |
| 2029 | |
| 2030 | const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id.value()); |
| 2031 | if (!maybe_package_id_int) { |
| 2032 | context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id.value() |
| 2033 | << "' is not a valid integer"); |
| 2034 | return 1; |
| 2035 | } |
| 2036 | |
| 2037 | const uint32_t package_id_int = maybe_package_id_int.value(); |
| 2038 | if (package_id_int < kAppPackageId || package_id_int > std::numeric_limits<uint8_t>::max()) { |
| 2039 | context.GetDiagnostics()->Error( |
| 2040 | DiagMessage() << StringPrintf( |
| 2041 | "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int)); |
| 2042 | return 1; |
| 2043 | } |
| 2044 | context.SetPackageId(static_cast<uint8_t>(package_id_int)); |
| 2045 | } |
| 2046 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2047 | // Populate the set of extra packages for which to generate R.java. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2048 | for (std::string& extra_package : extra_java_packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2049 | // A given package can actually be a colon separated list of packages. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2050 | for (StringPiece package : util::Split(extra_package, ':')) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 2051 | options.extra_java_packages.insert(package.to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2055 | if (product_list) { |
| 2056 | for (StringPiece product : util::Tokenize(product_list.value(), ',')) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2057 | if (product != "" && product != "default") { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 2058 | options.products.insert(product.to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2063 | std::unique_ptr<IConfigFilter> filter; |
Mihai Nita | f4dacf2 | 2017-04-07 08:25:06 -0700 | [diff] [blame] | 2064 | if (!configs.empty()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2065 | filter = ParseConfigFilterParameters(configs, context.GetDiagnostics()); |
| 2066 | if (filter == nullptr) { |
| 2067 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 2068 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2069 | options.table_splitter_options.config_filter = filter.get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2072 | if (preferred_density) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2073 | Maybe<uint16_t> density = |
| 2074 | ParseTargetDensityParameter(preferred_density.value(), context.GetDiagnostics()); |
| 2075 | if (!density) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2076 | return 1; |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 2077 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2078 | options.table_splitter_options.preferred_densities.push_back(density.value()); |
| 2079 | } |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 2080 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2081 | // Parse the split parameters. |
| 2082 | for (const std::string& split_arg : split_args) { |
| 2083 | options.split_paths.push_back({}); |
| 2084 | options.split_constraints.push_back({}); |
| 2085 | if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options.split_paths.back(), |
| 2086 | &options.split_constraints.back())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2087 | return 1; |
Adam Lesinski | 1e21ff0 | 2016-06-24 14:57:58 -0700 | [diff] [blame] | 2088 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2089 | } |
Adam Lesinski | 1e21ff0 | 2016-06-24 14:57:58 -0700 | [diff] [blame] | 2090 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2091 | if (context.GetPackageType() != PackageType::kStaticLib && stable_id_file_path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2092 | if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path.value(), |
| 2093 | &options.stable_id_map)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2094 | return 1; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 2095 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2096 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 2097 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2098 | // Populate some default no-compress extensions that are already compressed. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2099 | options.extensions_to_not_compress.insert( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2100 | {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg", |
| 2101 | ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl", |
| 2102 | ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2", |
| 2103 | ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"}); |
| 2104 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2105 | // Turn off auto versioning for static-libs. |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2106 | if (context.GetPackageType() == PackageType::kStaticLib) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2107 | options.no_auto_version = true; |
| 2108 | options.no_version_vectors = true; |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 2109 | options.no_version_transitions = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2110 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 2111 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2112 | LinkCommand cmd(&context, options); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2113 | return cmd.Run(arg_list); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2116 | } // namespace aapt |