blob: f952160a0787212494485618b03a57cabda906b2 [file] [log] [blame]
# Copyright 2022 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/bazel/bazel_action.gni")
import("//build/packages/prebuilt_package.gni")
# Wraps a `fuchsia_package()` in a `BUILD.bazel` file.
# Use this template to make a `fuchsia_package()` target in a `BUILD.bazel`
# file be available in the GN build. Depending on the GN target will ensure
# that the corresponding Bazel target is built.
#
# Example:
# BUILD.bazel:
# fuchsia_package(
# name = "pkg",
# package_name = "example_package",
# ...
# )
# BUILD.gn:
# bazel_fuchsia_package("pkg") {
# package_name = "example_package"
# }
#
# Args:
# bazel_target: (optional)
# Bazel target to wrap.
# This target should be a `fuchsia_package()` target.
# Default: //<target_dir>:target_name
# For instance: `bazel_fuchsia_package("foo") {}` in `//bar/BUILD.gn`
# will wrap the target `//bar:foo` defined in `//bar/BUILD.bazel`.
#
# bazel_inputs: (optional)
# List of GN labels to bazel_input_xxx() targets required by the
# bazel_target.
# Type: list of strings (GN labels)
#
# package_name: (optional)
# The `package_name` of the wrapped `fuchsia_test_package`.
# Default: target_name
#
# deps:
# testonly:
# visibility:
# Usual GN meaning.
template("bazel_fuchsia_package") {
if (defined(invoker.bazel_target)) {
bazel_target = invoker.bazel_target
} else {
bazel_target = ":$target_name"
}
if (defined(invoker.package_name)) {
package_name = invoker.package_name
} else {
package_name = target_name
}
bazel_build_target = "$target_name.bazel_build"
bazel_action(bazel_build_target) {
forward_variables_from(invoker,
[
"bazel_inputs",
"deps",
"metadata",
"testonly",
"visibility",
])
command = "build"
output_groups = [ "+build_id_dir" ]
allow_directory_in_outputs = true
bazel_targets = [ bazel_target + "_fuchsia_package" ]
copy_outputs = [
{
bazel = "{{BAZEL_TARGET_OUT_DIR}}/$package_name.far"
ninja = "$package_name.far"
},
]
build_id_directories = [
{
bazel = "{{BAZEL_TARGET_OUT_DIR}}/{{BAZEL_TARGET_NAME}}_build_id_dir"
ninja = "${bazel_build_target}_build_id_dir"
},
]
}
# From the perspective of GN/Ninja, this package is a prebuilt
# even though we just built it ourselves.
prebuilt_package(target_name) {
forward_variables_from(invoker,
[
"deps",
"testonly",
"visibility",
])
package_name = package_name
archive = "$target_out_dir/$package_name.far"
if (!defined(deps)) {
deps = []
}
deps += [ ":$bazel_build_target" ]
}
}