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