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