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