Avi Drissman | 73a09d1 | 2022-09-08 20:33:38 | [diff] [blame] | 1 | # Copyright 2014 The Chromium Authors |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # This file introduces two related templates that act like action and |
| 6 | # action_foreach but instead of running a Python script, it will compile a |
| 7 | # given tool in the host toolchain and run that (either once or over the list |
| 8 | # of inputs, depending on the variant). |
| 9 | # |
| 10 | # Parameters |
| 11 | # |
| 12 | # tool (required) |
| 13 | # [label] Label of the tool to run. This should be an executable, and |
| 14 | # this label should not include a toolchain (anything in parens). The |
| 15 | # host compile of this tool will be used. |
| 16 | # |
| 17 | # outputs (required) |
| 18 | # [list of files] Like the outputs of action (if using "compiled_action", |
| 19 | # this would be just the list of outputs), or action_foreach (if using |
| 20 | # "compiled_action_foreach", this would contain source expansions mapping |
| 21 | # input to output files). |
| 22 | # |
| 23 | # args (required) |
| 24 | # [list of strings] Same meaning as action/action_foreach. |
| 25 | # |
Brett Wilson | 4b04efb | 2014-08-28 20:24:32 | [diff] [blame] | 26 | # inputs (optional) |
| 27 | # Files the binary takes as input. The step will be re-run whenever any |
| 28 | # of these change. If inputs is empty, the step will run only when the |
| 29 | # binary itself changes. |
| 30 | # |
Dale Curtis | 7d284af | 2018-11-08 01:27:52 | [diff] [blame] | 31 | # depfile |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 32 | # deps |
Dale Curtis | 7d284af | 2018-11-08 01:27:52 | [diff] [blame] | 33 | # visibility (all optional) |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 34 | # Same meaning as action/action_foreach. |
| 35 | # |
| 36 | # |
| 37 | # Example of usage: |
| 38 | # |
| 39 | # compiled_action("run_my_tool") { |
| 40 | # tool = "//tools/something:mytool" |
| 41 | # outputs = [ |
| 42 | # "$target_gen_dir/mysource.cc", |
| 43 | # "$target_gen_dir/mysource.h", |
| 44 | # ] |
| 45 | # |
| 46 | # # The tool takes this input. |
[email protected] | a901cc9 | 2014-07-09 17:29:31 | [diff] [blame] | 47 | # inputs = [ "my_input_file.idl" ] |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 48 | # |
| 49 | # # In this case, the tool takes as arguments the input file and the output |
| 50 | # # build dir (both relative to the "cd" that the script will be run in) |
| 51 | # # and will produce the output files listed above. |
| 52 | # args = [ |
| 53 | # rebase_path("my_input_file.idl", root_build_dir), |
| 54 | # "--output-dir", rebase_path(target_gen_dir, root_build_dir), |
| 55 | # ] |
| 56 | # } |
| 57 | # |
| 58 | # You would typically declare your tool like this: |
| 59 | # if (host_toolchain == current_toolchain) { |
| 60 | # executable("mytool") { |
| 61 | # ... |
| 62 | # } |
| 63 | # } |
| 64 | # The if statement around the executable is optional. That says "I only care |
| 65 | # about this target in the host toolchain". Usually this is what you want, and |
| 66 | # saves unnecessarily compiling your tool for the target platform. But if you |
| 67 | # need a target build of your tool as well, just leave off the if statement. |
| 68 | |
dpranke | 4327621 | 2015-02-20 02:55:19 | [diff] [blame] | 69 | if (host_os == "win") { |
Brett Wilson | e1b05dc1 | 2014-09-02 19:50:29 | [diff] [blame] | 70 | _host_executable_suffix = ".exe" |
| 71 | } else { |
| 72 | _host_executable_suffix = "" |
| 73 | } |
| 74 | |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 75 | template("compiled_action") { |
| 76 | assert(defined(invoker.tool), "tool must be defined for $target_name") |
| 77 | assert(defined(invoker.outputs), "outputs must be defined for $target_name") |
| 78 | assert(defined(invoker.args), "args must be defined for $target_name") |
| 79 | |
Brett Wilson | 4b04efb | 2014-08-28 20:24:32 | [diff] [blame] | 80 | assert(!defined(invoker.sources), |
| 81 | "compiled_action doesn't take a sources arg. Use inputs instead.") |
| 82 | |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 83 | action(target_name) { |
agrieve | cdf0f11 | 2015-09-03 15:58:41 | [diff] [blame] | 84 | forward_variables_from(invoker, |
| 85 | [ |
Tom Anderson | 44027d4 | 2018-03-15 17:14:53 | [diff] [blame] | 86 | "data_deps", |
agrieve | cdf0f11 | 2015-09-03 15:58:41 | [diff] [blame] | 87 | "deps", |
Dale Curtis | 7d284af | 2018-11-08 01:27:52 | [diff] [blame] | 88 | "depfile", |
agrieve | cdf0f11 | 2015-09-03 15:58:41 | [diff] [blame] | 89 | "inputs", |
| 90 | "outputs", |
| 91 | "testonly", |
| 92 | "visibility", |
| 93 | ]) |
brettw | 0785c95 | 2016-05-03 01:06:33 | [diff] [blame] | 94 | if (!defined(deps)) { |
| 95 | deps = [] |
| 96 | } |
| 97 | if (!defined(inputs)) { |
| 98 | inputs = [] |
| 99 | } |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 100 | |
| 101 | script = "//build/gn_run_binary.py" |
| 102 | |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 103 | # Constuct the host toolchain version of the tool. |
| 104 | host_tool = invoker.tool + "($host_toolchain)" |
| 105 | |
| 106 | # Get the path to the executable. Currently, this assumes that the tool |
| 107 | # does not specify output_name so that the target name is the name to use. |
| 108 | # If that's not the case, we'll need another argument to the script to |
| 109 | # specify this, since we can't know what the output name is (it might be in |
| 110 | # another file not processed yet). |
scottmg | b199254 | 2014-12-02 00:25:20 | [diff] [blame] | 111 | host_executable = |
| 112 | get_label_info(host_tool, "root_out_dir") + "/" + |
| 113 | get_label_info(host_tool, "name") + _host_executable_suffix |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 114 | |
agrieve | cdf0f11 | 2015-09-03 15:58:41 | [diff] [blame] | 115 | deps += [ host_tool ] |
Takuto Ikuta | 3693d51 | 2024-08-14 08:13:00 | [diff] [blame] | 116 | inputs += [ host_executable ] |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 117 | |
| 118 | # The script takes as arguments the binary to run, and then the arguments |
| 119 | # to pass it. |
scottmg | b199254 | 2014-12-02 00:25:20 | [diff] [blame] | 120 | args = [ rebase_path(host_executable, root_build_dir) ] + invoker.args |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | template("compiled_action_foreach") { |
| 125 | assert(defined(invoker.sources), "sources must be defined for $target_name") |
| 126 | assert(defined(invoker.tool), "tool must be defined for $target_name") |
| 127 | assert(defined(invoker.outputs), "outputs must be defined for $target_name") |
| 128 | assert(defined(invoker.args), "args must be defined for $target_name") |
| 129 | |
| 130 | action_foreach(target_name) { |
agrieve | cdf0f11 | 2015-09-03 15:58:41 | [diff] [blame] | 131 | forward_variables_from(invoker, |
| 132 | [ |
| 133 | "deps", |
Dale Curtis | 7d284af | 2018-11-08 01:27:52 | [diff] [blame] | 134 | "depfile", |
agrieve | cdf0f11 | 2015-09-03 15:58:41 | [diff] [blame] | 135 | "inputs", |
| 136 | "outputs", |
| 137 | "sources", |
| 138 | "testonly", |
| 139 | "visibility", |
| 140 | ]) |
brettw | 0785c95 | 2016-05-03 01:06:33 | [diff] [blame] | 141 | if (!defined(deps)) { |
| 142 | deps = [] |
| 143 | } |
| 144 | if (!defined(inputs)) { |
| 145 | inputs = [] |
| 146 | } |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 147 | |
| 148 | script = "//build/gn_run_binary.py" |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 149 | |
| 150 | # Constuct the host toolchain version of the tool. |
| 151 | host_tool = invoker.tool + "($host_toolchain)" |
| 152 | |
| 153 | # Get the path to the executable. Currently, this assumes that the tool |
| 154 | # does not specify output_name so that the target name is the name to use. |
| 155 | # If that's not the case, we'll need another argument to the script to |
| 156 | # specify this, since we can't know what the output name is (it might be in |
| 157 | # another file not processed yet). |
scottmg | b199254 | 2014-12-02 00:25:20 | [diff] [blame] | 158 | host_executable = |
| 159 | get_label_info(host_tool, "root_out_dir") + "/" + |
| 160 | get_label_info(host_tool, "name") + _host_executable_suffix |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 161 | |
agrieve | cdf0f11 | 2015-09-03 15:58:41 | [diff] [blame] | 162 | deps += [ host_tool ] |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 163 | |
| 164 | # The script takes as arguments the binary to run, and then the arguments |
| 165 | # to pass it. |
scottmg | b199254 | 2014-12-02 00:25:20 | [diff] [blame] | 166 | args = [ rebase_path(host_executable, root_build_dir) ] + invoker.args |
[email protected] | 137dff6d | 2014-06-12 19:35:55 | [diff] [blame] | 167 | } |
| 168 | } |