blob: 685ff96398fb7e54cfa3a5ce265bdef465532b72 [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/assembly/sshd_config.gni")
import("//build/bazel/bazel_inputs.gni")
import("//build/bazel/bazel_workspace.gni")
import("//build/bazel/legacy_ninja_build_outputs.gni")
import("//build/images/vbmeta.gni")
import("//build/info/info.gni")
import("//build/product.gni")
import("//build/toolchain/rbe.gni")
import("//src/connectivity/policy/netcfg/delegated_network_provisioning.gni")
assert(
current_toolchain == default_toolchain,
"This BUILD.gn file should only be parsed in the default toolchain, to prevent generating Bazel workspace multiple times.")
# A GN target used to generate at GN-gen time a bazel inputs manifest file
# under the following path which is hard-coded in toplevel.WORKSPACE.bazel:
#
# $BAZEL_TOPDIR/legacy_ninja_build_outputs.inputs_manifest.json
#
# See bazel_inputs.gni comments for details on the manifest file format,
# and repository_rules.bzl:bazel_inputs_repository() to see how it is parsed
# to generate an external repository for the Bazel project.
#
# It is possible to build this target to ensure that all Ninja outputs seen
# by the Bazel workspace are properly generated, as in:
#
# fx build build/bazel:legacy_ninja_build_outputs
#
# IMPORTANT: Nothing in the GN graph should depend on this directly, except
# the top-level //:root_targets group in //BUILD.gn, to ensure
# that its output is generated at gn-gen time.
#
# Doing so allows breaking the testonly boundary, as required to allow
# non-testonly bazel_action() targets to depend on
# :generate_main_workspace below, without depending on this target.
#
generate_bazel_inputs_manifest("legacy_ninja_build_outputs") {
testonly = true
output = bazel_inputs_manifest
# IMPORTANT: All bazel_input_xxx() targets must be reachable from this
# dependencies list!
inputs_deps = gn_labels_for_bazel_inputs + extra_gn_labels_for_bazel_inputs
}
action("generate_main_workspace") {
script = "//build/bazel/scripts/update_workspace.py"
# The update script tracks the file and directory entries of FUCHSIA_DIR
# and will update the workspace if they change (i.e. if a file or
# directory is added to / removed from FUCHSIA_DIR itself). However,
# there is no way for Ninja to track these changes, and thus no way
# to enforce the action to be run when this happens, even though these
# are rare occurences, that could happen in the following cases:
#
# - A developer manually adds or removes a file to/from FUCHSIA_DIR.
# In this case, manually calling the script is needed, and this
# can be done by invoking any `fx bazel ...` command (even
# `fx bazel version`) or `fx build bazel_workspace`.
#
# - A `jiri update` modifies the content of FUCHSIA_DIR. Depend on
# the top-level //.git/index file to detect this. Note that the
# script will not necessarily update the workspace in this case,
# this simply ensures that it is run to do its checks.
#
# - A manual `git checkout HEAD^ -- .` command will modify the local
# workspace without modifying the index, so also depend on
# //.git/HEAD to detect this.
#
inputs = [
"//.git/index",
"//.git/HEAD",
# The bazel prebuilt is an implicit input for the script.
"//prebuilt/third_party/bazel/${host_os}-${host_cpu}/bazel",
# The script expands these templates
"//build/bazel/templates/template.bazel.sh",
"//build/bazel/templates/template.bazelrc",
"//build/bazel/templates/template.remote_services.bazelrc",
"//build/bazel/templates/template.platform_mappings",
# The script reads this configuration file
"//build/bazel/config/main_workspace_top_dir",
# The script imports these python files
"//build/bazel/scripts/check_ninja_build_plan.py",
"//build/bazel/scripts/compute_content_hash.py",
# The script reads RBE configs
rewrapper_config_file,
reproxy_config_file,
]
# This dependency is needed to properly populate the @fuchsia_sdk repository.
deps = [ "//build/bazel/bazel_sdk:idk" ]
# This dependency is needed to properly populate the @internal_sdk repository
# used experimentally to migrate non-SDK GN targets to Bazel
# (see https://fxbug.dev/42063353).
deps += [ "//sdk:platform" ]
depfile = "${target_gen_dir}/${target_name}.d"
outputs = [
# LINT.IfChange
"${bazel_main_top_dir}/bazel",
"${bazel_main_top_dir}/download_config_file",
"${bazel_main_top_dir}/generated-info.json",
"${bazel_main_top_dir}/workspace/.bazelrc",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/args.json",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/git",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/jiri_snapshot.xml",
# The list of content hash files
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/bazel_rules_fuchsia.hash",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/boringssl.hash",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/com_google_googletest.hash",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/fuchsia_clang.hash",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/fuchsia_sdk.hash",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/internal_sdk.hash",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/prebuilt_clang.hash",
"${bazel_main_top_dir}/workspace/fuchsia_build_generated/prebuilt_python.hash",
# Other files.
"${bazel_main_top_dir}/workspace/platform_mappings",
"${bazel_main_top_dir}/workspace/WORKSPACE.bazel",
# LINT.ThenChange(scripts/update_workspace.py)
]
args = [
"--gn_output_dir",
rebase_path(root_build_dir, root_build_dir),
"--target_arch",
target_cpu,
"--depfile",
rebase_path(depfile, root_build_dir),
]
# The generated wrapper includes the absolute path of the Ninja output
# directory, which will be read by Bazel repository rules during workspace
# setup.
no_output_dir_leaks = false
}
# Generate a symlink to the @fuchsia_sdk repository, after ensuring it was
# properly populated by Bazel. The symlink has a stable path of
# $BAZEL_TOPDIR/fuchsia_sdk while the actual location of the repository
# depends on Bazel internals (e.g. whether BzlMod is enabled or not).
action("generate_fuchsia_sdk_repository") {
script = "//build/bazel/scripts/generate-fuchsia-sdk-symlink.py"
outputs = [ "$bazel_main_top_dir/fuchsia_sdk" ]
deps = [ ":generate_main_workspace" ]
inputs = get_target_outputs(deps[0])
args = [
"--bazel-launcher",
rebase_path(inputs[0], root_build_dir),
"--output-symlink",
rebase_path(outputs[0], root_build_dir),
]
metadata = {
# Used by the //:bazel_sdk_info build API module target.
bazel_sdk_info = [
{
location = rebase_path(outputs[0], root_build_dir)
},
]
}
# This script invokes Bazel directly. Use console pool to prevent
# concurrent bazel invocations.
pool = "//:console($default_toolchain)"
# This cannot be hermetic as well since it creates a ton of
# files in the output base that cannot be listed in advance.
hermetic_deps = false
}
# Generate a build_args.bzl file that contains Bazel-specific constant
# declarations that match the current build configuration found in args.gn.
#
# NOTE: This is deprecated, use @fuchsia_build_info//:args.bzl to get the
# same constants now. This GN targets will be removed once the last
# //vendor/... references to it have been removed.
#
# Note that another way to pass configuration information is to add
# specific lines to the project's auto-generated .bazelrc file, or even
# to set specific options in the Bazel launcher script.
#
_build_args_bzl_output = "${target_out_dir}/build_args.bzl"
_build_args_bzl_target = "build_args.bzl.generated"
generated_file(_build_args_bzl_target) {
output_conversion = "list lines"
contents = [ "# Auto-generated - DO NOT EDIT" ]
# Export use_vbmeta boolean flag.
# Boolean values are capitalized differently in GN and Bazel ...
if (use_vbmeta) {
contents += [ "use_vbmeta = True" ]
} else {
contents += [ "use_vbmeta = False" ]
}
# Pass the authorized_ssh_keys_path GN arg to bazel, if it was set in args.gn
#
# This can't be directly specified within Bazel rules as it's really meant to
# be a configuration argument for the build.
if (authorized_ssh_keys_label != false) {
contents +=
[ "authorized_ssh_keys_label = \"${authorized_ssh_keys_label}\"" ]
} else {
contents += [ "authorized_ssh_keys_label = None" ]
}
# Pass the delegated_network_provisioning GN arg to bazel.
if (delegated_network_provisioning) {
contents += [ "delegated_network_provisioning = True" ]
} else {
contents += [ "delegated_network_provisioning = False" ]
}
# Pass the product name to Bazel.
contents += [ "build_info_product = \"${build_info_product}\"" ]
outputs = [ _build_args_bzl_output ]
}
bazel_input_resource("build_args_bazel_inputs") {
sources = [ _build_args_bzl_output ]
outputs = [ "build_args.bzl" ]
deps = [ ":${_build_args_bzl_target}" ]
}
group("tests") {
testonly = true
deps = [
"bazel_sdk:tests",
"scripts:tests",
"tests",
]
}