1*8975f5c5SAndroid Build Coastguard Worker# Copyright 2019 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# Wraps a target and any of its arguments to an executable script. 6*8975f5c5SAndroid Build Coastguard Worker# 7*8975f5c5SAndroid Build Coastguard Worker# Many executable targets have build-time-constant arguments. This 8*8975f5c5SAndroid Build Coastguard Worker# template allows those to be wrapped into a single, user- or bot-friendly 9*8975f5c5SAndroid Build Coastguard Worker# script at build time. 10*8975f5c5SAndroid Build Coastguard Worker# 11*8975f5c5SAndroid Build Coastguard Worker# Paths to be wrapped should be relative to root_build_dir and should be 12*8975f5c5SAndroid Build Coastguard Worker# wrapped in "@WrappedPath(...)"; see Example below. 13*8975f5c5SAndroid Build Coastguard Worker# 14*8975f5c5SAndroid Build Coastguard Worker# Variables: 15*8975f5c5SAndroid Build Coastguard Worker# generator_script: Path to the script to use to perform the wrapping. 16*8975f5c5SAndroid Build Coastguard Worker# Defaults to //build/util/generate_wrapper.py. Generally should only 17*8975f5c5SAndroid Build Coastguard Worker# be set by other templates. 18*8975f5c5SAndroid Build Coastguard Worker# wrapper_script: Output path. 19*8975f5c5SAndroid Build Coastguard Worker# executable: Path to the executable to wrap. Can be a script or a 20*8975f5c5SAndroid Build Coastguard Worker# build product. Paths can be relative to the containing gn file 21*8975f5c5SAndroid Build Coastguard Worker# or source-absolute. 22*8975f5c5SAndroid Build Coastguard Worker# executable_args: List of arguments to write into the wrapper. 23*8975f5c5SAndroid Build Coastguard Worker# 24*8975f5c5SAndroid Build Coastguard Worker# Example wrapping a checked-in script: 25*8975f5c5SAndroid Build Coastguard Worker# generate_wrapper("sample_wrapper") { 26*8975f5c5SAndroid Build Coastguard Worker# executable = "//for/bar/sample.py" 27*8975f5c5SAndroid Build Coastguard Worker# wrapper_script = "$root_build_dir/bin/run_sample" 28*8975f5c5SAndroid Build Coastguard Worker# 29*8975f5c5SAndroid Build Coastguard Worker# _sample_argument_path = "//sample/$target_cpu/lib/sample_lib.so" 30*8975f5c5SAndroid Build Coastguard Worker# _rebased_sample_argument_path = rebase_path( 31*8975f5c5SAndroid Build Coastguard Worker# _sample_argument_path, 32*8975f5c5SAndroid Build Coastguard Worker# root_build_dir) 33*8975f5c5SAndroid Build Coastguard Worker# executable_args = [ 34*8975f5c5SAndroid Build Coastguard Worker# "--sample-lib", "@WrappedPath(${_rebased_sample_argument_path})", 35*8975f5c5SAndroid Build Coastguard Worker# ] 36*8975f5c5SAndroid Build Coastguard Worker# } 37*8975f5c5SAndroid Build Coastguard Worker# 38*8975f5c5SAndroid Build Coastguard Worker# Example wrapping a build product: 39*8975f5c5SAndroid Build Coastguard Worker# generate_wrapper("sample_wrapper") { 40*8975f5c5SAndroid Build Coastguard Worker# executable = "$root_build_dir/sample_build_product" 41*8975f5c5SAndroid Build Coastguard Worker# wrapper_script = "$root_build_dir/bin/run_sample_build_product" 42*8975f5c5SAndroid Build Coastguard Worker# } 43*8975f5c5SAndroid Build Coastguard Workertemplate("generate_wrapper") { 44*8975f5c5SAndroid Build Coastguard Worker action(target_name) { 45*8975f5c5SAndroid Build Coastguard Worker if (defined(invoker.generator_script)) { 46*8975f5c5SAndroid Build Coastguard Worker script = invoker.generator_script 47*8975f5c5SAndroid Build Coastguard Worker } else { 48*8975f5c5SAndroid Build Coastguard Worker script = "//build/util/generate_wrapper.py" 49*8975f5c5SAndroid Build Coastguard Worker } 50*8975f5c5SAndroid Build Coastguard Worker _wrapper_script = invoker.wrapper_script 51*8975f5c5SAndroid Build Coastguard Worker if (is_win) { 52*8975f5c5SAndroid Build Coastguard Worker _wrapper_script += ".bat" 53*8975f5c5SAndroid Build Coastguard Worker } 54*8975f5c5SAndroid Build Coastguard Worker 55*8975f5c5SAndroid Build Coastguard Worker data = [ 56*8975f5c5SAndroid Build Coastguard Worker _wrapper_script, 57*8975f5c5SAndroid Build Coastguard Worker "//.vpython3", 58*8975f5c5SAndroid Build Coastguard Worker ] 59*8975f5c5SAndroid Build Coastguard Worker if (defined(invoker.data)) { 60*8975f5c5SAndroid Build Coastguard Worker data += invoker.data 61*8975f5c5SAndroid Build Coastguard Worker } 62*8975f5c5SAndroid Build Coastguard Worker outputs = [ _wrapper_script ] 63*8975f5c5SAndroid Build Coastguard Worker 64*8975f5c5SAndroid Build Coastguard Worker _rebased_executable_to_wrap = 65*8975f5c5SAndroid Build Coastguard Worker rebase_path(invoker.executable, root_build_dir) 66*8975f5c5SAndroid Build Coastguard Worker _rebased_wrapper_script = rebase_path(_wrapper_script, root_build_dir) 67*8975f5c5SAndroid Build Coastguard Worker if (is_win) { 68*8975f5c5SAndroid Build Coastguard Worker _script_language = "batch" 69*8975f5c5SAndroid Build Coastguard Worker } else { 70*8975f5c5SAndroid Build Coastguard Worker _script_language = "bash" 71*8975f5c5SAndroid Build Coastguard Worker } 72*8975f5c5SAndroid Build Coastguard Worker args = [ 73*8975f5c5SAndroid Build Coastguard Worker "--executable", 74*8975f5c5SAndroid Build Coastguard Worker "@WrappedPath(${_rebased_executable_to_wrap})", 75*8975f5c5SAndroid Build Coastguard Worker "--wrapper-script", 76*8975f5c5SAndroid Build Coastguard Worker _rebased_wrapper_script, 77*8975f5c5SAndroid Build Coastguard Worker "--output-directory", 78*8975f5c5SAndroid Build Coastguard Worker rebase_path(root_build_dir, root_build_dir), 79*8975f5c5SAndroid Build Coastguard Worker "--script-language", 80*8975f5c5SAndroid Build Coastguard Worker _script_language, 81*8975f5c5SAndroid Build Coastguard Worker ] 82*8975f5c5SAndroid Build Coastguard Worker 83*8975f5c5SAndroid Build Coastguard Worker if (defined(invoker.executable_args)) { 84*8975f5c5SAndroid Build Coastguard Worker args += [ "--" ] + invoker.executable_args 85*8975f5c5SAndroid Build Coastguard Worker } 86*8975f5c5SAndroid Build Coastguard Worker 87*8975f5c5SAndroid Build Coastguard Worker forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) 88*8975f5c5SAndroid Build Coastguard Worker forward_variables_from(invoker, 89*8975f5c5SAndroid Build Coastguard Worker "*", 90*8975f5c5SAndroid Build Coastguard Worker TESTONLY_AND_VISIBILITY + [ 91*8975f5c5SAndroid Build Coastguard Worker "data", 92*8975f5c5SAndroid Build Coastguard Worker "executable", 93*8975f5c5SAndroid Build Coastguard Worker "executable_args", 94*8975f5c5SAndroid Build Coastguard Worker "generator_script", 95*8975f5c5SAndroid Build Coastguard Worker "wrapper_script", 96*8975f5c5SAndroid Build Coastguard Worker ]) 97*8975f5c5SAndroid Build Coastguard Worker } 98*8975f5c5SAndroid Build Coastguard Worker} 99