blob: b84f2e068dc2704b6ef9bf07c3971bbaa19d2411 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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"
20#include "JavaClassGenerator.h"
21#include "NameMangler.h"
22#include "ProguardRules.h"
23#include "XmlDom.h"
24
25#include "compile/IdAssigner.h"
26#include "flatten/Archive.h"
27#include "flatten/TableFlattener.h"
28#include "flatten/XmlFlattener.h"
29#include "link/Linkers.h"
30#include "link/TableMerger.h"
31#include "process/IResourceTableConsumer.h"
32#include "process/SymbolTable.h"
33#include "unflatten/BinaryResourceParser.h"
34#include "unflatten/FileExportHeaderReader.h"
35#include "util/Files.h"
36#include "util/StringPiece.h"
37
38#include <fstream>
39#include <sys/stat.h>
40#include <utils/FileMap.h>
41#include <vector>
42
43namespace aapt {
44
45struct LinkOptions {
46 std::string outputPath;
47 std::string manifestPath;
48 std::vector<std::string> includePaths;
49 Maybe<std::string> generateJavaClassPath;
50 Maybe<std::string> generateProguardRulesPath;
51 bool noAutoVersion = false;
52 bool staticLib = false;
53 bool verbose = false;
54 bool outputToDirectory = false;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070055 Maybe<std::u16string> privateSymbols;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056};
57
58struct LinkContext : public IAaptContext {
59 StdErrDiagnostics mDiagnostics;
60 std::unique_ptr<NameMangler> mNameMangler;
61 std::u16string mCompilationPackage;
62 uint8_t mPackageId;
63 std::unique_ptr<ISymbolTable> mSymbols;
64
65 IDiagnostics* getDiagnostics() override {
66 return &mDiagnostics;
67 }
68
69 NameMangler* getNameMangler() override {
70 return mNameMangler.get();
71 }
72
73 StringPiece16 getCompilationPackage() override {
74 return mCompilationPackage;
75 }
76
77 uint8_t getPackageId() override {
78 return mPackageId;
79 }
80
81 ISymbolTable* getExternalSymbols() override {
82 return mSymbols.get();
83 }
84};
85
86struct LinkCommand {
87 LinkOptions mOptions;
88 LinkContext mContext;
89
90 std::string buildResourceFileName(const ResourceFile& resFile) {
91 std::stringstream out;
92 out << "res/" << resFile.name.type;
93 if (resFile.config != ConfigDescription{}) {
94 out << "-" << resFile.config;
95 }
96 out << "/";
97
98 if (mContext.getNameMangler()->shouldMangle(resFile.name.package)) {
99 out << NameMangler::mangleEntry(resFile.name.package, resFile.name.entry);
100 } else {
101 out << resFile.name.entry;
102 }
103 out << file::getExtension(resFile.source.path);
104 return out.str();
105 }
106
107 /**
108 * Creates a SymbolTable that loads symbols from the various APKs and caches the
109 * results for faster lookup.
110 */
111 std::unique_ptr<ISymbolTable> createSymbolTableFromIncludePaths() {
112 AssetManagerSymbolTableBuilder builder;
113 for (const std::string& path : mOptions.includePaths) {
114 if (mOptions.verbose) {
115 mContext.getDiagnostics()->note(
116 DiagMessage(Source{ path }) << "loading include path");
117 }
118
119 std::unique_ptr<android::AssetManager> assetManager =
120 util::make_unique<android::AssetManager>();
121 int32_t cookie = 0;
122 if (!assetManager->addAssetPath(android::String8(path.data(), path.size()), &cookie)) {
123 mContext.getDiagnostics()->error(
124 DiagMessage(Source{ path }) << "failed to load include path");
125 return {};
126 }
127 builder.add(std::move(assetManager));
128 }
129 return builder.build();
130 }
131
132 /**
133 * Loads the resource table (not inside an apk) at the given path.
134 */
135 std::unique_ptr<ResourceTable> loadTable(const std::string& input) {
136 std::string errorStr;
137 Maybe<android::FileMap> map = file::mmapPath(input, &errorStr);
138 if (!map) {
139 mContext.getDiagnostics()->error(DiagMessage(Source{ input }) << errorStr);
140 return {};
141 }
142
143 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
144 BinaryResourceParser parser(&mContext, table.get(), Source{ input },
145 map.value().getDataPtr(), map.value().getDataLength());
146 if (!parser.parse()) {
147 return {};
148 }
149 return table;
150 }
151
152 /**
153 * Inflates an XML file from the source path.
154 */
155 std::unique_ptr<XmlResource> loadXml(const std::string& path) {
156 std::ifstream fin(path, std::ifstream::binary);
157 if (!fin) {
158 mContext.getDiagnostics()->error(DiagMessage(Source{ path }) << strerror(errno));
159 return {};
160 }
161
162 return xml::inflate(&fin, mContext.getDiagnostics(), Source{ path });
163 }
164
165 /**
166 * Inflates a binary XML file from the source path.
167 */
168 std::unique_ptr<XmlResource> loadBinaryXmlSkipFileExport(const std::string& path) {
169 // Read header for symbol info and export info.
170 std::string errorStr;
171 Maybe<android::FileMap> maybeF = file::mmapPath(path, &errorStr);
172 if (!maybeF) {
173 mContext.getDiagnostics()->error(DiagMessage(path) << errorStr);
174 return {};
175 }
176
177 ssize_t offset = getWrappedDataOffset(maybeF.value().getDataPtr(),
178 maybeF.value().getDataLength(), &errorStr);
179 if (offset < 0) {
180 mContext.getDiagnostics()->error(DiagMessage(path) << errorStr);
181 return {};
182 }
183
184 std::unique_ptr<XmlResource> xmlRes = xml::inflate(
185 (const uint8_t*) maybeF.value().getDataPtr() + (size_t) offset,
186 maybeF.value().getDataLength() - offset,
187 mContext.getDiagnostics(), Source(path));
188 if (!xmlRes) {
189 return {};
190 }
191 return xmlRes;
192 }
193
194 Maybe<ResourceFile> loadFileExportHeader(const std::string& path) {
195 // Read header for symbol info and export info.
196 std::string errorStr;
197 Maybe<android::FileMap> maybeF = file::mmapPath(path, &errorStr);
198 if (!maybeF) {
199 mContext.getDiagnostics()->error(DiagMessage(path) << errorStr);
200 return {};
201 }
202
203 ResourceFile resFile;
204 ssize_t offset = unwrapFileExportHeader(maybeF.value().getDataPtr(),
205 maybeF.value().getDataLength(),
206 &resFile, &errorStr);
207 if (offset < 0) {
208 mContext.getDiagnostics()->error(DiagMessage(path) << errorStr);
209 return {};
210 }
211 return std::move(resFile);
212 }
213
214 bool copyFileToArchive(const std::string& path, const std::string& outPath, uint32_t flags,
215 IArchiveWriter* writer) {
216 std::string errorStr;
217 Maybe<android::FileMap> maybeF = file::mmapPath(path, &errorStr);
218 if (!maybeF) {
219 mContext.getDiagnostics()->error(DiagMessage(path) << errorStr);
220 return false;
221 }
222
223 ssize_t offset = getWrappedDataOffset(maybeF.value().getDataPtr(),
224 maybeF.value().getDataLength(),
225 &errorStr);
226 if (offset < 0) {
227 mContext.getDiagnostics()->error(DiagMessage(path) << errorStr);
228 return false;
229 }
230
231 ArchiveEntry* entry = writer->writeEntry(outPath, flags, &maybeF.value(),
232 offset, maybeF.value().getDataLength() - offset);
233 if (!entry) {
234 mContext.getDiagnostics()->error(
235 DiagMessage(mOptions.outputPath) << "failed to write file " << outPath);
236 return false;
237 }
238 return true;
239 }
240
241 Maybe<AppInfo> extractAppInfoFromManifest(XmlResource* xmlRes) {
242 xml::Node* node = xmlRes->root.get();
243
244 // Find the first xml::Element.
245 while (node && !xml::nodeCast<xml::Element>(node)) {
246 node = !node->children.empty() ? node->children.front().get() : nullptr;
247 }
248
249 // Make sure the first element is <manifest> with package attribute.
250 if (xml::Element* manifestEl = xml::nodeCast<xml::Element>(node)) {
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
260 bool verifyNoExternalPackages(ResourceTable* table) {
261 bool error = false;
262 for (const auto& package : table->packages) {
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) {
269 mContext.getDiagnostics()->error(DiagMessage(configValue.source)
270 << "defined resource '"
271 << ResourceNameRef(package->name,
272 type->type,
273 entry->name)
274 << "' for external package '"
275 << package->name << "'");
276 error = true;
277 }
278 }
279 }
280 }
281 }
282 return !error;
283 }
284
285 std::unique_ptr<IArchiveWriter> makeArchiveWriter() {
286 if (mOptions.outputToDirectory) {
287 return createDirectoryArchiveWriter(mOptions.outputPath);
288 } else {
289 return createZipFileArchiveWriter(mOptions.outputPath);
290 }
291 }
292
293 bool flattenTable(ResourceTable* table, IArchiveWriter* writer) {
294 BigBuffer buffer(1024);
295 TableFlattenerOptions options = {};
296 options.useExtendedChunks = mOptions.staticLib;
297 TableFlattener flattener(&buffer, options);
298 if (!flattener.consume(&mContext, table)) {
299 return false;
300 }
301
302 ArchiveEntry* entry = writer->writeEntry("resources.arsc", ArchiveEntry::kAlign, buffer);
303 if (!entry) {
304 mContext.getDiagnostics()->error(
305 DiagMessage() << "failed to write resources.arsc to archive");
306 return false;
307 }
308 return true;
309 }
310
311 bool flattenXml(XmlResource* xmlRes, const StringPiece& path, Maybe<size_t> maxSdkLevel,
312 IArchiveWriter* writer) {
313 BigBuffer buffer(1024);
314 XmlFlattenerOptions options = {};
315 options.keepRawValues = mOptions.staticLib;
316 options.maxSdkLevel = maxSdkLevel;
317 XmlFlattener flattener(&buffer, options);
318 if (!flattener.consume(&mContext, xmlRes)) {
319 return false;
320 }
321
322 ArchiveEntry* entry = writer->writeEntry(path, ArchiveEntry::kCompress, buffer);
323 if (!entry) {
324 mContext.getDiagnostics()->error(
325 DiagMessage() << "failed to write " << path << " to archive");
326 return false;
327 }
328 return true;
329 }
330
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700331 bool writeJavaFile(ResourceTable* table, const StringPiece16& packageNameToGenerate,
332 const StringPiece16& outPackage, JavaClassGeneratorOptions javaOptions) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700333 if (!mOptions.generateJavaClassPath) {
334 return true;
335 }
336
337 std::string outPath = mOptions.generateJavaClassPath.value();
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700338 file::appendPath(&outPath, file::packageToPath(util::utf16ToUtf8(outPackage)));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700339 file::mkdirs(outPath);
340 file::appendPath(&outPath, "R.java");
341
342 std::ofstream fout(outPath, std::ofstream::binary);
343 if (!fout) {
344 mContext.getDiagnostics()->error(DiagMessage() << strerror(errno));
345 return false;
346 }
347
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348 JavaClassGenerator generator(table, javaOptions);
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700349 if (!generator.generate(packageNameToGenerate, outPackage, &fout)) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700350 mContext.getDiagnostics()->error(DiagMessage(outPath) << generator.getError());
351 return false;
352 }
353 return true;
354 }
355
356 bool writeProguardFile(const proguard::KeepSet& keepSet) {
357 if (!mOptions.generateProguardRulesPath) {
358 return true;
359 }
360
361 std::ofstream fout(mOptions.generateProguardRulesPath.value(), std::ofstream::binary);
362 if (!fout) {
363 mContext.getDiagnostics()->error(DiagMessage() << strerror(errno));
364 return false;
365 }
366
367 proguard::writeKeepSet(&fout, keepSet);
368 if (!fout) {
369 mContext.getDiagnostics()->error(DiagMessage() << strerror(errno));
370 return false;
371 }
372 return true;
373 }
374
375 int run(const std::vector<std::string>& inputFiles) {
376 // Load the AndroidManifest.xml
377 std::unique_ptr<XmlResource> manifestXml = loadXml(mOptions.manifestPath);
378 if (!manifestXml) {
379 return 1;
380 }
381
382 if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get())) {
383 mContext.mCompilationPackage = maybeAppInfo.value().package;
384 } else {
385 mContext.getDiagnostics()->error(DiagMessage(mOptions.manifestPath)
386 << "no package specified in <manifest> tag");
387 return 1;
388 }
389
390 if (!util::isJavaPackageName(mContext.mCompilationPackage)) {
391 mContext.getDiagnostics()->error(DiagMessage(mOptions.manifestPath)
392 << "invalid package name '"
393 << mContext.mCompilationPackage
394 << "'");
395 return 1;
396 }
397
398 mContext.mNameMangler = util::make_unique<NameMangler>(
399 NameManglerPolicy{ mContext.mCompilationPackage });
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700400
401 if (mContext.mCompilationPackage == u"android") {
402 mContext.mPackageId = 0x01;
403 } else {
404 mContext.mPackageId = 0x7f;
405 }
406
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700407 mContext.mSymbols = createSymbolTableFromIncludePaths();
408 if (!mContext.mSymbols) {
409 return 1;
410 }
411
412 if (mOptions.verbose) {
413 mContext.getDiagnostics()->note(
414 DiagMessage() << "linking package '" << mContext.mCompilationPackage << "' "
415 << "with package ID " << std::hex << (int) mContext.mPackageId);
416 }
417
418 ResourceTable mergedTable;
419 TableMerger tableMerger(&mContext, &mergedTable);
420
421 struct FilesToProcess {
422 Source source;
423 ResourceFile file;
424 };
425
426 bool error = false;
427 std::queue<FilesToProcess> filesToProcess;
428 for (const std::string& input : inputFiles) {
429 if (util::stringEndsWith<char>(input, ".apk")) {
430 // TODO(adamlesinski): Load resources from a static library APK
431 // Merge the table into TableMerger.
432
433 } else if (util::stringEndsWith<char>(input, ".arsc.flat")) {
434 if (mOptions.verbose) {
435 mContext.getDiagnostics()->note(DiagMessage() << "linking " << input);
436 }
437
438 std::unique_ptr<ResourceTable> table = loadTable(input);
439 if (!table) {
440 return 1;
441 }
442
443 if (!tableMerger.merge(Source(input), table.get())) {
444 return 1;
445 }
446
447 } else {
448 // Extract the exported IDs here so we can build the resource table.
449 if (Maybe<ResourceFile> maybeF = loadFileExportHeader(input)) {
450 ResourceFile& f = maybeF.value();
451
452 if (f.name.package.empty()) {
453 f.name.package = mContext.getCompilationPackage().toString();
454 }
455
456 Maybe<ResourceName> mangledName = mContext.getNameMangler()->mangleName(f.name);
457
458 // Add this file to the table.
459 if (!mergedTable.addFileReference(mangledName ? mangledName.value() : f.name,
460 f.config, f.source,
461 util::utf8ToUtf16(buildResourceFileName(f)),
462 mContext.getDiagnostics())) {
463 error = true;
464 }
465
466 // Add the exports of this file to the table.
467 for (SourcedResourceName& exportedSymbol : f.exportedSymbols) {
468 if (exportedSymbol.name.package.empty()) {
469 exportedSymbol.name.package = mContext.getCompilationPackage()
470 .toString();
471 }
472
473 Maybe<ResourceName> mangledName = mContext.getNameMangler()->mangleName(
474 exportedSymbol.name);
475 if (!mergedTable.addResource(
476 mangledName ? mangledName.value() : exportedSymbol.name,
477 {}, {}, f.source.withLine(exportedSymbol.line),
478 util::make_unique<Id>(), mContext.getDiagnostics())) {
479 error = true;
480 }
481 }
482
483 filesToProcess.push(FilesToProcess{ Source(input), std::move(f) });
484 } else {
485 return 1;
486 }
487 }
488 }
489
490 if (error) {
491 mContext.getDiagnostics()->error(DiagMessage() << "failed parsing input");
492 return 1;
493 }
494
495 if (!verifyNoExternalPackages(&mergedTable)) {
496 return 1;
497 }
498
499 if (!mOptions.staticLib) {
500 PrivateAttributeMover mover;
501 if (!mover.consume(&mContext, &mergedTable)) {
502 mContext.getDiagnostics()->error(
503 DiagMessage() << "failed moving private attributes");
504 return 1;
505 }
506 }
507
508 {
509 IdAssigner idAssigner;
510 if (!idAssigner.consume(&mContext, &mergedTable)) {
511 mContext.getDiagnostics()->error(DiagMessage() << "failed assigning IDs");
512 return 1;
513 }
514 }
515
516 mContext.mNameMangler = util::make_unique<NameMangler>(
517 NameManglerPolicy{ mContext.mCompilationPackage, tableMerger.getMergedPackages() });
518 mContext.mSymbols = JoinedSymbolTableBuilder()
519 .addSymbolTable(util::make_unique<SymbolTableWrapper>(&mergedTable))
520 .addSymbolTable(std::move(mContext.mSymbols))
521 .build();
522
523 {
524 ReferenceLinker linker;
525 if (!linker.consume(&mContext, &mergedTable)) {
526 mContext.getDiagnostics()->error(DiagMessage() << "failed linking references");
527 return 1;
528 }
529 }
530
531 proguard::KeepSet proguardKeepSet;
532
533 std::unique_ptr<IArchiveWriter> archiveWriter = makeArchiveWriter();
534 if (!archiveWriter) {
535 mContext.getDiagnostics()->error(DiagMessage() << "failed to create archive");
536 return 1;
537 }
538
539 {
540 XmlReferenceLinker manifestLinker;
541 if (manifestLinker.consume(&mContext, manifestXml.get())) {
542
543 if (!proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath),
544 manifestXml.get(),
545 &proguardKeepSet)) {
546 error = true;
547 }
548
549 if (!flattenXml(manifestXml.get(), "AndroidManifest.xml", {},
550 archiveWriter.get())) {
551 error = true;
552 }
553 } else {
554 error = true;
555 }
556 }
557
558 for (; !filesToProcess.empty(); filesToProcess.pop()) {
559 FilesToProcess& f = filesToProcess.front();
560 if (f.file.name.type != ResourceType::kRaw &&
561 util::stringEndsWith<char>(f.source.path, ".xml.flat")) {
562 if (mOptions.verbose) {
563 mContext.getDiagnostics()->note(DiagMessage() << "linking " << f.source.path);
564 }
565
566 std::unique_ptr<XmlResource> xmlRes = loadBinaryXmlSkipFileExport(f.source.path);
567 if (!xmlRes) {
568 return 1;
569 }
570
571 xmlRes->file = std::move(f.file);
572
573 XmlReferenceLinker xmlLinker;
574 if (xmlLinker.consume(&mContext, xmlRes.get())) {
575 if (!proguard::collectProguardRules(xmlRes->file.source, xmlRes.get(),
576 &proguardKeepSet)) {
577 error = true;
578 }
579
580 Maybe<size_t> maxSdkLevel;
581 if (!mOptions.noAutoVersion) {
582 maxSdkLevel = std::max<size_t>(xmlRes->file.config.sdkVersion, 1u);
583 }
584
585 if (!flattenXml(xmlRes.get(), buildResourceFileName(xmlRes->file), maxSdkLevel,
586 archiveWriter.get())) {
587 error = true;
588 }
589
590 if (!mOptions.noAutoVersion) {
591 Maybe<ResourceTable::SearchResult> result = mergedTable.findResource(
592 xmlRes->file.name);
593 for (int sdkLevel : xmlLinker.getSdkLevels()) {
594 if (sdkLevel > xmlRes->file.config.sdkVersion &&
595 shouldGenerateVersionedResource(result.value().entry,
596 xmlRes->file.config,
597 sdkLevel)) {
598 xmlRes->file.config.sdkVersion = sdkLevel;
599 if (!mergedTable.addFileReference(xmlRes->file.name,
600 xmlRes->file.config,
601 xmlRes->file.source,
602 util::utf8ToUtf16(
603 buildResourceFileName(xmlRes->file)),
604 mContext.getDiagnostics())) {
605 error = true;
606 continue;
607 }
608
609 if (!flattenXml(xmlRes.get(), buildResourceFileName(xmlRes->file),
610 sdkLevel, archiveWriter.get())) {
611 error = true;
612 }
613 }
614 }
615 }
616
617 } else {
618 error = true;
619 }
620 } else {
621 if (mOptions.verbose) {
622 mContext.getDiagnostics()->note(DiagMessage() << "copying " << f.source.path);
623 }
624
625 if (!copyFileToArchive(f.source.path, buildResourceFileName(f.file), 0,
626 archiveWriter.get())) {
627 error = true;
628 }
629 }
630 }
631
632 if (error) {
633 mContext.getDiagnostics()->error(DiagMessage() << "failed linking file resources");
634 return 1;
635 }
636
637 if (!mOptions.noAutoVersion) {
638 AutoVersioner versioner;
639 if (!versioner.consume(&mContext, &mergedTable)) {
640 mContext.getDiagnostics()->error(DiagMessage() << "failed versioning styles");
641 return 1;
642 }
643 }
644
645 if (!flattenTable(&mergedTable, archiveWriter.get())) {
646 mContext.getDiagnostics()->error(DiagMessage() << "failed to write resources.arsc");
647 return 1;
648 }
649
650 if (mOptions.generateJavaClassPath) {
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700651 JavaClassGeneratorOptions options;
652 if (mOptions.staticLib) {
653 options.useFinal = false;
654 }
655
656 if (mOptions.privateSymbols) {
657 // If we defined a private symbols package, we only emit Public symbols
658 // to the original package, and private and public symbols to the private package.
659
660 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic;
661 if (!writeJavaFile(&mergedTable, mContext.getCompilationPackage(),
662 mContext.getCompilationPackage(), options)) {
663 return 1;
664 }
665
666 options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate;
667 if (!writeJavaFile(&mergedTable, mContext.getCompilationPackage(),
668 mOptions.privateSymbols.value(), options)) {
669 return 1;
670 }
671
672 } else {
673 // Emit Everything.
674
675 if (!writeJavaFile(&mergedTable, mContext.getCompilationPackage(),
676 mContext.getCompilationPackage(), options)) {
677 return 1;
678 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700679 }
680 }
681
682 if (mOptions.generateProguardRulesPath) {
683 if (!writeProguardFile(proguardKeepSet)) {
684 return 1;
685 }
686 }
687
688 if (mOptions.verbose) {
689 Debug::printTable(&mergedTable);
690 for (; !tableMerger.getFileMergeQueue()->empty();
691 tableMerger.getFileMergeQueue()->pop()) {
692 const FileToMerge& f = tableMerger.getFileMergeQueue()->front();
693 mContext.getDiagnostics()->note(
694 DiagMessage() << f.srcPath << " -> " << f.dstPath << " from (0x"
695 << std::hex << (uintptr_t) f.srcTable << std::dec);
696 }
697 }
698
699 return 0;
700 }
701};
702
703int link(const std::vector<StringPiece>& args) {
704 LinkOptions options;
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700705 Maybe<std::string> privateSymbolsPackage;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700706 Flags flags = Flags()
707 .requiredFlag("-o", "Output path", &options.outputPath)
708 .requiredFlag("--manifest", "Path to the Android manifest to build",
709 &options.manifestPath)
710 .optionalFlagList("-I", "Adds an Android APK to link against", &options.includePaths)
711 .optionalFlag("--java", "Directory in which to generate R.java",
712 &options.generateJavaClassPath)
713 .optionalFlag("--proguard", "Output file for generated Proguard rules",
714 &options.generateProguardRulesPath)
715 .optionalSwitch("--no-auto-version",
716 "Disables automatic style and layout SDK versioning",
717 &options.noAutoVersion)
718 .optionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified "
719 "by -o",
720 &options.outputToDirectory)
721 .optionalSwitch("--static-lib", "Generate a static Android library", &options.staticLib)
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700722 .optionalFlag("--private-symbols", "Package name to use when generating R.java for "
723 "private symbols. If not specified, public and private symbols will "
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700724 "use the application's package name", &privateSymbolsPackage)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700725 .optionalSwitch("-v", "Enables verbose logging", &options.verbose);
726
727 if (!flags.parse("aapt2 link", args, &std::cerr)) {
728 return 1;
729 }
730
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700731 if (privateSymbolsPackage) {
732 options.privateSymbols = util::utf8ToUtf16(privateSymbolsPackage.value());
733 }
734
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700735 LinkCommand cmd = { options };
736 return cmd.run(flags.getArgs());
737}
738
739} // namespace aapt