compiled_action.gni revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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# 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#
26#   visibility
27#   source_prereqs
28#   deps
29#   args   (all optional)
30#       Same meaning as action/action_foreach.
31#
32#
33# Example of usage:
34#
35#   compiled_action("run_my_tool") {
36#     tool = "//tools/something:mytool"
37#     outputs = [
38#       "$target_gen_dir/mysource.cc",
39#       "$target_gen_dir/mysource.h",
40#     ]
41#
42#     # The tool takes this input.
43#     source_prereqs = [ "my_input_file.idl" ]
44#
45#     # In this case, the tool takes as arguments the input file and the output
46#     # build dir (both relative to the "cd" that the script will be run in)
47#     # and will produce the output files listed above.
48#     args = [
49#       rebase_path("my_input_file.idl", root_build_dir),
50#       "--output-dir", rebase_path(target_gen_dir, root_build_dir),
51#     ]
52#   }
53#
54# You would typically declare your tool like this:
55#   if (host_toolchain == current_toolchain) {
56#     executable("mytool") {
57#       ...
58#     }
59#   }
60# The if statement around the executable is optional. That says "I only care
61# about this target in the host toolchain". Usually this is what you want, and
62# saves unnecessarily compiling your tool for the target platform. But if you
63# need a target build of your tool as well, just leave off the if statement.
64
65template("compiled_action") {
66  assert(defined(invoker.tool), "tool must be defined for $target_name")
67  assert(defined(invoker.outputs), "outputs must be defined for $target_name")
68  assert(defined(invoker.args), "args must be defined for $target_name")
69
70  action(target_name) {
71    if (defined(invoker.visibility)) {
72      visibility = invoker.visibility
73    }
74
75    script = "//build/gn_run_binary.py"
76
77    if (defined(invoker.source_prereqs)) {
78      source_prereqs = invoker.source_prereqs
79    }
80    outputs = invoker.outputs
81
82    # Constuct the host toolchain version of the tool.
83    host_tool = invoker.tool + "($host_toolchain)"
84
85    # Get the path to the executable. Currently, this assumes that the tool
86    # does not specify output_name so that the target name is the name to use.
87    # If that's not the case, we'll need another argument to the script to
88    # specify this, since we can't know what the output name is (it might be in
89    # another file not processed yet).
90    host_executable = get_label_info(host_tool, "root_out_dir") + "/" +
91      get_label_info(host_tool, "name")
92
93    deps = [ host_tool ]
94    if (defined(invoker.deps)) {
95      deps += invoker.deps
96    }
97
98    # The script takes as arguments the binary to run, and then the arguments
99    # to pass it.
100    args = [
101      rebase_path(host_executable, root_build_dir)
102    ] + invoker.args
103  }
104}
105
106template("compiled_action_foreach") {
107  assert(defined(invoker.sources), "sources must be defined for $target_name")
108  assert(defined(invoker.tool), "tool must be defined for $target_name")
109  assert(defined(invoker.outputs), "outputs must be defined for $target_name")
110  assert(defined(invoker.args), "args must be defined for $target_name")
111
112  action_foreach(target_name) {
113    # Otherwise this is a standalone action, define visibility if requested.
114    if (defined(invoker.visibility)) {
115      visibility = invoker.visibility
116    }
117
118    script = "//build/gn_run_binary.py"
119    sources = invoker.sources
120
121    if (defined(invoker.source_prereqs)) {
122      source_prereqs = invoker.source_prereqs
123    }
124    outputs = invoker.outputs
125
126    # Constuct the host toolchain version of the tool.
127    host_tool = invoker.tool + "($host_toolchain)"
128
129    # Get the path to the executable. Currently, this assumes that the tool
130    # does not specify output_name so that the target name is the name to use.
131    # If that's not the case, we'll need another argument to the script to
132    # specify this, since we can't know what the output name is (it might be in
133    # another file not processed yet).
134    host_executable = get_label_info(host_tool, "root_out_dir") + "/" +
135      get_label_info(host_tool, "name")
136
137    deps = [ host_tool ]
138    if (defined(invoker.deps)) {
139      deps += invoker.deps
140    }
141
142    # The script takes as arguments the binary to run, and then the arguments
143    # to pass it.
144    args = [
145      rebase_path(host_executable, root_build_dir)
146    ] + invoker.args
147  }
148}
149