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 "ConfigDescription.h" |
| 18 | #include "Diagnostics.h" |
| 19 | #include "Flags.h" |
| 20 | #include "ResourceParser.h" |
| 21 | #include "ResourceTable.h" |
| 22 | #include "XmlDom.h" |
| 23 | #include "XmlPullParser.h" |
| 24 | |
| 25 | #include "compile/IdAssigner.h" |
| 26 | #include "compile/Png.h" |
| 27 | #include "compile/XmlIdCollector.h" |
| 28 | #include "flatten/FileExportWriter.h" |
| 29 | #include "flatten/TableFlattener.h" |
| 30 | #include "flatten/XmlFlattener.h" |
| 31 | #include "util/Files.h" |
| 32 | #include "util/Maybe.h" |
| 33 | #include "util/Util.h" |
| 34 | |
| 35 | #include <fstream> |
| 36 | #include <string> |
| 37 | |
| 38 | namespace aapt { |
| 39 | |
| 40 | struct ResourcePathData { |
| 41 | Source source; |
| 42 | std::u16string resourceDir; |
| 43 | std::u16string name; |
| 44 | std::string extension; |
| 45 | |
| 46 | // Original config str. We keep this because when we parse the config, we may add on |
| 47 | // version qualifiers. We want to preserve the original input so the output is easily |
| 48 | // computed before hand. |
| 49 | std::string configStr; |
| 50 | ConfigDescription config; |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * Resource file paths are expected to look like: |
| 55 | * [--/res/]type[-config]/name |
| 56 | */ |
| 57 | static Maybe<ResourcePathData> extractResourcePathData(const std::string& path, |
| 58 | std::string* outError) { |
| 59 | std::vector<std::string> parts = util::split(path, file::sDirSep); |
| 60 | if (parts.size() < 2) { |
| 61 | if (outError) *outError = "bad resource path"; |
| 62 | return {}; |
| 63 | } |
| 64 | |
| 65 | std::string& dir = parts[parts.size() - 2]; |
| 66 | StringPiece dirStr = dir; |
| 67 | |
| 68 | StringPiece configStr; |
| 69 | ConfigDescription config; |
| 70 | size_t dashPos = dir.find('-'); |
| 71 | if (dashPos != std::string::npos) { |
| 72 | configStr = dirStr.substr(dashPos + 1, dir.size() - (dashPos + 1)); |
| 73 | if (!ConfigDescription::parse(configStr, &config)) { |
| 74 | if (outError) { |
| 75 | std::stringstream errStr; |
| 76 | errStr << "invalid configuration '" << configStr << "'"; |
| 77 | *outError = errStr.str(); |
| 78 | } |
| 79 | return {}; |
| 80 | } |
| 81 | dirStr = dirStr.substr(0, dashPos); |
| 82 | } |
| 83 | |
| 84 | std::string& filename = parts[parts.size() - 1]; |
| 85 | StringPiece name = filename; |
| 86 | StringPiece extension; |
| 87 | size_t dotPos = filename.find('.'); |
| 88 | if (dotPos != std::string::npos) { |
| 89 | extension = name.substr(dotPos + 1, filename.size() - (dotPos + 1)); |
| 90 | name = name.substr(0, dotPos); |
| 91 | } |
| 92 | |
| 93 | return ResourcePathData{ |
| 94 | Source{ path }, |
| 95 | util::utf8ToUtf16(dirStr), |
| 96 | util::utf8ToUtf16(name), |
| 97 | extension.toString(), |
| 98 | configStr.toString(), |
| 99 | config |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | struct CompileOptions { |
| 104 | std::string outputPath; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 105 | Maybe<std::u16string> product; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | bool verbose = false; |
| 107 | }; |
| 108 | |
| 109 | static std::string buildIntermediateFilename(const std::string outDir, |
| 110 | const ResourcePathData& data) { |
| 111 | std::stringstream name; |
| 112 | name << data.resourceDir; |
| 113 | if (!data.configStr.empty()) { |
| 114 | name << "-" << data.configStr; |
| 115 | } |
| 116 | name << "_" << data.name << "." << data.extension << ".flat"; |
| 117 | std::string outPath = outDir; |
| 118 | file::appendPath(&outPath, name.str()); |
| 119 | return outPath; |
| 120 | } |
| 121 | |
| 122 | static bool compileTable(IAaptContext* context, const CompileOptions& options, |
| 123 | const ResourcePathData& pathData, const std::string& outputPath) { |
| 124 | ResourceTable table; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | { |
| 126 | std::ifstream fin(pathData.source.path, std::ifstream::binary); |
| 127 | if (!fin) { |
| 128 | context->getDiagnostics()->error(DiagMessage(pathData.source) << strerror(errno)); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | // Parse the values file from XML. |
| 134 | XmlPullParser xmlParser(fin); |
| 135 | ResourceParser resParser(context->getDiagnostics(), &table, pathData.source, |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 136 | pathData.config, ResourceParserOptions{ options.product }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | if (!resParser.parse(&xmlParser)) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | fin.close(); |
| 142 | } |
| 143 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 144 | ResourceTablePackage* pkg = table.createPackage(context->getCompilationPackage()); |
| 145 | if (!pkg->id) { |
| 146 | // If no package ID was set while parsing (public identifiers), auto assign an ID. |
| 147 | pkg->id = context->getPackageId(); |
| 148 | } |
| 149 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | // Assign IDs to prepare the table for flattening. |
| 151 | IdAssigner idAssigner; |
| 152 | if (!idAssigner.consume(context, &table)) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // Flatten the table. |
| 157 | BigBuffer buffer(1024); |
| 158 | TableFlattenerOptions tableFlattenerOptions; |
| 159 | tableFlattenerOptions.useExtendedChunks = true; |
| 160 | TableFlattener flattener(&buffer, tableFlattenerOptions); |
| 161 | if (!flattener.consume(context, &table)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // Build the output filename. |
| 166 | std::ofstream fout(outputPath, std::ofstream::binary); |
| 167 | if (!fout) { |
| 168 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // Write it to disk. |
| 173 | if (!util::writeAll(fout, buffer)) { |
| 174 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 175 | return false; |
| 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | static bool compileXml(IAaptContext* context, const CompileOptions& options, |
| 181 | const ResourcePathData& pathData, const std::string& outputPath) { |
| 182 | |
| 183 | std::unique_ptr<XmlResource> xmlRes; |
| 184 | |
| 185 | { |
| 186 | std::ifstream fin(pathData.source.path, std::ifstream::binary); |
| 187 | if (!fin) { |
| 188 | context->getDiagnostics()->error(DiagMessage(pathData.source) << strerror(errno)); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | xmlRes = xml::inflate(&fin, context->getDiagnostics(), pathData.source); |
| 193 | |
| 194 | fin.close(); |
| 195 | } |
| 196 | |
| 197 | if (!xmlRes) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | // Collect IDs that are defined here. |
| 202 | XmlIdCollector collector; |
| 203 | if (!collector.consume(context, xmlRes.get())) { |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | xmlRes->file.name = ResourceName{ {}, *parseResourceType(pathData.resourceDir), pathData.name }; |
| 208 | xmlRes->file.config = pathData.config; |
| 209 | xmlRes->file.source = pathData.source; |
| 210 | |
| 211 | BigBuffer buffer(1024); |
| 212 | ChunkWriter fileExportWriter = wrapBufferWithFileExportHeader(&buffer, &xmlRes->file); |
| 213 | |
| 214 | XmlFlattenerOptions xmlFlattenerOptions; |
| 215 | xmlFlattenerOptions.keepRawValues = true; |
| 216 | XmlFlattener flattener(fileExportWriter.getBuffer(), xmlFlattenerOptions); |
| 217 | if (!flattener.consume(context, xmlRes.get())) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | fileExportWriter.finish(); |
| 222 | |
| 223 | std::ofstream fout(outputPath, std::ofstream::binary); |
| 224 | if (!fout) { |
| 225 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | // Write it to disk. |
| 230 | if (!util::writeAll(fout, buffer)) { |
| 231 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 232 | return false; |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | static bool compilePng(IAaptContext* context, const CompileOptions& options, |
| 238 | const ResourcePathData& pathData, const std::string& outputPath) { |
| 239 | BigBuffer buffer(4096); |
| 240 | ResourceFile resFile; |
| 241 | resFile.name = ResourceName{ {}, *parseResourceType(pathData.resourceDir), pathData.name }; |
| 242 | resFile.config = pathData.config; |
| 243 | resFile.source = pathData.source; |
| 244 | |
| 245 | ChunkWriter fileExportWriter = wrapBufferWithFileExportHeader(&buffer, &resFile); |
| 246 | |
| 247 | { |
| 248 | std::ifstream fin(pathData.source.path, std::ifstream::binary); |
| 249 | if (!fin) { |
| 250 | context->getDiagnostics()->error(DiagMessage(pathData.source) << strerror(errno)); |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | Png png(context->getDiagnostics()); |
| 255 | if (!png.process(pathData.source, &fin, fileExportWriter.getBuffer(), {})) { |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | fileExportWriter.finish(); |
| 261 | |
| 262 | std::ofstream fout(outputPath, std::ofstream::binary); |
| 263 | if (!fout) { |
| 264 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | if (!util::writeAll(fout, buffer)) { |
| 269 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 270 | return false; |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | static bool compileFile(IAaptContext* context, const CompileOptions& options, |
| 276 | const ResourcePathData& pathData, const std::string& outputPath) { |
| 277 | BigBuffer buffer(256); |
| 278 | ResourceFile resFile; |
| 279 | resFile.name = ResourceName{ {}, *parseResourceType(pathData.resourceDir), pathData.name }; |
| 280 | resFile.config = pathData.config; |
| 281 | resFile.source = pathData.source; |
| 282 | |
| 283 | ChunkWriter fileExportWriter = wrapBufferWithFileExportHeader(&buffer, &resFile); |
| 284 | |
| 285 | std::string errorStr; |
| 286 | Maybe<android::FileMap> f = file::mmapPath(pathData.source.path, &errorStr); |
| 287 | if (!f) { |
| 288 | context->getDiagnostics()->error(DiagMessage(pathData.source) << errorStr); |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | std::ofstream fout(outputPath, std::ofstream::binary); |
| 293 | if (!fout) { |
| 294 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | // Manually set the size and don't call finish(). This is because we are not copying from |
| 299 | // the buffer the entire file. |
| 300 | fileExportWriter.getChunkHeader()->size = |
| 301 | util::hostToDevice32(buffer.size() + f.value().getDataLength()); |
| 302 | if (!util::writeAll(fout, buffer)) { |
| 303 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | if (!fout.write((const char*) f.value().getDataPtr(), f.value().getDataLength())) { |
| 308 | context->getDiagnostics()->error(DiagMessage(Source{ outputPath }) << strerror(errno)); |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | class CompileContext : public IAaptContext { |
| 315 | private: |
| 316 | StdErrDiagnostics mDiagnostics; |
| 317 | |
| 318 | public: |
| 319 | IDiagnostics* getDiagnostics() override { |
| 320 | return &mDiagnostics; |
| 321 | } |
| 322 | |
| 323 | NameMangler* getNameMangler() override { |
| 324 | abort(); |
| 325 | return nullptr; |
| 326 | } |
| 327 | |
| 328 | StringPiece16 getCompilationPackage() override { |
| 329 | return {}; |
| 330 | } |
| 331 | |
| 332 | uint8_t getPackageId() override { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 333 | return 0x0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | ISymbolTable* getExternalSymbols() override { |
| 337 | abort(); |
| 338 | return nullptr; |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | /** |
| 343 | * Entry point for compilation phase. Parses arguments and dispatches to the correct steps. |
| 344 | */ |
| 345 | int compile(const std::vector<StringPiece>& args) { |
| 346 | CompileOptions options; |
| 347 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 348 | Maybe<std::string> product; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 349 | Flags flags = Flags() |
| 350 | .requiredFlag("-o", "Output path", &options.outputPath) |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 351 | .optionalFlag("--product", "Product type to compile", &product) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 352 | .optionalSwitch("-v", "Enables verbose logging", &options.verbose); |
| 353 | if (!flags.parse("aapt2 compile", args, &std::cerr)) { |
| 354 | return 1; |
| 355 | } |
| 356 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 357 | if (product) { |
| 358 | options.product = util::utf8ToUtf16(product.value()); |
| 359 | } |
| 360 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 361 | CompileContext context; |
| 362 | |
| 363 | std::vector<ResourcePathData> inputData; |
| 364 | inputData.reserve(flags.getArgs().size()); |
| 365 | |
| 366 | // Collect data from the path for each input file. |
| 367 | for (const std::string& arg : flags.getArgs()) { |
| 368 | std::string errorStr; |
| 369 | if (Maybe<ResourcePathData> pathData = extractResourcePathData(arg, &errorStr)) { |
| 370 | inputData.push_back(std::move(pathData.value())); |
| 371 | } else { |
| 372 | context.getDiagnostics()->error(DiagMessage() << errorStr << " (" << arg << ")"); |
| 373 | return 1; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | bool error = false; |
| 378 | for (ResourcePathData& pathData : inputData) { |
| 379 | if (options.verbose) { |
| 380 | context.getDiagnostics()->note(DiagMessage(pathData.source) << "processing"); |
| 381 | } |
| 382 | |
| 383 | if (pathData.resourceDir == u"values") { |
| 384 | // Overwrite the extension. |
| 385 | pathData.extension = "arsc"; |
| 386 | |
| 387 | const std::string outputFilename = buildIntermediateFilename( |
| 388 | options.outputPath, pathData); |
| 389 | if (!compileTable(&context, options, pathData, outputFilename)) { |
| 390 | error = true; |
| 391 | } |
| 392 | |
| 393 | } else { |
| 394 | const std::string outputFilename = buildIntermediateFilename(options.outputPath, |
| 395 | pathData); |
| 396 | if (const ResourceType* type = parseResourceType(pathData.resourceDir)) { |
| 397 | if (*type != ResourceType::kRaw) { |
| 398 | if (pathData.extension == "xml") { |
| 399 | if (!compileXml(&context, options, pathData, outputFilename)) { |
| 400 | error = true; |
| 401 | } |
| 402 | } else if (pathData.extension == "png" || pathData.extension == "9.png") { |
| 403 | if (!compilePng(&context, options, pathData, outputFilename)) { |
| 404 | error = true; |
| 405 | } |
| 406 | } else { |
| 407 | if (!compileFile(&context, options, pathData, outputFilename)) { |
| 408 | error = true; |
| 409 | } |
| 410 | } |
| 411 | } else { |
| 412 | if (!compileFile(&context, options, pathData, outputFilename)) { |
| 413 | error = true; |
| 414 | } |
| 415 | } |
| 416 | } else { |
| 417 | context.getDiagnostics()->error( |
| 418 | DiagMessage() << "invalid file path '" << pathData.source << "'"); |
| 419 | error = true; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (error) { |
| 425 | return 1; |
| 426 | } |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | } // namespace aapt |