Dump.cpp revision 355f285ffd000f6cfe76680eb22d010546d124bb
1/* 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 106 bool verbose() override { 107 return mVerbose; 108 } 109 110 void setVerbose(bool val) { 111 mVerbose = val; 112 } 113 114private: 115 StdErrDiagnostics mDiagnostics; 116 bool mVerbose = false; 117}; 118 119/** 120 * Entry point for dump command. 121 */ 122int dump(const std::vector<StringPiece>& args) { 123 bool verbose = false; 124 Flags flags = Flags() 125 .optionalSwitch("-v", "increase verbosity of output", &verbose); 126 if (!flags.parse("aapt2 dump", args, &std::cerr)) { 127 return 1; 128 } 129 130 DumpContext context; 131 context.setVerbose(verbose); 132 133 for (const std::string& arg : flags.getArgs()) { 134 tryDumpFile(&context, arg); 135 } 136 return 0; 137} 138 139} // namespace aapt 140