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 | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 34 | #include "link/ProductFilter.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 35 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 36 | #include "link/ManifestFixer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | #include "link/TableMerger.h" |
| 38 | #include "process/IResourceTableConsumer.h" |
| 39 | #include "process/SymbolTable.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 40 | #include "proto/ProtoSerialize.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 41 | #include "split/TableSplitter.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | #include "unflatten/BinaryResourceParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | #include "util/Files.h" |
| 44 | #include "util/StringPiece.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 45 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 47 | #include <google/protobuf/io/coded_stream.h> |
| 48 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | #include <fstream> |
| 50 | #include <sys/stat.h> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | #include <vector> |
| 52 | |
| 53 | namespace aapt { |
| 54 | |
| 55 | struct LinkOptions { |
| 56 | std::string outputPath; |
| 57 | std::string manifestPath; |
| 58 | std::vector<std::string> includePaths; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 59 | std::vector<std::string> overlayFiles; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | Maybe<std::string> generateJavaClassPath; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 61 | Maybe<std::u16string> customJavaPackage; |
| 62 | std::set<std::u16string> extraJavaPackages; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | Maybe<std::string> generateProguardRulesPath; |
| 64 | bool noAutoVersion = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 65 | bool noVersionVectors = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | bool staticLib = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 67 | bool noStaticLibPackages = false; |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 68 | bool generateNonFinalIds = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | bool outputToDirectory = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 70 | bool autoAddOverlay = false; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 71 | bool doNotCompressAnything = false; |
| 72 | std::vector<std::string> extensionsToNotCompress; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 73 | Maybe<std::u16string> privateSymbols; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 74 | ManifestFixerOptions manifestFixerOptions; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 75 | std::unordered_set<std::string> products; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 76 | TableSplitterOptions tableSplitterOptions; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 79 | class LinkContext : public IAaptContext { |
| 80 | public: |
| 81 | LinkContext() : mNameMangler({}) { |
| 82 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | |
| 84 | IDiagnostics* getDiagnostics() override { |
| 85 | return &mDiagnostics; |
| 86 | } |
| 87 | |
| 88 | NameMangler* getNameMangler() override { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 89 | return &mNameMangler; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 92 | void setNameManglerPolicy(const NameManglerPolicy& policy) { |
| 93 | mNameMangler = NameMangler(policy); |
| 94 | } |
| 95 | |
| 96 | const std::u16string& getCompilationPackage() override { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | return mCompilationPackage; |
| 98 | } |
| 99 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 100 | void setCompilationPackage(const StringPiece16& packageName) { |
| 101 | mCompilationPackage = packageName.toString(); |
| 102 | } |
| 103 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 104 | uint8_t getPackageId() override { |
| 105 | return mPackageId; |
| 106 | } |
| 107 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 108 | void setPackageId(uint8_t id) { |
| 109 | mPackageId = id; |
| 110 | } |
| 111 | |
| 112 | SymbolTable* getExternalSymbols() override { |
| 113 | return &mSymbols; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 114 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 115 | |
| 116 | bool verbose() override { |
| 117 | return mVerbose; |
| 118 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 119 | |
| 120 | void setVerbose(bool val) { |
| 121 | mVerbose = val; |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | StdErrDiagnostics mDiagnostics; |
| 126 | NameMangler mNameMangler; |
| 127 | std::u16string mCompilationPackage; |
| 128 | uint8_t mPackageId = 0x0; |
| 129 | SymbolTable mSymbols; |
| 130 | bool mVerbose = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 133 | static bool copyFileToArchive(io::IFile* file, const std::string& outPath, |
| 134 | uint32_t compressionFlags, |
| 135 | IArchiveWriter* writer, IAaptContext* context) { |
| 136 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 137 | if (!data) { |
| 138 | context->getDiagnostics()->error(DiagMessage(file->getSource()) |
| 139 | << "failed to open file"); |
| 140 | return false; |
| 141 | } |
| 142 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 143 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data->data()); |
| 144 | size_t bufferSize = data->size(); |
| 145 | |
| 146 | // If the file ends with .flat, we must strip off the CompiledFileHeader from it. |
| 147 | if (util::stringEndsWith<char>(file->getSource().path, ".flat")) { |
| 148 | CompiledFileInputStream inputStream(data->data(), data->size()); |
| 149 | if (!inputStream.CompiledFile()) { |
| 150 | context->getDiagnostics()->error(DiagMessage(file->getSource()) |
| 151 | << "invalid compiled file header"); |
| 152 | return false; |
| 153 | } |
| 154 | buffer = reinterpret_cast<const uint8_t*>(inputStream.data()); |
| 155 | bufferSize = inputStream.size(); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | if (context->verbose()) { |
| 159 | context->getDiagnostics()->note(DiagMessage() << "writing " << outPath << " to archive"); |
| 160 | } |
| 161 | |
| 162 | if (writer->startEntry(outPath, compressionFlags)) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 163 | if (writer->writeEntry(buffer, bufferSize)) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 164 | if (writer->finishEntry()) { |
| 165 | return true; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | context->getDiagnostics()->error(DiagMessage() << "failed to write file " << outPath); |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | static bool flattenXml(xml::XmlResource* xmlRes, const StringPiece& path, Maybe<size_t> maxSdkLevel, |
| 175 | bool keepRawValues, IArchiveWriter* writer, IAaptContext* context) { |
| 176 | BigBuffer buffer(1024); |
| 177 | XmlFlattenerOptions options = {}; |
| 178 | options.keepRawValues = keepRawValues; |
| 179 | options.maxSdkLevel = maxSdkLevel; |
| 180 | XmlFlattener flattener(&buffer, options); |
| 181 | if (!flattener.consume(context, xmlRes)) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | if (context->verbose()) { |
| 186 | DiagMessage msg; |
| 187 | msg << "writing " << path << " to archive"; |
| 188 | if (maxSdkLevel) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 189 | msg << " maxSdkLevel=" << maxSdkLevel.value() << " keepRawValues=" << keepRawValues; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 190 | } |
| 191 | context->getDiagnostics()->note(msg); |
| 192 | } |
| 193 | |
| 194 | if (writer->startEntry(path, ArchiveEntry::kCompress)) { |
| 195 | if (writer->writeEntry(buffer)) { |
| 196 | if (writer->finishEntry()) { |
| 197 | return true; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | context->getDiagnostics()->error(DiagMessage() << "failed to write " << path << " to archive"); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | /*static std::unique_ptr<ResourceTable> loadTable(const Source& source, const void* data, size_t len, |
| 206 | IDiagnostics* diag) { |
| 207 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
| 208 | BinaryResourceParser parser(diag, table.get(), source, data, len); |
| 209 | if (!parser.parse()) { |
| 210 | return {}; |
| 211 | } |
| 212 | return table; |
| 213 | }*/ |
| 214 | |
| 215 | static std::unique_ptr<ResourceTable> loadTableFromPb(const Source& source, |
| 216 | const void* data, size_t len, |
| 217 | IDiagnostics* diag) { |
| 218 | pb::ResourceTable pbTable; |
| 219 | if (!pbTable.ParseFromArray(data, len)) { |
| 220 | diag->error(DiagMessage(source) << "invalid compiled table"); |
| 221 | return {}; |
| 222 | } |
| 223 | |
| 224 | std::unique_ptr<ResourceTable> table = deserializeTableFromPb(pbTable, source, diag); |
| 225 | if (!table) { |
| 226 | return {}; |
| 227 | } |
| 228 | return table; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Inflates an XML file from the source path. |
| 233 | */ |
| 234 | static std::unique_ptr<xml::XmlResource> loadXml(const std::string& path, IDiagnostics* diag) { |
| 235 | std::ifstream fin(path, std::ifstream::binary); |
| 236 | if (!fin) { |
| 237 | diag->error(DiagMessage(path) << strerror(errno)); |
| 238 | return {}; |
| 239 | } |
| 240 | return xml::inflate(&fin, diag, Source(path)); |
| 241 | } |
| 242 | |
| 243 | static std::unique_ptr<xml::XmlResource> loadBinaryXmlSkipFileExport(const Source& source, |
| 244 | const void* data, size_t len, |
| 245 | IDiagnostics* diag) { |
| 246 | CompiledFileInputStream inputStream(data, len); |
| 247 | if (!inputStream.CompiledFile()) { |
| 248 | diag->error(DiagMessage(source) << "invalid compiled file header"); |
| 249 | return {}; |
| 250 | } |
| 251 | |
| 252 | const uint8_t* xmlData = reinterpret_cast<const uint8_t*>(inputStream.data()); |
| 253 | const size_t xmlDataLen = inputStream.size(); |
| 254 | |
| 255 | std::unique_ptr<xml::XmlResource> xmlRes = xml::inflate(xmlData, xmlDataLen, diag, source); |
| 256 | if (!xmlRes) { |
| 257 | return {}; |
| 258 | } |
| 259 | return xmlRes; |
| 260 | } |
| 261 | |
| 262 | static std::unique_ptr<ResourceFile> loadFileExportHeader(const Source& source, |
| 263 | const void* data, size_t len, |
| 264 | IDiagnostics* diag) { |
| 265 | CompiledFileInputStream inputStream(data, len); |
| 266 | const pb::CompiledFile* pbFile = inputStream.CompiledFile(); |
| 267 | if (!pbFile) { |
| 268 | diag->error(DiagMessage(source) << "invalid compiled file header"); |
| 269 | return {}; |
| 270 | } |
| 271 | |
| 272 | std::unique_ptr<ResourceFile> resFile = deserializeCompiledFileFromPb(*pbFile, source, diag); |
| 273 | if (!resFile) { |
| 274 | return {}; |
| 275 | } |
| 276 | return resFile; |
| 277 | } |
| 278 | |
| 279 | struct ResourceFileFlattenerOptions { |
| 280 | bool noAutoVersion = false; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 281 | bool noVersionVectors = false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 282 | bool keepRawValues = false; |
| 283 | bool doNotCompressAnything = false; |
| 284 | std::vector<std::string> extensionsToNotCompress; |
| 285 | }; |
| 286 | |
| 287 | class ResourceFileFlattener { |
| 288 | public: |
| 289 | ResourceFileFlattener(const ResourceFileFlattenerOptions& options, |
| 290 | IAaptContext* context, proguard::KeepSet* keepSet) : |
| 291 | mOptions(options), mContext(context), mKeepSet(keepSet) { |
| 292 | } |
| 293 | |
| 294 | bool flatten(ResourceTable* table, IArchiveWriter* archiveWriter); |
| 295 | |
| 296 | private: |
| 297 | struct FileOperation { |
| 298 | io::IFile* fileToCopy; |
| 299 | std::unique_ptr<xml::XmlResource> xmlToFlatten; |
| 300 | std::string dstPath; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 301 | bool skipVersion = false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | uint32_t getCompressionFlags(const StringPiece& str); |
| 305 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 306 | bool linkAndVersionXmlFile(const ResourceEntry* entry, const ResourceFile& fileDesc, |
| 307 | io::IFile* file, ResourceTable* table, FileOperation* outFileOp); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 308 | |
| 309 | ResourceFileFlattenerOptions mOptions; |
| 310 | IAaptContext* mContext; |
| 311 | proguard::KeepSet* mKeepSet; |
| 312 | }; |
| 313 | |
| 314 | uint32_t ResourceFileFlattener::getCompressionFlags(const StringPiece& str) { |
| 315 | if (mOptions.doNotCompressAnything) { |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | for (const std::string& extension : mOptions.extensionsToNotCompress) { |
| 320 | if (util::stringEndsWith<char>(str, extension)) { |
| 321 | return 0; |
| 322 | } |
| 323 | } |
| 324 | return ArchiveEntry::kCompress; |
| 325 | } |
| 326 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 327 | bool ResourceFileFlattener::linkAndVersionXmlFile(const ResourceEntry* entry, |
| 328 | const ResourceFile& fileDesc, |
| 329 | io::IFile* file, |
| 330 | ResourceTable* table, |
| 331 | FileOperation* outFileOp) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 332 | const StringPiece srcPath = file->getSource().path; |
| 333 | if (mContext->verbose()) { |
| 334 | mContext->getDiagnostics()->note(DiagMessage() << "linking " << srcPath); |
| 335 | } |
| 336 | |
| 337 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 338 | if (!data) { |
| 339 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) << "failed to open file"); |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 340 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 343 | if (util::stringEndsWith<char>(srcPath, ".flat")) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 344 | outFileOp->xmlToFlatten = loadBinaryXmlSkipFileExport(file->getSource(), |
| 345 | data->data(), data->size(), |
| 346 | mContext->getDiagnostics()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 347 | } else { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 348 | outFileOp->xmlToFlatten = xml::inflate(data->data(), data->size(), |
| 349 | mContext->getDiagnostics(), |
| 350 | file->getSource()); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 353 | if (!outFileOp->xmlToFlatten) { |
| 354 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // Copy the the file description header. |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 358 | outFileOp->xmlToFlatten->file = fileDesc; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 359 | |
| 360 | XmlReferenceLinker xmlLinker; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 361 | if (!xmlLinker.consume(mContext, outFileOp->xmlToFlatten.get())) { |
| 362 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 365 | if (!proguard::collectProguardRules(outFileOp->xmlToFlatten->file.source, |
| 366 | outFileOp->xmlToFlatten.get(), mKeepSet)) { |
| 367 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | if (!mOptions.noAutoVersion) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 371 | if (mOptions.noVersionVectors) { |
| 372 | // Skip this if it is a vector or animated-vector. |
| 373 | xml::Element* el = xml::findRootElement(outFileOp->xmlToFlatten.get()); |
| 374 | if (el && el->namespaceUri.empty()) { |
| 375 | if (el->name == u"vector" || el->name == u"animated-vector") { |
| 376 | // We are NOT going to version this file. |
| 377 | outFileOp->skipVersion = true; |
| 378 | return true; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 383 | // Find the first SDK level used that is higher than this defined config and |
| 384 | // not superseded by a lower or equal SDK level resource. |
| 385 | for (int sdkLevel : xmlLinker.getSdkLevels()) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 386 | if (sdkLevel > outFileOp->xmlToFlatten->file.config.sdkVersion) { |
| 387 | if (!shouldGenerateVersionedResource(entry, outFileOp->xmlToFlatten->file.config, |
| 388 | sdkLevel)) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 389 | // If we shouldn't generate a versioned resource, stop checking. |
| 390 | break; |
| 391 | } |
| 392 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 393 | ResourceFile versionedFileDesc = outFileOp->xmlToFlatten->file; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 394 | versionedFileDesc.config.sdkVersion = (uint16_t) sdkLevel; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 395 | |
| 396 | if (mContext->verbose()) { |
| 397 | mContext->getDiagnostics()->note(DiagMessage(versionedFileDesc.source) |
| 398 | << "auto-versioning resource from config '" |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 399 | << outFileOp->xmlToFlatten->file.config |
| 400 | << "' -> '" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 401 | << versionedFileDesc.config << "'"); |
| 402 | } |
| 403 | |
| 404 | std::u16string genPath = util::utf8ToUtf16(ResourceUtils::buildResourceFileName( |
| 405 | versionedFileDesc, mContext->getNameMangler())); |
| 406 | |
| 407 | bool added = table->addFileReferenceAllowMangled(versionedFileDesc.name, |
| 408 | versionedFileDesc.config, |
| 409 | versionedFileDesc.source, |
| 410 | genPath, |
| 411 | file, |
| 412 | mContext->getDiagnostics()); |
| 413 | if (!added) { |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 414 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 415 | } |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | } |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 420 | return true; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Do not insert or remove any resources while executing in this function. It will |
| 425 | * corrupt the iteration order. |
| 426 | */ |
| 427 | bool ResourceFileFlattener::flatten(ResourceTable* table, IArchiveWriter* archiveWriter) { |
| 428 | bool error = false; |
| 429 | std::map<std::pair<ConfigDescription, StringPiece16>, FileOperation> configSortedFiles; |
| 430 | |
| 431 | for (auto& pkg : table->packages) { |
| 432 | for (auto& type : pkg->types) { |
| 433 | // Sort by config and name, so that we get better locality in the zip file. |
| 434 | configSortedFiles.clear(); |
| 435 | for (auto& entry : type->entries) { |
| 436 | // Iterate via indices because auto generated values can be inserted ahead of |
| 437 | // the value being processed. |
| 438 | for (size_t i = 0; i < entry->values.size(); i++) { |
| 439 | ResourceConfigValue* configValue = entry->values[i].get(); |
| 440 | |
| 441 | FileReference* fileRef = valueCast<FileReference>(configValue->value.get()); |
| 442 | if (!fileRef) { |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | io::IFile* file = fileRef->file; |
| 447 | if (!file) { |
| 448 | mContext->getDiagnostics()->error(DiagMessage(fileRef->getSource()) |
| 449 | << "file not found"); |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | FileOperation fileOp; |
| 454 | fileOp.dstPath = util::utf16ToUtf8(*fileRef->path); |
| 455 | |
| 456 | const StringPiece srcPath = file->getSource().path; |
| 457 | if (type->type != ResourceType::kRaw && |
| 458 | (util::stringEndsWith<char>(srcPath, ".xml.flat") || |
| 459 | util::stringEndsWith<char>(srcPath, ".xml"))) { |
| 460 | ResourceFile fileDesc; |
| 461 | fileDesc.config = configValue->config; |
| 462 | fileDesc.name = ResourceName(pkg->name, type->type, entry->name); |
| 463 | fileDesc.source = fileRef->getSource(); |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 464 | if (!linkAndVersionXmlFile(entry.get(), fileDesc, file, table, &fileOp)) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 465 | error = true; |
| 466 | continue; |
| 467 | } |
| 468 | |
| 469 | } else { |
| 470 | fileOp.fileToCopy = file; |
| 471 | } |
| 472 | |
| 473 | // NOTE(adamlesinski): Explicitly construct a StringPiece16 here, or else |
| 474 | // we end up copying the string in the std::make_pair() method, then creating |
| 475 | // a StringPiece16 from the copy, which would cause us to end up referencing |
| 476 | // garbage in the map. |
| 477 | const StringPiece16 entryName(entry->name); |
| 478 | configSortedFiles[std::make_pair(configValue->config, entryName)] = |
| 479 | std::move(fileOp); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (error) { |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | // Now flatten the sorted values. |
| 488 | for (auto& mapEntry : configSortedFiles) { |
| 489 | const ConfigDescription& config = mapEntry.first.first; |
| 490 | const FileOperation& fileOp = mapEntry.second; |
| 491 | |
| 492 | if (fileOp.xmlToFlatten) { |
| 493 | Maybe<size_t> maxSdkLevel; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 494 | if (!mOptions.noAutoVersion && !fileOp.skipVersion) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 495 | maxSdkLevel = std::max<size_t>(config.sdkVersion, 1u); |
| 496 | } |
| 497 | |
| 498 | bool result = flattenXml(fileOp.xmlToFlatten.get(), fileOp.dstPath, maxSdkLevel, |
| 499 | mOptions.keepRawValues, |
| 500 | archiveWriter, mContext); |
| 501 | if (!result) { |
| 502 | error = true; |
| 503 | } |
| 504 | } else { |
| 505 | bool result = copyFileToArchive(fileOp.fileToCopy, fileOp.dstPath, |
| 506 | getCompressionFlags(fileOp.dstPath), |
| 507 | archiveWriter, mContext); |
| 508 | if (!result) { |
| 509 | error = true; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | return !error; |
| 516 | } |
| 517 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 518 | class LinkCommand { |
| 519 | public: |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 520 | LinkCommand(LinkContext* context, const LinkOptions& options) : |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 521 | mOptions(options), mContext(context), mFinalTable(), |
| 522 | mFileCollection(util::make_unique<io::FileCollection>()) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 523 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 524 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 525 | /** |
| 526 | * Creates a SymbolTable that loads symbols from the various APKs and caches the |
| 527 | * results for faster lookup. |
| 528 | */ |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 529 | bool loadSymbolsFromIncludePaths() { |
| 530 | std::unique_ptr<AssetManagerSymbolSource> assetSource = |
| 531 | util::make_unique<AssetManagerSymbolSource>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 532 | for (const std::string& path : mOptions.includePaths) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 533 | if (mContext->verbose()) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 534 | mContext->getDiagnostics()->note(DiagMessage(path) << "loading include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 537 | // First try to load the file as a static lib. |
| 538 | std::string errorStr; |
| 539 | std::unique_ptr<ResourceTable> staticInclude = loadStaticLibrary(path, &errorStr); |
| 540 | if (staticInclude) { |
| 541 | if (!mOptions.staticLib) { |
| 542 | // Can't include static libraries when not building a static library. |
| 543 | mContext->getDiagnostics()->error( |
| 544 | DiagMessage(path) << "can't include static library when building app"); |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | // If we are using --no-static-lib-packages, we need to rename the package of this |
| 549 | // table to our compilation package. |
| 550 | if (mOptions.noStaticLibPackages) { |
| 551 | if (ResourceTablePackage* pkg = staticInclude->findPackageById(0x7f)) { |
| 552 | pkg->name = mContext->getCompilationPackage(); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | mContext->getExternalSymbols()->appendSource( |
| 557 | util::make_unique<ResourceTableSymbolSource>(staticInclude.get())); |
| 558 | |
| 559 | mStaticTableIncludes.push_back(std::move(staticInclude)); |
| 560 | |
| 561 | } else if (!errorStr.empty()) { |
| 562 | // We had an error with reading, so fail. |
| 563 | mContext->getDiagnostics()->error(DiagMessage(path) << errorStr); |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | if (!assetSource->addAssetPath(path)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 568 | mContext->getDiagnostics()->error( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 569 | DiagMessage(path) << "failed to load include path"); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 570 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 571 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 572 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 573 | |
| 574 | mContext->getExternalSymbols()->appendSource(std::move(assetSource)); |
| 575 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 578 | Maybe<AppInfo> extractAppInfoFromManifest(xml::XmlResource* xmlRes) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 579 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 580 | if (xml::Element* manifestEl = xml::findRootElement(xmlRes->root.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 581 | if (manifestEl->namespaceUri.empty() && manifestEl->name == u"manifest") { |
| 582 | if (xml::Attribute* packageAttr = manifestEl->findAttribute({}, u"package")) { |
| 583 | return AppInfo{ packageAttr->value }; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | return {}; |
| 588 | } |
| 589 | |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 590 | /** |
| 591 | * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked. |
| 592 | * Postcondition: ResourceTable has only one package left. All others are stripped, or there |
| 593 | * is an error and false is returned. |
| 594 | */ |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 595 | bool verifyNoExternalPackages() { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 596 | auto isExtPackageFunc = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 597 | return mContext->getCompilationPackage() != pkg->name || |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 598 | !pkg->id || |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 599 | pkg->id.value() != mContext->getPackageId(); |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 600 | }; |
| 601 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 602 | bool error = false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 603 | for (const auto& package : mFinalTable.packages) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 604 | if (isExtPackageFunc(package)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 605 | // We have a package that is not related to the one we're building! |
| 606 | for (const auto& type : package->types) { |
| 607 | for (const auto& entry : type->entries) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 608 | ResourceNameRef resName(package->name, type->type, entry->name); |
| 609 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 610 | for (const auto& configValue : entry->values) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 611 | // Special case the occurrence of an ID that is being generated for the |
| 612 | // 'android' package. This is due to legacy reasons. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 613 | if (valueCast<Id>(configValue->value.get()) && |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 614 | package->name == u"android") { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 615 | mContext->getDiagnostics()->warn( |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 616 | DiagMessage(configValue->value->getSource()) |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 617 | << "generated id '" << resName |
| 618 | << "' for external package '" << package->name |
| 619 | << "'"); |
| 620 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 621 | mContext->getDiagnostics()->error( |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 622 | DiagMessage(configValue->value->getSource()) |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 623 | << "defined resource '" << resName |
| 624 | << "' for external package '" << package->name |
| 625 | << "'"); |
| 626 | error = true; |
| 627 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 633 | |
| 634 | auto newEndIter = std::remove_if(mFinalTable.packages.begin(), mFinalTable.packages.end(), |
| 635 | isExtPackageFunc); |
| 636 | mFinalTable.packages.erase(newEndIter, mFinalTable.packages.end()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 637 | return !error; |
| 638 | } |
| 639 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 640 | /** |
| 641 | * Returns true if no IDs have been set, false otherwise. |
| 642 | */ |
| 643 | bool verifyNoIdsSet() { |
| 644 | for (const auto& package : mFinalTable.packages) { |
| 645 | for (const auto& type : package->types) { |
| 646 | if (type->id) { |
| 647 | mContext->getDiagnostics()->error(DiagMessage() << "type " << type->type |
| 648 | << " has ID " << std::hex |
| 649 | << (int) type->id.value() |
| 650 | << std::dec << " assigned"); |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | for (const auto& entry : type->entries) { |
| 655 | if (entry->id) { |
| 656 | ResourceNameRef resName(package->name, type->type, entry->name); |
| 657 | mContext->getDiagnostics()->error(DiagMessage() << "entry " << resName |
| 658 | << " has ID " << std::hex |
| 659 | << (int) entry->id.value() |
| 660 | << std::dec << " assigned"); |
| 661 | return false; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | return true; |
| 667 | } |
| 668 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 669 | std::unique_ptr<IArchiveWriter> makeArchiveWriter() { |
| 670 | if (mOptions.outputToDirectory) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 671 | return createDirectoryArchiveWriter(mContext->getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 672 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 673 | return createZipFileArchiveWriter(mContext->getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
| 677 | bool flattenTable(ResourceTable* table, IArchiveWriter* writer) { |
| 678 | BigBuffer buffer(1024); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 679 | TableFlattener flattener(&buffer); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 680 | if (!flattener.consume(mContext, table)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 681 | return false; |
| 682 | } |
| 683 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 684 | if (writer->startEntry("resources.arsc", ArchiveEntry::kAlign)) { |
| 685 | if (writer->writeEntry(buffer)) { |
| 686 | if (writer->finishEntry()) { |
| 687 | return true; |
| 688 | } |
| 689 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 690 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 691 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 692 | mContext->getDiagnostics()->error( |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 693 | DiagMessage() << "failed to write resources.arsc to archive"); |
| 694 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 697 | bool flattenTableToPb(ResourceTable* table, IArchiveWriter* writer) { |
| 698 | // Create the file/zip entry. |
| 699 | if (!writer->startEntry("resources.arsc.flat", 0)) { |
| 700 | mContext->getDiagnostics()->error(DiagMessage() << "failed to open"); |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | std::unique_ptr<pb::ResourceTable> pbTable = serializeTableToPb(table); |
| 705 | |
| 706 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream |
| 707 | // interface. |
| 708 | { |
| 709 | google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer); |
| 710 | |
| 711 | if (!pbTable->SerializeToZeroCopyStream(&adaptor)) { |
| 712 | mContext->getDiagnostics()->error(DiagMessage() << "failed to write"); |
| 713 | return false; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | if (!writer->finishEntry()) { |
| 718 | mContext->getDiagnostics()->error(DiagMessage() << "failed to finish entry"); |
| 719 | return false; |
| 720 | } |
| 721 | return true; |
| 722 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 723 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 724 | bool writeJavaFile(ResourceTable* table, const StringPiece16& packageNameToGenerate, |
| 725 | const StringPiece16& outPackage, JavaClassGeneratorOptions javaOptions) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 726 | if (!mOptions.generateJavaClassPath) { |
| 727 | return true; |
| 728 | } |
| 729 | |
| 730 | std::string outPath = mOptions.generateJavaClassPath.value(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 731 | file::appendPath(&outPath, file::packageToPath(util::utf16ToUtf8(outPackage))); |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 732 | if (!file::mkdirs(outPath)) { |
| 733 | mContext->getDiagnostics()->error( |
| 734 | DiagMessage() << "failed to create directory '" << outPath << "'"); |
| 735 | return false; |
| 736 | } |
| 737 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 738 | file::appendPath(&outPath, "R.java"); |
| 739 | |
| 740 | std::ofstream fout(outPath, std::ofstream::binary); |
| 741 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 742 | mContext->getDiagnostics()->error( |
| 743 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 744 | return false; |
| 745 | } |
| 746 | |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 747 | JavaClassGenerator generator(mContext, table, javaOptions); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 748 | if (!generator.generate(packageNameToGenerate, outPackage, &fout)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 749 | mContext->getDiagnostics()->error(DiagMessage(outPath) << generator.getError()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 750 | return false; |
| 751 | } |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 752 | |
| 753 | if (!fout) { |
| 754 | mContext->getDiagnostics()->error( |
| 755 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
| 756 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 757 | return true; |
| 758 | } |
| 759 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 760 | bool writeManifestJavaFile(xml::XmlResource* manifestXml) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 761 | if (!mOptions.generateJavaClassPath) { |
| 762 | return true; |
| 763 | } |
| 764 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame^] | 765 | std::unique_ptr<ClassDefinition> manifestClass = generateManifestClass( |
| 766 | mContext->getDiagnostics(), manifestXml); |
| 767 | |
| 768 | if (!manifestClass) { |
| 769 | // Something bad happened, but we already logged it, so exit. |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | if (manifestClass->empty()) { |
| 774 | // Empty Manifest class, no need to generate it. |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | const std::string packageUtf8 = util::utf16ToUtf8(mContext->getCompilationPackage()); |
| 779 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 780 | std::string outPath = mOptions.generateJavaClassPath.value(); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame^] | 781 | file::appendPath(&outPath, file::packageToPath(packageUtf8)); |
| 782 | |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 783 | if (!file::mkdirs(outPath)) { |
| 784 | mContext->getDiagnostics()->error( |
| 785 | DiagMessage() << "failed to create directory '" << outPath << "'"); |
| 786 | return false; |
| 787 | } |
| 788 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 789 | file::appendPath(&outPath, "Manifest.java"); |
| 790 | |
| 791 | std::ofstream fout(outPath, std::ofstream::binary); |
| 792 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 793 | mContext->getDiagnostics()->error( |
| 794 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 795 | return false; |
| 796 | } |
| 797 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame^] | 798 | if (!ClassDefinition::writeJavaFile(manifestClass.get(), packageUtf8, true, &fout)) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 799 | mContext->getDiagnostics()->error( |
| 800 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 801 | return false; |
| 802 | } |
| 803 | return true; |
| 804 | } |
| 805 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 806 | bool writeProguardFile(const proguard::KeepSet& keepSet) { |
| 807 | if (!mOptions.generateProguardRulesPath) { |
| 808 | return true; |
| 809 | } |
| 810 | |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 811 | const std::string& outPath = mOptions.generateProguardRulesPath.value(); |
| 812 | std::ofstream fout(outPath, std::ofstream::binary); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 813 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 814 | mContext->getDiagnostics()->error( |
| 815 | DiagMessage() << "failed to open '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 816 | return false; |
| 817 | } |
| 818 | |
| 819 | proguard::writeKeepSet(&fout, keepSet); |
| 820 | if (!fout) { |
Adam Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 821 | mContext->getDiagnostics()->error( |
| 822 | DiagMessage() << "failed writing to '" << outPath << "': " << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 823 | return false; |
| 824 | } |
| 825 | return true; |
| 826 | } |
| 827 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 828 | std::unique_ptr<ResourceTable> loadStaticLibrary(const std::string& input, |
| 829 | std::string* outError) { |
| 830 | std::unique_ptr<io::ZipFileCollection> collection = io::ZipFileCollection::create( |
| 831 | input, outError); |
| 832 | if (!collection) { |
| 833 | return {}; |
| 834 | } |
| 835 | return loadTablePbFromCollection(collection.get()); |
| 836 | } |
| 837 | |
| 838 | std::unique_ptr<ResourceTable> loadTablePbFromCollection(io::IFileCollection* collection) { |
| 839 | io::IFile* file = collection->findFile("resources.arsc.flat"); |
| 840 | if (!file) { |
| 841 | return {}; |
| 842 | } |
| 843 | |
| 844 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 845 | return loadTableFromPb(file->getSource(), data->data(), data->size(), |
| 846 | mContext->getDiagnostics()); |
| 847 | } |
| 848 | |
| 849 | bool mergeStaticLibrary(const std::string& input, bool override) { |
| 850 | if (mContext->verbose()) { |
| 851 | mContext->getDiagnostics()->note(DiagMessage() << "merging static library " << input); |
| 852 | } |
| 853 | |
| 854 | std::string errorStr; |
| 855 | std::unique_ptr<io::ZipFileCollection> collection = |
| 856 | io::ZipFileCollection::create(input, &errorStr); |
| 857 | if (!collection) { |
| 858 | mContext->getDiagnostics()->error(DiagMessage(input) << errorStr); |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | std::unique_ptr<ResourceTable> table = loadTablePbFromCollection(collection.get()); |
| 863 | if (!table) { |
| 864 | mContext->getDiagnostics()->error(DiagMessage(input) << "invalid static library"); |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | ResourceTablePackage* pkg = table->findPackageById(0x7f); |
| 869 | if (!pkg) { |
| 870 | mContext->getDiagnostics()->error(DiagMessage(input) |
| 871 | << "static library has no package"); |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | bool result; |
| 876 | if (mOptions.noStaticLibPackages) { |
| 877 | // Merge all resources as if they were in the compilation package. This is the old |
| 878 | // behaviour of aapt. |
| 879 | |
| 880 | // Add the package to the set of --extra-packages so we emit an R.java for each |
| 881 | // library package. |
| 882 | if (!pkg->name.empty()) { |
| 883 | mOptions.extraJavaPackages.insert(pkg->name); |
| 884 | } |
| 885 | |
| 886 | pkg->name = u""; |
| 887 | if (override) { |
| 888 | result = mTableMerger->mergeOverlay(Source(input), table.get(), collection.get()); |
| 889 | } else { |
| 890 | result = mTableMerger->merge(Source(input), table.get(), collection.get()); |
| 891 | } |
| 892 | |
| 893 | } else { |
| 894 | // This is the proper way to merge libraries, where the package name is preserved |
| 895 | // and resource names are mangled. |
| 896 | result = mTableMerger->mergeAndMangle(Source(input), pkg->name, table.get(), |
| 897 | collection.get()); |
| 898 | } |
| 899 | |
| 900 | if (!result) { |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | // Make sure to move the collection into the set of IFileCollections. |
| 905 | mCollections.push_back(std::move(collection)); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 906 | return true; |
| 907 | } |
| 908 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 909 | bool mergeResourceTable(io::IFile* file, bool override) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 910 | if (mContext->verbose()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 911 | mContext->getDiagnostics()->note(DiagMessage() << "merging resource table " |
| 912 | << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 915 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 916 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 917 | mContext->getDiagnostics()->error(DiagMessage(file->getSource()) |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 918 | << "failed to open file"); |
| 919 | return false; |
| 920 | } |
| 921 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 922 | std::unique_ptr<ResourceTable> table = loadTableFromPb(file->getSource(), |
| 923 | data->data(), data->size(), |
| 924 | mContext->getDiagnostics()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 925 | if (!table) { |
| 926 | return false; |
| 927 | } |
| 928 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 929 | bool result = false; |
| 930 | if (override) { |
| 931 | result = mTableMerger->mergeOverlay(file->getSource(), table.get()); |
| 932 | } else { |
| 933 | result = mTableMerger->merge(file->getSource(), table.get()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 934 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 935 | return result; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 938 | bool mergeCompiledFile(io::IFile* file, ResourceFile* fileDesc, bool override) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 939 | if (mContext->verbose()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 940 | mContext->getDiagnostics()->note(DiagMessage() << "merging compiled file " |
| 941 | << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 944 | bool result = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 945 | if (override) { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 946 | result = mTableMerger->mergeFileOverlay(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 947 | } else { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 948 | result = mTableMerger->mergeFile(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 951 | if (!result) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 952 | return false; |
| 953 | } |
| 954 | |
| 955 | // Add the exports of this file to the table. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 956 | for (SourcedResourceName& exportedSymbol : fileDesc->exportedSymbols) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 957 | if (exportedSymbol.name.package.empty()) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 958 | exportedSymbol.name.package = mContext->getCompilationPackage(); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | ResourceNameRef resName = exportedSymbol.name; |
| 962 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 963 | Maybe<ResourceName> mangledName = mContext->getNameMangler()->mangleName( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 964 | exportedSymbol.name); |
| 965 | if (mangledName) { |
| 966 | resName = mangledName.value(); |
| 967 | } |
| 968 | |
| 969 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 970 | id->setSource(fileDesc->source.withLine(exportedSymbol.line)); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 971 | bool result = mFinalTable.addResourceAllowMangled( |
| 972 | resName, ConfigDescription::defaultConfig(), std::string(), std::move(id), |
| 973 | mContext->getDiagnostics()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 974 | if (!result) { |
| 975 | return false; |
| 976 | } |
| 977 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 978 | return true; |
| 979 | } |
| 980 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 981 | /** |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 982 | * Takes a path to load as a ZIP file and merges the files within into the master ResourceTable. |
| 983 | * If override is true, conflicting resources are allowed to override each other, in order of |
| 984 | * last seen. |
| 985 | * |
| 986 | * An io::IFileCollection is created from the ZIP file and added to the set of |
| 987 | * io::IFileCollections that are open. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 988 | */ |
| 989 | bool mergeArchive(const std::string& input, bool override) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 990 | if (mContext->verbose()) { |
| 991 | mContext->getDiagnostics()->note(DiagMessage() << "merging archive " << input); |
| 992 | } |
| 993 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 994 | std::string errorStr; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 995 | std::unique_ptr<io::ZipFileCollection> collection = |
| 996 | io::ZipFileCollection::create(input, &errorStr); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 997 | if (!collection) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 998 | mContext->getDiagnostics()->error(DiagMessage(input) << errorStr); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | bool error = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1003 | for (auto iter = collection->iterator(); iter->hasNext(); ) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1004 | if (!mergeFile(iter->next(), override)) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1005 | error = true; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | // Make sure to move the collection into the set of IFileCollections. |
| 1010 | mCollections.push_back(std::move(collection)); |
| 1011 | return !error; |
| 1012 | } |
| 1013 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1014 | /** |
| 1015 | * Takes a path to load and merge into the master ResourceTable. If override is true, |
| 1016 | * conflicting resources are allowed to override each other, in order of last seen. |
| 1017 | * |
| 1018 | * If the file path ends with .flata, .jar, .jack, or .zip the file is treated as ZIP archive |
| 1019 | * and the files within are merged individually. |
| 1020 | * |
| 1021 | * Otherwise the files is processed on its own. |
| 1022 | */ |
| 1023 | bool mergePath(const std::string& path, bool override) { |
Adam Lesinski | 656a577 | 2016-01-14 15:17:41 -0800 | [diff] [blame] | 1024 | if (util::stringEndsWith<char>(path, ".flata") || |
| 1025 | util::stringEndsWith<char>(path, ".jar") || |
| 1026 | util::stringEndsWith<char>(path, ".jack") || |
| 1027 | util::stringEndsWith<char>(path, ".zip")) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1028 | return mergeArchive(path, override); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1029 | } else if (util::stringEndsWith<char>(path, ".apk")) { |
| 1030 | return mergeStaticLibrary(path, override); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | io::IFile* file = mFileCollection->insertFile(path); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1034 | return mergeFile(file, override); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1037 | /** |
| 1038 | * Takes a file to load and merge into the master ResourceTable. If override is true, |
| 1039 | * conflicting resources are allowed to override each other, in order of last seen. |
| 1040 | * |
| 1041 | * If the file ends with .arsc.flat, then it is loaded as a ResourceTable and merged into the |
| 1042 | * master ResourceTable. If the file ends with .flat, then it is treated like a compiled file |
| 1043 | * and the header data is read and merged into the final ResourceTable. |
| 1044 | * |
| 1045 | * All other file types are ignored. This is because these files could be coming from a zip, |
| 1046 | * where we could have other files like classes.dex. |
| 1047 | */ |
| 1048 | bool mergeFile(io::IFile* file, bool override) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1049 | const Source& src = file->getSource(); |
| 1050 | if (util::stringEndsWith<char>(src.path, ".arsc.flat")) { |
| 1051 | return mergeResourceTable(file, override); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1052 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1053 | } else if (util::stringEndsWith<char>(src.path, ".flat")){ |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1054 | // Try opening the file and looking for an Export header. |
| 1055 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 1056 | if (!data) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1057 | mContext->getDiagnostics()->error(DiagMessage(src) << "failed to open"); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | std::unique_ptr<ResourceFile> resourceFile = loadFileExportHeader( |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1062 | src, data->data(), data->size(), mContext->getDiagnostics()); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1063 | if (resourceFile) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1064 | return mergeCompiledFile(file, resourceFile.get(), override); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1065 | } |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 1066 | return false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1067 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1068 | |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 1069 | // Ignore non .flat files. This could be classes.dex or something else that happens |
| 1070 | // to be in an archive. |
| 1071 | return true; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1074 | int run(const std::vector<std::string>& inputFiles) { |
| 1075 | // Load the AndroidManifest.xml |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1076 | std::unique_ptr<xml::XmlResource> manifestXml = loadXml(mOptions.manifestPath, |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1077 | mContext->getDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1078 | if (!manifestXml) { |
| 1079 | return 1; |
| 1080 | } |
| 1081 | |
| 1082 | if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get())) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1083 | mContext->setCompilationPackage(maybeAppInfo.value().package); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1084 | } else { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1085 | mContext->getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1086 | << "no package specified in <manifest> tag"); |
| 1087 | return 1; |
| 1088 | } |
| 1089 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1090 | if (!util::isJavaPackageName(mContext->getCompilationPackage())) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1091 | mContext->getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1092 | << "invalid package name '" |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1093 | << mContext->getCompilationPackage() |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1094 | << "'"); |
| 1095 | return 1; |
| 1096 | } |
| 1097 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1098 | mContext->setNameManglerPolicy(NameManglerPolicy{ mContext->getCompilationPackage() }); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1099 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1100 | if (mContext->getCompilationPackage() == u"android") { |
| 1101 | mContext->setPackageId(0x01); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1102 | } else { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1103 | mContext->setPackageId(0x7f); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1106 | if (!loadSymbolsFromIncludePaths()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1107 | return 1; |
| 1108 | } |
| 1109 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1110 | TableMergerOptions tableMergerOptions; |
| 1111 | tableMergerOptions.autoAddOverlay = mOptions.autoAddOverlay; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1112 | mTableMerger = util::make_unique<TableMerger>(mContext, &mFinalTable, tableMergerOptions); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1113 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1114 | if (mContext->verbose()) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1115 | mContext->getDiagnostics()->note( |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1116 | DiagMessage() << "linking package '" << mContext->getCompilationPackage() |
| 1117 | << "' with package ID " << std::hex |
| 1118 | << (int) mContext->getPackageId()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1121 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1122 | for (const std::string& input : inputFiles) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1123 | if (!mergePath(input, false)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1124 | mContext->getDiagnostics()->error(DiagMessage() << "failed parsing input"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1125 | return 1; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1126 | } |
| 1127 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1128 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1129 | for (const std::string& input : mOptions.overlayFiles) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1130 | if (!mergePath(input, true)) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1131 | mContext->getDiagnostics()->error(DiagMessage() << "failed parsing overlays"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1132 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1136 | if (!verifyNoExternalPackages()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1137 | return 1; |
| 1138 | } |
| 1139 | |
| 1140 | if (!mOptions.staticLib) { |
| 1141 | PrivateAttributeMover mover; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1142 | if (!mover.consume(mContext, &mFinalTable)) { |
| 1143 | mContext->getDiagnostics()->error( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1144 | DiagMessage() << "failed moving private attributes"); |
| 1145 | return 1; |
| 1146 | } |
| 1147 | } |
| 1148 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1149 | if (!mOptions.staticLib) { |
| 1150 | // Assign IDs if we are building a regular app. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1151 | IdAssigner idAssigner; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1152 | if (!idAssigner.consume(mContext, &mFinalTable)) { |
| 1153 | mContext->getDiagnostics()->error(DiagMessage() << "failed assigning IDs"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1154 | return 1; |
| 1155 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1156 | } else { |
| 1157 | // Static libs are merged with other apps, and ID collisions are bad, so verify that |
| 1158 | // no IDs have been set. |
| 1159 | if (!verifyNoIdsSet()) { |
| 1160 | return 1; |
| 1161 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1164 | // Add the names to mangle based on our source merge earlier. |
| 1165 | mContext->setNameManglerPolicy(NameManglerPolicy{ |
| 1166 | mContext->getCompilationPackage(), mTableMerger->getMergedPackages() }); |
| 1167 | |
| 1168 | // Add our table to the symbol table. |
| 1169 | mContext->getExternalSymbols()->prependSource( |
| 1170 | util::make_unique<ResourceTableSymbolSource>(&mFinalTable)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1171 | |
| 1172 | { |
| 1173 | ReferenceLinker linker; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1174 | if (!linker.consume(mContext, &mFinalTable)) { |
| 1175 | mContext->getDiagnostics()->error(DiagMessage() << "failed linking references"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1176 | return 1; |
| 1177 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1178 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1179 | if (mOptions.staticLib) { |
| 1180 | if (!mOptions.products.empty()) { |
| 1181 | mContext->getDiagnostics()->warn( |
| 1182 | DiagMessage() << "can't select products when building static library"); |
| 1183 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1184 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1185 | if (mOptions.tableSplitterOptions.configFilter != nullptr || |
| 1186 | mOptions.tableSplitterOptions.preferredDensity) { |
| 1187 | mContext->getDiagnostics()->warn( |
| 1188 | DiagMessage() << "can't strip resources when building static library"); |
| 1189 | } |
| 1190 | } else { |
| 1191 | ProductFilter productFilter(mOptions.products); |
| 1192 | if (!productFilter.consume(mContext, &mFinalTable)) { |
| 1193 | mContext->getDiagnostics()->error(DiagMessage() << "failed stripping products"); |
| 1194 | return 1; |
| 1195 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1196 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1197 | // TODO(adamlesinski): Actually pass in split constraints and handle splits at the file |
| 1198 | // level. |
| 1199 | TableSplitter tableSplitter({}, mOptions.tableSplitterOptions); |
| 1200 | if (!tableSplitter.verifySplitConstraints(mContext)) { |
| 1201 | return 1; |
| 1202 | } |
| 1203 | tableSplitter.splitTable(&mFinalTable); |
| 1204 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | proguard::KeepSet proguardKeepSet; |
| 1208 | |
| 1209 | std::unique_ptr<IArchiveWriter> archiveWriter = makeArchiveWriter(); |
| 1210 | if (!archiveWriter) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1211 | mContext->getDiagnostics()->error(DiagMessage() << "failed to create archive"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1212 | return 1; |
| 1213 | } |
| 1214 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1215 | bool error = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1216 | { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1217 | ManifestFixer manifestFixer(mOptions.manifestFixerOptions); |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1218 | if (!manifestFixer.consume(mContext, manifestXml.get())) { |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1219 | error = true; |
| 1220 | } |
| 1221 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1222 | // AndroidManifest.xml has no resource name, but the CallSite is built from the name |
| 1223 | // (aka, which package the AndroidManifest.xml is coming from). |
| 1224 | // So we give it a package name so it can see local resources. |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1225 | manifestXml->file.name.package = mContext->getCompilationPackage(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1226 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1227 | XmlReferenceLinker manifestLinker; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1228 | if (manifestLinker.consume(mContext, manifestXml.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1229 | if (!proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath), |
| 1230 | manifestXml.get(), |
| 1231 | &proguardKeepSet)) { |
| 1232 | error = true; |
| 1233 | } |
| 1234 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1235 | if (mOptions.generateJavaClassPath) { |
| 1236 | if (!writeManifestJavaFile(manifestXml.get())) { |
| 1237 | error = true; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1241 | const bool keepRawValues = mOptions.staticLib; |
| 1242 | bool result = flattenXml(manifestXml.get(), "AndroidManifest.xml", {}, |
| 1243 | keepRawValues, archiveWriter.get(), mContext); |
| 1244 | if (!result) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1245 | error = true; |
| 1246 | } |
| 1247 | } else { |
| 1248 | error = true; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1252 | if (error) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1253 | mContext->getDiagnostics()->error(DiagMessage() << "failed processing manifest"); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1254 | return 1; |
| 1255 | } |
| 1256 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1257 | ResourceFileFlattenerOptions fileFlattenerOptions; |
| 1258 | fileFlattenerOptions.keepRawValues = mOptions.staticLib; |
| 1259 | fileFlattenerOptions.doNotCompressAnything = mOptions.doNotCompressAnything; |
| 1260 | fileFlattenerOptions.extensionsToNotCompress = mOptions.extensionsToNotCompress; |
| 1261 | fileFlattenerOptions.noAutoVersion = mOptions.noAutoVersion; |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 1262 | fileFlattenerOptions.noVersionVectors = mOptions.noVersionVectors; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1263 | ResourceFileFlattener fileFlattener(fileFlattenerOptions, mContext, &proguardKeepSet); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 1264 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1265 | if (!fileFlattener.flatten(&mFinalTable, archiveWriter.get())) { |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1266 | mContext->getDiagnostics()->error(DiagMessage() << "failed linking file resources"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1267 | return 1; |
| 1268 | } |
| 1269 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 1270 | if (!mOptions.noAutoVersion) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1271 | AutoVersioner versioner; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1272 | if (!versioner.consume(mContext, &mFinalTable)) { |
| 1273 | mContext->getDiagnostics()->error(DiagMessage() << "failed versioning styles"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1274 | return 1; |
| 1275 | } |
| 1276 | } |
| 1277 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1278 | if (mOptions.staticLib) { |
| 1279 | if (!flattenTableToPb(&mFinalTable, archiveWriter.get())) { |
| 1280 | mContext->getDiagnostics()->error(DiagMessage() |
| 1281 | << "failed to write resources.arsc.flat"); |
| 1282 | return 1; |
| 1283 | } |
| 1284 | } else { |
| 1285 | if (!flattenTable(&mFinalTable, archiveWriter.get())) { |
| 1286 | mContext->getDiagnostics()->error(DiagMessage() |
| 1287 | << "failed to write resources.arsc"); |
| 1288 | return 1; |
| 1289 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | if (mOptions.generateJavaClassPath) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1293 | JavaClassGeneratorOptions options; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1294 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 1295 | |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 1296 | if (mOptions.staticLib || mOptions.generateNonFinalIds) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1297 | options.useFinal = false; |
| 1298 | } |
| 1299 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1300 | const StringPiece16 actualPackage = mContext->getCompilationPackage(); |
| 1301 | StringPiece16 outputPackage = mContext->getCompilationPackage(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1302 | if (mOptions.customJavaPackage) { |
| 1303 | // Override the output java package to the custom one. |
| 1304 | outputPackage = mOptions.customJavaPackage.value(); |
| 1305 | } |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1306 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1307 | if (mOptions.privateSymbols) { |
| 1308 | // If we defined a private symbols package, we only emit Public symbols |
| 1309 | // to the original package, and private and public symbols to the private package. |
| 1310 | |
| 1311 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1312 | if (!writeJavaFile(&mFinalTable, mContext->getCompilationPackage(), |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1313 | outputPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1314 | return 1; |
| 1315 | } |
| 1316 | |
| 1317 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1318 | outputPackage = mOptions.privateSymbols.value(); |
| 1319 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1320 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1321 | if (!writeJavaFile(&mFinalTable, actualPackage, outputPackage, options)) { |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1322 | return 1; |
| 1323 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1324 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1325 | for (const std::u16string& extraPackage : mOptions.extraJavaPackages) { |
| 1326 | if (!writeJavaFile(&mFinalTable, actualPackage, extraPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1327 | return 1; |
| 1328 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (mOptions.generateProguardRulesPath) { |
| 1333 | if (!writeProguardFile(proguardKeepSet)) { |
| 1334 | return 1; |
| 1335 | } |
| 1336 | } |
| 1337 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1338 | if (mContext->verbose()) { |
| 1339 | DebugPrintTableOptions debugPrintTableOptions; |
| 1340 | debugPrintTableOptions.showSources = true; |
| 1341 | Debug::printTable(&mFinalTable, debugPrintTableOptions); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1342 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1345 | |
| 1346 | private: |
| 1347 | LinkOptions mOptions; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1348 | LinkContext* mContext; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1349 | ResourceTable mFinalTable; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1350 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1351 | std::unique_ptr<TableMerger> mTableMerger; |
| 1352 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1353 | // A pointer to the FileCollection representing the filesystem (not archives). |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1354 | std::unique_ptr<io::FileCollection> mFileCollection; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1355 | |
| 1356 | // 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] | 1357 | std::vector<std::unique_ptr<io::IFileCollection>> mCollections; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1358 | |
| 1359 | // A vector of ResourceTables. This is here to retain ownership, so that the SymbolTable |
| 1360 | // can use these. |
| 1361 | std::vector<std::unique_ptr<ResourceTable>> mStaticTableIncludes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1362 | }; |
| 1363 | |
| 1364 | int link(const std::vector<StringPiece>& args) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1365 | LinkContext context; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1366 | LinkOptions options; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1367 | Maybe<std::string> privateSymbolsPackage; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1368 | Maybe<std::string> minSdkVersion, targetSdkVersion; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1369 | Maybe<std::string> renameManifestPackage, renameInstrumentationTargetPackage; |
| 1370 | Maybe<std::string> versionCode, versionName; |
| 1371 | Maybe<std::string> customJavaPackage; |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1372 | std::vector<std::string> extraJavaPackages; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1373 | Maybe<std::string> configs; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1374 | Maybe<std::string> preferredDensity; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1375 | Maybe<std::string> productList; |
Adam Lesinski | 8900aa8 | 2016-01-25 22:48:15 -0800 | [diff] [blame] | 1376 | bool legacyXFlag = false; |
| 1377 | bool requireLocalization = false; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1378 | bool verbose = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1379 | Flags flags = Flags() |
| 1380 | .requiredFlag("-o", "Output path", &options.outputPath) |
| 1381 | .requiredFlag("--manifest", "Path to the Android manifest to build", |
| 1382 | &options.manifestPath) |
| 1383 | .optionalFlagList("-I", "Adds an Android APK to link against", &options.includePaths) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1384 | .optionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n" |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1385 | "The last conflicting resource given takes precedence.", |
| 1386 | &options.overlayFiles) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1387 | .optionalFlag("--java", "Directory in which to generate R.java", |
| 1388 | &options.generateJavaClassPath) |
| 1389 | .optionalFlag("--proguard", "Output file for generated Proguard rules", |
| 1390 | &options.generateProguardRulesPath) |
| 1391 | .optionalSwitch("--no-auto-version", |
| 1392 | "Disables automatic style and layout SDK versioning", |
| 1393 | &options.noAutoVersion) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1394 | .optionalSwitch("--no-version-vectors", |
| 1395 | "Disables automatic versioning of vector drawables. Use this only\n" |
| 1396 | "when building with vector drawable support library", |
| 1397 | &options.noVersionVectors) |
Adam Lesinski | 8900aa8 | 2016-01-25 22:48:15 -0800 | [diff] [blame] | 1398 | .optionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01", |
| 1399 | &legacyXFlag) |
| 1400 | .optionalSwitch("-z", "Require localization of strings marked 'suggested'", |
| 1401 | &requireLocalization) |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1402 | .optionalFlag("-c", "Comma separated list of configurations to include. The default\n" |
| 1403 | "is all configurations", &configs) |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1404 | .optionalFlag("--preferred-density", |
| 1405 | "Selects the closest matching density and strips out all others.", |
| 1406 | &preferredDensity) |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1407 | .optionalFlag("--product", "Comma separated list of product names to keep", |
| 1408 | &productList) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1409 | .optionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified " |
| 1410 | "by -o", |
| 1411 | &options.outputToDirectory) |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1412 | .optionalFlag("--min-sdk-version", "Default minimum SDK version to use for " |
| 1413 | "AndroidManifest.xml", &minSdkVersion) |
| 1414 | .optionalFlag("--target-sdk-version", "Default target SDK version to use for " |
| 1415 | "AndroidManifest.xml", &targetSdkVersion) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1416 | .optionalFlag("--version-code", "Version code (integer) to inject into the " |
| 1417 | "AndroidManifest.xml if none is present", &versionCode) |
| 1418 | .optionalFlag("--version-name", "Version name to inject into the AndroidManifest.xml " |
| 1419 | "if none is present", &versionName) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1420 | .optionalSwitch("--static-lib", "Generate a static Android library", &options.staticLib) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1421 | .optionalSwitch("--no-static-lib-packages", |
| 1422 | "Merge all library resources under the app's package", |
| 1423 | &options.noStaticLibPackages) |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame] | 1424 | .optionalSwitch("--non-final-ids", "Generates R.java without the final modifier.\n" |
| 1425 | "This is implied when --static-lib is specified.", |
| 1426 | &options.generateNonFinalIds) |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1427 | .optionalFlag("--private-symbols", "Package name to use when generating R.java for " |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1428 | "private symbols.\n" |
| 1429 | "If not specified, public and private symbols will use the application's " |
| 1430 | "package name", &privateSymbolsPackage) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1431 | .optionalFlag("--custom-package", "Custom Java package under which to generate R.java", |
| 1432 | &customJavaPackage) |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 1433 | .optionalFlagList("--extra-packages", "Generate the same R.java but with different " |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1434 | "package names", &extraJavaPackages) |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1435 | .optionalSwitch("--auto-add-overlay", "Allows the addition of new resources in " |
| 1436 | "overlays without <add-resource> tags", &options.autoAddOverlay) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1437 | .optionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml", |
| 1438 | &renameManifestPackage) |
| 1439 | .optionalFlag("--rename-instrumentation-target-package", |
| 1440 | "Changes the name of the target package for instrumentation. Most useful " |
| 1441 | "when used\nin conjunction with --rename-manifest-package", |
| 1442 | &renameInstrumentationTargetPackage) |
| 1443 | .optionalFlagList("-0", "File extensions not to compress", |
| 1444 | &options.extensionsToNotCompress) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1445 | .optionalSwitch("-v", "Enables verbose logging", &verbose); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1446 | |
| 1447 | if (!flags.parse("aapt2 link", args, &std::cerr)) { |
| 1448 | return 1; |
| 1449 | } |
| 1450 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 1451 | if (verbose) { |
| 1452 | context.setVerbose(verbose); |
| 1453 | } |
| 1454 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 1455 | if (privateSymbolsPackage) { |
| 1456 | options.privateSymbols = util::utf8ToUtf16(privateSymbolsPackage.value()); |
| 1457 | } |
| 1458 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1459 | if (minSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1460 | options.manifestFixerOptions.minSdkVersionDefault = |
| 1461 | util::utf8ToUtf16(minSdkVersion.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | if (targetSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1465 | options.manifestFixerOptions.targetSdkVersionDefault = |
| 1466 | util::utf8ToUtf16(targetSdkVersion.value()); |
| 1467 | } |
| 1468 | |
| 1469 | if (renameManifestPackage) { |
| 1470 | options.manifestFixerOptions.renameManifestPackage = |
| 1471 | util::utf8ToUtf16(renameManifestPackage.value()); |
| 1472 | } |
| 1473 | |
| 1474 | if (renameInstrumentationTargetPackage) { |
| 1475 | options.manifestFixerOptions.renameInstrumentationTargetPackage = |
| 1476 | util::utf8ToUtf16(renameInstrumentationTargetPackage.value()); |
| 1477 | } |
| 1478 | |
| 1479 | if (versionCode) { |
| 1480 | options.manifestFixerOptions.versionCodeDefault = util::utf8ToUtf16(versionCode.value()); |
| 1481 | } |
| 1482 | |
| 1483 | if (versionName) { |
| 1484 | options.manifestFixerOptions.versionNameDefault = util::utf8ToUtf16(versionName.value()); |
| 1485 | } |
| 1486 | |
| 1487 | if (customJavaPackage) { |
| 1488 | options.customJavaPackage = util::utf8ToUtf16(customJavaPackage.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1491 | // Populate the set of extra packages for which to generate R.java. |
| 1492 | for (std::string& extraPackage : extraJavaPackages) { |
| 1493 | // A given package can actually be a colon separated list of packages. |
| 1494 | for (StringPiece package : util::split(extraPackage, ':')) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1495 | options.extraJavaPackages.insert(util::utf8ToUtf16(package)); |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 1499 | if (productList) { |
| 1500 | for (StringPiece product : util::tokenize<char>(productList.value(), ',')) { |
| 1501 | if (product != "" && product != "default") { |
| 1502 | options.products.insert(product.toString()); |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1507 | AxisConfigFilter filter; |
| 1508 | if (configs) { |
| 1509 | for (const StringPiece& configStr : util::tokenize<char>(configs.value(), ',')) { |
| 1510 | ConfigDescription config; |
| 1511 | LocaleValue lv; |
| 1512 | if (lv.initFromFilterString(configStr)) { |
| 1513 | lv.writeTo(&config); |
| 1514 | } else if (!ConfigDescription::parse(configStr, &config)) { |
| 1515 | context.getDiagnostics()->error( |
| 1516 | DiagMessage() << "invalid config '" << configStr << "' for -c option"); |
| 1517 | return 1; |
| 1518 | } |
| 1519 | |
| 1520 | if (config.density != 0) { |
| 1521 | context.getDiagnostics()->warn( |
| 1522 | DiagMessage() << "ignoring density '" << config << "' for -c option"); |
| 1523 | } else { |
| 1524 | filter.addConfig(config); |
| 1525 | } |
| 1526 | } |
| 1527 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 1528 | options.tableSplitterOptions.configFilter = &filter; |
| 1529 | } |
| 1530 | |
| 1531 | if (preferredDensity) { |
| 1532 | ConfigDescription preferredDensityConfig; |
| 1533 | if (!ConfigDescription::parse(preferredDensity.value(), &preferredDensityConfig)) { |
| 1534 | context.getDiagnostics()->error(DiagMessage() << "invalid density '" |
| 1535 | << preferredDensity.value() |
| 1536 | << "' for --preferred-density option"); |
| 1537 | return 1; |
| 1538 | } |
| 1539 | |
| 1540 | // Clear the version that can be automatically added. |
| 1541 | preferredDensityConfig.sdkVersion = 0; |
| 1542 | |
| 1543 | if (preferredDensityConfig.diff(ConfigDescription::defaultConfig()) |
| 1544 | != ConfigDescription::CONFIG_DENSITY) { |
| 1545 | context.getDiagnostics()->error(DiagMessage() << "invalid preferred density '" |
| 1546 | << preferredDensity.value() << "'. " |
| 1547 | << "Preferred density must only be a density value"); |
| 1548 | return 1; |
| 1549 | } |
| 1550 | options.tableSplitterOptions.preferredDensity = preferredDensityConfig.density; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
Adam Lesinski | 626a69f | 2016-03-03 10:09:26 -0800 | [diff] [blame] | 1553 | // Turn off auto versioning for static-libs. |
| 1554 | if (options.staticLib) { |
| 1555 | options.noAutoVersion = true; |
| 1556 | options.noVersionVectors = true; |
| 1557 | } |
| 1558 | |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 1559 | LinkCommand cmd(&context, options); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1560 | return cmd.run(flags.getArgs()); |
| 1561 | } |
| 1562 | |
| 1563 | } // namespace aapt |