| #!/bin/bash | |
| # Copyright 2025 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. | |
| #### CATEGORY=Build | |
| #### PREBUILTS=${PREBUILT_BAZEL} ${PREBUILT_JQ} ${PREBUILT_PYTHON3} | |
| ### Update the Bazel vendor directory to include all necessary Bazel deps. | |
| ## usage: fx update-bazel-vendor-dir [--no-build] | |
| ## | |
| ## Options: | |
| ## --no-build Do not build prerequisite targets before running `bazel vendor`. | |
| ## | |
| ## Versions of vendored repositories are based on the content of | |
| ## //build/bazel/toplevel.MODULE.bazel. If any of the direct or transitive | |
| ## dependencies there are updated to newer versions, make sure to rerun this | |
| ## script to update content of the Bazel vendor directory, otherwise the build | |
| ## will fail. | |
| ## | |
| ## NOTE: It's possible for transitive dependencies to change when local | |
| ## repositories (e.g. rules_fuchsia) update their dependencies, which will | |
| ## require a rerun of this script. | |
| ## | |
| ## NOTE: When new dependencies are introduced (either added directly with a | |
| ## bazel_dep in toplevel.MODULE.bazel, or picked up by a newer version of an | |
| ## existing dependency), you can add it to the Bazel vendor directory in two | |
| ## ways: | |
| ## | |
| ## 1. Add a `--repo='@@canonical_repo_name'` below, to download this | |
| ## dependency from upstream (Bazel Central Registry, BCR). Please follow | |
| ## the OSRB process [0] if it is the first time this repository is added | |
| ## to fuchsia.git. | |
| ## | |
| ## 2. Or, manually create a stub directory with necessary Bazel targets | |
| ## stubbed out at `//third_party/canonical_repo_name`, and add | |
| ## `--stub-repo='@@canonical_repo_name'` below. Contents of stub repos | |
| ## stay untouched by this script. | |
| ## | |
| ## [0] https://fuchsia.dev/fuchsia-src/contribute/governance/policy/osrb-process | |
| ## | |
| ## NOTE: Files or directories from vendored repositories that are not needed by | |
| ## the build (e.g. large prebuilt binaries) can be dropped by adding an | |
| ## `--exclude='<glob>'` below. The glob is relative to the Bazel vendor | |
| ## directory, and must match at least one path. | |
| ## | |
| # shellcheck source=/dev/null | |
| source "${FUCHSIA_DIR}/tools/devshell/lib/vars.sh" || exit $? | |
| fx-config-read | |
| do_build=true | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -h|--help) | |
| fx-command-help | |
| exit 0 | |
| ;; | |
| --no-build) | |
| do_build=false | |
| shift | |
| ;; | |
| *) | |
| fx-error "Unknown option: $1" | |
| fx-command-help | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| if [[ ! -d "${FUCHSIA_BUILD_DIR}" ]]; then | |
| fx-error "No Fuchsia build directory, please run 'fx set' or 'fx use'." | |
| fi | |
| # shellcheck source=/dev/null | |
| source "${FUCHSIA_DIR}/tools/devshell/lib/bazel_utils.sh" || exit $? | |
| if "${do_build}"; then | |
| fx-update-bazel-workspace | |
| fi | |
| # LINT.IfChange(bazel_vendor_dir) | |
| readonly BAZEL_VENDOR_DIR="${FUCHSIA_DIR}/third_party/bazel_vendor" | |
| # LINT.ThenChange(//build/bazel/enable_vendor_mode.bazelrc:bazel_vendor_dir) | |
| # LINT.IfChange(bazel_registry_dir) | |
| readonly BAZEL_REGISTRY_DIR="${FUCHSIA_DIR}/third_party/bazel_registries/bcr.bazel.build" | |
| # LINT.ThenChange(//build/bazel/enable_vendor_mode.bazelrc:bazel_registry_dir) | |
| # Excludes: | |
| # **/MODULE.bazel.lock: Bzlmod lockfiles inside vendored modules. Ignored by | |
| # Bazel for non-root modules. | |
| # rules_go+/tests: Upstream rules_go internal test suite (~3.1 MB). Unused by | |
| # the build. | |
| # rules_go+/third_party: Patch files (~7.3 MB, including a 6.1 MB patch | |
| # deleting gopls from org_golang_x_tools) used only by legacy WORKSPACE | |
| # go_rules_dependencies() for repositories that Fuchsia does not fetch. | |
| # rules_python+/gazelle/python/testdata: Upstream gazelle plugin test data | |
| # (~3.3 MB). Unused by the build. | |
| # rules_python+/tests: Upstream rules_python internal test suite (~2.3 MB). | |
| # Unused by the build. | |
| # rules_rust+/*/cargo-bazel{,.exe}: Prebuilt crate_universe generator binaries | |
| # (~185 MB) shipped in rules_rust release archives. Unused by the build. | |
| # rules_rust+/crate_universe/test_data: Upstream crate_universe test fixtures | |
| # (~12 MB). Unused by the build. | |
| # rules_rust+/crate_universe/tests: Upstream crate_universe test suite | |
| # (~4.2 MB). Unused by the build. | |
| # rules_rust+/test: Upstream rules_rust internal test suite (~4.3 MB). Unused | |
| # by the build. | |
| "${PREBUILT_PYTHON3}" "${FUCHSIA_DIR}"/build/bazel/scripts/update_bazel_vendor_dir.py \ | |
| --workspace="$(fx-get-bazel-workspace)" \ | |
| --bazel="$(fx-get-bazel)" \ | |
| --bazel-vendor-dir="${BAZEL_VENDOR_DIR}" \ | |
| --bazel-registry-dir="${BAZEL_REGISTRY_DIR}" \ | |
| --repo='@@bazel_features+' \ | |
| --repo='@@bazel_skylib+' \ | |
| --repo='@@package_metadata+' \ | |
| --repo='@@platforms' \ | |
| --repo='@@re2+' \ | |
| --repo='@@rules_cc+' \ | |
| --repo='@@rules_go+' \ | |
| --repo='@@rules_java+' \ | |
| --repo='@@rules_license+' \ | |
| --repo='@@rules_pkg+' \ | |
| --repo='@@rules_proto+' \ | |
| --repo='@@rules_python+' \ | |
| --repo='@@rules_rust+' \ | |
| --repo='@@rules_rust++i+rules_rust_tinyjson' \ | |
| --repo='@@rules_shell+' \ | |
| --repo='@@zlib+' \ | |
| --stub-repo='@@apple_support+' \ | |
| --stub-repo='@@bazel_tools+remote_coverage_tools_extension+remote_coverage_tools' \ | |
| --stub-repo='@@rules_kotlin+' \ | |
| --exclude='**/MODULE.bazel.lock' \ | |
| --exclude='rules_go+/tests' \ | |
| --exclude='rules_go+/third_party' \ | |
| --exclude='rules_python+/gazelle/python/testdata' \ | |
| --exclude='rules_python+/tests' \ | |
| --exclude='rules_rust+/*/cargo-bazel' \ | |
| --exclude='rules_rust+/*/cargo-bazel.exe' \ | |
| --exclude='rules_rust+/crate_universe/test_data' \ | |
| --exclude='rules_rust+/crate_universe/tests' \ | |
| --exclude='rules_rust+/test' |