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