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