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 | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 34 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 35 | #include "link/ManifestFixer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | #include "link/TableMerger.h" |
| 37 | #include "process/IResourceTableConsumer.h" |
| 38 | #include "process/SymbolTable.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 39 | #include "proto/ProtoSerialize.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | #include "unflatten/BinaryResourceParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | #include "util/Files.h" |
| 42 | #include "util/StringPiece.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 43 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 45 | #include <google/protobuf/io/coded_stream.h> |
| 46 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | #include <fstream> |
| 48 | #include <sys/stat.h> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | #include <vector> |
| 50 | |
| 51 | namespace aapt { |
| 52 | |
| 53 | struct LinkOptions { |
| 54 | std::string outputPath; |
| 55 | std::string manifestPath; |
| 56 | std::vector<std::string> includePaths; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 57 | std::vector<std::string> overlayFiles; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | Maybe<std::string> generateJavaClassPath; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 59 | Maybe<std::u16string> customJavaPackage; |
| 60 | std::set<std::u16string> extraJavaPackages; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | Maybe<std::string> generateProguardRulesPath; |
| 62 | bool noAutoVersion = false; |
| 63 | bool staticLib = false; |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 64 | bool generateNonFinalIds = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 65 | bool verbose = false; |
| 66 | bool outputToDirectory = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 67 | bool autoAddOverlay = false; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 68 | bool doNotCompressAnything = false; |
| 69 | std::vector<std::string> extensionsToNotCompress; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 70 | Maybe<std::u16string> privateSymbols; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 71 | ManifestFixerOptions manifestFixerOptions; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 72 | IConfigFilter* configFilter = nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct LinkContext : public IAaptContext { |
| 76 | StdErrDiagnostics mDiagnostics; |
| 77 | std::unique_ptr<NameMangler> mNameMangler; |
| 78 | std::u16string mCompilationPackage; |
| 79 | uint8_t mPackageId; |
| 80 | std::unique_ptr<ISymbolTable> mSymbols; |
| 81 | |
| 82 | IDiagnostics* getDiagnostics() override { |
| 83 | return &mDiagnostics; |
| 84 | } |
| 85 | |
| 86 | NameMangler* getNameMangler() override { |
| 87 | return mNameMangler.get(); |
| 88 | } |
| 89 | |
| 90 | StringPiece16 getCompilationPackage() override { |
| 91 | return mCompilationPackage; |
| 92 | } |
| 93 | |
| 94 | uint8_t getPackageId() override { |
| 95 | return mPackageId; |
| 96 | } |
| 97 | |
| 98 | ISymbolTable* getExternalSymbols() override { |
| 99 | return mSymbols.get(); |
| 100 | } |
| 101 | }; |
| 102 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 103 | class LinkCommand { |
| 104 | public: |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 105 | LinkCommand(LinkContext* context, const LinkOptions& options) : |
| 106 | mOptions(options), mContext(context), mFinalTable(), mFileCollection(nullptr) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 107 | std::unique_ptr<io::FileCollection> fileCollection = |
| 108 | util::make_unique<io::FileCollection>(); |
| 109 | |
| 110 | // Get a pointer to the FileCollection for convenience, but it will be owned by the vector. |
| 111 | mFileCollection = fileCollection.get(); |
| 112 | |
| 113 | // Move it to the collection. |
| 114 | mCollections.push_back(std::move(fileCollection)); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 115 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 116 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Creates a SymbolTable that loads symbols from the various APKs and caches the |
| 119 | * results for faster lookup. |
| 120 | */ |
| 121 | std::unique_ptr<ISymbolTable> createSymbolTableFromIncludePaths() { |
| 122 | AssetManagerSymbolTableBuilder builder; |
| 123 | for (const std::string& path : mOptions.includePaths) { |
| 124 | if (mOptions.verbose) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 125 | mContext->getDiagnostics()->note(DiagMessage(path) << "loading include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | std::unique_ptr<android::AssetManager> assetManager = |
| 129 | util::make_unique<android::AssetManager>(); |
| 130 | int32_t cookie = 0; |
| 131 | if (!assetManager->addAssetPath(android::String8(path.data(), path.size()), &cookie)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 132 | mContext->getDiagnostics()->error( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 133 | DiagMessage(path) << "failed to load include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | return {}; |
| 135 | } |
| 136 | builder.add(std::move(assetManager)); |
| 137 | } |
| 138 | return builder.build(); |
| 139 | } |
| 140 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 141 | std::unique_ptr<ResourceTable> loadTable(const Source& source, const void* data, size_t len) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 143 | BinaryResourceParser parser(mContext, table.get(), source, data, len); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | if (!parser.parse()) { |
| 145 | return {}; |
| 146 | } |
| 147 | return table; |
| 148 | } |
| 149 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 150 | std::unique_ptr<ResourceTable> loadTableFromPb(const Source& source, |
| 151 | const void* data, size_t len) { |
| 152 | pb::ResourceTable pbTable; |
| 153 | if (!pbTable.ParseFromArray(data, len)) { |
| 154 | mContext->getDiagnostics()->error(DiagMessage(source) << "invalid compiled table"); |
| 155 | return {}; |
| 156 | } |
| 157 | |
| 158 | std::unique_ptr<ResourceTable> table = deserializeTableFromPb(pbTable, source, |
| 159 | mContext->getDiagnostics()); |
| 160 | if (!table) { |
| 161 | return {}; |
| 162 | } |
| 163 | return table; |
| 164 | } |
| 165 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Inflates an XML file from the source path. |
| 168 | */ |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 169 | static std::unique_ptr<xml::XmlResource> loadXml(const std::string& path, IDiagnostics* diag) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 170 | std::ifstream fin(path, std::ifstream::binary); |
| 171 | if (!fin) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 172 | diag->error(DiagMessage(path) << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 173 | return {}; |
| 174 | } |
| 175 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 176 | return xml::inflate(&fin, diag, Source(path)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 179 | static std::unique_ptr<xml::XmlResource> loadBinaryXmlSkipFileExport( |
| 180 | const Source& source, |
| 181 | const void* data, size_t len, |
| 182 | IDiagnostics* diag) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 183 | CompiledFileInputStream inputStream(data, len); |
| 184 | if (!inputStream.CompiledFile()) { |
| 185 | diag->error(DiagMessage(source) << "invalid compiled file header"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | return {}; |
| 187 | } |
| 188 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 189 | const uint8_t* xmlData = reinterpret_cast<const uint8_t*>(inputStream.data()); |
| 190 | const size_t xmlDataLen = inputStream.size(); |
| 191 | |
| 192 | std::unique_ptr<xml::XmlResource> xmlRes = xml::inflate(xmlData, xmlDataLen, diag, source); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 193 | if (!xmlRes) { |
| 194 | return {}; |
| 195 | } |
| 196 | return xmlRes; |
| 197 | } |
| 198 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 199 | static std::unique_ptr<ResourceFile> loadFileExportHeader(const Source& source, |
| 200 | const void* data, size_t len, |
| 201 | IDiagnostics* diag) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 202 | CompiledFileInputStream inputStream(data, len); |
| 203 | const pb::CompiledFile* pbFile = inputStream.CompiledFile(); |
| 204 | if (!pbFile) { |
| 205 | diag->error(DiagMessage(source) << "invalid compiled file header"); |
| 206 | return {}; |
| 207 | } |
| 208 | |
| 209 | std::unique_ptr<ResourceFile> resFile = deserializeCompiledFileFromPb(*pbFile, source, |
| 210 | diag); |
| 211 | if (!resFile) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 212 | return {}; |
| 213 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 214 | return resFile; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 217 | uint32_t getCompressionFlags(const StringPiece& str) { |
| 218 | if (mOptions.doNotCompressAnything) { |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | for (const std::string& extension : mOptions.extensionsToNotCompress) { |
| 223 | if (util::stringEndsWith<char>(str, extension)) { |
| 224 | return 0; |
| 225 | } |
| 226 | } |
| 227 | return ArchiveEntry::kCompress; |
| 228 | } |
| 229 | |
| 230 | bool copyFileToArchive(io::IFile* file, const std::string& outPath, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 231 | IArchiveWriter* writer) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 232 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 233 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 234 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 235 | << "failed to open file"); |
| 236 | return false; |
| 237 | } |
| 238 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 239 | CompiledFileInputStream inputStream(data->data(), data->size()); |
| 240 | if (!inputStream.CompiledFile()) { |
| 241 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) |
| 242 | << "invalid compiled file header"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 246 | if (writer->startEntry(outPath, getCompressionFlags(outPath))) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 247 | if (writer->writeEntry(reinterpret_cast<const uint8_t*>(inputStream.data()), |
| 248 | inputStream.size())) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 249 | if (writer->finishEntry()) { |
| 250 | return true; |
| 251 | } |
| 252 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 253 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 254 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 255 | mContext->getDiagnostics()->error( |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 256 | DiagMessage(mOptions.outputPath) << "failed to write file " << outPath); |
| 257 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 260 | Maybe<AppInfo> extractAppInfoFromManifest(xml::XmlResource* xmlRes) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 261 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 262 | if (xml::Element* manifestEl = xml::findRootElement(xmlRes->root.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 263 | if (manifestEl->namespaceUri.empty() && manifestEl->name == u"manifest") { |
| 264 | if (xml::Attribute* packageAttr = manifestEl->findAttribute({}, u"package")) { |
| 265 | return AppInfo{ packageAttr->value }; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | return {}; |
| 270 | } |
| 271 | |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 272 | /** |
| 273 | * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked. |
| 274 | * Postcondition: ResourceTable has only one package left. All others are stripped, or there |
| 275 | * is an error and false is returned. |
| 276 | */ |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 277 | bool verifyNoExternalPackages() { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 278 | auto isExtPackageFunc = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 279 | return mContext->getCompilationPackage() != pkg->name || |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 280 | !pkg->id || |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 281 | pkg->id.value() != mContext->getPackageId(); |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 282 | }; |
| 283 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 284 | bool error = false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 285 | for (const auto& package : mFinalTable.packages) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 286 | if (isExtPackageFunc(package)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 287 | // We have a package that is not related to the one we're building! |
| 288 | for (const auto& type : package->types) { |
| 289 | for (const auto& entry : type->entries) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 290 | ResourceNameRef resName(package->name, type->type, entry->name); |
| 291 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 292 | for (const auto& configValue : entry->values) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 293 | // Special case the occurrence of an ID that is being generated for the |
| 294 | // 'android' package. This is due to legacy reasons. |
| 295 | if (valueCast<Id>(configValue.value.get()) && |
| 296 | package->name == u"android") { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 297 | mContext->getDiagnostics()->warn( |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 298 | DiagMessage(configValue.value->getSource()) |
| 299 | << "generated id '" << resName |
| 300 | << "' for external package '" << package->name |
| 301 | << "'"); |
| 302 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 303 | mContext->getDiagnostics()->error( |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 304 | DiagMessage(configValue.value->getSource()) |
| 305 | << "defined resource '" << resName |
| 306 | << "' for external package '" << package->name |
| 307 | << "'"); |
| 308 | error = true; |
| 309 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 315 | |
| 316 | auto newEndIter = std::remove_if(mFinalTable.packages.begin(), mFinalTable.packages.end(), |
| 317 | isExtPackageFunc); |
| 318 | mFinalTable.packages.erase(newEndIter, mFinalTable.packages.end()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 319 | return !error; |
| 320 | } |
| 321 | |
| 322 | std::unique_ptr<IArchiveWriter> makeArchiveWriter() { |
| 323 | if (mOptions.outputToDirectory) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 324 | return createDirectoryArchiveWriter(mContext->getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 325 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 326 | return createZipFileArchiveWriter(mContext->getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | bool flattenTable(ResourceTable* table, IArchiveWriter* writer) { |
| 331 | BigBuffer buffer(1024); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 332 | TableFlattener flattener(&buffer); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 333 | if (!flattener.consume(mContext, table)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 334 | return false; |
| 335 | } |
| 336 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 337 | if (writer->startEntry("resources.arsc", ArchiveEntry::kAlign)) { |
| 338 | if (writer->writeEntry(buffer)) { |
| 339 | if (writer->finishEntry()) { |
| 340 | return true; |
| 341 | } |
| 342 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 343 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 344 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 345 | mContext->getDiagnostics()->error( |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 346 | DiagMessage() << "failed to write resources.arsc to archive"); |
| 347 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 350 | bool flattenXml(xml::XmlResource* xmlRes, const StringPiece& path, Maybe<size_t> maxSdkLevel, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 351 | IArchiveWriter* writer) { |
| 352 | BigBuffer buffer(1024); |
| 353 | XmlFlattenerOptions options = {}; |
| 354 | options.keepRawValues = mOptions.staticLib; |
| 355 | options.maxSdkLevel = maxSdkLevel; |
| 356 | XmlFlattener flattener(&buffer, options); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 357 | if (!flattener.consume(mContext, xmlRes)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 358 | return false; |
| 359 | } |
| 360 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 361 | if (writer->startEntry(path, ArchiveEntry::kCompress)) { |
| 362 | if (writer->writeEntry(buffer)) { |
| 363 | if (writer->finishEntry()) { |
| 364 | return true; |
| 365 | } |
| 366 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 367 | } |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 368 | mContext->getDiagnostics()->error( |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 369 | DiagMessage() << "failed to write " << path << " to archive"); |
| 370 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 373 | bool writeJavaFile(ResourceTable* table, const StringPiece16& packageNameToGenerate, |
| 374 | const StringPiece16& outPackage, JavaClassGeneratorOptions javaOptions) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 375 | if (!mOptions.generateJavaClassPath) { |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | std::string outPath = mOptions.generateJavaClassPath.value(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 380 | file::appendPath(&outPath, file::packageToPath(util::utf16ToUtf8(outPackage))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 381 | file::mkdirs(outPath); |
| 382 | file::appendPath(&outPath, "R.java"); |
| 383 | |
| 384 | std::ofstream fout(outPath, std::ofstream::binary); |
| 385 | if (!fout) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 386 | mContext->getDiagnostics()->error(DiagMessage() << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 390 | JavaClassGenerator generator(table, javaOptions); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 391 | if (!generator.generate(packageNameToGenerate, outPackage, &fout)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 392 | mContext->getDiagnostics()->error(DiagMessage(outPath) << generator.getError()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 398 | bool writeManifestJavaFile(xml::XmlResource* manifestXml) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 399 | if (!mOptions.generateJavaClassPath) { |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | std::string outPath = mOptions.generateJavaClassPath.value(); |
| 404 | file::appendPath(&outPath, |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 405 | file::packageToPath(util::utf16ToUtf8(mContext->getCompilationPackage()))); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 406 | file::mkdirs(outPath); |
| 407 | file::appendPath(&outPath, "Manifest.java"); |
| 408 | |
| 409 | std::ofstream fout(outPath, std::ofstream::binary); |
| 410 | if (!fout) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 411 | mContext->getDiagnostics()->error(DiagMessage() << strerror(errno)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 412 | return false; |
| 413 | } |
| 414 | |
| 415 | ManifestClassGenerator generator; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 416 | if (!generator.generate(mContext->getDiagnostics(), mContext->getCompilationPackage(), |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 417 | manifestXml, &fout)) { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | if (!fout) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 422 | mContext->getDiagnostics()->error(DiagMessage() << strerror(errno)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | return true; |
| 426 | } |
| 427 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 428 | bool writeProguardFile(const proguard::KeepSet& keepSet) { |
| 429 | if (!mOptions.generateProguardRulesPath) { |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | std::ofstream fout(mOptions.generateProguardRulesPath.value(), std::ofstream::binary); |
| 434 | if (!fout) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 435 | mContext->getDiagnostics()->error(DiagMessage() << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
| 439 | proguard::writeKeepSet(&fout, keepSet); |
| 440 | if (!fout) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 441 | mContext->getDiagnostics()->error(DiagMessage() << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 447 | bool mergeStaticLibrary(const std::string& input) { |
| 448 | // TODO(adamlesinski): Load resources from a static library APK and merge the table into |
| 449 | // TableMerger. |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 450 | mContext->getDiagnostics()->warn(DiagMessage() |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 451 | << "linking static libraries not supported yet: " |
| 452 | << input); |
| 453 | return true; |
| 454 | } |
| 455 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 456 | bool mergeResourceTable(io::IFile* file, bool override) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 457 | if (mOptions.verbose) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 458 | mContext->getDiagnostics()->note(DiagMessage() << "linking " << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 461 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 462 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 463 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 464 | << "failed to open file"); |
| 465 | return false; |
| 466 | } |
| 467 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame^] | 468 | std::unique_ptr<ResourceTable> table = loadTableFromPb(file->getSource(), data->data(), |
| 469 | data->size()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 470 | if (!table) { |
| 471 | return false; |
| 472 | } |
| 473 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 474 | bool result = false; |
| 475 | if (override) { |
| 476 | result = mTableMerger->mergeOverlay(file->getSource(), table.get()); |
| 477 | } else { |
| 478 | result = mTableMerger->merge(file->getSource(), table.get()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 479 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 480 | return result; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 483 | bool mergeCompiledFile(io::IFile* file, std::unique_ptr<ResourceFile> fileDesc, bool overlay) { |
| 484 | if (mOptions.verbose) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 485 | mContext->getDiagnostics()->note(DiagMessage() << "adding " << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 488 | bool result = false; |
| 489 | if (overlay) { |
| 490 | result = mTableMerger->mergeFileOverlay(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 491 | } else { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 492 | result = mTableMerger->mergeFile(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 495 | if (!result) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | |
| 499 | // Add the exports of this file to the table. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 500 | for (SourcedResourceName& exportedSymbol : fileDesc->exportedSymbols) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 501 | if (exportedSymbol.name.package.empty()) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 502 | exportedSymbol.name.package = mContext->getCompilationPackage().toString(); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | ResourceNameRef resName = exportedSymbol.name; |
| 506 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 507 | Maybe<ResourceName> mangledName = mContext->getNameMangler()->mangleName( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 508 | exportedSymbol.name); |
| 509 | if (mangledName) { |
| 510 | resName = mangledName.value(); |
| 511 | } |
| 512 | |
| 513 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 514 | id->setSource(fileDesc->source.withLine(exportedSymbol.line)); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 515 | bool result = mFinalTable.addResourceAllowMangled(resName, {}, std::move(id), |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 516 | mContext->getDiagnostics()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 517 | if (!result) { |
| 518 | return false; |
| 519 | } |
| 520 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 521 | return true; |
| 522 | } |
| 523 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 524 | /** |
| 525 | * Creates an io::IFileCollection from the ZIP archive and processes the files within. |
| 526 | */ |
| 527 | bool mergeArchive(const std::string& input, bool override) { |
| 528 | std::string errorStr; |
| 529 | std::unique_ptr<io::ZipFileCollection> collection = io::ZipFileCollection::create( |
| 530 | input, &errorStr); |
| 531 | if (!collection) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 532 | mContext->getDiagnostics()->error(DiagMessage(input) << errorStr); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 533 | return false; |
| 534 | } |
| 535 | |
| 536 | bool error = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 537 | for (auto iter = collection->iterator(); iter->hasNext(); ) { |
| 538 | if (!processFile(iter->next(), override)) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 539 | error = true; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // Make sure to move the collection into the set of IFileCollections. |
| 544 | mCollections.push_back(std::move(collection)); |
| 545 | return !error; |
| 546 | } |
| 547 | |
| 548 | bool processFile(const std::string& path, bool override) { |
Adam Lesinski | 656a577 | 2016-01-14 15:17:41 -0800 | [diff] [blame] | 549 | if (util::stringEndsWith<char>(path, ".flata") || |
| 550 | util::stringEndsWith<char>(path, ".jar") || |
| 551 | util::stringEndsWith<char>(path, ".jack") || |
| 552 | util::stringEndsWith<char>(path, ".zip")) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 553 | return mergeArchive(path, override); |
| 554 | } |
| 555 | |
| 556 | io::IFile* file = mFileCollection->insertFile(path); |
| 557 | return processFile(file, override); |
| 558 | } |
| 559 | |
| 560 | bool processFile(io::IFile* file, bool override) { |
| 561 | const Source& src = file->getSource(); |
| 562 | if (util::stringEndsWith<char>(src.path, ".arsc.flat")) { |
| 563 | return mergeResourceTable(file, override); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 564 | } else if (util::stringEndsWith<char>(src.path, ".flat")){ |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 565 | // Try opening the file and looking for an Export header. |
| 566 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 567 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 568 | mContext->getDiagnostics()->error(DiagMessage(src) << "failed to open"); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
| 572 | std::unique_ptr<ResourceFile> resourceFile = loadFileExportHeader( |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 573 | src, data->data(), data->size(), mContext->getDiagnostics()); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 574 | if (resourceFile) { |
| 575 | return mergeCompiledFile(file, std::move(resourceFile), override); |
| 576 | } |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 577 | |
| 578 | return false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 579 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 580 | |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 581 | // Ignore non .flat files. This could be classes.dex or something else that happens |
| 582 | // to be in an archive. |
| 583 | return true; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 586 | int run(const std::vector<std::string>& inputFiles) { |
| 587 | // Load the AndroidManifest.xml |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 588 | std::unique_ptr<xml::XmlResource> manifestXml = loadXml(mOptions.manifestPath, |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 589 | mContext->getDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 590 | if (!manifestXml) { |
| 591 | return 1; |
| 592 | } |
| 593 | |
| 594 | if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get())) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 595 | mContext->mCompilationPackage = maybeAppInfo.value().package; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 596 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 597 | mContext->getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 598 | << "no package specified in <manifest> tag"); |
| 599 | return 1; |
| 600 | } |
| 601 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 602 | if (!util::isJavaPackageName(mContext->mCompilationPackage)) { |
| 603 | mContext->getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 604 | << "invalid package name '" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 605 | << mContext->mCompilationPackage |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 606 | << "'"); |
| 607 | return 1; |
| 608 | } |
| 609 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 610 | mContext->mNameMangler = util::make_unique<NameMangler>( |
| 611 | NameManglerPolicy{ mContext->mCompilationPackage }); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 612 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 613 | if (mContext->mCompilationPackage == u"android") { |
| 614 | mContext->mPackageId = 0x01; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 615 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 616 | mContext->mPackageId = 0x7f; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 619 | mContext->mSymbols = createSymbolTableFromIncludePaths(); |
| 620 | if (!mContext->mSymbols) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 621 | return 1; |
| 622 | } |
| 623 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 624 | TableMergerOptions tableMergerOptions; |
| 625 | tableMergerOptions.autoAddOverlay = mOptions.autoAddOverlay; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 626 | tableMergerOptions.filter = mOptions.configFilter; |
| 627 | mTableMerger = util::make_unique<TableMerger>(mContext, &mFinalTable, tableMergerOptions); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 628 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 629 | if (mOptions.verbose) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 630 | mContext->getDiagnostics()->note( |
| 631 | DiagMessage() << "linking package '" << mContext->mCompilationPackage << "' " |
| 632 | << "with package ID " << std::hex << (int) mContext->mPackageId); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 635 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 636 | for (const std::string& input : inputFiles) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 637 | if (!processFile(input, false)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 638 | mContext->getDiagnostics()->error(DiagMessage() << "failed parsing input"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 639 | return 1; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 642 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 643 | for (const std::string& input : mOptions.overlayFiles) { |
| 644 | if (!processFile(input, true)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 645 | mContext->getDiagnostics()->error(DiagMessage() << "failed parsing overlays"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 646 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 650 | if (!verifyNoExternalPackages()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | if (!mOptions.staticLib) { |
| 655 | PrivateAttributeMover mover; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 656 | if (!mover.consume(mContext, &mFinalTable)) { |
| 657 | mContext->getDiagnostics()->error( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 658 | DiagMessage() << "failed moving private attributes"); |
| 659 | return 1; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | { |
| 664 | IdAssigner idAssigner; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 665 | if (!idAssigner.consume(mContext, &mFinalTable)) { |
| 666 | mContext->getDiagnostics()->error(DiagMessage() << "failed assigning IDs"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 667 | return 1; |
| 668 | } |
| 669 | } |
| 670 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 671 | mContext->mNameMangler = util::make_unique<NameMangler>(NameManglerPolicy{ |
| 672 | mContext->mCompilationPackage, mTableMerger->getMergedPackages() }); |
| 673 | mContext->mSymbols = JoinedSymbolTableBuilder() |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 674 | .addSymbolTable(util::make_unique<SymbolTableWrapper>(&mFinalTable)) |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 675 | .addSymbolTable(std::move(mContext->mSymbols)) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 676 | .build(); |
| 677 | |
| 678 | { |
| 679 | ReferenceLinker linker; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 680 | if (!linker.consume(mContext, &mFinalTable)) { |
| 681 | mContext->getDiagnostics()->error(DiagMessage() << "failed linking references"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 682 | return 1; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | proguard::KeepSet proguardKeepSet; |
| 687 | |
| 688 | std::unique_ptr<IArchiveWriter> archiveWriter = makeArchiveWriter(); |
| 689 | if (!archiveWriter) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 690 | mContext->getDiagnostics()->error(DiagMessage() << "failed to create archive"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 691 | return 1; |
| 692 | } |
| 693 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 694 | bool error = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 695 | { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 696 | ManifestFixer manifestFixer(mOptions.manifestFixerOptions); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 697 | if (!manifestFixer.consume(mContext, manifestXml.get())) { |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 698 | error = true; |
| 699 | } |
| 700 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 701 | // AndroidManifest.xml has no resource name, but the CallSite is built from the name |
| 702 | // (aka, which package the AndroidManifest.xml is coming from). |
| 703 | // So we give it a package name so it can see local resources. |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 704 | manifestXml->file.name.package = mContext->getCompilationPackage().toString(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 705 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 706 | XmlReferenceLinker manifestLinker; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 707 | if (manifestLinker.consume(mContext, manifestXml.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 708 | if (!proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath), |
| 709 | manifestXml.get(), |
| 710 | &proguardKeepSet)) { |
| 711 | error = true; |
| 712 | } |
| 713 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 714 | if (mOptions.generateJavaClassPath) { |
| 715 | if (!writeManifestJavaFile(manifestXml.get())) { |
| 716 | error = true; |
| 717 | } |
| 718 | } |
| 719 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 720 | if (!flattenXml(manifestXml.get(), "AndroidManifest.xml", {}, |
| 721 | archiveWriter.get())) { |
| 722 | error = true; |
| 723 | } |
| 724 | } else { |
| 725 | error = true; |
| 726 | } |
| 727 | } |
| 728 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 729 | if (error) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 730 | mContext->getDiagnostics()->error(DiagMessage() << "failed processing manifest"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 731 | return 1; |
| 732 | } |
| 733 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 734 | for (auto& mergeEntry : mTableMerger->getFilesToMerge()) { |
| 735 | const ResourceKeyRef& key = mergeEntry.first; |
| 736 | const FileToMerge& fileToMerge = mergeEntry.second; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 737 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 738 | const StringPiece path = fileToMerge.file->getSource().path; |
| 739 | |
| 740 | if (key.name.type != ResourceType::kRaw && |
| 741 | (util::stringEndsWith<char>(path, ".xml.flat") || |
| 742 | util::stringEndsWith<char>(path, ".xml"))) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 743 | if (mOptions.verbose) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 744 | mContext->getDiagnostics()->note(DiagMessage() << "linking " << path); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 747 | io::IFile* file = fileToMerge.file; |
| 748 | std::unique_ptr<io::IData> data = file->openAsData(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 749 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 750 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 751 | << "failed to open file"); |
| 752 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 755 | std::unique_ptr<xml::XmlResource> xmlRes; |
| 756 | if (util::stringEndsWith<char>(path, ".flat")) { |
| 757 | xmlRes = loadBinaryXmlSkipFileExport(file->getSource(), |
| 758 | data->data(), data->size(), |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 759 | mContext->getDiagnostics()); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 760 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 761 | xmlRes = xml::inflate(data->data(), data->size(), mContext->getDiagnostics(), |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 762 | file->getSource()); |
| 763 | } |
| 764 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 765 | if (!xmlRes) { |
| 766 | return 1; |
| 767 | } |
| 768 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 769 | // Create the file description header. |
| 770 | xmlRes->file = ResourceFile{ |
| 771 | key.name.toResourceName(), |
| 772 | key.config, |
| 773 | fileToMerge.originalSource, |
| 774 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 775 | |
| 776 | XmlReferenceLinker xmlLinker; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 777 | if (xmlLinker.consume(mContext, xmlRes.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 778 | if (!proguard::collectProguardRules(xmlRes->file.source, xmlRes.get(), |
| 779 | &proguardKeepSet)) { |
| 780 | error = true; |
| 781 | } |
| 782 | |
| 783 | Maybe<size_t> maxSdkLevel; |
| 784 | if (!mOptions.noAutoVersion) { |
| 785 | maxSdkLevel = std::max<size_t>(xmlRes->file.config.sdkVersion, 1u); |
| 786 | } |
| 787 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 788 | if (!flattenXml(xmlRes.get(), fileToMerge.dstPath, maxSdkLevel, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 789 | archiveWriter.get())) { |
| 790 | error = true; |
| 791 | } |
| 792 | |
| 793 | if (!mOptions.noAutoVersion) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 794 | Maybe<ResourceTable::SearchResult> result = mFinalTable.findResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 795 | xmlRes->file.name); |
| 796 | for (int sdkLevel : xmlLinker.getSdkLevels()) { |
| 797 | if (sdkLevel > xmlRes->file.config.sdkVersion && |
| 798 | shouldGenerateVersionedResource(result.value().entry, |
| 799 | xmlRes->file.config, |
| 800 | sdkLevel)) { |
| 801 | xmlRes->file.config.sdkVersion = sdkLevel; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 802 | |
| 803 | std::string genResourcePath = ResourceUtils::buildResourceFileName( |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 804 | xmlRes->file, mContext->getNameMangler()); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 805 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 806 | bool added = mFinalTable.addFileReference( |
| 807 | xmlRes->file.name, |
| 808 | xmlRes->file.config, |
| 809 | xmlRes->file.source, |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 810 | util::utf8ToUtf16(genResourcePath), |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 811 | mContext->getDiagnostics()); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 812 | if (!added) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 813 | error = true; |
| 814 | continue; |
| 815 | } |
| 816 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 817 | if (!flattenXml(xmlRes.get(), genResourcePath, sdkLevel, |
| 818 | archiveWriter.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 819 | error = true; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | } else { |
| 826 | error = true; |
| 827 | } |
| 828 | } else { |
| 829 | if (mOptions.verbose) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 830 | mContext->getDiagnostics()->note(DiagMessage() << "copying " << path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 833 | if (!copyFileToArchive(fileToMerge.file, fileToMerge.dstPath, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 834 | archiveWriter.get())) { |
| 835 | error = true; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | if (error) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 841 | mContext->getDiagnostics()->error(DiagMessage() << "failed linking file resources"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 842 | return 1; |
| 843 | } |
| 844 | |
| 845 | if (!mOptions.noAutoVersion) { |
| 846 | AutoVersioner versioner; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 847 | if (!versioner.consume(mContext, &mFinalTable)) { |
| 848 | mContext->getDiagnostics()->error(DiagMessage() << "failed versioning styles"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 849 | return 1; |
| 850 | } |
| 851 | } |
| 852 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 853 | if (!flattenTable(&mFinalTable, archiveWriter.get())) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 854 | mContext->getDiagnostics()->error(DiagMessage() << "failed to write resources.arsc"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 855 | return 1; |
| 856 | } |
| 857 | |
| 858 | if (mOptions.generateJavaClassPath) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 859 | JavaClassGeneratorOptions options; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 860 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 861 | |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 862 | if (mOptions.staticLib || mOptions.generateNonFinalIds) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 863 | options.useFinal = false; |
| 864 | } |
| 865 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 866 | const StringPiece16 actualPackage = mContext->getCompilationPackage(); |
| 867 | StringPiece16 outputPackage = mContext->getCompilationPackage(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 868 | if (mOptions.customJavaPackage) { |
| 869 | // Override the output java package to the custom one. |
| 870 | outputPackage = mOptions.customJavaPackage.value(); |
| 871 | } |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 872 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 873 | if (mOptions.privateSymbols) { |
| 874 | // If we defined a private symbols package, we only emit Public symbols |
| 875 | // to the original package, and private and public symbols to the private package. |
| 876 | |
| 877 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 878 | if (!writeJavaFile(&mFinalTable, mContext->getCompilationPackage(), |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 879 | outputPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 880 | return 1; |
| 881 | } |
| 882 | |
| 883 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 884 | outputPackage = mOptions.privateSymbols.value(); |
| 885 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 886 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 887 | if (!writeJavaFile(&mFinalTable, actualPackage, outputPackage, options)) { |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 888 | return 1; |
| 889 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 890 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 891 | for (const std::u16string& extraPackage : mOptions.extraJavaPackages) { |
| 892 | if (!writeJavaFile(&mFinalTable, actualPackage, extraPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 893 | return 1; |
| 894 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | if (mOptions.generateProguardRulesPath) { |
| 899 | if (!writeProguardFile(proguardKeepSet)) { |
| 900 | return 1; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | if (mOptions.verbose) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 905 | Debug::printTable(&mFinalTable); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 906 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 907 | return 0; |
| 908 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 909 | |
| 910 | private: |
| 911 | LinkOptions mOptions; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 912 | LinkContext* mContext; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 913 | ResourceTable mFinalTable; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 914 | |
| 915 | ResourceTable mLocalFileTable; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 916 | std::unique_ptr<TableMerger> mTableMerger; |
| 917 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 918 | // A pointer to the FileCollection representing the filesystem (not archives). |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 919 | io::FileCollection* mFileCollection; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 920 | |
| 921 | // 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] | 922 | std::vector<std::unique_ptr<io::IFileCollection>> mCollections; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 923 | }; |
| 924 | |
| 925 | int link(const std::vector<StringPiece>& args) { |
| 926 | LinkOptions options; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 927 | Maybe<std::string> privateSymbolsPackage; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 928 | Maybe<std::string> minSdkVersion, targetSdkVersion; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 929 | Maybe<std::string> renameManifestPackage, renameInstrumentationTargetPackage; |
| 930 | Maybe<std::string> versionCode, versionName; |
| 931 | Maybe<std::string> customJavaPackage; |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 932 | std::vector<std::string> extraJavaPackages; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 933 | Maybe<std::string> configs; |
Adam Lesinski | 8900aa8 | 2016-01-25 22:48:15 -0800 | [diff] [blame] | 934 | bool legacyXFlag = false; |
| 935 | bool requireLocalization = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 936 | Flags flags = Flags() |
| 937 | .requiredFlag("-o", "Output path", &options.outputPath) |
| 938 | .requiredFlag("--manifest", "Path to the Android manifest to build", |
| 939 | &options.manifestPath) |
| 940 | .optionalFlagList("-I", "Adds an Android APK to link against", &options.includePaths) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 941 | .optionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n" |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 942 | "The last conflicting resource given takes precedence.", |
| 943 | &options.overlayFiles) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 944 | .optionalFlag("--java", "Directory in which to generate R.java", |
| 945 | &options.generateJavaClassPath) |
| 946 | .optionalFlag("--proguard", "Output file for generated Proguard rules", |
| 947 | &options.generateProguardRulesPath) |
| 948 | .optionalSwitch("--no-auto-version", |
| 949 | "Disables automatic style and layout SDK versioning", |
| 950 | &options.noAutoVersion) |
Adam Lesinski | 8900aa8 | 2016-01-25 22:48:15 -0800 | [diff] [blame] | 951 | .optionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01", |
| 952 | &legacyXFlag) |
| 953 | .optionalSwitch("-z", "Require localization of strings marked 'suggested'", |
| 954 | &requireLocalization) |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 955 | .optionalFlag("-c", "Comma separated list of configurations to include. The default\n" |
| 956 | "is all configurations", &configs) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 957 | .optionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified " |
| 958 | "by -o", |
| 959 | &options.outputToDirectory) |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 960 | .optionalFlag("--min-sdk-version", "Default minimum SDK version to use for " |
| 961 | "AndroidManifest.xml", &minSdkVersion) |
| 962 | .optionalFlag("--target-sdk-version", "Default target SDK version to use for " |
| 963 | "AndroidManifest.xml", &targetSdkVersion) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 964 | .optionalFlag("--version-code", "Version code (integer) to inject into the " |
| 965 | "AndroidManifest.xml if none is present", &versionCode) |
| 966 | .optionalFlag("--version-name", "Version name to inject into the AndroidManifest.xml " |
| 967 | "if none is present", &versionName) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 968 | .optionalSwitch("--static-lib", "Generate a static Android library", &options.staticLib) |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 969 | .optionalSwitch("--non-final-ids", "Generates R.java without the final modifier.\n" |
| 970 | "This is implied when --static-lib is specified.", |
| 971 | &options.generateNonFinalIds) |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 972 | .optionalFlag("--private-symbols", "Package name to use when generating R.java for " |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 973 | "private symbols.\n" |
| 974 | "If not specified, public and private symbols will use the application's " |
| 975 | "package name", &privateSymbolsPackage) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 976 | .optionalFlag("--custom-package", "Custom Java package under which to generate R.java", |
| 977 | &customJavaPackage) |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 978 | .optionalFlagList("--extra-packages", "Generate the same R.java but with different " |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 979 | "package names", &extraJavaPackages) |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 980 | .optionalSwitch("--auto-add-overlay", "Allows the addition of new resources in " |
| 981 | "overlays without <add-resource> tags", &options.autoAddOverlay) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 982 | .optionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml", |
| 983 | &renameManifestPackage) |
| 984 | .optionalFlag("--rename-instrumentation-target-package", |
| 985 | "Changes the name of the target package for instrumentation. Most useful " |
| 986 | "when used\nin conjunction with --rename-manifest-package", |
| 987 | &renameInstrumentationTargetPackage) |
| 988 | .optionalFlagList("-0", "File extensions not to compress", |
| 989 | &options.extensionsToNotCompress) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 990 | .optionalSwitch("-v", "Enables verbose logging", &options.verbose); |
| 991 | |
| 992 | if (!flags.parse("aapt2 link", args, &std::cerr)) { |
| 993 | return 1; |
| 994 | } |
| 995 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 996 | LinkContext context; |
| 997 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 998 | if (privateSymbolsPackage) { |
| 999 | options.privateSymbols = util::utf8ToUtf16(privateSymbolsPackage.value()); |
| 1000 | } |
| 1001 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1002 | if (minSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1003 | options.manifestFixerOptions.minSdkVersionDefault = |
| 1004 | util::utf8ToUtf16(minSdkVersion.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | if (targetSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1008 | options.manifestFixerOptions.targetSdkVersionDefault = |
| 1009 | util::utf8ToUtf16(targetSdkVersion.value()); |
| 1010 | } |
| 1011 | |
| 1012 | if (renameManifestPackage) { |
| 1013 | options.manifestFixerOptions.renameManifestPackage = |
| 1014 | util::utf8ToUtf16(renameManifestPackage.value()); |
| 1015 | } |
| 1016 | |
| 1017 | if (renameInstrumentationTargetPackage) { |
| 1018 | options.manifestFixerOptions.renameInstrumentationTargetPackage = |
| 1019 | util::utf8ToUtf16(renameInstrumentationTargetPackage.value()); |
| 1020 | } |
| 1021 | |
| 1022 | if (versionCode) { |
| 1023 | options.manifestFixerOptions.versionCodeDefault = util::utf8ToUtf16(versionCode.value()); |
| 1024 | } |
| 1025 | |
| 1026 | if (versionName) { |
| 1027 | options.manifestFixerOptions.versionNameDefault = util::utf8ToUtf16(versionName.value()); |
| 1028 | } |
| 1029 | |
| 1030 | if (customJavaPackage) { |
| 1031 | options.customJavaPackage = util::utf8ToUtf16(customJavaPackage.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1034 | // Populate the set of extra packages for which to generate R.java. |
| 1035 | for (std::string& extraPackage : extraJavaPackages) { |
| 1036 | // A given package can actually be a colon separated list of packages. |
| 1037 | for (StringPiece package : util::split(extraPackage, ':')) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1038 | options.extraJavaPackages.insert(util::utf8ToUtf16(package)); |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1042 | AxisConfigFilter filter; |
| 1043 | if (configs) { |
| 1044 | for (const StringPiece& configStr : util::tokenize<char>(configs.value(), ',')) { |
| 1045 | ConfigDescription config; |
| 1046 | LocaleValue lv; |
| 1047 | if (lv.initFromFilterString(configStr)) { |
| 1048 | lv.writeTo(&config); |
| 1049 | } else if (!ConfigDescription::parse(configStr, &config)) { |
| 1050 | context.getDiagnostics()->error( |
| 1051 | DiagMessage() << "invalid config '" << configStr << "' for -c option"); |
| 1052 | return 1; |
| 1053 | } |
| 1054 | |
| 1055 | if (config.density != 0) { |
| 1056 | context.getDiagnostics()->warn( |
| 1057 | DiagMessage() << "ignoring density '" << config << "' for -c option"); |
| 1058 | } else { |
| 1059 | filter.addConfig(config); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | options.configFilter = &filter; |
| 1064 | } |
| 1065 | |
| 1066 | LinkCommand cmd(&context, options); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1067 | return cmd.run(flags.getArgs()); |
| 1068 | } |
| 1069 | |
| 1070 | } // namespace aapt |