blob: 915fae80fcbb97e0c6ec1f96ed8f06beacec1a42 [file] [log] [blame]
Adam Lesinski59e04c62016-02-04 15:59:23 -08001/*
2 * Copyright (C) 2016 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 "Debug.h"
18#include "Diagnostics.h"
19#include "Flags.h"
20#include "process/IResourceTableConsumer.h"
21#include "proto/ProtoSerialize.h"
22#include "util/Files.h"
23#include "util/StringPiece.h"
24
25#include <vector>
26
27namespace aapt {
28
29//struct DumpOptions {
30//
31//};
32
33void dumpCompiledFile(const pb::CompiledFile& pbFile, const void* data, size_t len,
34 const Source& source, IAaptContext* context) {
35 std::unique_ptr<ResourceFile> file = deserializeCompiledFileFromPb(pbFile, source,
36 context->getDiagnostics());
37 if (!file) {
38 return;
39 }
40
41 std::cout << "Resource: " << file->name << "\n"
42 << "Config: " << file->config << "\n"
43 << "Source: " << file->source << "\n";
44}
45
46void dumpCompiledTable(const pb::ResourceTable& pbTable, const Source& source,
47 IAaptContext* context) {
48 std::unique_ptr<ResourceTable> table = deserializeTableFromPb(pbTable, source,
49 context->getDiagnostics());
50 if (!table) {
51 return;
52 }
53
54 Debug::printTable(table.get());
55}
56
57void tryDumpFile(IAaptContext* context, const std::string& filePath) {
58 std::string err;
59 Maybe<android::FileMap> file = file::mmapPath(filePath, &err);
60 if (!file) {
61 context->getDiagnostics()->error(DiagMessage(filePath) << err);
62 return;
63 }
64
65 android::FileMap* fileMap = &file.value();
66
67 // Try as a compiled table.
68 pb::ResourceTable pbTable;
69 if (pbTable.ParseFromArray(fileMap->getDataPtr(), fileMap->getDataLength())) {
70 dumpCompiledTable(pbTable, Source(filePath), context);
71 return;
72 }
73
74 // Try as a compiled file.
75 CompiledFileInputStream input(fileMap->getDataPtr(), fileMap->getDataLength());
76 if (const pb::CompiledFile* pbFile = input.CompiledFile()) {
77 dumpCompiledFile(*pbFile, input.data(), input.size(), Source(filePath), context);
78 return;
79 }
80}
81
82class DumpContext : public IAaptContext {
83public:
84 IDiagnostics* getDiagnostics() override {
85 return &mDiagnostics;
86 }
87
88 NameMangler* getNameMangler() override {
89 abort();
90 return nullptr;
91 }
92
93 StringPiece16 getCompilationPackage() override {
94 return {};
95 }
96
97 uint8_t getPackageId() override {
98 return 0;
99 }
100
101 ISymbolTable* getExternalSymbols() override {
102 abort();
103 return nullptr;
104 }
105
106private:
107 StdErrDiagnostics mDiagnostics;
108};
109
110/**
111 * Entry point for dump command.
112 */
113int dump(const std::vector<StringPiece>& args) {
114 //DumpOptions options;
115 Flags flags = Flags();
116 if (!flags.parse("aapt2 dump", args, &std::cerr)) {
117 return 1;
118 }
119
120 DumpContext context;
121
122 for (const std::string& arg : flags.getArgs()) {
123 tryDumpFile(&context, arg);
124 }
125 return 0;
126}
127
128} // namespace aapt