blob: 3061cf6b9ca5ac8c4cddfb050a3b753e6fde531b [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
### 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
local use_dir_list=false
if [[ "${#directories[@]}" -eq 0 ]]; then
fx-gen # To make sure `BAZEL2GN_DIR_LIST` is up-to-date.
use_dir_list=true
fi
# This Bazel build needs to run after `fx-gen`, which can clear out the symlinks in bazel-bin.
if [[ "${build}" == "true" ]]; then
fx-run-bazel false "$(fx-get-bazel)" build --config=host "${BAZEL2GN_TARGET}"
fi
if [[ "${use_dir_list}" == "true" ]]; then
"${BAZEL2GN_BIN}" \
--directory_list "${BAZEL2GN_DIR_LIST}" \
--fuchsia_dir "${FUCHSIA_DIR}" \
--gn_bin "${PREBUILT_GN}" \
--buildifier_bin "${PREBUILT_BUILDIFIER}"
else
"${BAZEL2GN_BIN}" \
--gn_bin "${PREBUILT_GN}" \
--buildifier_bin "${PREBUILT_BUILDIFIER}" \
"${directories[@]}"
fi
}
main "$@"