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 | |
| 17 | #include "AppInfo.h" |
| 18 | #include "Debug.h" |
| 19 | #include "Flags.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 20 | #include "Locale.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "NameMangler.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 22 | #include "ResourceUtils.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "compile/IdAssigner.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 24 | #include "filter/ConfigFilter.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "flatten/Archive.h" |
| 26 | #include "flatten/TableFlattener.h" |
| 27 | #include "flatten/XmlFlattener.h" |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 28 | #include "io/FileSystem.h" |
| 29 | #include "io/ZipArchive.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 30 | #include "java/JavaClassGenerator.h" |
| 31 | #include "java/ManifestClassGenerator.h" |
| 32 | #include "java/ProguardRules.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | #include "link/Linkers.h" |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 34 | #include "link/ProductFilter.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 35 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 36 | #include "link/ManifestFixer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | #include "link/TableMerger.h" |
| 38 | #include "process/IResourceTableConsumer.h" |
| 39 | #include "process/SymbolTable.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 40 | #include "proto/ProtoSerialize.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 41 | #include "split/TableSplitter.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | #include "unflatten/BinaryResourceParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | #include "util/Files.h" |
| 44 | #include "util/StringPiece.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 45 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 47 | #include <google/protobuf/io/coded_stream.h> |
| 48 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | #include <fstream> |
| 50 | #include <sys/stat.h> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | #include <vector> |
| 52 | |
| 53 | namespace aapt { |
| 54 | |
| 55 | struct LinkOptions { |
| 56 | std::string outputPath; |
| 57 | std::string manifestPath; |
| 58 | std::vector<std::string> includePaths; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 59 | std::vector<std::string> overlayFiles; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | Maybe<std::string> generateJavaClassPath; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 61 | Maybe<std::u16string> customJavaPackage; |
| 62 | std::set<std::u16string> extraJavaPackages; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | Maybe<std::string> generateProguardRulesPath; |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 64 | Maybe<std::string> generateMainDexProguardRulesPath; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | bool noAutoVersion = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 66 | bool noVersionVectors = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | bool staticLib = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 68 | bool noStaticLibPackages = false; |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 69 | bool generateNonFinalIds = false; |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 70 | std::vector<std::string> javadocAnnotations; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | bool outputToDirectory = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 72 | bool autoAddOverlay = false; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 73 | bool doNotCompressAnything = false; |
| 74 | std::vector<std::string> extensionsToNotCompress; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 75 | Maybe<std::u16string> privateSymbols; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 76 | ManifestFixerOptions manifestFixerOptions; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 77 | std::unordered_set<std::string> products; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 78 | TableSplitterOptions tableSplitterOptions; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 81 | class LinkContext : public IAaptContext { |
| 82 | public: |
| 83 | LinkContext() : mNameMangler({}) { |
| 84 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 85 | |
| 86 | IDiagnostics* getDiagnostics() override { |
| 87 | return &mDiagnostics; |
| 88 | } |
| 89 | |
| 90 | NameMangler* getNameMangler() override { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 91 | return &mNameMangler; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 94 | void setNameManglerPolicy(const NameManglerPolicy& policy) { |
| 95 | mNameMangler = NameMangler(policy); |
| 96 | } |
| 97 | |
| 98 | const std::u16string& getCompilationPackage() override { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | return mCompilationPackage; |
| 100 | } |
| 101 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 102 | void setCompilationPackage(const StringPiece16& packageName) { |
| 103 | mCompilationPackage = packageName.toString(); |
| 104 | } |
| 105 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | uint8_t getPackageId() override { |
| 107 | return mPackageId; |
| 108 | } |
| 109 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 110 | void setPackageId(uint8_t id) { |
| 111 | mPackageId = id; |
| 112 | } |
| 113 | |
| 114 | SymbolTable* getExternalSymbols() override { |
| 115 | return &mSymbols; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 116 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 117 | |
| 118 | bool verbose() override { |
| 119 | return mVerbose; |
| 120 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 121 | |
| 122 | void setVerbose(bool val) { |
| 123 | mVerbose = val; |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | StdErrDiagnostics mDiagnostics; |
| 128 | NameMangler mNameMangler; |
| 129 | std::u16string mCompilationPackage; |
| 130 | uint8_t mPackageId = 0x0; |
| 131 | SymbolTable mSymbols; |
| 132 | bool mVerbose = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 135 | static bool copyFileToArchive(io::IFile* file, const std::string& outPath, |
| 136 | uint32_t compressionFlags, |
| 137 | IArchiveWriter* writer, IAaptContext* context) { |
| 138 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 139 | if (!data) { |
| 140 | context->getDiagnostics()->error(DiagMessage(file->getSource()) |
| 141 | << "failed to open file"); |
| 142 | return false; |
| 143 | } |
| 144 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 145 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data()); |
| 146 | size_t bufferSize = data->size(); |
| 147 | |
| 148 | // If the file ends with .flat, we must strip off the CompiledFileHeader from it. |
| 149 | if (util::stringEndsWith<char>(file->getSource().path, ".flat")) { |
| 150 | CompiledFileInputStream inputStream(data->data(), data->size()); |
| 151 | if (!inputStream.CompiledFile()) { |
| 152 | context->getDiagnostics()->error(DiagMessage(file->getSource()) |
| 153 | << "invalid compiled file header"); |
| 154 | return false; |
| 155 | } |
| 156 | buffer = reinterpret_cast<const uint8_t*>(inputStream.data()); |
| 157 | bufferSize = inputStream.size(); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (context->verbose()) { |
| 161 | context->getDiagnostics()->note(DiagMessage() << "writing " << outPath << " to archive"); |
| 162 | } |
| 163 | |
| 164 | if (writer->startEntry(outPath, compressionFlags)) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 165 | if (writer->writeEntry(buffer, bufferSize)) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 166 | if (writer->finishEntry()) { |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | context->getDiagnostics()->error(DiagMessage() << "failed to write file " << outPath); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | static bool flattenXml(xml::XmlResource* xmlRes, const StringPiece& path, Maybe<size_t> maxSdkLevel, |
| 177 | bool keepRawValues, IArchiveWriter* writer, IAaptContext* context) { |
| 178 | BigBuffer buffer(1024); |
| 179 | XmlFlattenerOptions options = {}; |
| 180 | options.keepRawValues = keepRawValues; |
| 181 | options.maxSdkLevel = maxSdkLevel; |
| 182 | XmlFlattener flattener(&buffer, options); |
| 183 | if (!flattener.consume(context, xmlRes)) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | if (context->verbose()) { |
| 188 | DiagMessage msg; |
| 189 | msg << "writing " << path << " to archive"; |
| 190 | if (maxSdkLevel) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 191 | msg << " maxSdkLevel=" << maxSdkLevel.value() << " keepRawValues=" << keepRawValues; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 192 | } |
| 193 | context->getDiagnostics()->note(msg); |
| 194 | } |
| 195 | |
| 196 | if (writer->startEntry(path, ArchiveEntry::kCompress)) { |
| 197 | if (writer->writeEntry(buffer)) { |
| 198 | if (writer->finishEntry()) { |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | context->getDiagnostics()->error(DiagMessage() << "failed to write " << path << " to archive"); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | /*static std::unique_ptr<ResourceTable> loadTable(const Source& source, const void* data, size_t len, |
| 208 | IDiagnostics* diag) { |
| 209 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
| 210 | BinaryResourceParser parser(diag, table.get(), source, data, len); |
| 211 | if (!parser.parse()) { |
| 212 | return {}; |
| 213 | } |
| 214 | return table; |
| 215 | }*/ |
| 216 | |
| 217 | static std::unique_ptr<ResourceTable> loadTableFromPb(const Source& source, |
| 218 | const void* data, size_t len, |
| 219 | IDiagnostics* diag) { |
| 220 | pb::ResourceTable pbTable; |
| 221 | if (!pbTable.ParseFromArray(data, len)) { |
| 222 | diag->error(DiagMessage(source) << "invalid compiled table"); |
| 223 | return {}; |
| 224 | } |
| 225 | |
| 226 | std::unique_ptr<ResourceTable> table = deserializeTableFromPb(pbTable, source, diag); |
| 227 | if (!table) { |
| 228 | return {}; |
| 229 | } |
| 230 | return table; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Inflates an XML file from the source path. |
| 235 | */ |
| 236 | static std::unique_ptr<xml::XmlResource> loadXml(const std::string& path, IDiagnostics* diag) { |
| 237 | std::ifstream fin(path, std::ifstream::binary); |
| 238 | if (!fin) { |
| 239 | diag->error(DiagMessage(path) << strerror(errno)); |
| 240 | return {}; |
| 241 | } |
| 242 | return xml::inflate(&fin, diag, Source(path)); |
| 243 | } |
| 244 | |
| 245 | static std::unique_ptr<xml::XmlResource> loadBinaryXmlSkipFileExport(const Source& source, |
| 246 | const void* data, size_t len, |
| 247 | IDiagnostics* diag) { |
| 248 | CompiledFileInputStream inputStream(data, len); |
| 249 | if (!inputStream.CompiledFile()) { |
| 250 | diag->error(DiagMessage(source) << "invalid compiled file header"); |
| 251 | return {}; |
| 252 | } |
| 253 | |
| 254 | const uint8_t* xmlData = reinterpret_cast<const uint8_t*>(inputStream.data()); |
| 255 | const size_t xmlDataLen = inputStream.size(); |
| 256 | |
| 257 | std::unique_ptr<xml::XmlResource> xmlRes = xml::inflate(xmlData, xmlDataLen, diag, source); |
| 258 | if (!xmlRes) { |
| 259 | return {}; |
| 260 | } |
| 261 | return xmlRes; |
| 262 | } |
| 263 | |
| 264 | static std::unique_ptr<ResourceFile> loadFileExportHeader(const Source& source, |
| 265 | const void* data, size_t len, |
| 266 | IDiagnostics* diag) { |
| 267 | CompiledFileInputStream inputStream(data, len); |
| 268 | const pb::CompiledFile* pbFile = inputStream.CompiledFile(); |
| 269 | if (!pbFile) { |
| 270 | diag->error(DiagMessage(source) << "invalid compiled file header"); |
| 271 | return {}; |
| 272 | } |
| 273 | |
| 274 | std::unique_ptr<ResourceFile> resFile = deserializeCompiledFileFromPb(*pbFile, source, diag); |
| 275 | if (!resFile) { |
| 276 | return {}; |
| 277 | } |
| 278 | return resFile; |
| 279 | } |
| 280 | |
| 281 | struct ResourceFileFlattenerOptions { |
| 282 | bool noAutoVersion = false; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 283 | bool noVersionVectors = false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 284 | bool keepRawValues = false; |
| 285 | bool doNotCompressAnything = false; |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 286 | bool updateProguardSpec = false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 287 | std::vector<std::string> extensionsToNotCompress; |
| 288 | }; |
| 289 | |
| 290 | class ResourceFileFlattener { |
| 291 | public: |
| 292 | ResourceFileFlattener(const ResourceFileFlattenerOptions& options, |
| 293 | IAaptContext* context, proguard::KeepSet* keepSet) : |
| 294 | mOptions(options), mContext(context), mKeepSet(keepSet) { |
| 295 | } |
| 296 | |
| 297 | bool flatten(ResourceTable* table, IArchiveWriter* archiveWriter); |
| 298 | |
| 299 | private: |
| 300 | struct FileOperation { |
| 301 | io::IFile* fileToCopy; |
| 302 | std::unique_ptr<xml::XmlResource> xmlToFlatten; |
| 303 | std::string dstPath; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 304 | bool skipVersion = false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | uint32_t getCompressionFlags(const StringPiece& str); |
| 308 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 309 | bool linkAndVersionXmlFile(const ResourceEntry* entry, const ResourceFile& fileDesc, |
| 310 | io::IFile* file, ResourceTable* table, FileOperation* outFileOp); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 311 | |
| 312 | ResourceFileFlattenerOptions mOptions; |
| 313 | IAaptContext* mContext; |
| 314 | proguard::KeepSet* mKeepSet; |
| 315 | }; |
| 316 | |
| 317 | uint32_t ResourceFileFlattener::getCompressionFlags(const StringPiece& str) { |
| 318 | if (mOptions.doNotCompressAnything) { |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | for (const std::string& extension : mOptions.extensionsToNotCompress) { |
| 323 | if (util::stringEndsWith<char>(str, extension)) { |
| 324 | return 0; |
| 325 | } |
| 326 | } |
| 327 | return ArchiveEntry::kCompress; |
| 328 | } |
| 329 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 330 | bool ResourceFileFlattener::linkAndVersionXmlFile(const ResourceEntry* entry, |
| 331 | const ResourceFile& fileDesc, |
| 332 | io::IFile* file, |
| 333 | ResourceTable* table, |
| 334 | FileOperation* outFileOp) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 335 | const StringPiece srcPath = file->getSource().path; |
| 336 | if (mContext->verbose()) { |
| 337 | mContext->getDiagnostics()->note(DiagMessage() << "linking " << srcPath); |
| 338 | } |
| 339 | |
| 340 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 341 | if (!data) { |
| 342 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) << "failed to open file"); |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 343 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 346 | if (util::stringEndsWith<char>(srcPath, ".flat")) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 347 | outFileOp->xmlToFlatten = loadBinaryXmlSkipFileExport(file->getSource(), |
| 348 | data->data(), data->size(), |
| 349 | mContext->getDiagnostics()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 350 | } else { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 351 | outFileOp->xmlToFlatten = xml::inflate(data->data(), data->size(), |
| 352 | mContext->getDiagnostics(), |
| 353 | file->getSource()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 356 | if (!outFileOp->xmlToFlatten) { |
| 357 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Copy the the file description header. |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 361 | outFileOp->xmlToFlatten->file = fileDesc; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 362 | |
| 363 | XmlReferenceLinker xmlLinker; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 364 | if (!xmlLinker.consume(mContext, outFileOp->xmlToFlatten.get())) { |
| 365 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 368 | if (mOptions.updateProguardSpec && !proguard::collectProguardRules( |
| 369 | outFileOp->xmlToFlatten->file.source, outFileOp->xmlToFlatten.get(), mKeepSet)) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 370 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (!mOptions.noAutoVersion) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 374 | if (mOptions.noVersionVectors) { |
| 375 | // Skip this if it is a vector or animated-vector. |
| 376 | xml::Element* el = xml::findRootElement(outFileOp->xmlToFlatten.get()); |
| 377 | if (el && el->namespaceUri.empty()) { |
| 378 | if (el->name == u"vector" || el->name == u"animated-vector") { |
| 379 | // We are NOT going to version this file. |
| 380 | outFileOp->skipVersion = true; |
| 381 | return true; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 386 | // Find the first SDK level used that is higher than this defined config and |
| 387 | // not superseded by a lower or equal SDK level resource. |
| 388 | for (int sdkLevel : xmlLinker.getSdkLevels()) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 389 | if (sdkLevel > outFileOp->xmlToFlatten->file.config.sdkVersion) { |
| 390 | if (!shouldGenerateVersionedResource(entry, outFileOp->xmlToFlatten->file.config, |
| 391 | sdkLevel)) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 392 | // If we shouldn't generate a versioned resource, stop checking. |
| 393 | break; |
| 394 | } |
| 395 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 396 | ResourceFile versionedFileDesc = outFileOp->xmlToFlatten->file; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 397 | versionedFileDesc.config.sdkVersion = (uint16_t) sdkLevel; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 398 | |
| 399 | if (mContext->verbose()) { |
| 400 | mContext->getDiagnostics()->note(DiagMessage(versionedFileDesc.source) |
| 401 | << "auto-versioning resource from config '" |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 402 | << outFileOp->xmlToFlatten->file.config |
| 403 | << "' -> '" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 404 | << versionedFileDesc.config << "'"); |
| 405 | } |
| 406 | |
| 407 | std::u16string genPath = util::utf8ToUtf16(ResourceUtils::buildResourceFileName( |
| 408 | versionedFileDesc, mContext->getNameMangler())); |
| 409 | |
| 410 | bool added = table->addFileReferenceAllowMangled(versionedFileDesc.name, |
| 411 | versionedFileDesc.config, |
| 412 | versionedFileDesc.source, |
| 413 | genPath, |
| 414 | file, |
| 415 | mContext->getDiagnostics()); |
| 416 | if (!added) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 417 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 418 | } |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | } |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 423 | return true; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Do not insert or remove any resources while executing in this function. It will |
| 428 | * corrupt the iteration order. |
| 429 | */ |
| 430 | bool ResourceFileFlattener::flatten(ResourceTable* table, IArchiveWriter* archiveWriter) { |
| 431 | bool error = false; |
| 432 | std::map<std::pair<ConfigDescription, StringPiece16>, FileOperation> configSortedFiles; |
| 433 | |
| 434 | for (auto& pkg : table->packages) { |
| 435 | for (auto& type : pkg->types) { |
| 436 | // Sort by config and name, so that we get better locality in the zip file. |
| 437 | configSortedFiles.clear(); |
| 438 | for (auto& entry : type->entries) { |
| 439 | // Iterate via indices because auto generated values can be inserted ahead of |
| 440 | // the value being processed. |
| 441 | for (size_t i = 0; i < entry->values.size(); i++) { |
| 442 | ResourceConfigValue* configValue = entry->values[i].get(); |
| 443 | |
| 444 | FileReference* fileRef = valueCast<FileReference>(configValue->value.get()); |
| 445 | if (!fileRef) { |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | io::IFile* file = fileRef->file; |
| 450 | if (!file) { |
| 451 | mContext->getDiagnostics()->error(DiagMessage(fileRef->getSource()) |
| 452 | << "file not found"); |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | FileOperation fileOp; |
| 457 | fileOp.dstPath = util::utf16ToUtf8(*fileRef->path); |
| 458 | |
| 459 | const StringPiece srcPath = file->getSource().path; |
| 460 | if (type->type != ResourceType::kRaw && |
| 461 | (util::stringEndsWith<char>(srcPath, ".xml.flat") || |
| 462 | util::stringEndsWith<char>(srcPath, ".xml"))) { |
| 463 | ResourceFile fileDesc; |
| 464 | fileDesc.config = configValue->config; |
| 465 | fileDesc.name = ResourceName(pkg->name, type->type, entry->name); |
| 466 | fileDesc.source = fileRef->getSource(); |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 467 | if (!linkAndVersionXmlFile(entry.get(), fileDesc, file, table, &fileOp)) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 468 | error = true; |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | } else { |
| 473 | fileOp.fileToCopy = file; |
| 474 | } |
| 475 | |
| 476 | // NOTE(adamlesinski): Explicitly construct a StringPiece16 here, or else |
| 477 | // we end up copying the string in the std::make_pair() method, then creating |
| 478 | // a StringPiece16 from the copy, which would cause us to end up referencing |
| 479 | // garbage in the map. |
| 480 | const StringPiece16 entryName(entry->name); |
| 481 | configSortedFiles[std::make_pair(configValue->config, entryName)] = |
| 482 | std::move(fileOp); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (error) { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | // Now flatten the sorted values. |
| 491 | for (auto& mapEntry : configSortedFiles) { |
| 492 | const ConfigDescription& config = mapEntry.first.first; |
| 493 | const FileOperation& fileOp = mapEntry.second; |
| 494 | |
| 495 | if (fileOp.xmlToFlatten) { |
| 496 | Maybe<size_t> maxSdkLevel; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 497 | if (!mOptions.noAutoVersion && !fileOp.skipVersion) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 498 | maxSdkLevel = std::max<size_t>(config.sdkVersion, 1u); |
| 499 | } |
| 500 | |
| 501 | bool result = flattenXml(fileOp.xmlToFlatten.get(), fileOp.dstPath, maxSdkLevel, |
| 502 | mOptions.keepRawValues, |
| 503 | archiveWriter, mContext); |
| 504 | if (!result) { |
| 505 | error = true; |
| 506 | } |
| 507 | } else { |
| 508 | bool result = copyFileToArchive(fileOp.fileToCopy, fileOp.dstPath, |
| 509 | getCompressionFlags(fileOp.dstPath), |
| 510 | archiveWriter, mContext); |
| 511 | if (!result) { |
| 512 | error = true; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return !error; |
| 519 | } |
| 520 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 521 | class LinkCommand { |
| 522 | public: |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 523 | LinkCommand(LinkContext* context, const LinkOptions& options) : |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 524 | mOptions(options), mContext(context), mFinalTable(), |
| 525 | mFileCollection(util::make_unique<io::FileCollection>()) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 526 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 527 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 528 | /** |
| 529 | * Creates a SymbolTable that loads symbols from the various APKs and caches the |
| 530 | * results for faster lookup. |
| 531 | */ |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 532 | bool loadSymbolsFromIncludePaths() { |
| 533 | std::unique_ptr<AssetManagerSymbolSource> assetSource = |
| 534 | util::make_unique<AssetManagerSymbolSource>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 535 | for (const std::string& path : mOptions.includePaths) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 536 | if (mContext->verbose()) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 537 | mContext->getDiagnostics()->note(DiagMessage(path) << "loading include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 540 | // First try to load the file as a static lib. |
| 541 | std::string errorStr; |
| 542 | std::unique_ptr<ResourceTable> staticInclude = loadStaticLibrary(path, &errorStr); |
| 543 | if (staticInclude) { |
| 544 | if (!mOptions.staticLib) { |
| 545 | // Can't include static libraries when not building a static library. |
| 546 | mContext->getDiagnostics()->error( |
| 547 | DiagMessage(path) << "can't include static library when building app"); |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | // If we are using --no-static-lib-packages, we need to rename the package of this |
| 552 | // table to our compilation package. |
| 553 | if (mOptions.noStaticLibPackages) { |
| 554 | if (ResourceTablePackage* pkg = staticInclude->findPackageById(0x7f)) { |
| 555 | pkg->name = mContext->getCompilationPackage(); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | mContext->getExternalSymbols()->appendSource( |
| 560 | util::make_unique<ResourceTableSymbolSource>(staticInclude.get())); |
| 561 | |
| 562 | mStaticTableIncludes.push_back(std::move(staticInclude)); |
| 563 | |
| 564 | } else if (!errorStr.empty()) { |
| 565 | // We had an error with reading, so fail. |
| 566 | mContext->getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | if (!assetSource->addAssetPath(path)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 571 | mContext->getDiagnostics()->error( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 572 | DiagMessage(path) << "failed to load include path"); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 573 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 574 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 575 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 576 | |
| 577 | mContext->getExternalSymbols()->appendSource(std::move(assetSource)); |
| 578 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 581 | Maybe<AppInfo> extractAppInfoFromManifest(xml::XmlResource* xmlRes) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 582 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 583 | if (xml::Element* manifestEl = xml::findRootElement(xmlRes->root.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 584 | if (manifestEl->namespaceUri.empty() && manifestEl->name == u"manifest") { |
| 585 | if (xml::Attribute* packageAttr = manifestEl->findAttribute({}, u"package")) { |
| 586 | return AppInfo{ packageAttr->value }; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | return {}; |
| 591 | } |
| 592 | |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 593 | /** |
| 594 | * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked. |
| 595 | * Postcondition: ResourceTable has only one package left. All others are stripped, or there |
| 596 | * is an error and false is returned. |
| 597 | */ |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 598 | bool verifyNoExternalPackages() { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 599 | auto isExtPackageFunc = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 600 | return mContext->getCompilationPackage() != pkg->name || |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 601 | !pkg->id || |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 602 | pkg->id.value() != mContext->getPackageId(); |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 603 | }; |
| 604 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 605 | bool error = false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 606 | for (const auto& package : mFinalTable.packages) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 607 | if (isExtPackageFunc(package)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 608 | // We have a package that is not related to the one we're building! |
| 609 | for (const auto& type : package->types) { |
| 610 | for (const auto& entry : type->entries) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 611 | ResourceNameRef resName(package->name, type->type, entry->name); |
| 612 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 613 | for (const auto& configValue : entry->values) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 614 | // Special case the occurrence of an ID that is being generated for the |
| 615 | // 'android' package. This is due to legacy reasons. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 616 | if (valueCast<Id>(configValue->value.get()) && |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 617 | package->name == u"android") { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 618 | mContext->getDiagnostics()->warn( |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 619 | DiagMessage(configValue->value->getSource()) |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 620 | << "generated id '" << resName |
| 621 | << "' for external package '" << package->name |
| 622 | << "'"); |
| 623 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 624 | mContext->getDiagnostics()->error( |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 625 | DiagMessage(configValue->value->getSource()) |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 626 | << "defined resource '" << resName |
| 627 | << "' for external package '" << package->name |
| 628 | << "'"); |
| 629 | error = true; |
| 630 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 636 | |
| 637 | auto newEndIter = std::remove_if(mFinalTable.packages.begin(), mFinalTable.packages.end(), |
| 638 | isExtPackageFunc); |
| 639 | mFinalTable.packages.erase(newEndIter, mFinalTable.packages.end()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 640 | return !error; |
| 641 | } |
| 642 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 643 | /** |
| 644 | * Returns true if no IDs have been set, false otherwise. |
| 645 | */ |
| 646 | bool verifyNoIdsSet() { |
| 647 | for (const auto& package : mFinalTable.packages) { |
| 648 | for (const auto& type : package->types) { |
| 649 | if (type->id) { |
| 650 | mContext->getDiagnostics()->error(DiagMessage() << "type " << type->type |
| 651 | << " has ID " << std::hex |
| 652 | << (int) type->id.value() |
| 653 | << std::dec << " assigned"); |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | for (const auto& entry : type->entries) { |
| 658 | if (entry->id) { |
| 659 | ResourceNameRef resName(package->name, type->type, entry->name); |
| 660 | mContext->getDiagnostics()->error(DiagMessage() << "entry " << resName |
| 661 | << " has ID " << std::hex |
| 662 | << (int) entry->id.value() |
| 663 | << std::dec << " assigned"); |
| 664 | return false; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | return true; |
| 670 | } |
| 671 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 672 | std::unique_ptr<IArchiveWriter> makeArchiveWriter() { |
| 673 | if (mOptions.outputToDirectory) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 674 | return createDirectoryArchiveWriter(mContext->getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 675 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 676 | return createZipFileArchiveWriter(mContext->getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | bool flattenTable(ResourceTable* table, IArchiveWriter* writer) { |
| 681 | BigBuffer buffer(1024); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 682 | TableFlattener flattener(&buffer); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 683 | if (!flattener.consume(mContext, table)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 684 | return false; |
| 685 | } |
| 686 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 687 | if (writer->startEntry("resources.arsc", ArchiveEntry::kAlign)) { |
| 688 | if (writer->writeEntry(buffer)) { |
| 689 | if (writer->finishEntry()) { |
| 690 | return true; |
| 691 | } |
| 692 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 693 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 694 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 695 | mContext->getDiagnostics()->error( |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 696 | DiagMessage() << "failed to write resources.arsc to archive"); |
| 697 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 700 | bool flattenTableToPb(ResourceTable* table, IArchiveWriter* writer) { |
| 701 | // Create the file/zip entry. |
| 702 | if (!writer->startEntry("resources.arsc.flat", 0)) { |
| 703 | mContext->getDiagnostics()->error(DiagMessage() << "failed to open"); |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | std::unique_ptr<pb::ResourceTable> pbTable = serializeTableToPb(table); |
| 708 | |
| 709 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream |
| 710 | // interface. |
| 711 | { |
| 712 | google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer); |
| 713 | |
| 714 | if (!pbTable->SerializeToZeroCopyStream(&adaptor)) { |
| 715 | mContext->getDiagnostics()->error(DiagMessage() << "failed to write"); |
| 716 | return false; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (!writer->finishEntry()) { |
| 721 | mContext->getDiagnostics()->error(DiagMessage() << "failed to finish entry"); |
| 722 | return false; |
| 723 | } |
| 724 | return true; |
| 725 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 726 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 727 | bool writeJavaFile(ResourceTable* table, const StringPiece16& packageNameToGenerate, |
| 728 | const StringPiece16& outPackage, JavaClassGeneratorOptions javaOptions) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 729 | if (!mOptions.generateJavaClassPath) { |
| 730 | return true; |
| 731 | } |
| 732 | |
| 733 | std::string outPath = mOptions.generateJavaClassPath.value(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 734 | file::appendPath(&outPath, file::packageToPath(util::utf16ToUtf8(outPackage))); |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 735 | if (!file::mkdirs(outPath)) { |
| 736 | mContext->getDiagnostics()->error( |
| 737 | DiagMessage() << "failed to create directory '" << outPath << "'"); |
| 738 | return false; |
| 739 | } |
| 740 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 741 | file::appendPath(&outPath, "R.java"); |
| 742 | |
| 743 | std::ofstream fout(outPath, std::ofstream::binary); |
| 744 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 745 | mContext->getDiagnostics()->error( |
| 746 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 747 | return false; |
| 748 | } |
| 749 | |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 750 | JavaClassGenerator generator(mContext, table, javaOptions); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 751 | if (!generator.generate(packageNameToGenerate, outPackage, &fout)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 752 | mContext->getDiagnostics()->error(DiagMessage(outPath) << generator.getError()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 753 | return false; |
| 754 | } |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 755 | |
| 756 | if (!fout) { |
| 757 | mContext->getDiagnostics()->error( |
| 758 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
| 759 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 760 | return true; |
| 761 | } |
| 762 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 763 | bool writeManifestJavaFile(xml::XmlResource* manifestXml) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 764 | if (!mOptions.generateJavaClassPath) { |
| 765 | return true; |
| 766 | } |
| 767 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 768 | std::unique_ptr<ClassDefinition> manifestClass = generateManifestClass( |
| 769 | mContext->getDiagnostics(), manifestXml); |
| 770 | |
| 771 | if (!manifestClass) { |
| 772 | // Something bad happened, but we already logged it, so exit. |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | if (manifestClass->empty()) { |
| 777 | // Empty Manifest class, no need to generate it. |
| 778 | return true; |
| 779 | } |
| 780 | |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 781 | // Add any JavaDoc annotations to the generated class. |
| 782 | for (const std::string& annotation : mOptions.javadocAnnotations) { |
| 783 | std::string properAnnotation = "@"; |
| 784 | properAnnotation += annotation; |
| 785 | manifestClass->getCommentBuilder()->appendComment(properAnnotation); |
| 786 | } |
| 787 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 788 | const std::string packageUtf8 = util::utf16ToUtf8(mContext->getCompilationPackage()); |
| 789 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 790 | std::string outPath = mOptions.generateJavaClassPath.value(); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 791 | file::appendPath(&outPath, file::packageToPath(packageUtf8)); |
| 792 | |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 793 | if (!file::mkdirs(outPath)) { |
| 794 | mContext->getDiagnostics()->error( |
| 795 | DiagMessage() << "failed to create directory '" << outPath << "'"); |
| 796 | return false; |
| 797 | } |
| 798 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 799 | file::appendPath(&outPath, "Manifest.java"); |
| 800 | |
| 801 | std::ofstream fout(outPath, std::ofstream::binary); |
| 802 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 803 | mContext->getDiagnostics()->error( |
| 804 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 805 | return false; |
| 806 | } |
| 807 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 808 | if (!ClassDefinition::writeJavaFile(manifestClass.get(), packageUtf8, true, &fout)) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 809 | mContext->getDiagnostics()->error( |
| 810 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 811 | return false; |
| 812 | } |
| 813 | return true; |
| 814 | } |
| 815 | |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 816 | bool writeProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keepSet) { |
| 817 | if (!out) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 818 | return true; |
| 819 | } |
| 820 | |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 821 | const std::string& outPath = out.value(); |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 822 | std::ofstream fout(outPath, std::ofstream::binary); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 823 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 824 | mContext->getDiagnostics()->error( |
| 825 | DiagMessage() << "failed to open '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 826 | return false; |
| 827 | } |
| 828 | |
| 829 | proguard::writeKeepSet(&fout, keepSet); |
| 830 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 831 | mContext->getDiagnostics()->error( |
| 832 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 833 | return false; |
| 834 | } |
| 835 | return true; |
| 836 | } |
| 837 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 838 | std::unique_ptr<ResourceTable> loadStaticLibrary(const std::string& input, |
| 839 | std::string* outError) { |
| 840 | std::unique_ptr<io::ZipFileCollection> collection = io::ZipFileCollection::create( |
| 841 | input, outError); |
| 842 | if (!collection) { |
| 843 | return {}; |
| 844 | } |
| 845 | return loadTablePbFromCollection(collection.get()); |
| 846 | } |
| 847 | |
| 848 | std::unique_ptr<ResourceTable> loadTablePbFromCollection(io::IFileCollection* collection) { |
| 849 | io::IFile* file = collection->findFile("resources.arsc.flat"); |
| 850 | if (!file) { |
| 851 | return {}; |
| 852 | } |
| 853 | |
| 854 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 855 | return loadTableFromPb(file->getSource(), data->data(), data->size(), |
| 856 | mContext->getDiagnostics()); |
| 857 | } |
| 858 | |
| 859 | bool mergeStaticLibrary(const std::string& input, bool override) { |
| 860 | if (mContext->verbose()) { |
| 861 | mContext->getDiagnostics()->note(DiagMessage() << "merging static library " << input); |
| 862 | } |
| 863 | |
| 864 | std::string errorStr; |
| 865 | std::unique_ptr<io::ZipFileCollection> collection = |
| 866 | io::ZipFileCollection::create(input, &errorStr); |
| 867 | if (!collection) { |
| 868 | mContext->getDiagnostics()->error(DiagMessage(input) << errorStr); |
| 869 | return false; |
| 870 | } |
| 871 | |
| 872 | std::unique_ptr<ResourceTable> table = loadTablePbFromCollection(collection.get()); |
| 873 | if (!table) { |
| 874 | mContext->getDiagnostics()->error(DiagMessage(input) << "invalid static library"); |
| 875 | return false; |
| 876 | } |
| 877 | |
| 878 | ResourceTablePackage* pkg = table->findPackageById(0x7f); |
| 879 | if (!pkg) { |
| 880 | mContext->getDiagnostics()->error(DiagMessage(input) |
| 881 | << "static library has no package"); |
| 882 | return false; |
| 883 | } |
| 884 | |
| 885 | bool result; |
| 886 | if (mOptions.noStaticLibPackages) { |
| 887 | // Merge all resources as if they were in the compilation package. This is the old |
| 888 | // behaviour of aapt. |
| 889 | |
| 890 | // Add the package to the set of --extra-packages so we emit an R.java for each |
| 891 | // library package. |
| 892 | if (!pkg->name.empty()) { |
| 893 | mOptions.extraJavaPackages.insert(pkg->name); |
| 894 | } |
| 895 | |
| 896 | pkg->name = u""; |
| 897 | if (override) { |
| 898 | result = mTableMerger->mergeOverlay(Source(input), table.get(), collection.get()); |
| 899 | } else { |
| 900 | result = mTableMerger->merge(Source(input), table.get(), collection.get()); |
| 901 | } |
| 902 | |
| 903 | } else { |
| 904 | // This is the proper way to merge libraries, where the package name is preserved |
| 905 | // and resource names are mangled. |
| 906 | result = mTableMerger->mergeAndMangle(Source(input), pkg->name, table.get(), |
| 907 | collection.get()); |
| 908 | } |
| 909 | |
| 910 | if (!result) { |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | // Make sure to move the collection into the set of IFileCollections. |
| 915 | mCollections.push_back(std::move(collection)); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 916 | return true; |
| 917 | } |
| 918 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 919 | bool mergeResourceTable(io::IFile* file, bool override) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 920 | if (mContext->verbose()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 921 | mContext->getDiagnostics()->note(DiagMessage() << "merging resource table " |
| 922 | << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 925 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 926 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 927 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 928 | << "failed to open file"); |
| 929 | return false; |
| 930 | } |
| 931 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 932 | std::unique_ptr<ResourceTable> table = loadTableFromPb(file->getSource(), |
| 933 | data->data(), data->size(), |
| 934 | mContext->getDiagnostics()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 935 | if (!table) { |
| 936 | return false; |
| 937 | } |
| 938 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 939 | bool result = false; |
| 940 | if (override) { |
| 941 | result = mTableMerger->mergeOverlay(file->getSource(), table.get()); |
| 942 | } else { |
| 943 | result = mTableMerger->merge(file->getSource(), table.get()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 944 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 945 | return result; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 948 | bool mergeCompiledFile(io::IFile* file, ResourceFile* fileDesc, bool override) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 949 | if (mContext->verbose()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 950 | mContext->getDiagnostics()->note(DiagMessage() << "merging compiled file " |
| 951 | << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 954 | bool result = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 955 | if (override) { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 956 | result = mTableMerger->mergeFileOverlay(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 957 | } else { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 958 | result = mTableMerger->mergeFile(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 961 | if (!result) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | |
| 965 | // Add the exports of this file to the table. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 966 | for (SourcedResourceName& exportedSymbol : fileDesc->exportedSymbols) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 967 | if (exportedSymbol.name.package.empty()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 968 | exportedSymbol.name.package = mContext->getCompilationPackage(); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | ResourceNameRef resName = exportedSymbol.name; |
| 972 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 973 | Maybe<ResourceName> mangledName = mContext->getNameMangler()->mangleName( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 974 | exportedSymbol.name); |
| 975 | if (mangledName) { |
| 976 | resName = mangledName.value(); |
| 977 | } |
| 978 | |
| 979 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 980 | id->setSource(fileDesc->source.withLine(exportedSymbol.line)); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 981 | bool result = mFinalTable.addResourceAllowMangled( |
| 982 | resName, ConfigDescription::defaultConfig(), std::string(), std::move(id), |
| 983 | mContext->getDiagnostics()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 984 | if (!result) { |
| 985 | return false; |
| 986 | } |
| 987 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 991 | /** |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 992 | * Takes a path to load as a ZIP file and merges the files within into the master ResourceTable. |
| 993 | * If override is true, conflicting resources are allowed to override each other, in order of |
| 994 | * last seen. |
| 995 | * |
| 996 | * An io::IFileCollection is created from the ZIP file and added to the set of |
| 997 | * io::IFileCollections that are open. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 998 | */ |
| 999 | bool mergeArchive(const std::string& input, bool override) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1000 | if (mContext->verbose()) { |
| 1001 | mContext->getDiagnostics()->note(DiagMessage() << "merging archive " << input); |
| 1002 | } |
| 1003 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1004 | std::string errorStr; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1005 | std::unique_ptr<io::ZipFileCollection> collection = |
| 1006 | io::ZipFileCollection::create(input, &errorStr); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1007 | if (!collection) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1008 | mContext->getDiagnostics()->error(DiagMessage(input) << errorStr); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | bool error = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1013 | for (auto iter = collection->iterator(); iter->hasNext(); ) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1014 | if (!mergeFile(iter->next(), override)) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1015 | error = true; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | // Make sure to move the collection into the set of IFileCollections. |
| 1020 | mCollections.push_back(std::move(collection)); |
| 1021 | return !error; |
| 1022 | } |
| 1023 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1024 | /** |
| 1025 | * Takes a path to load and merge into the master ResourceTable. If override is true, |
| 1026 | * conflicting resources are allowed to override each other, in order of last seen. |
| 1027 | * |
| 1028 | * If the file path ends with .flata, .jar, .jack, or .zip the file is treated as ZIP archive |
| 1029 | * and the files within are merged individually. |
| 1030 | * |
| 1031 | * Otherwise the files is processed on its own. |
| 1032 | */ |
| 1033 | bool mergePath(const std::string& path, bool override) { |
Adam Lesinski | 656a577 | 2016-01-14 15:17:41 -0800 | [diff] [blame] | 1034 | if (util::stringEndsWith<char>(path, ".flata") || |
| 1035 | util::stringEndsWith<char>(path, ".jar") || |
| 1036 | util::stringEndsWith<char>(path, ".jack") || |
| 1037 | util::stringEndsWith<char>(path, ".zip")) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1038 | return mergeArchive(path, override); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1039 | } else if (util::stringEndsWith<char>(path, ".apk")) { |
| 1040 | return mergeStaticLibrary(path, override); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | io::IFile* file = mFileCollection->insertFile(path); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1044 | return mergeFile(file, override); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1047 | /** |
| 1048 | * Takes a file to load and merge into the master ResourceTable. If override is true, |
| 1049 | * conflicting resources are allowed to override each other, in order of last seen. |
| 1050 | * |
| 1051 | * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and merged into the |
| 1052 | * master ResourceTable. If the file ends with .flat, then it is treated like a compiled file |
| 1053 | * and the header data is read and merged into the final ResourceTable. |
| 1054 | * |
| 1055 | * All other file types are ignored. This is because these files could be coming from a zip, |
| 1056 | * where we could have other files like classes.dex. |
| 1057 | */ |
| 1058 | bool mergeFile(io::IFile* file, bool override) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1059 | const Source& src = file->getSource(); |
| 1060 | if (util::stringEndsWith<char>(src.path, ".arsc.flat")) { |
| 1061 | return mergeResourceTable(file, override); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1062 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1063 | } else if (util::stringEndsWith<char>(src.path, ".flat")){ |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1064 | // Try opening the file and looking for an Export header. |
| 1065 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 1066 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1067 | mContext->getDiagnostics()->error(DiagMessage(src) << "failed to open"); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | std::unique_ptr<ResourceFile> resourceFile = loadFileExportHeader( |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1072 | src, data->data(), data->size(), mContext->getDiagnostics()); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1073 | if (resourceFile) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1074 | return mergeCompiledFile(file, resourceFile.get(), override); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1075 | } |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 1076 | return false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1077 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1078 | |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 1079 | // Ignore non .flat files. This could be classes.dex or something else that happens |
| 1080 | // to be in an archive. |
| 1081 | return true; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1084 | int run(const std::vector<std::string>& inputFiles) { |
| 1085 | // Load the AndroidManifest.xml |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1086 | std::unique_ptr<xml::XmlResource> manifestXml = loadXml(mOptions.manifestPath, |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1087 | mContext->getDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1088 | if (!manifestXml) { |
| 1089 | return 1; |
| 1090 | } |
| 1091 | |
| 1092 | if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get())) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1093 | mContext->setCompilationPackage(maybeAppInfo.value().package); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1094 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1095 | mContext->getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1096 | << "no package specified in <manifest> tag"); |
| 1097 | return 1; |
| 1098 | } |
| 1099 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1100 | if (!util::isJavaPackageName(mContext->getCompilationPackage())) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1101 | mContext->getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1102 | << "invalid package name '" |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1103 | << mContext->getCompilationPackage() |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1104 | << "'"); |
| 1105 | return 1; |
| 1106 | } |
| 1107 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1108 | mContext->setNameManglerPolicy(NameManglerPolicy{ mContext->getCompilationPackage() }); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1109 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1110 | if (mContext->getCompilationPackage() == u"android") { |
| 1111 | mContext->setPackageId(0x01); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1112 | } else { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1113 | mContext->setPackageId(0x7f); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1116 | if (!loadSymbolsFromIncludePaths()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1117 | return 1; |
| 1118 | } |
| 1119 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1120 | TableMergerOptions tableMergerOptions; |
| 1121 | tableMergerOptions.autoAddOverlay = mOptions.autoAddOverlay; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1122 | mTableMerger = util::make_unique<TableMerger>(mContext, &mFinalTable, tableMergerOptions); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1123 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1124 | if (mContext->verbose()) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1125 | mContext->getDiagnostics()->note( |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1126 | DiagMessage() << "linking package '" << mContext->getCompilationPackage() |
| 1127 | << "' with package ID " << std::hex |
| 1128 | << (int) mContext->getPackageId()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1131 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1132 | for (const std::string& input : inputFiles) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1133 | if (!mergePath(input, false)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1134 | mContext->getDiagnostics()->error(DiagMessage() << "failed parsing input"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1135 | return 1; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1136 | } |
| 1137 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1138 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1139 | for (const std::string& input : mOptions.overlayFiles) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1140 | if (!mergePath(input, true)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1141 | mContext->getDiagnostics()->error(DiagMessage() << "failed parsing overlays"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1142 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1146 | if (!verifyNoExternalPackages()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1147 | return 1; |
| 1148 | } |
| 1149 | |
| 1150 | if (!mOptions.staticLib) { |
| 1151 | PrivateAttributeMover mover; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1152 | if (!mover.consume(mContext, &mFinalTable)) { |
| 1153 | mContext->getDiagnostics()->error( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1154 | DiagMessage() << "failed moving private attributes"); |
| 1155 | return 1; |
| 1156 | } |
| 1157 | } |
| 1158 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1159 | if (!mOptions.staticLib) { |
| 1160 | // Assign IDs if we are building a regular app. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1161 | IdAssigner idAssigner; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1162 | if (!idAssigner.consume(mContext, &mFinalTable)) { |
| 1163 | mContext->getDiagnostics()->error(DiagMessage() << "failed assigning IDs"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1164 | return 1; |
| 1165 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1166 | } else { |
| 1167 | // Static libs are merged with other apps, and ID collisions are bad, so verify that |
| 1168 | // no IDs have been set. |
| 1169 | if (!verifyNoIdsSet()) { |
| 1170 | return 1; |
| 1171 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1174 | // Add the names to mangle based on our source merge earlier. |
| 1175 | mContext->setNameManglerPolicy(NameManglerPolicy{ |
| 1176 | mContext->getCompilationPackage(), mTableMerger->getMergedPackages() }); |
| 1177 | |
| 1178 | // Add our table to the symbol table. |
| 1179 | mContext->getExternalSymbols()->prependSource( |
| 1180 | util::make_unique<ResourceTableSymbolSource>(&mFinalTable)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1181 | |
| 1182 | { |
| 1183 | ReferenceLinker linker; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1184 | if (!linker.consume(mContext, &mFinalTable)) { |
| 1185 | mContext->getDiagnostics()->error(DiagMessage() << "failed linking references"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1186 | return 1; |
| 1187 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1188 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1189 | if (mOptions.staticLib) { |
| 1190 | if (!mOptions.products.empty()) { |
| 1191 | mContext->getDiagnostics()->warn( |
| 1192 | DiagMessage() << "can't select products when building static library"); |
| 1193 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1194 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1195 | if (mOptions.tableSplitterOptions.configFilter != nullptr || |
| 1196 | mOptions.tableSplitterOptions.preferredDensity) { |
| 1197 | mContext->getDiagnostics()->warn( |
| 1198 | DiagMessage() << "can't strip resources when building static library"); |
| 1199 | } |
| 1200 | } else { |
| 1201 | ProductFilter productFilter(mOptions.products); |
| 1202 | if (!productFilter.consume(mContext, &mFinalTable)) { |
| 1203 | mContext->getDiagnostics()->error(DiagMessage() << "failed stripping products"); |
| 1204 | return 1; |
| 1205 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1206 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1207 | // TODO(adamlesinski): Actually pass in split constraints and handle splits at the file |
| 1208 | // level. |
| 1209 | TableSplitter tableSplitter({}, mOptions.tableSplitterOptions); |
| 1210 | if (!tableSplitter.verifySplitConstraints(mContext)) { |
| 1211 | return 1; |
| 1212 | } |
| 1213 | tableSplitter.splitTable(&mFinalTable); |
| 1214 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | proguard::KeepSet proguardKeepSet; |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 1218 | proguard::KeepSet proguardMainDexKeepSet; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1219 | |
| 1220 | std::unique_ptr<IArchiveWriter> archiveWriter = makeArchiveWriter(); |
| 1221 | if (!archiveWriter) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1222 | mContext->getDiagnostics()->error(DiagMessage() << "failed to create archive"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1223 | return 1; |
| 1224 | } |
| 1225 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1226 | bool error = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1227 | { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1228 | ManifestFixer manifestFixer(mOptions.manifestFixerOptions); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1229 | if (!manifestFixer.consume(mContext, manifestXml.get())) { |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1230 | error = true; |
| 1231 | } |
| 1232 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1233 | // AndroidManifest.xml has no resource name, but the CallSite is built from the name |
| 1234 | // (aka, which package the AndroidManifest.xml is coming from). |
| 1235 | // So we give it a package name so it can see local resources. |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1236 | manifestXml->file.name.package = mContext->getCompilationPackage(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1237 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1238 | XmlReferenceLinker manifestLinker; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1239 | if (manifestLinker.consume(mContext, manifestXml.get())) { |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 1240 | if (mOptions.generateProguardRulesPath && |
| 1241 | !proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath), |
| 1242 | manifestXml.get(), |
| 1243 | &proguardKeepSet)) { |
| 1244 | error = true; |
| 1245 | } |
| 1246 | |
| 1247 | if (mOptions.generateMainDexProguardRulesPath && |
| 1248 | !proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath), |
| 1249 | manifestXml.get(), |
| 1250 | &proguardMainDexKeepSet, |
| 1251 | true)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1252 | error = true; |
| 1253 | } |
| 1254 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1255 | if (mOptions.generateJavaClassPath) { |
| 1256 | if (!writeManifestJavaFile(manifestXml.get())) { |
| 1257 | error = true; |
| 1258 | } |
| 1259 | } |
| 1260 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1261 | const bool keepRawValues = mOptions.staticLib; |
| 1262 | bool result = flattenXml(manifestXml.get(), "AndroidManifest.xml", {}, |
| 1263 | keepRawValues, archiveWriter.get(), mContext); |
| 1264 | if (!result) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1265 | error = true; |
| 1266 | } |
| 1267 | } else { |
| 1268 | error = true; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1272 | if (error) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1273 | mContext->getDiagnostics()->error(DiagMessage() << "failed processing manifest"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1274 | return 1; |
| 1275 | } |
| 1276 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1277 | ResourceFileFlattenerOptions fileFlattenerOptions; |
| 1278 | fileFlattenerOptions.keepRawValues = mOptions.staticLib; |
| 1279 | fileFlattenerOptions.doNotCompressAnything = mOptions.doNotCompressAnything; |
| 1280 | fileFlattenerOptions.extensionsToNotCompress = mOptions.extensionsToNotCompress; |
| 1281 | fileFlattenerOptions.noAutoVersion = mOptions.noAutoVersion; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 1282 | fileFlattenerOptions.noVersionVectors = mOptions.noVersionVectors; |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 1283 | fileFlattenerOptions.updateProguardSpec = |
| 1284 | static_cast<bool>(mOptions.generateProguardRulesPath); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1285 | ResourceFileFlattener fileFlattener(fileFlattenerOptions, mContext, &proguardKeepSet); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1286 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1287 | if (!fileFlattener.flatten(&mFinalTable, archiveWriter.get())) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1288 | mContext->getDiagnostics()->error(DiagMessage() << "failed linking file resources"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1289 | return 1; |
| 1290 | } |
| 1291 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 1292 | if (!mOptions.noAutoVersion) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1293 | AutoVersioner versioner; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1294 | if (!versioner.consume(mContext, &mFinalTable)) { |
| 1295 | mContext->getDiagnostics()->error(DiagMessage() << "failed versioning styles"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1296 | return 1; |
| 1297 | } |
| 1298 | } |
| 1299 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1300 | if (mOptions.staticLib) { |
| 1301 | if (!flattenTableToPb(&mFinalTable, archiveWriter.get())) { |
| 1302 | mContext->getDiagnostics()->error(DiagMessage() |
| 1303 | << "failed to write resources.arsc.flat"); |
| 1304 | return 1; |
| 1305 | } |
| 1306 | } else { |
| 1307 | if (!flattenTable(&mFinalTable, archiveWriter.get())) { |
| 1308 | mContext->getDiagnostics()->error(DiagMessage() |
| 1309 | << "failed to write resources.arsc"); |
| 1310 | return 1; |
| 1311 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | if (mOptions.generateJavaClassPath) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1315 | JavaClassGeneratorOptions options; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1316 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 1317 | options.javadocAnnotations = mOptions.javadocAnnotations; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1318 | |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 1319 | if (mOptions.staticLib || mOptions.generateNonFinalIds) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1320 | options.useFinal = false; |
| 1321 | } |
| 1322 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1323 | const StringPiece16 actualPackage = mContext->getCompilationPackage(); |
| 1324 | StringPiece16 outputPackage = mContext->getCompilationPackage(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1325 | if (mOptions.customJavaPackage) { |
| 1326 | // Override the output java package to the custom one. |
| 1327 | outputPackage = mOptions.customJavaPackage.value(); |
| 1328 | } |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1329 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1330 | if (mOptions.privateSymbols) { |
| 1331 | // If we defined a private symbols package, we only emit Public symbols |
| 1332 | // to the original package, and private and public symbols to the private package. |
| 1333 | |
| 1334 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1335 | if (!writeJavaFile(&mFinalTable, mContext->getCompilationPackage(), |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1336 | outputPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1337 | return 1; |
| 1338 | } |
| 1339 | |
| 1340 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1341 | outputPackage = mOptions.privateSymbols.value(); |
| 1342 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1343 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1344 | if (!writeJavaFile(&mFinalTable, actualPackage, outputPackage, options)) { |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1345 | return 1; |
| 1346 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1347 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1348 | for (const std::u16string& extraPackage : mOptions.extraJavaPackages) { |
| 1349 | if (!writeJavaFile(&mFinalTable, actualPackage, extraPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1350 | return 1; |
| 1351 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 1355 | if (!writeProguardFile(mOptions.generateProguardRulesPath, proguardKeepSet)) { |
| 1356 | return 1; |
| 1357 | } |
| 1358 | |
| 1359 | if (!writeProguardFile(mOptions.generateMainDexProguardRulesPath, proguardMainDexKeepSet)) { |
| 1360 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1363 | if (mContext->verbose()) { |
| 1364 | DebugPrintTableOptions debugPrintTableOptions; |
| 1365 | debugPrintTableOptions.showSources = true; |
| 1366 | Debug::printTable(&mFinalTable, debugPrintTableOptions); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1367 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1370 | |
| 1371 | private: |
| 1372 | LinkOptions mOptions; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1373 | LinkContext* mContext; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1374 | ResourceTable mFinalTable; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1375 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1376 | std::unique_ptr<TableMerger> mTableMerger; |
| 1377 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1378 | // A pointer to the FileCollection representing the filesystem (not archives). |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1379 | std::unique_ptr<io::FileCollection> mFileCollection; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1380 | |
| 1381 | // A vector of IFileCollections. This is mainly here to keep ownership of the collections. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1382 | std::vector<std::unique_ptr<io::IFileCollection>> mCollections; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1383 | |
| 1384 | // A vector of ResourceTables. This is here to retain ownership, so that the SymbolTable |
| 1385 | // can use these. |
| 1386 | std::vector<std::unique_ptr<ResourceTable>> mStaticTableIncludes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1387 | }; |
| 1388 | |
| 1389 | int link(const std::vector<StringPiece>& args) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1390 | LinkContext context; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1391 | LinkOptions options; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1392 | Maybe<std::string> privateSymbolsPackage; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1393 | Maybe<std::string> minSdkVersion, targetSdkVersion; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1394 | Maybe<std::string> renameManifestPackage, renameInstrumentationTargetPackage; |
| 1395 | Maybe<std::string> versionCode, versionName; |
| 1396 | Maybe<std::string> customJavaPackage; |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1397 | std::vector<std::string> extraJavaPackages; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1398 | Maybe<std::string> configs; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1399 | Maybe<std::string> preferredDensity; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1400 | Maybe<std::string> productList; |
Adam Lesinski | 8900aa8 | 2016-01-25 22:48:15 -0800 | [diff] [blame] | 1401 | bool legacyXFlag = false; |
| 1402 | bool requireLocalization = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1403 | bool verbose = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1404 | Flags flags = Flags() |
| 1405 | .requiredFlag("-o", "Output path", &options.outputPath) |
| 1406 | .requiredFlag("--manifest", "Path to the Android manifest to build", |
| 1407 | &options.manifestPath) |
| 1408 | .optionalFlagList("-I", "Adds an Android APK to link against", &options.includePaths) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1409 | .optionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n" |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1410 | "The last conflicting resource given takes precedence.", |
| 1411 | &options.overlayFiles) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1412 | .optionalFlag("--java", "Directory in which to generate R.java", |
| 1413 | &options.generateJavaClassPath) |
| 1414 | .optionalFlag("--proguard", "Output file for generated Proguard rules", |
| 1415 | &options.generateProguardRulesPath) |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 1416 | .optionalFlag("--proguard-main-dex", |
| 1417 | "Output file for generated Proguard rules for the main dex", |
| 1418 | &options.generateMainDexProguardRulesPath) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1419 | .optionalSwitch("--no-auto-version", |
| 1420 | "Disables automatic style and layout SDK versioning", |
| 1421 | &options.noAutoVersion) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1422 | .optionalSwitch("--no-version-vectors", |
| 1423 | "Disables automatic versioning of vector drawables. Use this only\n" |
| 1424 | "when building with vector drawable support library", |
| 1425 | &options.noVersionVectors) |
Adam Lesinski | 8900aa8 | 2016-01-25 22:48:15 -0800 | [diff] [blame] | 1426 | .optionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01", |
| 1427 | &legacyXFlag) |
| 1428 | .optionalSwitch("-z", "Require localization of strings marked 'suggested'", |
| 1429 | &requireLocalization) |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1430 | .optionalFlag("-c", "Comma separated list of configurations to include. The default\n" |
| 1431 | "is all configurations", &configs) |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1432 | .optionalFlag("--preferred-density", |
| 1433 | "Selects the closest matching density and strips out all others.", |
| 1434 | &preferredDensity) |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1435 | .optionalFlag("--product", "Comma separated list of product names to keep", |
| 1436 | &productList) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1437 | .optionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified " |
| 1438 | "by -o", |
| 1439 | &options.outputToDirectory) |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1440 | .optionalFlag("--min-sdk-version", "Default minimum SDK version to use for " |
| 1441 | "AndroidManifest.xml", &minSdkVersion) |
| 1442 | .optionalFlag("--target-sdk-version", "Default target SDK version to use for " |
| 1443 | "AndroidManifest.xml", &targetSdkVersion) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1444 | .optionalFlag("--version-code", "Version code (integer) to inject into the " |
| 1445 | "AndroidManifest.xml if none is present", &versionCode) |
| 1446 | .optionalFlag("--version-name", "Version name to inject into the AndroidManifest.xml " |
| 1447 | "if none is present", &versionName) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1448 | .optionalSwitch("--static-lib", "Generate a static Android library", &options.staticLib) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1449 | .optionalSwitch("--no-static-lib-packages", |
| 1450 | "Merge all library resources under the app's package", |
| 1451 | &options.noStaticLibPackages) |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 1452 | .optionalSwitch("--non-final-ids", "Generates R.java without the final modifier.\n" |
| 1453 | "This is implied when --static-lib is specified.", |
| 1454 | &options.generateNonFinalIds) |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1455 | .optionalFlag("--private-symbols", "Package name to use when generating R.java for " |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1456 | "private symbols.\n" |
| 1457 | "If not specified, public and private symbols will use the application's " |
| 1458 | "package name", &privateSymbolsPackage) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1459 | .optionalFlag("--custom-package", "Custom Java package under which to generate R.java", |
| 1460 | &customJavaPackage) |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1461 | .optionalFlagList("--extra-packages", "Generate the same R.java but with different " |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1462 | "package names", &extraJavaPackages) |
Adam Lesinski | 3524a23 | 2016-04-01 19:19:24 -0700 | [diff] [blame] | 1463 | .optionalFlagList("--add-javadoc-annotation", "Adds a JavaDoc annotation to all " |
| 1464 | "generated Java classes", &options.javadocAnnotations) |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1465 | .optionalSwitch("--auto-add-overlay", "Allows the addition of new resources in " |
| 1466 | "overlays without <add-resource> tags", &options.autoAddOverlay) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1467 | .optionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml", |
| 1468 | &renameManifestPackage) |
| 1469 | .optionalFlag("--rename-instrumentation-target-package", |
| 1470 | "Changes the name of the target package for instrumentation. Most useful " |
| 1471 | "when used\nin conjunction with --rename-manifest-package", |
| 1472 | &renameInstrumentationTargetPackage) |
| 1473 | .optionalFlagList("-0", "File extensions not to compress", |
| 1474 | &options.extensionsToNotCompress) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1475 | .optionalSwitch("-v", "Enables verbose logging", &verbose); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1476 | |
| 1477 | if (!flags.parse("aapt2 link", args, &std::cerr)) { |
| 1478 | return 1; |
| 1479 | } |
| 1480 | |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 1481 | // Expand all argument-files passed into the command line. These start with '@'. |
| 1482 | std::vector<std::string> argList; |
| 1483 | for (const std::string& arg : flags.getArgs()) { |
| 1484 | if (util::stringStartsWith<char>(arg, "@")) { |
| 1485 | const std::string path = arg.substr(1, arg.size() - 1); |
| 1486 | std::string error; |
| 1487 | if (!file::appendArgsFromFile(path, &argList, &error)) { |
| 1488 | context.getDiagnostics()->error(DiagMessage(path) << error); |
| 1489 | return 1; |
| 1490 | } |
| 1491 | } else { |
| 1492 | argList.push_back(arg); |
| 1493 | } |
| 1494 | } |
| 1495 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1496 | if (verbose) { |
| 1497 | context.setVerbose(verbose); |
| 1498 | } |
| 1499 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1500 | if (privateSymbolsPackage) { |
| 1501 | options.privateSymbols = util::utf8ToUtf16(privateSymbolsPackage.value()); |
| 1502 | } |
| 1503 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1504 | if (minSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1505 | options.manifestFixerOptions.minSdkVersionDefault = |
| 1506 | util::utf8ToUtf16(minSdkVersion.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | if (targetSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1510 | options.manifestFixerOptions.targetSdkVersionDefault = |
| 1511 | util::utf8ToUtf16(targetSdkVersion.value()); |
| 1512 | } |
| 1513 | |
| 1514 | if (renameManifestPackage) { |
| 1515 | options.manifestFixerOptions.renameManifestPackage = |
| 1516 | util::utf8ToUtf16(renameManifestPackage.value()); |
| 1517 | } |
| 1518 | |
| 1519 | if (renameInstrumentationTargetPackage) { |
| 1520 | options.manifestFixerOptions.renameInstrumentationTargetPackage = |
| 1521 | util::utf8ToUtf16(renameInstrumentationTargetPackage.value()); |
| 1522 | } |
| 1523 | |
| 1524 | if (versionCode) { |
| 1525 | options.manifestFixerOptions.versionCodeDefault = util::utf8ToUtf16(versionCode.value()); |
| 1526 | } |
| 1527 | |
| 1528 | if (versionName) { |
| 1529 | options.manifestFixerOptions.versionNameDefault = util::utf8ToUtf16(versionName.value()); |
| 1530 | } |
| 1531 | |
| 1532 | if (customJavaPackage) { |
| 1533 | options.customJavaPackage = util::utf8ToUtf16(customJavaPackage.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1536 | // Populate the set of extra packages for which to generate R.java. |
| 1537 | for (std::string& extraPackage : extraJavaPackages) { |
| 1538 | // A given package can actually be a colon separated list of packages. |
| 1539 | for (StringPiece package : util::split(extraPackage, ':')) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1540 | options.extraJavaPackages.insert(util::utf8ToUtf16(package)); |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1544 | if (productList) { |
| 1545 | for (StringPiece product : util::tokenize<char>(productList.value(), ',')) { |
| 1546 | if (product != "" && product != "default") { |
| 1547 | options.products.insert(product.toString()); |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1552 | AxisConfigFilter filter; |
| 1553 | if (configs) { |
| 1554 | for (const StringPiece& configStr : util::tokenize<char>(configs.value(), ',')) { |
| 1555 | ConfigDescription config; |
| 1556 | LocaleValue lv; |
| 1557 | if (lv.initFromFilterString(configStr)) { |
| 1558 | lv.writeTo(&config); |
| 1559 | } else if (!ConfigDescription::parse(configStr, &config)) { |
| 1560 | context.getDiagnostics()->error( |
| 1561 | DiagMessage() << "invalid config '" << configStr << "' for -c option"); |
| 1562 | return 1; |
| 1563 | } |
| 1564 | |
| 1565 | if (config.density != 0) { |
| 1566 | context.getDiagnostics()->warn( |
| 1567 | DiagMessage() << "ignoring density '" << config << "' for -c option"); |
| 1568 | } else { |
| 1569 | filter.addConfig(config); |
| 1570 | } |
| 1571 | } |
| 1572 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1573 | options.tableSplitterOptions.configFilter = &filter; |
| 1574 | } |
| 1575 | |
| 1576 | if (preferredDensity) { |
| 1577 | ConfigDescription preferredDensityConfig; |
| 1578 | if (!ConfigDescription::parse(preferredDensity.value(), &preferredDensityConfig)) { |
| 1579 | context.getDiagnostics()->error(DiagMessage() << "invalid density '" |
| 1580 | << preferredDensity.value() |
| 1581 | << "' for --preferred-density option"); |
| 1582 | return 1; |
| 1583 | } |
| 1584 | |
| 1585 | // Clear the version that can be automatically added. |
| 1586 | preferredDensityConfig.sdkVersion = 0; |
| 1587 | |
| 1588 | if (preferredDensityConfig.diff(ConfigDescription::defaultConfig()) |
| 1589 | != ConfigDescription::CONFIG_DENSITY) { |
| 1590 | context.getDiagnostics()->error(DiagMessage() << "invalid preferred density '" |
| 1591 | << preferredDensity.value() << "'. " |
| 1592 | << "Preferred density must only be a density value"); |
| 1593 | return 1; |
| 1594 | } |
| 1595 | options.tableSplitterOptions.preferredDensity = preferredDensityConfig.density; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 1598 | // Turn off auto versioning for static-libs. |
| 1599 | if (options.staticLib) { |
| 1600 | options.noAutoVersion = true; |
| 1601 | options.noVersionVectors = true; |
| 1602 | } |
| 1603 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1604 | LinkCommand cmd(&context, options); |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 1605 | return cmd.run(argList); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | } // namespace aapt |