Index: tools/gn/functions_target.cc |
diff --git a/tools/gn/functions_target.cc b/tools/gn/functions_target.cc |
index 1441b8e6fa0d59ecf906aae5b8db708959e92cf9..655c32a5cbbe78466be2264069a73bfc624adf0c 100644 |
--- a/tools/gn/functions_target.cc |
+++ b/tools/gn/functions_target.cc |
@@ -9,6 +9,7 @@ |
#include "tools/gn/parse_tree.h" |
#include "tools/gn/scope.h" |
#include "tools/gn/target_generator.h" |
+#include "tools/gn/template.h" |
#include "tools/gn/value.h" |
#include "tools/gn/variables.h" |
@@ -457,4 +458,68 @@ Value RunStaticLibrary(Scope* scope, |
block, err); |
} |
+// target --------------------------------------------------------------------- |
+ |
+const char kTarget[] = "target"; |
+const char kTarget_HelpShort[] = |
+ "target: Declare an target with the given programmatic type."; |
+const char kTarget_Help[] = |
+ "target: Declare an target with the given programmatic type.\n" |
+ "\n" |
+ " target(target_type_string, target_name_string) { ... }\n" |
+ "\n" |
+ " The target() function is a way to invoke a built-in target or template\n" |
+ " with a type determined at runtime. This is useful for cases where the\n" |
+ " type of a target might not be known statically.\n" |
+ "\n" |
+ " Only templates and built-in target functions are supported for the\n" |
+ " target_type_string parameter. Arbitrary functions, configs, and\n" |
+ " toolchains are not supported.\n" |
+ "\n" |
+ " The call:\n" |
+ " target(\"source_set\", \"doom_melon\") {\n" |
+ " Is equivalent to:\n" |
+ " source_set(\"doom_melon\") {\n" |
+ "\n" |
+ "Example\n" |
+ "\n" |
+ " if (foo_build_as_shared) {\n" |
+ " my_type = \"shared_library\"\n" |
+ " } else {\n" |
+ " my_type = \"source_set\"\n" |
+ " }\n" |
+ "\n" |
+ " target(my_type, \"foo\") {\n" |
+ " ...\n" |
+ " }\n"; |
+Value RunTarget(Scope* scope, |
+ const FunctionCallNode* function, |
+ const std::vector<Value>& args, |
+ BlockNode* block, |
+ Err* err) { |
+ if (args.size() != 2) { |
+ *err = Err(function, "Expected two arguments.", |
+ "Dude, try \"gn help target\"."); |
+ return Value(); |
+ } |
+ |
+ // The first argument must be a string (the target type). Don't type-check |
+ // the second argument since the target-specific function will do that. |
+ if (!args[0].VerifyTypeIs(Value::STRING, err)) |
+ return Value(); |
+ const std::string& target_type = args[0].string_value(); |
+ |
+ // The rest of the args are passed to the function. |
+ std::vector<Value> sub_args(args.begin() + 1, args.end()); |
+ |
+ // Run a template if it is one. |
+ const Template* templ = scope->GetTemplate(target_type); |
+ if (templ) |
+ return templ->Invoke(scope, function, sub_args, block, err); |
+ |
+ // Otherwise, assume the target is a built-in target type. |
+ return ExecuteGenericTarget(target_type.c_str(), scope, function, sub_args, |
+ block, err); |
+} |
+ |
} // namespace functions |