OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "tools/gn/bundle_data_target_generator.h" |
| 6 |
| 7 #include "tools/gn/parse_tree.h" |
| 8 #include "tools/gn/scope.h" |
| 9 #include "tools/gn/substitution_type.h" |
| 10 #include "tools/gn/target.h" |
| 11 #include "tools/gn/value.h" |
| 12 #include "tools/gn/variables.h" |
| 13 |
| 14 BundleDataTargetGenerator::BundleDataTargetGenerator( |
| 15 Target* target, |
| 16 Scope* scope, |
| 17 const FunctionCallNode* function_call, |
| 18 Err* err) : TargetGenerator(target, scope, function_call, err) {} |
| 19 |
| 20 BundleDataTargetGenerator::~BundleDataTargetGenerator() {} |
| 21 |
| 22 void BundleDataTargetGenerator::DoRun() { |
| 23 target_->set_output_type(Target::BUNDLE_DATA); |
| 24 |
| 25 if (!FillSources()) |
| 26 return; |
| 27 if (!FillOutputs()) |
| 28 return; |
| 29 |
| 30 if (target_->sources().empty()) { |
| 31 *err_ = Err(function_call_, "Empty sources for bundle_data target." |
| 32 "You have to specify at least one file in the \"sources\"."); |
| 33 return; |
| 34 } |
| 35 if (target_->action_values().outputs().list().size() != 1) { |
| 36 *err_ = Err(function_call_, |
| 37 "Target bundle_data must have exactly one ouput.", |
| 38 "You must specify exactly one value in the \"output\" array for the" |
| 39 "destination\ninto the generated bundle (see \"gn help bundle_data\"). " |
| 40 "If there are multiple\nsources to copy, use source expansion (see " |
| 41 "\"gn help source_expansion\")."); |
| 42 return; |
| 43 } |
| 44 } |
| 45 |
| 46 bool BundleDataTargetGenerator::FillOutputs() { |
| 47 const Value* value = scope_->GetValue(variables::kOutputs, true); |
| 48 if (!value) |
| 49 return true; |
| 50 |
| 51 SubstitutionList& outputs = target_->action_values().outputs(); |
| 52 if (!outputs.Parse(*value, err_)) |
| 53 return false; |
| 54 |
| 55 // Check the substitutions used are valid for this purpose. |
| 56 for (SubstitutionType type : outputs.required_types()) { |
| 57 if (!IsValidBundleDataSubstitution(type)) { |
| 58 *err_ = Err(value->origin(), "Invalid substitution type.", |
| 59 "The substitution " + std::string(kSubstitutionNames[type]) + |
| 60 " isn't valid for something\n" |
| 61 "operating on a bundle_data file such as this."); |
| 62 return false; |
| 63 } |
| 64 } |
| 65 |
| 66 // Validate that outputs are in the bundle. |
| 67 CHECK(outputs.list().size() == value->list_value().size()); |
| 68 for (size_t i = 0; i < outputs.list().size(); i++) { |
| 69 if (!EnsureSubstitutionIsInBundleDir(outputs.list()[i], |
| 70 value->list_value()[i])) |
| 71 return false; |
| 72 } |
| 73 |
| 74 return true; |
| 75 } |
| 76 |
| 77 bool BundleDataTargetGenerator::EnsureSubstitutionIsInBundleDir( |
| 78 const SubstitutionPattern& pattern, |
| 79 const Value& original_value) { |
| 80 if (pattern.ranges().empty()) { |
| 81 // Pattern is empty, error out (this prevents weirdness below). |
| 82 *err_ = Err(original_value, "This has an empty value in it."); |
| 83 return false; |
| 84 } |
| 85 |
| 86 if (SubstitutionIsInBundleDir(pattern.ranges()[0].type)) |
| 87 return true; |
| 88 |
| 89 *err_ = Err(original_value, |
| 90 "File is not inside bundle directory.", |
| 91 "The given file should be in the output directory. Normally you\n" |
| 92 "would specify {{bundle_resources_dir}} or such substitution."); |
| 93 return false; |
| 94 } |
OLD | NEW |