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