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 | |
| 15 | import("python_script.gni") |
| 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 | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 48 | # Example: |
| 49 | # |
| 50 | # pw_exec("hello_world") { |
| 51 | # program = "/bin/sh" |
| 52 | # args = [ |
| 53 | # "-c", |
| 54 | # "echo hello \$WORLD", |
| 55 | # ] |
| 56 | # env = [ |
| 57 | # "WORLD=world", |
| 58 | # ] |
| 59 | # } |
| 60 | # |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 61 | template("pw_exec") { |
| 62 | assert(defined(invoker.program), "pw_exec requires a program to run") |
| 63 | |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 64 | _script_args = [] |
| 65 | |
| 66 | if (defined(invoker.env_file)) { |
| 67 | _script_args += [ |
| 68 | "--env-file", |
| 69 | get_path_info(invoker.env_file, "abspath"), |
| 70 | ] |
| 71 | } |
| 72 | |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame^] | 73 | if (defined(invoker.args_file)) { |
| 74 | _script_args += [ |
| 75 | "--args-file", |
| 76 | get_path_info(invoker.args_file, "abspath"), |
| 77 | ] |
| 78 | |
| 79 | if (defined(invoker.skip_empty_args) && invoker.skip_empty_args) { |
| 80 | _script_args += [ "--skip-empty-args" ] |
| 81 | } |
| 82 | } |
| 83 | |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 84 | if (defined(invoker.env)) { |
| 85 | foreach(_env, invoker.env) { |
| 86 | _script_args += [ |
| 87 | "--env", |
| 88 | _env, |
| 89 | ] |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | _script_args += [ |
| 94 | "--", |
| 95 | invoker.program, |
| 96 | ] |
| 97 | if (defined(invoker.args)) { |
| 98 | _script_args += invoker.args |
| 99 | } |
| 100 | |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 101 | pw_python_script(target_name) { |
| 102 | script = "$dir_pw_build/py/exec.py" |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 103 | args = _script_args |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 104 | |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 105 | forward_variables_from(invoker, |
| 106 | [ |
| 107 | "deps", |
| 108 | "inputs", |
Alexei Frolov | c15a988 | 2019-12-23 14:29:02 -0800 | [diff] [blame^] | 109 | "pool", |
Alexei Frolov | d74ae48 | 2019-12-19 15:44:15 -0800 | [diff] [blame] | 110 | ]) |
| 111 | |
| 112 | if (!defined(inputs)) { |
| 113 | inputs = [] |
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 | if (defined(invoker.env_file)) { |
| 116 | inputs += [ invoker.env_file ] |
Alexei Frolov | 293b09b | 2019-12-17 13:22:18 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (defined(invoker.outputs)) { |
| 120 | outputs = invoker.outputs |
| 121 | } else { |
| 122 | stamp = true |
| 123 | } |
| 124 | } |
| 125 | } |