xref: /aosp_15_r20/external/pigweed/pw_build/exec.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2019 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Workerimport("python_action.gni")
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker# Runs a program which isn't in Python.
18*61c4878aSAndroid Build Coastguard Worker#
19*61c4878aSAndroid Build Coastguard Worker# This is provided to avoid having to write a new Python wrapper script every
20*61c4878aSAndroid Build Coastguard Worker# time a program needs to be run from GN.
21*61c4878aSAndroid Build Coastguard Worker#
22*61c4878aSAndroid Build Coastguard Worker# Args:
23*61c4878aSAndroid Build Coastguard Worker#  program: The program to run. Can be a full path or just a name (in which case
24*61c4878aSAndroid Build Coastguard Worker#    $PATH is searched).
25*61c4878aSAndroid Build Coastguard Worker#
26*61c4878aSAndroid Build Coastguard Worker#  args: Optional list of arguments to the program.
27*61c4878aSAndroid Build Coastguard Worker#
28*61c4878aSAndroid Build Coastguard Worker#  depfile: Optional depfile of the underlying target.
29*61c4878aSAndroid Build Coastguard Worker#
30*61c4878aSAndroid Build Coastguard Worker#  deps: Dependencies for this target.
31*61c4878aSAndroid Build Coastguard Worker#
32*61c4878aSAndroid Build Coastguard Worker#  public_deps: Public dependencies for this target. In addition to outputs from
33*61c4878aSAndroid Build Coastguard Worker#    this target, outputs generated by public dependencies can be used as inputs
34*61c4878aSAndroid Build Coastguard Worker#    from targets that depend on this one. This is not the case for private
35*61c4878aSAndroid Build Coastguard Worker#    deps.
36*61c4878aSAndroid Build Coastguard Worker#
37*61c4878aSAndroid Build Coastguard Worker#  inputs: Optional list of build inputs to the program.
38*61c4878aSAndroid Build Coastguard Worker#
39*61c4878aSAndroid Build Coastguard Worker#  outputs: Optional list of artifacts produced by the program's execution.
40*61c4878aSAndroid Build Coastguard Worker#
41*61c4878aSAndroid Build Coastguard Worker#  env: Optional list of key-value pairs defining environment variables for
42*61c4878aSAndroid Build Coastguard Worker#    the program.
43*61c4878aSAndroid Build Coastguard Worker#
44*61c4878aSAndroid Build Coastguard Worker#  env_file: Optional path to a file containing a list of newline-separated
45*61c4878aSAndroid Build Coastguard Worker#    key-value pairs defining environment variables for the program.
46*61c4878aSAndroid Build Coastguard Worker#
47*61c4878aSAndroid Build Coastguard Worker#  args_file: Optional path to a file containing additional positional arguments
48*61c4878aSAndroid Build Coastguard Worker#    to the program. Each line of the file is appended to the invocation. Useful
49*61c4878aSAndroid Build Coastguard Worker#    for specifying arguments from GN metadata.
50*61c4878aSAndroid Build Coastguard Worker#
51*61c4878aSAndroid Build Coastguard Worker#  skip_empty_args: If args_file is provided, boolean indicating whether to skip
52*61c4878aSAndroid Build Coastguard Worker#    running the program if the file is empty. Used to avoid running commands
53*61c4878aSAndroid Build Coastguard Worker#    which error when called without arguments.
54*61c4878aSAndroid Build Coastguard Worker#
55*61c4878aSAndroid Build Coastguard Worker#  capture_output: If true, output from the program is hidden unless the program
56*61c4878aSAndroid Build Coastguard Worker#    exits with an error. Defaults to true.
57*61c4878aSAndroid Build Coastguard Worker#
58*61c4878aSAndroid Build Coastguard Worker#  working_directory: The working directory to execute the subprocess with. If
59*61c4878aSAndroid Build Coastguard Worker#    not specified it will not be set and the subprocess will have whatever the
60*61c4878aSAndroid Build Coastguard Worker#    parent current working directory is.
61*61c4878aSAndroid Build Coastguard Worker#
62*61c4878aSAndroid Build Coastguard Worker#  venv: Python virtualenv to pass along to the underlying pw_python_action.
63*61c4878aSAndroid Build Coastguard Worker#
64*61c4878aSAndroid Build Coastguard Worker#  visibility: GN visibility to apply to the underlying target.
65*61c4878aSAndroid Build Coastguard Worker#
66*61c4878aSAndroid Build Coastguard Worker# Example:
67*61c4878aSAndroid Build Coastguard Worker#
68*61c4878aSAndroid Build Coastguard Worker#   pw_exec("hello_world") {
69*61c4878aSAndroid Build Coastguard Worker#     program = "/bin/sh"
70*61c4878aSAndroid Build Coastguard Worker#     args = [
71*61c4878aSAndroid Build Coastguard Worker#       "-c",
72*61c4878aSAndroid Build Coastguard Worker#       "echo hello \$WORLD",
73*61c4878aSAndroid Build Coastguard Worker#     ]
74*61c4878aSAndroid Build Coastguard Worker#     env = [
75*61c4878aSAndroid Build Coastguard Worker#       "WORLD=world",
76*61c4878aSAndroid Build Coastguard Worker#     ]
77*61c4878aSAndroid Build Coastguard Worker#   }
78*61c4878aSAndroid Build Coastguard Worker#
79*61c4878aSAndroid Build Coastguard Workertemplate("pw_exec") {
80*61c4878aSAndroid Build Coastguard Worker  assert(defined(invoker.program), "pw_exec requires a program to run")
81*61c4878aSAndroid Build Coastguard Worker
82*61c4878aSAndroid Build Coastguard Worker  _script_args = [
83*61c4878aSAndroid Build Coastguard Worker    "--target",
84*61c4878aSAndroid Build Coastguard Worker    target_name,
85*61c4878aSAndroid Build Coastguard Worker  ]
86*61c4878aSAndroid Build Coastguard Worker
87*61c4878aSAndroid Build Coastguard Worker  if (defined(invoker.env_file)) {
88*61c4878aSAndroid Build Coastguard Worker    _script_args += [
89*61c4878aSAndroid Build Coastguard Worker      "--env-file",
90*61c4878aSAndroid Build Coastguard Worker      rebase_path(invoker.env_file, root_build_dir),
91*61c4878aSAndroid Build Coastguard Worker    ]
92*61c4878aSAndroid Build Coastguard Worker  }
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker  if (defined(invoker.args_file)) {
95*61c4878aSAndroid Build Coastguard Worker    _script_args += [
96*61c4878aSAndroid Build Coastguard Worker      "--args-file",
97*61c4878aSAndroid Build Coastguard Worker      rebase_path(invoker.args_file, root_build_dir),
98*61c4878aSAndroid Build Coastguard Worker    ]
99*61c4878aSAndroid Build Coastguard Worker
100*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.skip_empty_args) && invoker.skip_empty_args) {
101*61c4878aSAndroid Build Coastguard Worker      _script_args += [ "--skip-empty-args" ]
102*61c4878aSAndroid Build Coastguard Worker    }
103*61c4878aSAndroid Build Coastguard Worker  }
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard Worker  if (defined(invoker.env)) {
106*61c4878aSAndroid Build Coastguard Worker    foreach(_env, invoker.env) {
107*61c4878aSAndroid Build Coastguard Worker      _script_args += [
108*61c4878aSAndroid Build Coastguard Worker        "--env",
109*61c4878aSAndroid Build Coastguard Worker        _env,
110*61c4878aSAndroid Build Coastguard Worker      ]
111*61c4878aSAndroid Build Coastguard Worker    }
112*61c4878aSAndroid Build Coastguard Worker  }
113*61c4878aSAndroid Build Coastguard Worker
114*61c4878aSAndroid Build Coastguard Worker  if (!defined(invoker.capture_output) || invoker.capture_output) {
115*61c4878aSAndroid Build Coastguard Worker    _script_args += [ "--capture-output" ]
116*61c4878aSAndroid Build Coastguard Worker    _capture_output = true
117*61c4878aSAndroid Build Coastguard Worker  } else {
118*61c4878aSAndroid Build Coastguard Worker    _capture_output = false
119*61c4878aSAndroid Build Coastguard Worker  }
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard Worker  if (defined(invoker.working_directory)) {
122*61c4878aSAndroid Build Coastguard Worker    _script_args += [
123*61c4878aSAndroid Build Coastguard Worker      "--working-directory",
124*61c4878aSAndroid Build Coastguard Worker      invoker.working_directory,
125*61c4878aSAndroid Build Coastguard Worker    ]
126*61c4878aSAndroid Build Coastguard Worker  }
127*61c4878aSAndroid Build Coastguard Worker
128*61c4878aSAndroid Build Coastguard Worker  _script_args += [
129*61c4878aSAndroid Build Coastguard Worker    "--",
130*61c4878aSAndroid Build Coastguard Worker    invoker.program,
131*61c4878aSAndroid Build Coastguard Worker  ]
132*61c4878aSAndroid Build Coastguard Worker  if (defined(invoker.args)) {
133*61c4878aSAndroid Build Coastguard Worker    _script_args += invoker.args
134*61c4878aSAndroid Build Coastguard Worker  }
135*61c4878aSAndroid Build Coastguard Worker
136*61c4878aSAndroid Build Coastguard Worker  pw_python_action(target_name) {
137*61c4878aSAndroid Build Coastguard Worker    script = "$dir_pw_build/py/pw_build/exec.py"
138*61c4878aSAndroid Build Coastguard Worker    args = _script_args
139*61c4878aSAndroid Build Coastguard Worker    capture_output = _capture_output
140*61c4878aSAndroid Build Coastguard Worker
141*61c4878aSAndroid Build Coastguard Worker    forward_variables_from(invoker,
142*61c4878aSAndroid Build Coastguard Worker                           [
143*61c4878aSAndroid Build Coastguard Worker                             "cflags",
144*61c4878aSAndroid Build Coastguard Worker                             "cflags_c",
145*61c4878aSAndroid Build Coastguard Worker                             "configs",
146*61c4878aSAndroid Build Coastguard Worker                             "defines",
147*61c4878aSAndroid Build Coastguard Worker                             "depfile",
148*61c4878aSAndroid Build Coastguard Worker                             "deps",
149*61c4878aSAndroid Build Coastguard Worker                             "inputs",
150*61c4878aSAndroid Build Coastguard Worker                             "pool",
151*61c4878aSAndroid Build Coastguard Worker                             "public_configs",
152*61c4878aSAndroid Build Coastguard Worker                             "public_deps",
153*61c4878aSAndroid Build Coastguard Worker                             "venv",
154*61c4878aSAndroid Build Coastguard Worker                             "visibility",
155*61c4878aSAndroid Build Coastguard Worker                           ])
156*61c4878aSAndroid Build Coastguard Worker
157*61c4878aSAndroid Build Coastguard Worker    if (!defined(inputs)) {
158*61c4878aSAndroid Build Coastguard Worker      inputs = []
159*61c4878aSAndroid Build Coastguard Worker    }
160*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.env_file)) {
161*61c4878aSAndroid Build Coastguard Worker      inputs += [ invoker.env_file ]
162*61c4878aSAndroid Build Coastguard Worker    }
163*61c4878aSAndroid Build Coastguard Worker
164*61c4878aSAndroid Build Coastguard Worker    if (defined(invoker.outputs)) {
165*61c4878aSAndroid Build Coastguard Worker      outputs = invoker.outputs
166*61c4878aSAndroid Build Coastguard Worker    } else {
167*61c4878aSAndroid Build Coastguard Worker      stamp = true
168*61c4878aSAndroid Build Coastguard Worker    }
169*61c4878aSAndroid Build Coastguard Worker  }
170*61c4878aSAndroid Build Coastguard Worker}
171