xref: /aosp_15_r20/external/pigweed/pw_build/relative_source_file_names.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 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("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/python_action.gni")
18
19# This isn't in a declare_args() block as it likely isn't necessary to change
20# this.
21pw_build_RELATIVE_PATH_TRANSFORM_JSON =
22    "$root_build_dir/relative_path_transformations.json"
23
24# Creates a JSON file containing an array of source file names with
25# -ffile-prefix-map style transformations applied to match __FILE__ as seen in
26# C/C++ sources when using pw_build's `relative_paths` config.
27#
28# Args:
29#    deps (required): A list of targets to recursively extract file names from.
30#    outputs (required): An array with a single element: the path to write the
31#        transformed file paths to. The output format is a JSON array of
32#        strings.
33template("pw_relative_source_file_names") {
34  _raw_file_names_json = "$target_gen_dir/${target_name}.raw.json"
35
36  # The various pw_* templates add pw_source_files metadata which we
37  # aggregate here.
38  generated_file("${target_name}.raw_source_files") {
39    forward_variables_from(invoker, [ "deps" ])
40
41    # Rebase the paths so that they match those that are passed to the
42    # compiler.
43    rebase = root_build_dir
44    outputs = [ _raw_file_names_json ]
45    data_keys = [ "pw_source_files" ]
46    output_conversion = "json"
47  }
48
49  pw_python_action(target_name) {
50    deps = [ ":${target_name}.raw_source_files" ]
51    python_deps = [ "$dir_pw_build/py" ]
52    module = "pw_build.file_prefix_map"
53
54    # GN-ism: You can't do invoker.outputs[0], but _outs[0] works.
55    _outs = invoker.outputs
56    args = [
57      rebase_path(_raw_file_names_json, root_build_dir),
58      "--prefix-map-json",
59      rebase_path(pw_build_RELATIVE_PATH_TRANSFORM_JSON, root_build_dir),
60      "--out",
61      rebase_path(_outs[0], root_build_dir),
62    ]
63
64    inputs = [ _raw_file_names_json ]
65    forward_variables_from(invoker, [ "outputs" ])
66  }
67}
68