blob: 09a04826bc61fd664d437b18cbc4ae9e7be840e2 [file]
# Copyright 2026 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/group_with_inputs.gni")
# Generate the //build/ninja_implicit_inputs:manifest file
#
# See comments in //build/ninja_implicit_inputs/BUILD.gn
# for documentation and the schema of the generated files.
#
# This template only produces generated_file() targets, so
# their outputs will be available just after `gn gen`, and
# before building anything.
#
# Arguments:
# root_labels: A list of GN labels, each one will generate
# a sub-manifest containing collected GN metadata for the
# root target. The final manifest will point to each
# target-specific sub-manifest.
#
template("generate_ninja_implicit_inputs_manifest") {
_manifest_target = target_name
# For each root label, generate a sub-manifest and record its target name.
_submanifest_index = 1
_submanifest_target_list = []
_manifest_content = []
_manifest_deps = []
foreach(_root_label, invoker.root_labels) {
_submanifest_target = "${_manifest_target}.${_submanifest_index}"
_submanifest_index += 1
_submanifest_target_list += [ _submanifest_target ]
_submanifest_path = "$target_gen_dir/${_submanifest_target}.json"
_manifest_content += [
{
gn_label = _root_label
manifest_path = rebase_path(_submanifest_path, root_build_dir)
},
]
_manifest_deps += [ ":${_submanifest_target}" ]
generated_file(_submanifest_target) {
outputs = [ _submanifest_path ]
# For now use different metadata keys for different types of
# implicit inputs, even if they all produce the same types of
# entries. This is for debugging the GN graph and should not
# impact `gn gen` time or the generated manifest's content.
data_keys = [
"ninja_implicit_cxx_headers",
"ninja_implicit_directory_inputs",
"ninja_implicit_file_inputs",
]
walk_keys = [
"ninja_implicit_cxx_headers_barrier",
"ninja_implicit_directory_inputs_barrier",
"ninja_implicit_file_inputs_barrier",
]
deps = [ _root_label ]
output_conversion = "json"
testonly = true
}
}
# Then generate the top-level manifest.
generated_file(_manifest_target) {
contents = _manifest_content
deps = _manifest_deps
outputs = [ "$target_gen_dir/${target_name}.json" ]
output_conversion = "json"
testonly = true
}
}
# A target that generates GN metadata describing a set of source files
# as possible inputs.
#
# This template should only be used in rare cases where it is not possible
# to compute an accurate set of source paths in the 'inputs' or 'sources'
# arguments of a parent target.
#
# Over-specifying the input files is acceptable, but will result in
# Bazel invoked un-necessary Ninja build actions.
#
# Arguments:
# files: A list of GN paths to source files. All files must exist.
#
template("ninja_implicit_file_inputs") {
group_with_inputs(target_name) {
inputs = invoker.files
deps = []
metadata = {
ninja_implicit_file_inputs = [
{
gn_label = get_label_info(":$target_name", "label_with_toolchain")
files = rebase_path(invoker.files, root_build_dir)
},
]
}
}
}
# A target that generates GN metadata describing a set of directories whose
# content may be used as possible input by a given target.
#
# This template should only be used in rare cases.
#
# This is only intended for directories whose content doesn't change during
# development, such as prebuilt toolchains or sysroots. Due to Ninja and Bazel
# limitations, any change to their content may trigger a `gn gen` operation
# on the next incremental build.
#
# Arguments:
# directories: A list of GN paths to source directories to be
# tracked as inputs for its dependents. All directories must
# exist.
#
template("ninja_implicit_directory_inputs") {
group(target_name) {
metadata = {
ninja_implicit_directory_inputs = [
{
gn_label = get_label_info(":$target_name", "label_with_toolchain")
directories = rebase_path(invoker.directories, root_build_dir)
},
]
}
}
}