Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 1 | # Copyright 2019 The Pigweed Authors |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | # use this file except in compliance with the License. You may obtain a copy of |
| 5 | # the License at |
| 6 | # |
| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations under |
| 13 | # the License. |
| 14 | |
Wyatt Hepler | 51ded74 | 2020-10-19 14:45:27 -0700 | [diff] [blame] | 15 | import("python_action.gni") |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 16 | |
| 17 | # Runs a program which isn't in Python. |
| 18 | # |
| 19 | # This is provided to avoid having to write a new Python wrapper script every |
| 20 | # time a program needs to be run from GN. |
| 21 | # |
| 22 | # Args: |
| 23 | # program: The program to run. Can be a full path or just a name (in which case |
| 24 | # $PATH is searched). |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 25 | # |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 26 | # args: Optional list of arguments to the program. |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 27 | # |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 28 | # deps: Dependencies for this target. |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 29 | # |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 30 | # inputs: Optional list of build inputs to the program. |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 31 | # |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 32 | # outputs: Optional list of artifacts produced by the program's execution. |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 33 | # |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 34 | # env: Optional list of key-value pairs defining environment variables for |
| 35 | # the program. |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 36 | # |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 37 | # env_file: Optional path to a file containing a list of newline-separated |
| 38 | # key-value pairs defining environment variables for the program. |
| 39 | # |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 40 | # args_file: Optional path to a file containing additional positional arguments |
| 41 | # to the program. Each line of the file is appended to the invocation. Useful |
| 42 | # for specifying arguments from GN metadata. |
| 43 | # |
| 44 | # skip_empty_args: If args_file is provided, boolean indicating whether to skip |
| 45 | # running the program if the file is empty. Used to avoid running commands |
| 46 | # which error when called without arguments. |
| 47 | # |
Alexei Frolov | 32d8b4d | 2020-01-03 13:23:54 -0800 | [diff] [blame] | 48 | # capture_output: If true, output from the program is hidden unless the program |
| 49 | # exits with an error. Defaults to true. |
| 50 | # |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 51 | # Example: |
| 52 | # |
| 53 | # pw_exec("hello_world") { |
| 54 | # program = "/bin/sh" |
| 55 | # args = [ |
| 56 | # "-c", |
| 57 | # "echo hello \$WORLD", |
| 58 | # ] |
| 59 | # env = [ |
| 60 | # "WORLD=world", |
| 61 | # ] |
| 62 | # } |
| 63 | # |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 64 | template("pw_exec") { |
| 65 | assert(defined(invoker.program), "pw_exec requires a program to run") |
| 66 | |
Alexei Frolov | 32d8b4d | 2020-01-03 13:23:54 -0800 | [diff] [blame] | 67 | _script_args = [ |
| 68 | "--target", |
| 69 | target_name, |
| 70 | ] |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 71 | |
| 72 | if (defined(invoker.env_file)) { |
| 73 | _script_args += [ |
| 74 | "--env-file", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 75 | rebase_path(invoker.env_file, root_build_dir), |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 76 | ] |
| 77 | } |
| 78 | |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 79 | if (defined(invoker.args_file)) { |
| 80 | _script_args += [ |
| 81 | "--args-file", |
Michael Spang | c8b9390 | 2021-05-30 15:53:56 -0400 | [diff] [blame] | 82 | rebase_path(invoker.args_file, root_build_dir), |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 83 | ] |
| 84 | |
| 85 | if (defined(invoker.skip_empty_args) && invoker.skip_empty_args) { |
| 86 | _script_args += [ "--skip-empty-args" ] |
| 87 | } |
| 88 | } |
| 89 | |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 90 | if (defined(invoker.env)) { |
| 91 | foreach(_env, invoker.env) { |
| 92 | _script_args += [ |
| 93 | "--env", |
| 94 | _env, |
| 95 | ] |
| 96 | } |
| 97 | } |
| 98 | |
Alexei Frolov | 32d8b4d | 2020-01-03 13:23:54 -0800 | [diff] [blame] | 99 | if (!defined(invoker.capture_output) || invoker.capture_output) { |
| 100 | _script_args += [ "--capture-output" ] |
| 101 | } |
| 102 | |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 103 | _script_args += [ |
| 104 | "--", |
| 105 | invoker.program, |
| 106 | ] |
| 107 | if (defined(invoker.args)) { |
| 108 | _script_args += invoker.args |
| 109 | } |
| 110 | |
Wyatt Hepler | c8e05a4 | 2020-10-19 14:49:39 -0700 | [diff] [blame] | 111 | pw_python_action(target_name) { |
Wyatt Hepler | 27f69f0 | 2020-07-27 09:06:21 -0700 | [diff] [blame] | 112 | script = "$dir_pw_build/py/pw_build/exec.py" |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 113 | args = _script_args |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 114 | |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 115 | forward_variables_from(invoker, |
| 116 | [ |
| 117 | "deps", |
| 118 | "inputs", |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame] | 119 | "pool", |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 120 | ]) |
| 121 | |
| 122 | if (!defined(inputs)) { |
| 123 | inputs = [] |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 124 | } |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 125 | if (defined(invoker.env_file)) { |
| 126 | inputs += [ invoker.env_file ] |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (defined(invoker.outputs)) { |
| 130 | outputs = invoker.outputs |
| 131 | } else { |
| 132 | stamp = true |
| 133 | } |
| 134 | } |
| 135 | } |