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" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include "compile/IdAssigner.h" |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 23 | #include "compile/InlineXmlFormatParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 24 | #include "compile/Png.h" |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 25 | #include "compile/PseudolocaleGenerator.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "compile/XmlIdCollector.h" |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 27 | #include "flatten/Archive.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 28 | #include "flatten/XmlFlattener.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 29 | #include "proto/ProtoSerialize.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "util/Files.h" |
| 31 | #include "util/Maybe.h" |
| 32 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 33 | #include "xml/XmlDom.h" |
| 34 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 36 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
| 37 | #include <google/protobuf/io/coded_stream.h> |
| 38 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 39 | #include <dirent.h> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | #include <fstream> |
| 41 | #include <string> |
| 42 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 43 | using google::protobuf::io::CopyingOutputStreamAdaptor; |
| 44 | using google::protobuf::io::ZeroCopyOutputStream; |
| 45 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | namespace aapt { |
| 47 | |
| 48 | struct ResourcePathData { |
| 49 | Source source; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 50 | std::string resourceDir; |
| 51 | std::string name; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 52 | std::string extension; |
| 53 | |
| 54 | // Original config str. We keep this because when we parse the config, we may add on |
| 55 | // version qualifiers. We want to preserve the original input so the output is easily |
| 56 | // computed before hand. |
| 57 | std::string configStr; |
| 58 | ConfigDescription config; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * Resource file paths are expected to look like: |
| 63 | * [--/res/]type[-config]/name |
| 64 | */ |
| 65 | static Maybe<ResourcePathData> extractResourcePathData(const std::string& path, |
| 66 | std::string* outError) { |
| 67 | std::vector<std::string> parts = util::split(path, file::sDirSep); |
| 68 | if (parts.size() < 2) { |
| 69 | if (outError) *outError = "bad resource path"; |
| 70 | return {}; |
| 71 | } |
| 72 | |
| 73 | std::string& dir = parts[parts.size() - 2]; |
| 74 | StringPiece dirStr = dir; |
| 75 | |
| 76 | StringPiece configStr; |
| 77 | ConfigDescription config; |
| 78 | size_t dashPos = dir.find('-'); |
| 79 | if (dashPos != std::string::npos) { |
| 80 | configStr = dirStr.substr(dashPos + 1, dir.size() - (dashPos + 1)); |
| 81 | if (!ConfigDescription::parse(configStr, &config)) { |
| 82 | if (outError) { |
| 83 | std::stringstream errStr; |
| 84 | errStr << "invalid configuration '" << configStr << "'"; |
| 85 | *outError = errStr.str(); |
| 86 | } |
| 87 | return {}; |
| 88 | } |
| 89 | dirStr = dirStr.substr(0, dashPos); |
| 90 | } |
| 91 | |
| 92 | std::string& filename = parts[parts.size() - 1]; |
| 93 | StringPiece name = filename; |
| 94 | StringPiece extension; |
| 95 | size_t dotPos = filename.find('.'); |
| 96 | if (dotPos != std::string::npos) { |
| 97 | extension = name.substr(dotPos + 1, filename.size() - (dotPos + 1)); |
| 98 | name = name.substr(0, dotPos); |
| 99 | } |
| 100 | |
| 101 | return ResourcePathData{ |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 102 | Source(path), |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 103 | dirStr.toString(), |
| 104 | name.toString(), |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 105 | extension.toString(), |
| 106 | configStr.toString(), |
| 107 | config |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | struct CompileOptions { |
| 112 | std::string outputPath; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 113 | Maybe<std::string> resDir; |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 114 | bool pseudolocalize = false; |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 115 | bool legacyMode = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 116 | bool verbose = false; |
| 117 | }; |
| 118 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 119 | static std::string buildIntermediateFilename(const ResourcePathData& data) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 120 | std::stringstream name; |
| 121 | name << data.resourceDir; |
| 122 | if (!data.configStr.empty()) { |
| 123 | name << "-" << data.configStr; |
| 124 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 125 | name << "_" << data.name; |
| 126 | if (!data.extension.empty()) { |
| 127 | name << "." << data.extension; |
| 128 | } |
| 129 | name << ".flat"; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 130 | return name.str(); |
| 131 | } |
| 132 | |
| 133 | static bool isHidden(const StringPiece& filename) { |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 134 | return util::stringStartsWith(filename, "."); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Walks the res directory structure, looking for resource files. |
| 139 | */ |
| 140 | static bool loadInputFilesFromDir(IAaptContext* context, const CompileOptions& options, |
| 141 | std::vector<ResourcePathData>* outPathData) { |
| 142 | const std::string& rootDir = options.resDir.value(); |
| 143 | std::unique_ptr<DIR, decltype(closedir)*> d(opendir(rootDir.data()), closedir); |
| 144 | if (!d) { |
| 145 | context->getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | while (struct dirent* entry = readdir(d.get())) { |
| 150 | if (isHidden(entry->d_name)) { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | std::string prefixPath = rootDir; |
| 155 | file::appendPath(&prefixPath, entry->d_name); |
| 156 | |
| 157 | if (file::getFileType(prefixPath) != file::FileType::kDirectory) { |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | std::unique_ptr<DIR, decltype(closedir)*> subDir(opendir(prefixPath.data()), closedir); |
| 162 | if (!subDir) { |
| 163 | context->getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | while (struct dirent* leafEntry = readdir(subDir.get())) { |
| 168 | if (isHidden(leafEntry->d_name)) { |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | std::string fullPath = prefixPath; |
| 173 | file::appendPath(&fullPath, leafEntry->d_name); |
| 174 | |
| 175 | std::string errStr; |
| 176 | Maybe<ResourcePathData> pathData = extractResourcePathData(fullPath, &errStr); |
| 177 | if (!pathData) { |
| 178 | context->getDiagnostics()->error(DiagMessage() << errStr); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | outPathData->push_back(std::move(pathData.value())); |
| 183 | } |
| 184 | } |
| 185 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static bool compileTable(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 189 | const ResourcePathData& pathData, IArchiveWriter* writer, |
| 190 | const std::string& outputPath) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 191 | ResourceTable table; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 192 | { |
| 193 | std::ifstream fin(pathData.source.path, std::ifstream::binary); |
| 194 | if (!fin) { |
| 195 | context->getDiagnostics()->error(DiagMessage(pathData.source) << strerror(errno)); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | // Parse the values file from XML. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 201 | xml::XmlPullParser xmlParser(fin); |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 202 | |
| 203 | ResourceParserOptions parserOptions; |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 204 | parserOptions.errorOnPositionalArguments = !options.legacyMode; |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 205 | |
| 206 | // If the filename includes donottranslate, then the default translatable is false. |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 207 | parserOptions.translatable = pathData.name.find("donottranslate") == std::string::npos; |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 208 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 209 | ResourceParser resParser(context->getDiagnostics(), &table, pathData.source, |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 210 | pathData.config, parserOptions); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 211 | if (!resParser.parse(&xmlParser)) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | fin.close(); |
| 216 | } |
| 217 | |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 218 | if (options.pseudolocalize) { |
| 219 | // Generate pseudo-localized strings (en-XA and ar-XB). |
| 220 | // These are created as weak symbols, and are only generated from default configuration |
| 221 | // strings and plurals. |
| 222 | PseudolocaleGenerator pseudolocaleGenerator; |
| 223 | if (!pseudolocaleGenerator.consume(context, &table)) { |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 228 | // Ensure we have the compilation package at least. |
| 229 | table.createPackage(context->getCompilationPackage()); |
| 230 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 231 | // Assign an ID to any package that has resources. |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 232 | for (auto& pkg : table.packages) { |
| 233 | if (!pkg->id) { |
| 234 | // If no package ID was set while parsing (public identifiers), auto assign an ID. |
| 235 | pkg->id = context->getPackageId(); |
| 236 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 239 | // Create the file/zip entry. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 240 | if (!writer->startEntry(outputPath, 0)) { |
| 241 | context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to open"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 245 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry(). |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 246 | { |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 247 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream |
| 248 | // interface. |
| 249 | CopyingOutputStreamAdaptor copyingAdaptor(writer); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 250 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 251 | std::unique_ptr<pb::ResourceTable> pbTable = serializeTableToPb(&table); |
| 252 | if (!pbTable->SerializeToZeroCopyStream(©ingAdaptor)) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 253 | context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to write"); |
| 254 | return false; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 255 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 256 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 257 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 258 | if (!writer->finishEntry()) { |
| 259 | context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to finish entry"); |
| 260 | return false; |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | static bool writeHeaderAndBufferToWriter(const StringPiece& outputPath, const ResourceFile& file, |
| 266 | const BigBuffer& buffer, IArchiveWriter* writer, |
| 267 | IDiagnostics* diag) { |
| 268 | // Start the entry so we can write the header. |
| 269 | if (!writer->startEntry(outputPath, 0)) { |
| 270 | diag->error(DiagMessage(outputPath) << "failed to open file"); |
| 271 | return false; |
| 272 | } |
| 273 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 274 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry(). |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 275 | { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 276 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream |
| 277 | // interface. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 278 | CopyingOutputStreamAdaptor copyingAdaptor(writer); |
| 279 | CompiledFileOutputStream outputStream(©ingAdaptor); |
| 280 | |
| 281 | // Number of CompiledFiles. |
| 282 | outputStream.WriteLittleEndian32(1); |
| 283 | |
| 284 | std::unique_ptr<pb::CompiledFile> compiledFile = serializeCompiledFileToPb(file); |
| 285 | outputStream.WriteCompiledFile(compiledFile.get()); |
| 286 | outputStream.WriteData(&buffer); |
| 287 | |
| 288 | if (outputStream.HadError()) { |
| 289 | diag->error(DiagMessage(outputPath) << "failed to write data"); |
| 290 | return false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | if (!writer->finishEntry()) { |
| 295 | diag->error(DiagMessage(outputPath) << "failed to finish writing data"); |
| 296 | return false; |
| 297 | } |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | static bool writeHeaderAndMmapToWriter(const StringPiece& outputPath, const ResourceFile& file, |
| 302 | const android::FileMap& map, IArchiveWriter* writer, |
| 303 | IDiagnostics* diag) { |
| 304 | // Start the entry so we can write the header. |
| 305 | if (!writer->startEntry(outputPath, 0)) { |
| 306 | diag->error(DiagMessage(outputPath) << "failed to open file"); |
| 307 | return false; |
| 308 | } |
| 309 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 310 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry(). |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 311 | { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 312 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream |
| 313 | // interface. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 314 | CopyingOutputStreamAdaptor copyingAdaptor(writer); |
| 315 | CompiledFileOutputStream outputStream(©ingAdaptor); |
| 316 | |
| 317 | // Number of CompiledFiles. |
| 318 | outputStream.WriteLittleEndian32(1); |
| 319 | |
| 320 | std::unique_ptr<pb::CompiledFile> compiledFile = serializeCompiledFileToPb(file); |
| 321 | outputStream.WriteCompiledFile(compiledFile.get()); |
| 322 | outputStream.WriteData(map.getDataPtr(), map.getDataLength()); |
| 323 | |
| 324 | if (outputStream.HadError()) { |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 325 | diag->error(DiagMessage(outputPath) << "failed to write data"); |
| 326 | return false; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (!writer->finishEntry()) { |
| 331 | diag->error(DiagMessage(outputPath) << "failed to finish writing data"); |
| 332 | return false; |
| 333 | } |
| 334 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 337 | static bool flattenXmlToOutStream(IAaptContext* context, const StringPiece& outputPath, |
| 338 | xml::XmlResource* xmlRes, |
| 339 | CompiledFileOutputStream* out) { |
| 340 | BigBuffer buffer(1024); |
| 341 | XmlFlattenerOptions xmlFlattenerOptions; |
| 342 | xmlFlattenerOptions.keepRawValues = true; |
| 343 | XmlFlattener flattener(&buffer, xmlFlattenerOptions); |
| 344 | if (!flattener.consume(context, xmlRes)) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | std::unique_ptr<pb::CompiledFile> pbCompiledFile = serializeCompiledFileToPb(xmlRes->file); |
| 349 | out->WriteCompiledFile(pbCompiledFile.get()); |
| 350 | out->WriteData(&buffer); |
| 351 | |
| 352 | if (out->HadError()) { |
| 353 | context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to write data"); |
| 354 | return false; |
| 355 | } |
| 356 | return true; |
| 357 | } |
| 358 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 359 | static bool compileXml(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 360 | const ResourcePathData& pathData, IArchiveWriter* writer, |
| 361 | const std::string& outputPath) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 362 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 363 | std::unique_ptr<xml::XmlResource> xmlRes; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 364 | { |
| 365 | std::ifstream fin(pathData.source.path, std::ifstream::binary); |
| 366 | if (!fin) { |
| 367 | context->getDiagnostics()->error(DiagMessage(pathData.source) << strerror(errno)); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | xmlRes = xml::inflate(&fin, context->getDiagnostics(), pathData.source); |
| 372 | |
| 373 | fin.close(); |
| 374 | } |
| 375 | |
| 376 | if (!xmlRes) { |
| 377 | return false; |
| 378 | } |
| 379 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 380 | xmlRes->file.name = ResourceName({}, *parseResourceType(pathData.resourceDir), pathData.name); |
| 381 | xmlRes->file.config = pathData.config; |
| 382 | xmlRes->file.source = pathData.source; |
| 383 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 384 | // Collect IDs that are defined here. |
| 385 | XmlIdCollector collector; |
| 386 | if (!collector.consume(context, xmlRes.get())) { |
| 387 | return false; |
| 388 | } |
| 389 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 390 | // Look for and process any <aapt:attr> tags and create sub-documents. |
| 391 | InlineXmlFormatParser inlineXmlFormatParser; |
| 392 | if (!inlineXmlFormatParser.consume(context, xmlRes.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame^] | 396 | // Start the entry so we can write the header. |
| 397 | if (!writer->startEntry(outputPath, 0)) { |
| 398 | context->getDiagnostics()->error(DiagMessage(outputPath) << "failed to open file"); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->finishEntry(). |
| 403 | { |
| 404 | // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream |
| 405 | // interface. |
| 406 | CopyingOutputStreamAdaptor copyingAdaptor(writer); |
| 407 | CompiledFileOutputStream outputStream(©ingAdaptor); |
| 408 | |
| 409 | std::vector<std::unique_ptr<xml::XmlResource>>& inlineDocuments = |
| 410 | inlineXmlFormatParser.getExtractedInlineXmlDocuments(); |
| 411 | |
| 412 | // Number of CompiledFiles. |
| 413 | outputStream.WriteLittleEndian32(1 + inlineDocuments.size()); |
| 414 | |
| 415 | if (!flattenXmlToOutStream(context, outputPath, xmlRes.get(), &outputStream)) { |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | for (auto& inlineXmlDoc : inlineDocuments) { |
| 420 | if (!flattenXmlToOutStream(context, outputPath, inlineXmlDoc.get(), &outputStream)) { |
| 421 | return false; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if (!writer->finishEntry()) { |
| 427 | context->getDiagnostics()->error(DiagMessage(outputPath) |
| 428 | << "failed to finish writing data"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 429 | return false; |
| 430 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 431 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static bool compilePng(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 435 | const ResourcePathData& pathData, IArchiveWriter* writer, |
| 436 | const std::string& outputPath) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 437 | BigBuffer buffer(4096); |
| 438 | ResourceFile resFile; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 439 | resFile.name = ResourceName({}, *parseResourceType(pathData.resourceDir), pathData.name); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 440 | resFile.config = pathData.config; |
| 441 | resFile.source = pathData.source; |
| 442 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 443 | { |
| 444 | std::ifstream fin(pathData.source.path, std::ifstream::binary); |
| 445 | if (!fin) { |
| 446 | context->getDiagnostics()->error(DiagMessage(pathData.source) << strerror(errno)); |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | Png png(context->getDiagnostics()); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 451 | if (!png.process(pathData.source, &fin, &buffer, {})) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 452 | return false; |
| 453 | } |
| 454 | } |
| 455 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 456 | if (!writeHeaderAndBufferToWriter(outputPath, resFile, buffer, writer, |
| 457 | context->getDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 458 | return false; |
| 459 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 460 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static bool compileFile(IAaptContext* context, const CompileOptions& options, |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 464 | const ResourcePathData& pathData, IArchiveWriter* writer, |
| 465 | const std::string& outputPath) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 466 | BigBuffer buffer(256); |
| 467 | ResourceFile resFile; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 468 | resFile.name = ResourceName({}, *parseResourceType(pathData.resourceDir), pathData.name); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 469 | resFile.config = pathData.config; |
| 470 | resFile.source = pathData.source; |
| 471 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 472 | std::string errorStr; |
| 473 | Maybe<android::FileMap> f = file::mmapPath(pathData.source.path, &errorStr); |
| 474 | if (!f) { |
| 475 | context->getDiagnostics()->error(DiagMessage(pathData.source) << errorStr); |
| 476 | return false; |
| 477 | } |
| 478 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 479 | if (!writeHeaderAndMmapToWriter(outputPath, resFile, f.value(), writer, |
| 480 | context->getDiagnostics())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 481 | return false; |
| 482 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 483 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | class CompileContext : public IAaptContext { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 487 | public: |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 488 | void setVerbose(bool val) { |
| 489 | mVerbose = val; |
| 490 | } |
| 491 | |
| 492 | bool verbose() override { |
| 493 | return mVerbose; |
| 494 | } |
| 495 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 496 | IDiagnostics* getDiagnostics() override { |
| 497 | return &mDiagnostics; |
| 498 | } |
| 499 | |
| 500 | NameMangler* getNameMangler() override { |
| 501 | abort(); |
| 502 | return nullptr; |
| 503 | } |
| 504 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 505 | const std::string& getCompilationPackage() override { |
| 506 | static std::string empty; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 507 | return empty; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | uint8_t getPackageId() override { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 511 | return 0x0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 514 | SymbolTable* getExternalSymbols() override { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 515 | abort(); |
| 516 | return nullptr; |
| 517 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 518 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 519 | int getMinSdkVersion() override { |
| 520 | return 0; |
| 521 | } |
| 522 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 523 | private: |
| 524 | StdErrDiagnostics mDiagnostics; |
| 525 | bool mVerbose = false; |
| 526 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 527 | }; |
| 528 | |
| 529 | /** |
| 530 | * Entry point for compilation phase. Parses arguments and dispatches to the correct steps. |
| 531 | */ |
| 532 | int compile(const std::vector<StringPiece>& args) { |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 533 | CompileContext context; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 534 | CompileOptions options; |
| 535 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 536 | bool verbose = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 537 | Flags flags = Flags() |
| 538 | .requiredFlag("-o", "Output path", &options.outputPath) |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 539 | .optionalFlag("--dir", "Directory to scan for resources", &options.resDir) |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 540 | .optionalSwitch("--pseudo-localize", "Generate resources for pseudo-locales " |
| 541 | "(en-XA and ar-XB)", &options.pseudolocalize) |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 542 | .optionalSwitch("--legacy", "Treat errors that used to be valid in AAPT as warnings", |
| 543 | &options.legacyMode) |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 544 | .optionalSwitch("-v", "Enables verbose logging", &verbose); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 545 | if (!flags.parse("aapt2 compile", args, &std::cerr)) { |
| 546 | return 1; |
| 547 | } |
| 548 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 549 | context.setVerbose(verbose); |
| 550 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 551 | std::unique_ptr<IArchiveWriter> archiveWriter; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 552 | |
| 553 | std::vector<ResourcePathData> inputData; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 554 | if (options.resDir) { |
| 555 | if (!flags.getArgs().empty()) { |
| 556 | // Can't have both files and a resource directory. |
| 557 | context.getDiagnostics()->error(DiagMessage() << "files given but --dir specified"); |
| 558 | flags.usage("aapt2 compile", &std::cerr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 559 | return 1; |
| 560 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 561 | |
| 562 | if (!loadInputFilesFromDir(&context, options, &inputData)) { |
| 563 | return 1; |
| 564 | } |
| 565 | |
| 566 | archiveWriter = createZipFileArchiveWriter(context.getDiagnostics(), options.outputPath); |
| 567 | |
| 568 | } else { |
| 569 | inputData.reserve(flags.getArgs().size()); |
| 570 | |
| 571 | // Collect data from the path for each input file. |
| 572 | for (const std::string& arg : flags.getArgs()) { |
| 573 | std::string errorStr; |
| 574 | if (Maybe<ResourcePathData> pathData = extractResourcePathData(arg, &errorStr)) { |
| 575 | inputData.push_back(std::move(pathData.value())); |
| 576 | } else { |
| 577 | context.getDiagnostics()->error(DiagMessage() << errorStr << " (" << arg << ")"); |
| 578 | return 1; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | archiveWriter = createDirectoryArchiveWriter(context.getDiagnostics(), options.outputPath); |
| 583 | } |
| 584 | |
| 585 | if (!archiveWriter) { |
| 586 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | bool error = false; |
| 590 | for (ResourcePathData& pathData : inputData) { |
| 591 | if (options.verbose) { |
| 592 | context.getDiagnostics()->note(DiagMessage(pathData.source) << "processing"); |
| 593 | } |
| 594 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 595 | if (pathData.resourceDir == "values") { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 596 | // Overwrite the extension. |
| 597 | pathData.extension = "arsc"; |
| 598 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 599 | const std::string outputFilename = buildIntermediateFilename(pathData); |
| 600 | if (!compileTable(&context, options, pathData, archiveWriter.get(), outputFilename)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 601 | error = true; |
| 602 | } |
| 603 | |
| 604 | } else { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 605 | const std::string outputFilename = buildIntermediateFilename(pathData); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 606 | if (const ResourceType* type = parseResourceType(pathData.resourceDir)) { |
| 607 | if (*type != ResourceType::kRaw) { |
| 608 | if (pathData.extension == "xml") { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 609 | if (!compileXml(&context, options, pathData, archiveWriter.get(), |
| 610 | outputFilename)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 611 | error = true; |
| 612 | } |
| 613 | } else if (pathData.extension == "png" || pathData.extension == "9.png") { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 614 | if (!compilePng(&context, options, pathData, archiveWriter.get(), |
| 615 | outputFilename)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 616 | error = true; |
| 617 | } |
| 618 | } else { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 619 | if (!compileFile(&context, options, pathData, archiveWriter.get(), |
| 620 | outputFilename)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 621 | error = true; |
| 622 | } |
| 623 | } |
| 624 | } else { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 625 | if (!compileFile(&context, options, pathData, archiveWriter.get(), |
| 626 | outputFilename)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 627 | error = true; |
| 628 | } |
| 629 | } |
| 630 | } else { |
| 631 | context.getDiagnostics()->error( |
| 632 | DiagMessage() << "invalid file path '" << pathData.source << "'"); |
| 633 | error = true; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if (error) { |
| 639 | return 1; |
| 640 | } |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | } // namespace aapt |