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