blob: afa2f482d035140a837e288e357ab61bf003aa0a [file] [log] [blame]
Alexei Frolov293b09b2019-12-17 13:22:18 -08001# 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
15import("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).
25# args: Optional list of arguments to the program.
Alexei Frolovd74ae482019-12-19 15:44:15 -080026# deps: Dependencies for this target.
Alexei Frolov293b09b2019-12-17 13:22:18 -080027# inputs: Optional list of build inputs to the program.
28# outputs: Optional list of artifacts produced by the program's execution.
Alexei Frolovd74ae482019-12-19 15:44:15 -080029# env: Optional list of key-value pairs defining environment variables for
30# the program.
31# env_file: Optional path to a file containing a list of newline-separated
32# key-value pairs defining environment variables for the program.
33#
34# Example:
35#
36# pw_exec("hello_world") {
37# program = "/bin/sh"
38# args = [
39# "-c",
40# "echo hello \$WORLD",
41# ]
42# env = [
43# "WORLD=world",
44# ]
45# }
46#
Alexei Frolov293b09b2019-12-17 13:22:18 -080047template("pw_exec") {
48 assert(defined(invoker.program), "pw_exec requires a program to run")
49
Alexei Frolovd74ae482019-12-19 15:44:15 -080050 _script_args = []
51
52 if (defined(invoker.env_file)) {
53 _script_args += [
54 "--env-file",
55 get_path_info(invoker.env_file, "abspath"),
56 ]
57 }
58
59 if (defined(invoker.env)) {
60 foreach(_env, invoker.env) {
61 _script_args += [
62 "--env",
63 _env,
64 ]
65 }
66 }
67
68 _script_args += [
69 "--",
70 invoker.program,
71 ]
72 if (defined(invoker.args)) {
73 _script_args += invoker.args
74 }
75
Alexei Frolov293b09b2019-12-17 13:22:18 -080076 pw_python_script(target_name) {
77 script = "$dir_pw_build/py/exec.py"
Alexei Frolovd74ae482019-12-19 15:44:15 -080078 args = _script_args
Alexei Frolov293b09b2019-12-17 13:22:18 -080079
Alexei Frolovd74ae482019-12-19 15:44:15 -080080 forward_variables_from(invoker,
81 [
82 "deps",
83 "inputs",
84 ])
85
86 if (!defined(inputs)) {
87 inputs = []
Alexei Frolov293b09b2019-12-17 13:22:18 -080088 }
Alexei Frolovd74ae482019-12-19 15:44:15 -080089 if (defined(invoker.env_file)) {
90 inputs += [ invoker.env_file ]
Alexei Frolov293b09b2019-12-17 13:22:18 -080091 }
92
93 if (defined(invoker.outputs)) {
94 outputs = invoker.outputs
95 } else {
96 stamp = true
97 }
98 }
99}