xref: /aosp_15_r20/external/pigweed/pw_build/evaluate_path_expressions.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2022 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# Scans files for special GN target expressions and evaluates them, modifying
18*61c4878aSAndroid Build Coastguard Worker# the files in-place.
19*61c4878aSAndroid Build Coastguard Worker#
20*61c4878aSAndroid Build Coastguard Worker# The supported expressions are the same as within the arguments list of a
21*61c4878aSAndroid Build Coastguard Worker# pw_python_action target, namely:
22*61c4878aSAndroid Build Coastguard Worker#
23*61c4878aSAndroid Build Coastguard Worker#   <TARGET_FILE(//some/label:here)> - expands to the
24*61c4878aSAndroid Build Coastguard Worker#       output file (such as a .a or .elf) from a GN target
25*61c4878aSAndroid Build Coastguard Worker#   <TARGET_FILE_IF_EXISTS(//some/label:here)> - expands to
26*61c4878aSAndroid Build Coastguard Worker#       the output file if the target exists, or nothing
27*61c4878aSAndroid Build Coastguard Worker#   <TARGET_OBJECTS(//some/label:here)> - expands to the
28*61c4878aSAndroid Build Coastguard Worker#       object files produced by the provided GN target
29*61c4878aSAndroid Build Coastguard Worker#
30*61c4878aSAndroid Build Coastguard Worker# This template may be useful, for example, to generate an artifact file
31*61c4878aSAndroid Build Coastguard Worker# containing a list of binary objects produced by targets within a build.
32*61c4878aSAndroid Build Coastguard Worker# Typically, this is used as a subtarget of a larger template, rather than a
33*61c4878aSAndroid Build Coastguard Worker# standalone target.
34*61c4878aSAndroid Build Coastguard Worker#
35*61c4878aSAndroid Build Coastguard Worker# Args:
36*61c4878aSAndroid Build Coastguard Worker#   files: List of scopes containing `source` and `dest` files. `source` is
37*61c4878aSAndroid Build Coastguard Worker#          scanned for expressions, and the result is written to `dest`.
38*61c4878aSAndroid Build Coastguard Worker#
39*61c4878aSAndroid Build Coastguard Worker# Example:
40*61c4878aSAndroid Build Coastguard Worker#
41*61c4878aSAndroid Build Coastguard Worker#   template("executable_with_artifacts") {
42*61c4878aSAndroid Build Coastguard Worker#     executable("${target_name}.exe") {
43*61c4878aSAndroid Build Coastguard Worker#       sources = invoker.sources
44*61c4878aSAndroid Build Coastguard Worker#     }
45*61c4878aSAndroid Build Coastguard Worker#
46*61c4878aSAndroid Build Coastguard Worker#     _artifacts_input = "$target_gen_dir/${target_name}_artifacts.json.in"
47*61c4878aSAndroid Build Coastguard Worker#     _artifacts_output = "$target_gen_dir/${target_name}_artifacts.json"
48*61c4878aSAndroid Build Coastguard Worker#     _artifacts = {
49*61c4878aSAndroid Build Coastguard Worker#       binary = "<TARGET_FILE(:${target_name}.exe)>"
50*61c4878aSAndroid Build Coastguard Worker#       objects = "<TARGET_OBJECTS(:${target_name}.exe)>"
51*61c4878aSAndroid Build Coastguard Worker#     }
52*61c4878aSAndroid Build Coastguard Worker#     write_file(_artifacts_input, _artifacts, "json")
53*61c4878aSAndroid Build Coastguard Worker#
54*61c4878aSAndroid Build Coastguard Worker#     pw_evaluate_path_expressions("${target_name}.evaluate") {
55*61c4878aSAndroid Build Coastguard Worker#       files = [
56*61c4878aSAndroid Build Coastguard Worker#         {
57*61c4878aSAndroid Build Coastguard Worker#           source = _artifacts_input
58*61c4878aSAndroid Build Coastguard Worker#           dest = _artifacts_output
59*61c4878aSAndroid Build Coastguard Worker#         },
60*61c4878aSAndroid Build Coastguard Worker#       ]
61*61c4878aSAndroid Build Coastguard Worker#     }
62*61c4878aSAndroid Build Coastguard Worker#
63*61c4878aSAndroid Build Coastguard Worker#     group(target_name) {
64*61c4878aSAndroid Build Coastguard Worker#       deps = [
65*61c4878aSAndroid Build Coastguard Worker#         ":${target_name}.exe",
66*61c4878aSAndroid Build Coastguard Worker#         ":${target_name}.evaluate",
67*61c4878aSAndroid Build Coastguard Worker#       ]
68*61c4878aSAndroid Build Coastguard Worker#     }
69*61c4878aSAndroid Build Coastguard Worker#   }
70*61c4878aSAndroid Build Coastguard Worker#
71*61c4878aSAndroid Build Coastguard Workertemplate("pw_evaluate_path_expressions") {
72*61c4878aSAndroid Build Coastguard Worker  assert(defined(invoker.files),
73*61c4878aSAndroid Build Coastguard Worker         "pw_evaluate_path_expressions requires input files to scan")
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard Worker  _script_args = [
76*61c4878aSAndroid Build Coastguard Worker    "--gn-root",
77*61c4878aSAndroid Build Coastguard Worker    rebase_path("//", root_build_dir),
78*61c4878aSAndroid Build Coastguard Worker    "--current-path",
79*61c4878aSAndroid Build Coastguard Worker    rebase_path(".", root_build_dir),
80*61c4878aSAndroid Build Coastguard Worker    "--default-toolchain",
81*61c4878aSAndroid Build Coastguard Worker    default_toolchain,
82*61c4878aSAndroid Build Coastguard Worker    "--current-toolchain",
83*61c4878aSAndroid Build Coastguard Worker    current_toolchain,
84*61c4878aSAndroid Build Coastguard Worker  ]
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard Worker  _inputs = []
87*61c4878aSAndroid Build Coastguard Worker  _outputs = []
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker  foreach(_file, invoker.files) {
90*61c4878aSAndroid Build Coastguard Worker    _inputs += [ _file.source ]
91*61c4878aSAndroid Build Coastguard Worker    _outputs += [ _file.dest ]
92*61c4878aSAndroid Build Coastguard Worker    _arg = rebase_path(_file.source) + ":" + rebase_path(_file.dest)
93*61c4878aSAndroid Build Coastguard Worker    _script_args += [ _arg ]
94*61c4878aSAndroid Build Coastguard Worker  }
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard Worker  pw_python_action(target_name) {
97*61c4878aSAndroid Build Coastguard Worker    script = "$dir_pw_build/py/pw_build/gn_resolver.py"
98*61c4878aSAndroid Build Coastguard Worker    inputs = _inputs
99*61c4878aSAndroid Build Coastguard Worker    outputs = _outputs
100*61c4878aSAndroid Build Coastguard Worker    args = _script_args
101*61c4878aSAndroid Build Coastguard Worker  }
102*61c4878aSAndroid Build Coastguard Worker}
103