blob: fc6a5f9503acdaeb74a65a4fa4acc829607c05f6 [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
Wyatt Hepler51ded742020-10-19 14:45:27 -070015import("python_action.gni")
Alexei Frolov293b09b2019-12-17 13:22:18 -080016
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 Frolovc15a9882019-12-23 14:29:02 -080025#
Alexei Frolov293b09b2019-12-17 13:22:18 -080026# args: Optional list of arguments to the program.
Alexei Frolovc15a9882019-12-23 14:29:02 -080027#
Alexei Frolovd74ae482019-12-19 15:44:15 -080028# deps: Dependencies for this target.
Alexei Frolovc15a9882019-12-23 14:29:02 -080029#
Alexei Frolov293b09b2019-12-17 13:22:18 -080030# inputs: Optional list of build inputs to the program.
Alexei Frolovc15a9882019-12-23 14:29:02 -080031#
Alexei Frolov293b09b2019-12-17 13:22:18 -080032# outputs: Optional list of artifacts produced by the program's execution.
Alexei Frolovc15a9882019-12-23 14:29:02 -080033#
Alexei Frolovd74ae482019-12-19 15:44:15 -080034# env: Optional list of key-value pairs defining environment variables for
35# the program.
Alexei Frolovc15a9882019-12-23 14:29:02 -080036#
Alexei Frolovd74ae482019-12-19 15:44:15 -080037# 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 Frolovc15a9882019-12-23 14:29:02 -080040# 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 Frolov32d8b4d2020-01-03 13:23:54 -080048# capture_output: If true, output from the program is hidden unless the program
49# exits with an error. Defaults to true.
50#
Alexei Frolovd74ae482019-12-19 15:44:15 -080051# 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 Frolov293b09b2019-12-17 13:22:18 -080064template("pw_exec") {
65 assert(defined(invoker.program), "pw_exec requires a program to run")
66
Alexei Frolov32d8b4d2020-01-03 13:23:54 -080067 _script_args = [
68 "--target",
69 target_name,
70 ]
Alexei Frolovd74ae482019-12-19 15:44:15 -080071
72 if (defined(invoker.env_file)) {
73 _script_args += [
74 "--env-file",
Michael Spangc8b93902021-05-30 15:53:56 -040075 rebase_path(invoker.env_file, root_build_dir),
Alexei Frolovd74ae482019-12-19 15:44:15 -080076 ]
77 }
78
Alexei Frolovc15a9882019-12-23 14:29:02 -080079 if (defined(invoker.args_file)) {
80 _script_args += [
81 "--args-file",
Michael Spangc8b93902021-05-30 15:53:56 -040082 rebase_path(invoker.args_file, root_build_dir),
Alexei Frolovc15a9882019-12-23 14:29:02 -080083 ]
84
85 if (defined(invoker.skip_empty_args) && invoker.skip_empty_args) {
86 _script_args += [ "--skip-empty-args" ]
87 }
88 }
89
Alexei Frolovd74ae482019-12-19 15:44:15 -080090 if (defined(invoker.env)) {
91 foreach(_env, invoker.env) {
92 _script_args += [
93 "--env",
94 _env,
95 ]
96 }
97 }
98
Alexei Frolov32d8b4d2020-01-03 13:23:54 -080099 if (!defined(invoker.capture_output) || invoker.capture_output) {
100 _script_args += [ "--capture-output" ]
101 }
102
Alexei Frolovd74ae482019-12-19 15:44:15 -0800103 _script_args += [
104 "--",
105 invoker.program,
106 ]
107 if (defined(invoker.args)) {
108 _script_args += invoker.args
109 }
110
Wyatt Heplerc8e05a42020-10-19 14:49:39 -0700111 pw_python_action(target_name) {
Wyatt Hepler27f69f02020-07-27 09:06:21 -0700112 script = "$dir_pw_build/py/pw_build/exec.py"
Alexei Frolovd74ae482019-12-19 15:44:15 -0800113 args = _script_args
Alexei Frolov293b09b2019-12-17 13:22:18 -0800114
Alexei Frolovd74ae482019-12-19 15:44:15 -0800115 forward_variables_from(invoker,
116 [
117 "deps",
118 "inputs",
Alexei Frolovc15a9882019-12-23 14:29:02 -0800119 "pool",
Alexei Frolovd74ae482019-12-19 15:44:15 -0800120 ])
121
122 if (!defined(inputs)) {
123 inputs = []
Alexei Frolov293b09b2019-12-17 13:22:18 -0800124 }
Alexei Frolovd74ae482019-12-19 15:44:15 -0800125 if (defined(invoker.env_file)) {
126 inputs += [ invoker.env_file ]
Alexei Frolov293b09b2019-12-17 13:22:18 -0800127 }
128
129 if (defined(invoker.outputs)) {
130 outputs = invoker.outputs
131 } else {
132 stamp = true
133 }
134 }
135}