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