blob: 94b27cf5a4167dbd1cfa100c0cdf3d24e0f689cc [file]
#!/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
#### PREBUILTS=${PREBUILT_BAZEL} ${PREBUILT_BUILDIFIER} ${PREBUILT_GN} ${PREBUILT_JQ} ${PREBUILT_PYTHON3}
### 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 files from the BUILD.bazel files in their directories. Without
## -d, syncs every directory whose BUILD.gn contains the bazel2gn sentinel.
set -eo pipefail
_script_dir="${BASH_SOURCE[0]%/*}";
if [[ "${_script_dir}" == "${BASH_SOURCE[0]}" ]]; then _script_dir="."; fi
readonly _script_dir
# shellcheck source=/dev/null
source "${_script_dir}/../lib/vars.sh" || exit $?
fx-config-read
# shellcheck source=/dev/null
source "${_script_dir}/../lib/bazel_utils.sh" || exit $?
declare -r BAZEL2GN_TARGET="//build/tools/bazel2gn"
BAZEL2GN_BIN="$(fx-get-bazel-workspace)/bazel-bin/build/tools/bazel2gn/bazel2gn_/bazel2gn"
readonly BAZEL2GN_BIN
dir_list_file=""
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
# Without explicit directories, bazel2gn discovers all directories whose
# BUILD.gn contains the bazel2gn sentinel.
local -a dir_list_args=()
if [[ "${#directories[@]}" -gt 0 ]]; then
# Write the directories to a temporary file.
dir_list_file="$(mktemp /tmp/bazel2gn-dirlist-XXXXXX)"
trap 'rm -f "${dir_list_file}"' EXIT
for directory in "${directories[@]}"; do
printf "%s\n" "${directory}" >> "${dir_list_file}"
done
dir_list_args=(--directory_list "${dir_list_file}")
fi
if [[ "${build}" == "true" ]]; then
fx-run-bazel false "$(fx-get-bazel)" build --config=host "${BAZEL2GN_TARGET}"
fi
"${BAZEL2GN_BIN}" \
"${dir_list_args[@]}" \
--fuchsia_dir "${FUCHSIA_DIR}" \
--gn_bin "${PREBUILT_GN}" \
--buildifier_bin "${PREBUILT_BUILDIFIER}"
}
main "$@"