blob: 439c51e2d146df8b460985e32d83a3235f03312b [file] [log] [blame]
# Copyright 2019 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.
# Declare a data file to be accessible at runtime on the target device.
#
# A resource() target looks just like a copy() target but $outputs are
# relative target paths. Using $data_deps to this resource() target in
# each target whose code uses $outputs at runtime ensures that the files
# will be present on the system.
#
# If the file is not in the source tree, it should be generated by another
# target in the build listed in $deps. If that would be a generated_file()
# target, then use generated_resource() instead of resource().
#
# Parameters
#
# outputs
# Required: List of one runtime path. This must be a relative path (no
# leading `/`). It can use placeholders based on $sources; see copy()
# and `gn help source_expansion`. When this resource() target is in
# the dependency graph of a zbi() target, then this is the path within
# the BOOTFS, which appears at /boot in the namespace of early-boot and
# standalone Zircon processes. When it's instead in the dependency graph
# of a package, then this is the path within the package's file namespace.
# TODO(mcgrathr): Clarify package case when actually implemented.
# Type: list(path)
#
# sources
# Required: List of files in the source tree or build that become $outputs.
# See copy() for details.
# Type: list(file)
#
# See copy() for other parameters.
#
template("resource") {
group(target_name) {
forward_variables_from(invoker,
"*",
[
"metadata",
"outputs",
"sources",
])
metadata = {
manifest_lines = []
if (defined(invoker.metadata)) {
forward_variables_from(invoker.metadata, "*")
}
foreach(source, invoker.sources) {
foreach(target, process_file_template([ source ], invoker.outputs)) {
assert(rebase_path(target, "foo") != target,
"`outputs` in resource() cannot start with /")
manifest_lines +=
[ target + "=" + rebase_path(source, root_build_dir) ]
}
}
}
}
}
# Looks just like a generated_file() target but $outputs is like resource().
#
# A generated_resource() target is like a resource() target whose $sources
# matches the $outputs of a generated_file() target, but rolled into one.
# Using $data_deps to this generated_resource() target in each target whose
# code uses $outputs at runtime ensures that the files will be present on
# the system. The files can have fixed contents given directly in the
# target or be generated from metadata collection, as in generated_file().
#
# Parameters
#
# outputs
# Required: See resource().
#
# See generated_file() for other parameters.
#
template("generated_resource") {
generated_file(target_name) {
forward_variables_from(invoker, "*", [ "metadata" ])
assert(outputs == [ outputs[0] ],
"generated_resource() requires a single element in `outputs")
# Select a place to generate the contents at `gn gen` time.
file = "$target_gen_dir/$target_name"
if (defined(output_conversion) && output_conversion == "json") {
file += ".json"
} else {
file += ".txt"
}
# Add metadata to add that file to a filesystem image.
metadata = {
manifest_lines = []
if (defined(invoker.metadata)) {
forward_variables_from(invoker.metadata, "*")
}
manifest_lines += [ "${outputs[0]}=" + rebase_path(file, root_build_dir) ]
}
# That static file is the actual output of this target.
outputs = []
outputs = [
file,
]
}
}