| Index: tools/gn/function_get_target_gen_path.cc
|
| diff --git a/tools/gn/function_get_target_gen_path.cc b/tools/gn/function_get_target_gen_path.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3f74dae2520970ca3bf28ffbda50defa3410fa20
|
| --- /dev/null
|
| +++ b/tools/gn/function_get_target_gen_path.cc
|
| @@ -0,0 +1,139 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "tools/gn/build_settings.h"
|
| +#include "tools/gn/filesystem_utils.h"
|
| +#include "tools/gn/functions.h"
|
| +#include "tools/gn/parse_tree.h"
|
| +#include "tools/gn/scope.h"
|
| +#include "tools/gn/settings.h"
|
| +#include "tools/gn/source_dir.h"
|
| +
|
| +namespace functions {
|
| +
|
| +namespace {
|
| +
|
| +void ConvertOneTargetName(Scope* scope,
|
| + const FunctionCallNode* function,
|
| + const Value& target_name,
|
| + std::string suffix,
|
| + Value* result,
|
| + Err* err) {
|
| + Label label = Label::Resolve(scope->GetSourceDir(),
|
| + scope->settings()->toolchain_label(),
|
| + target_name,
|
| + err);
|
| + if (err->has_error()) {
|
| + return;
|
| + }
|
| + if (!label.dir().is_source_absolute()) {
|
| + *err = Err(function,
|
| + "This path seems to be outside the source directory: " +
|
| + target_name.string_value());
|
| + return;
|
| + }
|
| +
|
| + SourceDir dir = GetGenDirForSourceDir(scope->settings(), label.dir());
|
| + SourceFile file = dir.ResolveRelativeFile(label.name() + suffix);
|
| + if (file.is_null()) {
|
| + *err = Err(function, "Couldn't resolve file.");
|
| + return;
|
| + }
|
| + result->string_value() = file.value();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +const char kGetTargetGenPath[] = "get_target_gen_path";
|
| +const char kGetTargetGenPath_HelpShort[] =
|
| + "get_target_gen_path: Get an output path based on a target name.";
|
| +const char kGetTargetGenPath_Help[] =
|
| + "get_target_gen_path: Get an output path based on a target name.\n"
|
| + "\n"
|
| + " Takes a string argument representing a target name, or a list of such\n"
|
| + " strings and converts it/them to a string/list of paths with the\n"
|
| + " provided suffix.\n"
|
| + "\n"
|
| + "Arguments:\n"
|
| + "\n"
|
| + " input\n"
|
| + " A string or list of strings representing target names.\n"
|
| + "\n"
|
| + " suffix\n"
|
| + " A suffix to be appended to all the generated paths.\n"
|
| + "\n"
|
| + "Return value\n"
|
| + "\n"
|
| + " The return value will be the same type as the input value (either a\n"
|
| + " string or a list of strings). For each target in the input, the output\n"
|
| + " contains a path to that target's target_gen_dir with the provided\n"
|
| + " suffix.\n"
|
| + "\n"
|
| + " These paths are the same as the evaluation of\n"
|
| + " \"${target_gen_dir}/${target_name}${suffix}\" within the context of\n"
|
| + " each target.\n"
|
| + "\n"
|
| + "Example\n"
|
| + "\n"
|
| + " # Get the path to this target's .foo output.\n"
|
| + " foo_path = get_target_gen_path(target_name, \".foo\")\n"
|
| + "\n"
|
| + " # Get the paths to the .foo outputs of all of this target's foo_deps.\n"
|
| + " foo_deps_paths = get_target_gen_path(foo_deps, \".foo\")\n"
|
| + ;
|
| +
|
| +Value RunGetTargetGenPath(Scope* scope,
|
| + const FunctionCallNode* function,
|
| + const std::vector<Value>& args,
|
| + Err* err) {
|
| + Value result;
|
| +
|
| + // Argument indices.
|
| + static const size_t kArgIndexInputs = 0;
|
| + static const size_t kArgIndexSuffix = 1;
|
| +
|
| + // Inputs.
|
| + if (args.size() != 2) {
|
| + *err = Err(function->function(),
|
| + "Wrong # of arguments for get_target_gen_path.");
|
| + return result;
|
| + }
|
| + const Value& inputs = args[kArgIndexInputs];
|
| + const Value& suffix_value = args[kArgIndexSuffix];
|
| + if (!suffix_value.VerifyTypeIs(Value::STRING, err))
|
| + return result;
|
| +
|
| + const std::string& suffix = suffix_value.string_value();
|
| +
|
| + // Path conversion.
|
| + if (inputs.type() == Value::STRING) {
|
| + result = Value(function, Value::STRING);
|
| + ConvertOneTargetName(scope, function, inputs, suffix, &result, err);
|
| + return result;
|
| + } else if (inputs.type() == Value::LIST) {
|
| + result = Value(function, Value::LIST);
|
| + result.list_value().reserve(inputs.list_value().size());
|
| +
|
| + for (size_t i = 0; i < inputs.list_value().size(); i++) {
|
| + result.list_value().push_back(Value(function, Value::STRING));
|
| + ConvertOneTargetName(scope,
|
| + function,
|
| + inputs.list_value()[i],
|
| + suffix,
|
| + &result.list_value().back(),
|
| + err);
|
| + if (err->has_error()) {
|
| + result = Value();
|
| + return result;
|
| + }
|
| + }
|
| + return result;
|
| + }
|
| +
|
| + *err = Err(function->function(),
|
| + "get_target_gen_path requires a list or a string.");
|
| + return result;
|
| +}
|
| +
|
| +} // namespace functions
|
|
|