| # Copyright 2019 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import("$zx/public/gn/toolchain/environment_redirect.gni") |
| |
| # Depend on a build host tool. |
| # |
| # This defines its target as an environment_redirect() that finds the |
| # actual target that host_tool_action() would use to run its tool. |
| # |
| # Parameters |
| # |
| # tool |
| # Required: See host_tool_action(). |
| # |
| template("host_tool_redirect") { |
| # Dispatch to a host toolchain to get the tool built. |
| environment_redirect(target_name) { |
| forward_variables_from(invoker, |
| [ |
| "assert_no_deps", |
| "testonly", |
| "visibility", |
| ]) |
| environment_label = "$zx/public/gn/toolchain:host" |
| cpu = host_cpu |
| os = host_os |
| direct = true |
| deps = [ |
| invoker.tool, |
| ] |
| } |
| } |
| |
| # Subroutine of host_tool_action() and host_tool_action_foreach(). |
| template("_host_tool_action_target") { |
| action_target = target_name |
| assert(defined(invoker.tool), "tool must be defined for $target_name") |
| assert(defined(invoker.outputs), "outputs must be defined for $target_name") |
| |
| tool_target = "_host_tool_action.tool.$action_target" |
| rspfile_target = "_host_tool_action.rsp.$action_target" |
| |
| host_tool_redirect(tool_target) { |
| forward_variables_from(invoker, [ "testonly" ]) |
| visibility = [ |
| ":$action_target", |
| ":$rspfile_target", |
| ] |
| tool = invoker.tool |
| } |
| |
| # The response file generated by host_tool() gives the files used (like |
| # the executable itself), then "--", then the arguments that make up the |
| # command line prefix to invoke the tool. host_tool_action.sh consumes that. |
| rspfile = "$target_gen_dir/$target_name._host_tool_action.rsp" |
| generated_file(rspfile_target) { |
| forward_variables_from(invoker, [ "testonly" ]) |
| visibility = [ ":$action_target" ] |
| deps = [ |
| ":$tool_target", |
| ] |
| outputs = [ |
| rspfile, |
| ] |
| data_keys = [ "host_tool_rspfile" ] |
| walk_keys = [ "host_tool_barrier" ] |
| } |
| |
| target(invoker._target_type, action_target) { |
| forward_variables_from(invoker, |
| [ |
| "assert_no_deps", |
| "data_deps", |
| "deps", |
| "inputs", |
| "outputs", |
| "public_deps", |
| "sources", |
| "testonly", |
| "visibility", |
| ]) |
| if (!defined(deps)) { |
| deps = [] |
| } |
| if (!defined(inputs)) { |
| inputs = [] |
| } |
| deps += [ |
| ":$rspfile_target", |
| ":$tool_target", |
| ] |
| inputs += [ rspfile ] |
| |
| depfile = |
| "$target_out_dir/$target_name.{{source_file_part}}._host_tool_action.d" |
| if (defined(invoker.depfile)) { |
| orig_depfile = rebase_path(invoker.depfile, root_build_dir) |
| } else { |
| orig_depfile = "-" |
| } |
| |
| script = "$zx/public/gn/host_tool_action.sh" |
| args = rebase_path([ |
| rspfile, |
| outputs[0], |
| depfile, |
| ], |
| root_build_dir) + [ orig_depfile ] |
| if (defined(invoker.args)) { |
| args += invoker.args |
| } |
| |
| metadata = { |
| # This can be used to prune metadata walks that want to treat |
| # any sort of action target as an opaque atom. |
| action_barrier = [] |
| |
| # Never follow the deps to find link outputs from invoker.tool. |
| # TODO: Maybe should still follow invoker.deps? |
| link_barrier = [] |
| if (defined(invoker.metadata)) { |
| forward_variables_from(invoker.metadata, "*") |
| } |
| } |
| } |
| } |
| |
| # Run an action() using a host_tool() compiled for the build host. |
| # |
| # This takes all the same parameters as action() except that $script is |
| # replaced by $tool. See also host_tool_action_foreach(). |
| # |
| # Parameters |
| # |
| # tool |
| # Required: Label (without toolchain) of a host_tool() target. |
| # The tool will be built for the build host if necessary, and |
| # then run with the $args list just like $script runs in action(). |
| # Type: label_no_toolchain |
| # |
| # Other parameters are as for action(). |
| template("host_tool_action") { |
| _host_tool_action_target(target_name) { |
| _target_type = "action" |
| forward_variables_from(invoker, "*") |
| } |
| } |
| |
| # Run an action_foreach() using a host_tool() compiled for the build host. |
| # |
| # This takes all the same parameters as action_foreach() except that |
| # $script is replaced by $tool. host_tool_action_foreach() is to as |
| # action_foreach() as host_tool_action() is to action(). |
| # |
| # Parameters |
| # |
| # tool |
| # Required: Label (without toolchain) of a host_tool() target. |
| # The tool will be built for the build host if necessary, and |
| # then run with the $args list just like $script runs in action(). |
| # Type: label_no_toolchain |
| # |
| # Other parameters are as for action_foreach(). |
| template("host_tool_action_foreach") { |
| _host_tool_action_target(target_name) { |
| _target_type = "action_foreach" |
| forward_variables_from(invoker, "*") |
| } |
| } |