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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "NameMangler.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "XmlDom.h" |
| 22 | |
| 23 | #include "compile/IdAssigner.h" |
| 24 | #include "flatten/Archive.h" |
| 25 | #include "flatten/TableFlattener.h" |
| 26 | #include "flatten/XmlFlattener.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 27 | #include "java/JavaClassGenerator.h" |
| 28 | #include "java/ManifestClassGenerator.h" |
| 29 | #include "java/ProguardRules.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "link/Linkers.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 31 | #include "link/ManifestFixer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | #include "link/TableMerger.h" |
| 33 | #include "process/IResourceTableConsumer.h" |
| 34 | #include "process/SymbolTable.h" |
| 35 | #include "unflatten/BinaryResourceParser.h" |
| 36 | #include "unflatten/FileExportHeaderReader.h" |
| 37 | #include "util/Files.h" |
| 38 | #include "util/StringPiece.h" |
| 39 | |
| 40 | #include <fstream> |
| 41 | #include <sys/stat.h> |
| 42 | #include <utils/FileMap.h> |
| 43 | #include <vector> |
| 44 | |
| 45 | namespace aapt { |
| 46 | |
| 47 | struct LinkOptions { |
| 48 | std::string outputPath; |
| 49 | std::string manifestPath; |
| 50 | std::vector<std::string> includePaths; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 51 | std::vector<std::string> overlayFiles; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | Maybe<std::string> generateJavaClassPath; |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 53 | std::vector<std::string> extraJavaPackages; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | Maybe<std::string> generateProguardRulesPath; |
| 55 | bool noAutoVersion = false; |
| 56 | bool staticLib = false; |
| 57 | bool verbose = false; |
| 58 | bool outputToDirectory = false; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 59 | Maybe<std::u16string> privateSymbols; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 60 | Maybe<std::u16string> minSdkVersionDefault; |
| 61 | Maybe<std::u16string> targetSdkVersionDefault; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct LinkContext : public IAaptContext { |
| 65 | StdErrDiagnostics mDiagnostics; |
| 66 | std::unique_ptr<NameMangler> mNameMangler; |
| 67 | std::u16string mCompilationPackage; |
| 68 | uint8_t mPackageId; |
| 69 | std::unique_ptr<ISymbolTable> mSymbols; |
| 70 | |
| 71 | IDiagnostics* getDiagnostics() override { |
| 72 | return &mDiagnostics; |
| 73 | } |
| 74 | |
| 75 | NameMangler* getNameMangler() override { |
| 76 | return mNameMangler.get(); |
| 77 | } |
| 78 | |
| 79 | StringPiece16 getCompilationPackage() override { |
| 80 | return mCompilationPackage; |
| 81 | } |
| 82 | |
| 83 | uint8_t getPackageId() override { |
| 84 | return mPackageId; |
| 85 | } |
| 86 | |
| 87 | ISymbolTable* getExternalSymbols() override { |
| 88 | return mSymbols.get(); |
| 89 | } |
| 90 | }; |
| 91 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 92 | class LinkCommand { |
| 93 | public: |
| 94 | LinkCommand(const LinkOptions& options) : |
| 95 | mOptions(options), mContext(), mFinalTable() { |
| 96 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | |
| 98 | std::string buildResourceFileName(const ResourceFile& resFile) { |
| 99 | std::stringstream out; |
| 100 | out << "res/" << resFile.name.type; |
| 101 | if (resFile.config != ConfigDescription{}) { |
| 102 | out << "-" << resFile.config; |
| 103 | } |
| 104 | out << "/"; |
| 105 | |
| 106 | if (mContext.getNameMangler()->shouldMangle(resFile.name.package)) { |
| 107 | out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry); |
| 108 | } else { |
| 109 | out << resFile.name.entry; |
| 110 | } |
| 111 | out << file::getExtension(resFile.source.path); |
| 112 | return out.str(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Creates a SymbolTable that loads symbols from the various APKs and caches the |
| 117 | * results for faster lookup. |
| 118 | */ |
| 119 | std::unique_ptr<ISymbolTable> createSymbolTableFromIncludePaths() { |
| 120 | AssetManagerSymbolTableBuilder builder; |
| 121 | for (const std::string& path : mOptions.includePaths) { |
| 122 | if (mOptions.verbose) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 123 | mContext.getDiagnostics()->note(DiagMessage(path) << "loading include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::unique_ptr<android::AssetManager> assetManager = |
| 127 | util::make_unique<android::AssetManager>(); |
| 128 | int32_t cookie = 0; |
| 129 | if (!assetManager->addAssetPath(android::String8(path.data(), path.size()), &cookie)) { |
| 130 | mContext.getDiagnostics()->error( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 131 | DiagMessage(path) << "failed to load include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 132 | return {}; |
| 133 | } |
| 134 | builder.add(std::move(assetManager)); |
| 135 | } |
| 136 | return builder.build(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Loads the resource table (not inside an apk) at the given path. |
| 141 | */ |
| 142 | std::unique_ptr<ResourceTable> loadTable(const std::string& input) { |
| 143 | std::string errorStr; |
| 144 | Maybe<android::FileMap> map = file::mmapPath(input, &errorStr); |
| 145 | if (!map) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 146 | mContext.getDiagnostics()->error(DiagMessage(input) << errorStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | return {}; |
| 148 | } |
| 149 | |
| 150 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 151 | BinaryResourceParser parser(&mContext, table.get(), Source(input), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | map.value().getDataPtr(), map.value().getDataLength()); |
| 153 | if (!parser.parse()) { |
| 154 | return {}; |
| 155 | } |
| 156 | return table; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Inflates an XML file from the source path. |
| 161 | */ |
| 162 | std::unique_ptr<XmlResource> loadXml(const std::string& path) { |
| 163 | std::ifstream fin(path, std::ifstream::binary); |
| 164 | if (!fin) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 165 | mContext.getDiagnostics()->error(DiagMessage(path) << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 166 | return {}; |
| 167 | } |
| 168 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 169 | return xml::inflate(&fin, mContext.getDiagnostics(), Source(path)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Inflates a binary XML file from the source path. |
| 174 | */ |
| 175 | std::unique_ptr<XmlResource> loadBinaryXmlSkipFileExport(const std::string& path) { |
| 176 | // Read header for symbol info and export info. |
| 177 | std::string errorStr; |
| 178 | Maybe<android::FileMap> maybeF = file::mmapPath(path, &errorStr); |
| 179 | if (!maybeF) { |
| 180 | mContext.getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 181 | return {}; |
| 182 | } |
| 183 | |
| 184 | ssize_t offset = getWrappedDataOffset(maybeF.value().getDataPtr(), |
| 185 | maybeF.value().getDataLength(), &errorStr); |
| 186 | if (offset < 0) { |
| 187 | mContext.getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 188 | return {}; |
| 189 | } |
| 190 | |
| 191 | std::unique_ptr<XmlResource> xmlRes = xml::inflate( |
| 192 | (const uint8_t*) maybeF.value().getDataPtr() + (size_t) offset, |
| 193 | maybeF.value().getDataLength() - offset, |
| 194 | mContext.getDiagnostics(), Source(path)); |
| 195 | if (!xmlRes) { |
| 196 | return {}; |
| 197 | } |
| 198 | return xmlRes; |
| 199 | } |
| 200 | |
| 201 | Maybe<ResourceFile> loadFileExportHeader(const std::string& path) { |
| 202 | // Read header for symbol info and export info. |
| 203 | std::string errorStr; |
| 204 | Maybe<android::FileMap> maybeF = file::mmapPath(path, &errorStr); |
| 205 | if (!maybeF) { |
| 206 | mContext.getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 207 | return {}; |
| 208 | } |
| 209 | |
| 210 | ResourceFile resFile; |
| 211 | ssize_t offset = unwrapFileExportHeader(maybeF.value().getDataPtr(), |
| 212 | maybeF.value().getDataLength(), |
| 213 | &resFile, &errorStr); |
| 214 | if (offset < 0) { |
| 215 | mContext.getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 216 | return {}; |
| 217 | } |
| 218 | return std::move(resFile); |
| 219 | } |
| 220 | |
| 221 | bool copyFileToArchive(const std::string& path, const std::string& outPath, uint32_t flags, |
| 222 | IArchiveWriter* writer) { |
| 223 | std::string errorStr; |
| 224 | Maybe<android::FileMap> maybeF = file::mmapPath(path, &errorStr); |
| 225 | if (!maybeF) { |
| 226 | mContext.getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | ssize_t offset = getWrappedDataOffset(maybeF.value().getDataPtr(), |
| 231 | maybeF.value().getDataLength(), |
| 232 | &errorStr); |
| 233 | if (offset < 0) { |
| 234 | mContext.getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | ArchiveEntry* entry = writer->writeEntry(outPath, flags, &maybeF.value(), |
| 239 | offset, maybeF.value().getDataLength() - offset); |
| 240 | if (!entry) { |
| 241 | mContext.getDiagnostics()->error( |
| 242 | DiagMessage(mOptions.outputPath) << "failed to write file " << outPath); |
| 243 | return false; |
| 244 | } |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | Maybe<AppInfo> extractAppInfoFromManifest(XmlResource* xmlRes) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 249 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 250 | if (xml::Element* manifestEl = xml::findRootElement(xmlRes->root.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 251 | if (manifestEl->namespaceUri.empty() && manifestEl->name == u"manifest") { |
| 252 | if (xml::Attribute* packageAttr = manifestEl->findAttribute({}, u"package")) { |
| 253 | return AppInfo{ packageAttr->value }; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return {}; |
| 258 | } |
| 259 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 260 | bool verifyNoExternalPackages() { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 261 | bool error = false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 262 | for (const auto& package : mFinalTable.packages) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 263 | if (mContext.getCompilationPackage() != package->name || |
| 264 | !package->id || package->id.value() != mContext.getPackageId()) { |
| 265 | // We have a package that is not related to the one we're building! |
| 266 | for (const auto& type : package->types) { |
| 267 | for (const auto& entry : type->entries) { |
| 268 | for (const auto& configValue : entry->values) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 269 | mContext.getDiagnostics()->error( |
| 270 | DiagMessage(configValue.value->getSource()) |
| 271 | << "defined resource '" |
| 272 | << ResourceNameRef(package->name, |
| 273 | type->type, |
| 274 | entry->name) |
| 275 | << "' for external package '" |
| 276 | << package->name << "'"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 277 | error = true; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | return !error; |
| 284 | } |
| 285 | |
| 286 | std::unique_ptr<IArchiveWriter> makeArchiveWriter() { |
| 287 | if (mOptions.outputToDirectory) { |
| 288 | return createDirectoryArchiveWriter(mOptions.outputPath); |
| 289 | } else { |
| 290 | return createZipFileArchiveWriter(mOptions.outputPath); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | bool flattenTable(ResourceTable* table, IArchiveWriter* writer) { |
| 295 | BigBuffer buffer(1024); |
| 296 | TableFlattenerOptions options = {}; |
| 297 | options.useExtendedChunks = mOptions.staticLib; |
| 298 | TableFlattener flattener(&buffer, options); |
| 299 | if (!flattener.consume(&mContext, table)) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | ArchiveEntry* entry = writer->writeEntry("resources.arsc", ArchiveEntry::kAlign, buffer); |
| 304 | if (!entry) { |
| 305 | mContext.getDiagnostics()->error( |
| 306 | DiagMessage() << "failed to write resources.arsc to archive"); |
| 307 | return false; |
| 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | bool flattenXml(XmlResource* xmlRes, const StringPiece& path, Maybe<size_t> maxSdkLevel, |
| 313 | IArchiveWriter* writer) { |
| 314 | BigBuffer buffer(1024); |
| 315 | XmlFlattenerOptions options = {}; |
| 316 | options.keepRawValues = mOptions.staticLib; |
| 317 | options.maxSdkLevel = maxSdkLevel; |
| 318 | XmlFlattener flattener(&buffer, options); |
| 319 | if (!flattener.consume(&mContext, xmlRes)) { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | ArchiveEntry* entry = writer->writeEntry(path, ArchiveEntry::kCompress, buffer); |
| 324 | if (!entry) { |
| 325 | mContext.getDiagnostics()->error( |
| 326 | DiagMessage() << "failed to write " << path << " to archive"); |
| 327 | return false; |
| 328 | } |
| 329 | return true; |
| 330 | } |
| 331 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 332 | bool writeJavaFile(ResourceTable* table, const StringPiece16& packageNameToGenerate, |
| 333 | const StringPiece16& outPackage, JavaClassGeneratorOptions javaOptions) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 334 | if (!mOptions.generateJavaClassPath) { |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | std::string outPath = mOptions.generateJavaClassPath.value(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 339 | file::appendPath(&outPath, file::packageToPath(util::utf16ToUtf8(outPackage))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 340 | file::mkdirs(outPath); |
| 341 | file::appendPath(&outPath, "R.java"); |
| 342 | |
| 343 | std::ofstream fout(outPath, std::ofstream::binary); |
| 344 | if (!fout) { |
| 345 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 346 | return false; |
| 347 | } |
| 348 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 349 | JavaClassGenerator generator(table, javaOptions); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 350 | if (!generator.generate(packageNameToGenerate, outPackage, &fout)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 351 | mContext.getDiagnostics()->error(DiagMessage(outPath) << generator.getError()); |
| 352 | return false; |
| 353 | } |
| 354 | return true; |
| 355 | } |
| 356 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 357 | bool writeManifestJavaFile(XmlResource* manifestXml) { |
| 358 | if (!mOptions.generateJavaClassPath) { |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | std::string outPath = mOptions.generateJavaClassPath.value(); |
| 363 | file::appendPath(&outPath, |
| 364 | file::packageToPath(util::utf16ToUtf8(mContext.getCompilationPackage()))); |
| 365 | file::mkdirs(outPath); |
| 366 | file::appendPath(&outPath, "Manifest.java"); |
| 367 | |
| 368 | std::ofstream fout(outPath, std::ofstream::binary); |
| 369 | if (!fout) { |
| 370 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | ManifestClassGenerator generator; |
| 375 | if (!generator.generate(mContext.getDiagnostics(), mContext.getCompilationPackage(), |
| 376 | manifestXml, &fout)) { |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | if (!fout) { |
| 381 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 382 | return false; |
| 383 | } |
| 384 | return true; |
| 385 | } |
| 386 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 387 | bool writeProguardFile(const proguard::KeepSet& keepSet) { |
| 388 | if (!mOptions.generateProguardRulesPath) { |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | std::ofstream fout(mOptions.generateProguardRulesPath.value(), std::ofstream::binary); |
| 393 | if (!fout) { |
| 394 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | proguard::writeKeepSet(&fout, keepSet); |
| 399 | if (!fout) { |
| 400 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 401 | return false; |
| 402 | } |
| 403 | return true; |
| 404 | } |
| 405 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 406 | bool mergeStaticLibrary(const std::string& input) { |
| 407 | // TODO(adamlesinski): Load resources from a static library APK and merge the table into |
| 408 | // TableMerger. |
| 409 | mContext.getDiagnostics()->warn(DiagMessage() |
| 410 | << "linking static libraries not supported yet: " |
| 411 | << input); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | bool mergeResourceTable(const std::string& input, bool override) { |
| 416 | if (mOptions.verbose) { |
| 417 | mContext.getDiagnostics()->note(DiagMessage() << "linking " << input); |
| 418 | } |
| 419 | |
| 420 | std::unique_ptr<ResourceTable> table = loadTable(input); |
| 421 | if (!table) { |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | if (!mTableMerger->merge(Source(input), table.get(), override)) { |
| 426 | return false; |
| 427 | } |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | bool mergeCompiledFile(const std::string& input, ResourceFile&& file, bool override) { |
| 432 | if (file.name.package.empty()) { |
| 433 | file.name.package = mContext.getCompilationPackage().toString(); |
| 434 | } |
| 435 | |
| 436 | ResourceNameRef resName = file.name; |
| 437 | |
| 438 | Maybe<ResourceName> mangledName = mContext.getNameMangler()->mangleName(file.name); |
| 439 | if (mangledName) { |
| 440 | resName = mangledName.value(); |
| 441 | } |
| 442 | |
| 443 | std::function<int(Value*,Value*)> resolver; |
| 444 | if (override) { |
| 445 | resolver = [](Value* a, Value* b) -> int { |
| 446 | int result = ResourceTable::resolveValueCollision(a, b); |
| 447 | if (result == 0) { |
| 448 | // Always accept the new value if it would normally conflict (override). |
| 449 | result = 1; |
| 450 | } |
| 451 | return result; |
| 452 | }; |
| 453 | } else { |
| 454 | // Otherwise use the default resolution. |
| 455 | resolver = ResourceTable::resolveValueCollision; |
| 456 | } |
| 457 | |
| 458 | // Add this file to the table. |
| 459 | if (!mFinalTable.addFileReference(resName, file.config, file.source, |
| 460 | util::utf8ToUtf16(buildResourceFileName(file)), |
| 461 | resolver, mContext.getDiagnostics())) { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | // Add the exports of this file to the table. |
| 466 | for (SourcedResourceName& exportedSymbol : file.exportedSymbols) { |
| 467 | if (exportedSymbol.name.package.empty()) { |
| 468 | exportedSymbol.name.package = mContext.getCompilationPackage().toString(); |
| 469 | } |
| 470 | |
| 471 | ResourceNameRef resName = exportedSymbol.name; |
| 472 | |
| 473 | Maybe<ResourceName> mangledName = mContext.getNameMangler()->mangleName( |
| 474 | exportedSymbol.name); |
| 475 | if (mangledName) { |
| 476 | resName = mangledName.value(); |
| 477 | } |
| 478 | |
| 479 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
| 480 | id->setSource(file.source.withLine(exportedSymbol.line)); |
| 481 | bool result = mFinalTable.addResourceAllowMangled(resName, {}, std::move(id), |
| 482 | mContext.getDiagnostics()); |
| 483 | if (!result) { |
| 484 | return false; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | mFilesToProcess[resName.toResourceName()] = FileToProcess{ Source(input), std::move(file) }; |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | bool processFile(const std::string& input, bool override) { |
| 493 | if (util::stringEndsWith<char>(input, ".apk")) { |
| 494 | return mergeStaticLibrary(input); |
| 495 | } else if (util::stringEndsWith<char>(input, ".arsc.flat")) { |
| 496 | return mergeResourceTable(input, override); |
| 497 | } else if (Maybe<ResourceFile> maybeF = loadFileExportHeader(input)) { |
| 498 | return mergeCompiledFile(input, std::move(maybeF.value()), override); |
| 499 | } |
| 500 | return false; |
| 501 | } |
| 502 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 503 | int run(const std::vector<std::string>& inputFiles) { |
| 504 | // Load the AndroidManifest.xml |
| 505 | std::unique_ptr<XmlResource> manifestXml = loadXml(mOptions.manifestPath); |
| 506 | if (!manifestXml) { |
| 507 | return 1; |
| 508 | } |
| 509 | |
| 510 | if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get())) { |
| 511 | mContext.mCompilationPackage = maybeAppInfo.value().package; |
| 512 | } else { |
| 513 | mContext.getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
| 514 | << "no package specified in <manifest> tag"); |
| 515 | return 1; |
| 516 | } |
| 517 | |
| 518 | if (!util::isJavaPackageName(mContext.mCompilationPackage)) { |
| 519 | mContext.getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
| 520 | << "invalid package name '" |
| 521 | << mContext.mCompilationPackage |
| 522 | << "'"); |
| 523 | return 1; |
| 524 | } |
| 525 | |
| 526 | mContext.mNameMangler = util::make_unique<NameMangler>( |
| 527 | NameManglerPolicy{ mContext.mCompilationPackage }); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 528 | |
| 529 | if (mContext.mCompilationPackage == u"android") { |
| 530 | mContext.mPackageId = 0x01; |
| 531 | } else { |
| 532 | mContext.mPackageId = 0x7f; |
| 533 | } |
| 534 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 535 | mContext.mSymbols = createSymbolTableFromIncludePaths(); |
| 536 | if (!mContext.mSymbols) { |
| 537 | return 1; |
| 538 | } |
| 539 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 540 | mTableMerger = util::make_unique<TableMerger>(&mContext, &mFinalTable); |
| 541 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 542 | if (mOptions.verbose) { |
| 543 | mContext.getDiagnostics()->note( |
| 544 | DiagMessage() << "linking package '" << mContext.mCompilationPackage << "' " |
| 545 | << "with package ID " << std::hex << (int) mContext.mPackageId); |
| 546 | } |
| 547 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 548 | bool error = false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 549 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 550 | for (const std::string& input : inputFiles) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 551 | if (!processFile(input, false)) { |
| 552 | error = true; |
| 553 | } |
| 554 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 555 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 556 | for (const std::string& input : mOptions.overlayFiles) { |
| 557 | if (!processFile(input, true)) { |
| 558 | error = true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | if (error) { |
| 563 | mContext.getDiagnostics()->error(DiagMessage() << "failed parsing input"); |
| 564 | return 1; |
| 565 | } |
| 566 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 567 | if (!verifyNoExternalPackages()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | if (!mOptions.staticLib) { |
| 572 | PrivateAttributeMover mover; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 573 | if (!mover.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 574 | mContext.getDiagnostics()->error( |
| 575 | DiagMessage() << "failed moving private attributes"); |
| 576 | return 1; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | { |
| 581 | IdAssigner idAssigner; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 582 | if (!idAssigner.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 583 | mContext.getDiagnostics()->error(DiagMessage() << "failed assigning IDs"); |
| 584 | return 1; |
| 585 | } |
| 586 | } |
| 587 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 588 | mContext.mNameMangler = util::make_unique<NameMangler>(NameManglerPolicy{ |
| 589 | mContext.mCompilationPackage, mTableMerger->getMergedPackages() }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 590 | mContext.mSymbols = JoinedSymbolTableBuilder() |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 591 | .addSymbolTable(util::make_unique<SymbolTableWrapper>(&mFinalTable)) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 592 | .addSymbolTable(std::move(mContext.mSymbols)) |
| 593 | .build(); |
| 594 | |
| 595 | { |
| 596 | ReferenceLinker linker; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 597 | if (!linker.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 598 | mContext.getDiagnostics()->error(DiagMessage() << "failed linking references"); |
| 599 | return 1; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | proguard::KeepSet proguardKeepSet; |
| 604 | |
| 605 | std::unique_ptr<IArchiveWriter> archiveWriter = makeArchiveWriter(); |
| 606 | if (!archiveWriter) { |
| 607 | mContext.getDiagnostics()->error(DiagMessage() << "failed to create archive"); |
| 608 | return 1; |
| 609 | } |
| 610 | |
| 611 | { |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 612 | ManifestFixerOptions manifestFixerOptions; |
| 613 | manifestFixerOptions.minSdkVersionDefault = mOptions.minSdkVersionDefault; |
| 614 | manifestFixerOptions.targetSdkVersionDefault = mOptions.targetSdkVersionDefault; |
| 615 | ManifestFixer manifestFixer(manifestFixerOptions); |
| 616 | if (!manifestFixer.consume(&mContext, manifestXml.get())) { |
| 617 | error = true; |
| 618 | } |
| 619 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 620 | XmlReferenceLinker manifestLinker; |
| 621 | if (manifestLinker.consume(&mContext, manifestXml.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 622 | if (!proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath), |
| 623 | manifestXml.get(), |
| 624 | &proguardKeepSet)) { |
| 625 | error = true; |
| 626 | } |
| 627 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 628 | if (mOptions.generateJavaClassPath) { |
| 629 | if (!writeManifestJavaFile(manifestXml.get())) { |
| 630 | error = true; |
| 631 | } |
| 632 | } |
| 633 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 634 | if (!flattenXml(manifestXml.get(), "AndroidManifest.xml", {}, |
| 635 | archiveWriter.get())) { |
| 636 | error = true; |
| 637 | } |
| 638 | } else { |
| 639 | error = true; |
| 640 | } |
| 641 | } |
| 642 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 643 | for (auto& pair : mFilesToProcess) { |
| 644 | FileToProcess& file = pair.second; |
| 645 | if (file.file.name.type != ResourceType::kRaw && |
| 646 | util::stringEndsWith<char>(file.source.path, ".xml.flat")) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 647 | if (mOptions.verbose) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 648 | mContext.getDiagnostics()->note(DiagMessage() << "linking " << file.source.path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 651 | std::unique_ptr<XmlResource> xmlRes = loadBinaryXmlSkipFileExport(file.source.path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 652 | if (!xmlRes) { |
| 653 | return 1; |
| 654 | } |
| 655 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 656 | xmlRes->file = std::move(file.file); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 657 | |
| 658 | XmlReferenceLinker xmlLinker; |
| 659 | if (xmlLinker.consume(&mContext, xmlRes.get())) { |
| 660 | if (!proguard::collectProguardRules(xmlRes->file.source, xmlRes.get(), |
| 661 | &proguardKeepSet)) { |
| 662 | error = true; |
| 663 | } |
| 664 | |
| 665 | Maybe<size_t> maxSdkLevel; |
| 666 | if (!mOptions.noAutoVersion) { |
| 667 | maxSdkLevel = std::max<size_t>(xmlRes->file.config.sdkVersion, 1u); |
| 668 | } |
| 669 | |
| 670 | if (!flattenXml(xmlRes.get(), buildResourceFileName(xmlRes->file), maxSdkLevel, |
| 671 | archiveWriter.get())) { |
| 672 | error = true; |
| 673 | } |
| 674 | |
| 675 | if (!mOptions.noAutoVersion) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 676 | Maybe<ResourceTable::SearchResult> result = mFinalTable.findResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 677 | xmlRes->file.name); |
| 678 | for (int sdkLevel : xmlLinker.getSdkLevels()) { |
| 679 | if (sdkLevel > xmlRes->file.config.sdkVersion && |
| 680 | shouldGenerateVersionedResource(result.value().entry, |
| 681 | xmlRes->file.config, |
| 682 | sdkLevel)) { |
| 683 | xmlRes->file.config.sdkVersion = sdkLevel; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 684 | if (!mFinalTable.addFileReference(xmlRes->file.name, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 685 | xmlRes->file.config, |
| 686 | xmlRes->file.source, |
| 687 | util::utf8ToUtf16( |
| 688 | buildResourceFileName(xmlRes->file)), |
| 689 | mContext.getDiagnostics())) { |
| 690 | error = true; |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | if (!flattenXml(xmlRes.get(), buildResourceFileName(xmlRes->file), |
| 695 | sdkLevel, archiveWriter.get())) { |
| 696 | error = true; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | } else { |
| 703 | error = true; |
| 704 | } |
| 705 | } else { |
| 706 | if (mOptions.verbose) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 707 | mContext.getDiagnostics()->note(DiagMessage() << "copying " |
| 708 | << file.source.path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 711 | if (!copyFileToArchive(file.source.path, buildResourceFileName(file.file), 0, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 712 | archiveWriter.get())) { |
| 713 | error = true; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (error) { |
| 719 | mContext.getDiagnostics()->error(DiagMessage() << "failed linking file resources"); |
| 720 | return 1; |
| 721 | } |
| 722 | |
| 723 | if (!mOptions.noAutoVersion) { |
| 724 | AutoVersioner versioner; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 725 | if (!versioner.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 726 | mContext.getDiagnostics()->error(DiagMessage() << "failed versioning styles"); |
| 727 | return 1; |
| 728 | } |
| 729 | } |
| 730 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 731 | if (!flattenTable(&mFinalTable, archiveWriter.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 732 | mContext.getDiagnostics()->error(DiagMessage() << "failed to write resources.arsc"); |
| 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | if (mOptions.generateJavaClassPath) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 737 | JavaClassGeneratorOptions options; |
| 738 | if (mOptions.staticLib) { |
| 739 | options.useFinal = false; |
| 740 | } |
| 741 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 742 | StringPiece16 actualPackage = mContext.getCompilationPackage(); |
| 743 | StringPiece16 outputPackage = mContext.getCompilationPackage(); |
| 744 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 745 | if (mOptions.privateSymbols) { |
| 746 | // If we defined a private symbols package, we only emit Public symbols |
| 747 | // to the original package, and private and public symbols to the private package. |
| 748 | |
| 749 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 750 | if (!writeJavaFile(&mFinalTable, mContext.getCompilationPackage(), |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 751 | mContext.getCompilationPackage(), options)) { |
| 752 | return 1; |
| 753 | } |
| 754 | |
| 755 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 756 | outputPackage = mOptions.privateSymbols.value(); |
| 757 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 758 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 759 | if (!writeJavaFile(&mFinalTable, actualPackage, outputPackage, options)) { |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 760 | return 1; |
| 761 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 762 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 763 | for (std::string& extraPackage : mOptions.extraJavaPackages) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 764 | if (!writeJavaFile(&mFinalTable, actualPackage, util::utf8ToUtf16(extraPackage), |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 765 | options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 766 | return 1; |
| 767 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | if (mOptions.generateProguardRulesPath) { |
| 772 | if (!writeProguardFile(proguardKeepSet)) { |
| 773 | return 1; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if (mOptions.verbose) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 778 | Debug::printTable(&mFinalTable); |
| 779 | for (; !mTableMerger->getFileMergeQueue()->empty(); |
| 780 | mTableMerger->getFileMergeQueue()->pop()) { |
| 781 | const FileToMerge& f = mTableMerger->getFileMergeQueue()->front(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 782 | mContext.getDiagnostics()->note( |
| 783 | DiagMessage() << f.srcPath << " -> " << f.dstPath << " from (0x" |
| 784 | << std::hex << (uintptr_t) f.srcTable << std::dec); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return 0; |
| 789 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 790 | |
| 791 | private: |
| 792 | LinkOptions mOptions; |
| 793 | LinkContext mContext; |
| 794 | ResourceTable mFinalTable; |
| 795 | std::unique_ptr<TableMerger> mTableMerger; |
| 796 | |
| 797 | struct FileToProcess { |
| 798 | Source source; |
| 799 | ResourceFile file; |
| 800 | }; |
| 801 | std::map<ResourceName, FileToProcess> mFilesToProcess; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 802 | }; |
| 803 | |
| 804 | int link(const std::vector<StringPiece>& args) { |
| 805 | LinkOptions options; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 806 | Maybe<std::string> privateSymbolsPackage; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 807 | Maybe<std::string> minSdkVersion, targetSdkVersion; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 808 | Flags flags = Flags() |
| 809 | .requiredFlag("-o", "Output path", &options.outputPath) |
| 810 | .requiredFlag("--manifest", "Path to the Android manifest to build", |
| 811 | &options.manifestPath) |
| 812 | .optionalFlagList("-I", "Adds an Android APK to link against", &options.includePaths) |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 813 | .optionalFlagList("-R", "Compilation unit to link, using `overlay` semantics. " |
| 814 | "The last conflicting resource given takes precedence.", |
| 815 | &options.overlayFiles) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 816 | .optionalFlag("--java", "Directory in which to generate R.java", |
| 817 | &options.generateJavaClassPath) |
| 818 | .optionalFlag("--proguard", "Output file for generated Proguard rules", |
| 819 | &options.generateProguardRulesPath) |
| 820 | .optionalSwitch("--no-auto-version", |
| 821 | "Disables automatic style and layout SDK versioning", |
| 822 | &options.noAutoVersion) |
| 823 | .optionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified " |
| 824 | "by -o", |
| 825 | &options.outputToDirectory) |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 826 | .optionalFlag("--min-sdk-version", "Default minimum SDK version to use for " |
| 827 | "AndroidManifest.xml", &minSdkVersion) |
| 828 | .optionalFlag("--target-sdk-version", "Default target SDK version to use for " |
| 829 | "AndroidManifest.xml", &targetSdkVersion) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 830 | .optionalSwitch("--static-lib", "Generate a static Android library", &options.staticLib) |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 831 | .optionalFlag("--private-symbols", "Package name to use when generating R.java for " |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 832 | "private symbols.\n" |
| 833 | "If not specified, public and private symbols will use the application's " |
| 834 | "package name", &privateSymbolsPackage) |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 835 | .optionalFlagList("--extra-packages", "Generate the same R.java but with different " |
| 836 | "package names", &options.extraJavaPackages) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 837 | .optionalSwitch("-v", "Enables verbose logging", &options.verbose); |
| 838 | |
| 839 | if (!flags.parse("aapt2 link", args, &std::cerr)) { |
| 840 | return 1; |
| 841 | } |
| 842 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 843 | if (privateSymbolsPackage) { |
| 844 | options.privateSymbols = util::utf8ToUtf16(privateSymbolsPackage.value()); |
| 845 | } |
| 846 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 847 | if (minSdkVersion) { |
| 848 | options.minSdkVersionDefault = util::utf8ToUtf16(minSdkVersion.value()); |
| 849 | } |
| 850 | |
| 851 | if (targetSdkVersion) { |
| 852 | options.targetSdkVersionDefault = util::utf8ToUtf16(targetSdkVersion.value()); |
| 853 | } |
| 854 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame^] | 855 | LinkCommand cmd(options); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 856 | return cmd.run(flags.getArgs()); |
| 857 | } |
| 858 | |
| 859 | } // namespace aapt |