compiled_action.gni 5.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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.

# This file introduces two related templates that act like action and
# action_foreach but instead of running a Python script, it will compile a
# given tool in the host toolchain and run that (either once or over the list
# of inputs, depending on the variant).
#
# Parameters
#
#   tool (required)
#       [label] Label of the tool to run. This should be an executable, and
#       this label should not include a toolchain (anything in parens). The
#       host compile of this tool will be used.
#
#   outputs (required)
#       [list of files] Like the outputs of action (if using "compiled_action",
#       this would be just the list of outputs), or action_foreach (if using
#       "compiled_action_foreach", this would contain source expansions mapping
#       input to output files).
#
#   args (required)
#       [list of strings] Same meaning as action/action_foreach.
#
26 27 28 29 30
#   inputs (optional)
#       Files the binary takes as input. The step will be re-run whenever any
#       of these change. If inputs is empty, the step will run only when the
#       binary itself changes.
#
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#   visibility
#   deps
#   args   (all optional)
#       Same meaning as action/action_foreach.
#
#
# Example of usage:
#
#   compiled_action("run_my_tool") {
#     tool = "//tools/something:mytool"
#     outputs = [
#       "$target_gen_dir/mysource.cc",
#       "$target_gen_dir/mysource.h",
#     ]
#
#     # The tool takes this input.
47
#     inputs = [ "my_input_file.idl" ]
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#
#     # In this case, the tool takes as arguments the input file and the output
#     # build dir (both relative to the "cd" that the script will be run in)
#     # and will produce the output files listed above.
#     args = [
#       rebase_path("my_input_file.idl", root_build_dir),
#       "--output-dir", rebase_path(target_gen_dir, root_build_dir),
#     ]
#   }
#
# You would typically declare your tool like this:
#   if (host_toolchain == current_toolchain) {
#     executable("mytool") {
#       ...
#     }
#   }
# The if statement around the executable is optional. That says "I only care
# about this target in the host toolchain". Usually this is what you want, and
# saves unnecessarily compiling your tool for the target platform. But if you
# need a target build of your tool as well, just leave off the if statement.

69
if (host_os == "win") {
70 71 72 73 74
  _host_executable_suffix = ".exe"
} else {
  _host_executable_suffix = ""
}

75 76 77 78 79
template("compiled_action") {
  assert(defined(invoker.tool), "tool must be defined for $target_name")
  assert(defined(invoker.outputs), "outputs must be defined for $target_name")
  assert(defined(invoker.args), "args must be defined for $target_name")

80 81 82
  assert(!defined(invoker.sources),
         "compiled_action doesn't take a sources arg. Use inputs instead.")

83
  action(target_name) {
84 85 86 87 88 89 90 91
    forward_variables_from(invoker,
                           [
                             "deps",
                             "inputs",
                             "outputs",
                             "testonly",
                             "visibility",
                           ])
92 93 94 95 96 97
    if (!defined(deps)) {
      deps = []
    }
    if (!defined(inputs)) {
      inputs = []
    }
98 99 100 101 102 103 104 105 106 107 108

    script = "//build/gn_run_binary.py"

    # Constuct the host toolchain version of the tool.
    host_tool = invoker.tool + "($host_toolchain)"

    # Get the path to the executable. Currently, this assumes that the tool
    # does not specify output_name so that the target name is the name to use.
    # If that's not the case, we'll need another argument to the script to
    # specify this, since we can't know what the output name is (it might be in
    # another file not processed yet).
scottmg's avatar
scottmg committed
109 110 111
    host_executable =
        get_label_info(host_tool, "root_out_dir") + "/" +
        get_label_info(host_tool, "name") + _host_executable_suffix
112

113 114 115
    # Add the executable itself as an input.
    inputs += [ host_executable ]

116
    deps += [ host_tool ]
117 118 119

    # The script takes as arguments the binary to run, and then the arguments
    # to pass it.
scottmg's avatar
scottmg committed
120
    args = [ rebase_path(host_executable, root_build_dir) ] + invoker.args
121 122 123 124 125 126 127 128 129 130
  }
}

template("compiled_action_foreach") {
  assert(defined(invoker.sources), "sources must be defined for $target_name")
  assert(defined(invoker.tool), "tool must be defined for $target_name")
  assert(defined(invoker.outputs), "outputs must be defined for $target_name")
  assert(defined(invoker.args), "args must be defined for $target_name")

  action_foreach(target_name) {
131 132 133 134 135 136 137 138 139
    forward_variables_from(invoker,
                           [
                             "deps",
                             "inputs",
                             "outputs",
                             "sources",
                             "testonly",
                             "visibility",
                           ])
140 141 142 143 144 145
    if (!defined(deps)) {
      deps = []
    }
    if (!defined(inputs)) {
      inputs = []
    }
146 147 148 149 150 151 152 153 154 155 156

    script = "//build/gn_run_binary.py"

    # Constuct the host toolchain version of the tool.
    host_tool = invoker.tool + "($host_toolchain)"

    # Get the path to the executable. Currently, this assumes that the tool
    # does not specify output_name so that the target name is the name to use.
    # If that's not the case, we'll need another argument to the script to
    # specify this, since we can't know what the output name is (it might be in
    # another file not processed yet).
scottmg's avatar
scottmg committed
157 158 159
    host_executable =
        get_label_info(host_tool, "root_out_dir") + "/" +
        get_label_info(host_tool, "name") + _host_executable_suffix
160

161 162 163
    # Add the executable itself as an input.
    inputs += [ host_executable ]

164
    deps += [ host_tool ]
165 166 167

    # The script takes as arguments the binary to run, and then the arguments
    # to pass it.
scottmg's avatar
scottmg committed
168
    args = [ rebase_path(host_executable, root_build_dir) ] + invoker.args
169 170
  }
}