| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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/build_settings.h" |
| 6 #include "tools/gn/filesystem_utils.h" |
| 7 #include "tools/gn/functions.h" |
| 8 #include "tools/gn/parse_tree.h" |
| 9 #include "tools/gn/scope.h" |
| 10 #include "tools/gn/settings.h" |
| 11 #include "tools/gn/source_dir.h" |
| 12 |
| 13 namespace functions { |
| 14 |
| 15 namespace { |
| 16 |
| 17 void ConvertOneTargetName(Scope* scope, |
| 18 const FunctionCallNode* function, |
| 19 const Value& target_name, |
| 20 std::string suffix, |
| 21 Value* result, |
| 22 Err* err) { |
| 23 Label label = Label::Resolve(scope->GetSourceDir(), |
| 24 scope->settings()->toolchain_label(), |
| 25 target_name, |
| 26 err); |
| 27 if (err->has_error()) { |
| 28 return; |
| 29 } |
| 30 if (!label.dir().is_source_absolute()) { |
| 31 *err = Err(function, |
| 32 "This path seems to be outside the source directory: " + |
| 33 target_name.string_value()); |
| 34 return; |
| 35 } |
| 36 |
| 37 SourceDir dir = GetGenDirForSourceDir(scope->settings(), label.dir()); |
| 38 SourceFile file = dir.ResolveRelativeFile(label.name() + suffix); |
| 39 if (file.is_null()) { |
| 40 *err = Err(function, "Couldn't resolve file."); |
| 41 return; |
| 42 } |
| 43 result->string_value() = file.value(); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 const char kGetTargetGenPath[] = "get_target_gen_path"; |
| 49 const char kGetTargetGenPath_HelpShort[] = |
| 50 "get_target_gen_path: Get an output path based on a target name."; |
| 51 const char kGetTargetGenPath_Help[] = |
| 52 "get_target_gen_path: Get an output path based on a target name.\n" |
| 53 "\n" |
| 54 " Takes a string argument representing a target name, or a list of such\n" |
| 55 " strings and converts it/them to a string/list of paths with the\n" |
| 56 " provided suffix.\n" |
| 57 "\n" |
| 58 "Arguments:\n" |
| 59 "\n" |
| 60 " input\n" |
| 61 " A string or list of strings representing target names.\n" |
| 62 "\n" |
| 63 " suffix\n" |
| 64 " A suffix to be appended to all the generated paths.\n" |
| 65 "\n" |
| 66 "Return value\n" |
| 67 "\n" |
| 68 " The return value will be the same type as the input value (either a\n" |
| 69 " string or a list of strings). For each target in the input, the output\n" |
| 70 " contains a path to that target's target_gen_dir with the provided\n" |
| 71 " suffix.\n" |
| 72 "\n" |
| 73 " These paths are the same as the evaluation of\n" |
| 74 " \"${target_gen_dir}/${target_name}${suffix}\" within the context of\n" |
| 75 " each target.\n" |
| 76 "\n" |
| 77 "Example\n" |
| 78 "\n" |
| 79 " # Get the path to this target's .foo output.\n" |
| 80 " foo_path = get_target_gen_path(target_name, \".foo\")\n" |
| 81 "\n" |
| 82 " # Get the paths to the .foo outputs of all of this target's foo_deps.\n" |
| 83 " foo_deps_paths = get_target_gen_path(foo_deps, \".foo\")\n" |
| 84 ; |
| 85 |
| 86 Value RunGetTargetGenPath(Scope* scope, |
| 87 const FunctionCallNode* function, |
| 88 const std::vector<Value>& args, |
| 89 Err* err) { |
| 90 Value result; |
| 91 |
| 92 // Argument indices. |
| 93 static const size_t kArgIndexInputs = 0; |
| 94 static const size_t kArgIndexSuffix = 1; |
| 95 |
| 96 // Inputs. |
| 97 if (args.size() != 2) { |
| 98 *err = Err(function->function(), |
| 99 "Wrong # of arguments for get_target_gen_path."); |
| 100 return result; |
| 101 } |
| 102 const Value& inputs = args[kArgIndexInputs]; |
| 103 const Value& suffix_value = args[kArgIndexSuffix]; |
| 104 if (!suffix_value.VerifyTypeIs(Value::STRING, err)) |
| 105 return result; |
| 106 |
| 107 const std::string& suffix = suffix_value.string_value(); |
| 108 |
| 109 // Path conversion. |
| 110 if (inputs.type() == Value::STRING) { |
| 111 result = Value(function, Value::STRING); |
| 112 ConvertOneTargetName(scope, function, inputs, suffix, &result, err); |
| 113 return result; |
| 114 } else if (inputs.type() == Value::LIST) { |
| 115 result = Value(function, Value::LIST); |
| 116 result.list_value().reserve(inputs.list_value().size()); |
| 117 |
| 118 for (size_t i = 0; i < inputs.list_value().size(); i++) { |
| 119 result.list_value().push_back(Value(function, Value::STRING)); |
| 120 ConvertOneTargetName(scope, |
| 121 function, |
| 122 inputs.list_value()[i], |
| 123 suffix, |
| 124 &result.list_value().back(), |
| 125 err); |
| 126 if (err->has_error()) { |
| 127 result = Value(); |
| 128 return result; |
| 129 } |
| 130 } |
| 131 return result; |
| 132 } |
| 133 |
| 134 *err = Err(function->function(), |
| 135 "get_target_gen_path requires a list or a string."); |
| 136 return result; |
| 137 } |
| 138 |
| 139 } // namespace functions |
| OLD | NEW |