| #!/bin/bash |
| # Copyright 2024 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=Source tree |
| ### Syncs BUILD.gn and BUILD.bazel for targets that are dual-building in both. |
| |
| ## usage: fx bazel2gn [-d|--directory <path/to/dir>]... [-h|--help] [--no-build] |
| ## |
| ## -h|--help Print out this message. |
| ## -d|--directory Directory to sync, repeatable. |
| ## --no-build Skip building required dependencies. |
| ## |
| ## Updates BUILD.gn listed in this script based on the BUILD.bazel files in |
| ## their directories. |
| |
| set -eo pipefail |
| |
| _script_dir="${BASH_SOURCE[0]%/*}"; |
| if [[ "${_script_dir}" == "${BASH_SOURCE[0]}" ]]; then _script_dir="."; fi |
| readonly _script_dir |
| |
| source "${_script_dir}/../lib/vars.sh" || exit $? |
| fx-config-read |
| |
| source "${_script_dir}/../lib/bazel_utils.sh" || exit $? |
| |
| declare -r BAZEL2GN_TARGET="//build/tools/bazel2gn" |
| declare -r BAZEL2GN_BIN="$(fx-get-bazel-workspace)/bazel-bin/build/tools/bazel2gn/bazel2gn_/bazel2gn" |
| declare -r BAZEL2GN_DIR_LIST="${FUCHSIA_BUILD_DIR}/bazel2gn_dir_list" |
| |
| function main { |
| local build=true |
| local directories=() |
| while [[ "${#}" -ge 1 ]]; do |
| case "${1}" in |
| -d|--directory) |
| shift |
| directories+=("${1}") |
| ;; |
| -h|--help) |
| fx-command-help |
| exit 0 |
| ;; |
| --no-build) |
| build=false |
| ;; |
| *) |
| fx-error "Unexpected command line arg ${1}" |
| fx-command-help |
| exit 1 |
| esac |
| shift |
| done |
| |
| # If no directories are provided by user, sync all known directories. |
| # Read known directories to sync from $BAZEL2GN_DIR_LIST. |
| if [[ -z "${directories}" ]]; then |
| while IFS= read -r dir; do |
| directories+=("${dir}") |
| done < "${BAZEL2GN_DIR_LIST}" |
| fi |
| |
| if [[ "${build}" == "true" ]]; then |
| fx-run-bazel false "$(fx-get-bazel)" build --config=host "${BAZEL2GN_TARGET}" |
| fi |
| |
| for dir in "${directories[@]}"; do |
| build_bazel="${FUCHSIA_DIR}/${dir}/BUILD.bazel" |
| build_gn="${FUCHSIA_DIR}/${dir}/BUILD.gn" |
| |
| # Format the input BUILD.bazel file first to ensure consistent output. |
| # |
| # For example, buildifier can reorder attributes of targets in BUILD.bazel, |
| # resulting in different BUILD.gn outputs since `gn format` doesn't care |
| # about field order. This can cause confusing failures if users run |
| # `fx format-code` after `fx bazel2gn`. |
| "${PREBUILT_BUILDIFIER}" -mode=fix "${build_bazel}" |
| "${BAZEL2GN_BIN}" \ |
| --bazel_input_path "${build_bazel}" \ |
| --gn_output_path "${build_gn}" \ |
| --gn_bin "${PREBUILT_GN}" |
| done |
| } |
| |
| main "$@" |