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