blob: 7c8765d1bd98b09d02db013229c2b1efdf4f143d [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).
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 Frolovd74ae482019-12-19 15:44:15 -080048# 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 Frolov293b09b2019-12-17 13:22:18 -080061template("pw_exec") {
62 assert(defined(invoker.program), "pw_exec requires a program to run")
63
Alexei Frolovd74ae482019-12-19 15:44:15 -080064 _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 Frolovc15a9882019-12-23 14:29:02 -080073 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 Frolovd74ae482019-12-19 15:44:15 -080084 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 Frolov293b09b2019-12-17 13:22:18 -0800101 pw_python_script(target_name) {
102 script = "$dir_pw_build/py/exec.py"
Alexei Frolovd74ae482019-12-19 15:44:15 -0800103 args = _script_args
Alexei Frolov293b09b2019-12-17 13:22:18 -0800104
Alexei Frolovd74ae482019-12-19 15:44:15 -0800105 forward_variables_from(invoker,
106 [
107 "deps",
108 "inputs",
Alexei Frolovc15a9882019-12-23 14:29:02 -0800109 "pool",
Alexei Frolovd74ae482019-12-19 15:44:15 -0800110 ])
111
112 if (!defined(inputs)) {
113 inputs = []
Alexei Frolov293b09b2019-12-17 13:22:18 -0800114 }
Alexei Frolovd74ae482019-12-19 15:44:15 -0800115 if (defined(invoker.env_file)) {
116 inputs += [ invoker.env_file ]
Alexei Frolov293b09b2019-12-17 13:22:18 -0800117 }
118
119 if (defined(invoker.outputs)) {
120 outputs = invoker.outputs
121 } else {
122 stamp = true
123 }
124 }
125}