blob: 9cb6d94192ca8e91834d5df79ae3933c601b7331 [file] [log] [blame]
#!/bin/bash
# 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.
set -e
# This script is used to fetch the prebuilt that is needed by driver build.
readonly REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/..
readonly BAZELISK_TOOL="${REPO_ROOT}/tools/bazel"
function get_os {
uname -s | tr '[:upper:]' '[:lower:]'
}
function get_arch {
local ARCH="$(uname -m)"
case "${ARCH}" in
x86_64)
ARCH=amd64
;;
aarch64)
ARCH=arm64
;;
esac
echo "$ARCH"
}
create_symlink() {
local TOOL
TOOL="$1"
SYMLINK="$2"
mkdir -p "$(dirname "${SYMLINK}")"
ln -f -s "${TOOL}" "${SYMLINK}"
}
create_bazel_symlink() {
local BAZELISK_BIN="${REPO_ROOT}/vendor/bazelisk/bazelisk-$(get_os)-$(get_arch)"
if [[ ! -x "${BAZELISK_BIN}" ]]; then
>&2 echo "Bazel wrapper not supported in this OS/architecture. Could not find executable: ${BAZELISK_BIN}"
exit 1
fi
create_symlink "$BAZELISK_BIN" "$BAZELISK_TOOL"
}
create_tools_symlinks() {
create_symlink "${REPO_ROOT}/bazel-iwlwifi/external/fuchsia_sdk/tools/x64/ffx" ${REPO_ROOT}/tools/ffx
}
# TODO: this is probably not needed when the DDK becomes part of the core SDK.
update_sdk_from_archive() {
SDK_ARCHIVE="$1"
if [[ ! -f "$SDK_ARCHIVE" ]]; then
echo "ERROR: sdk archive cannot be found: $SDK_ARCHIVE"
exit 1
fi
mkdir -p sdk
echo "Extracting SDK from archive..."
tar xzf "$SDK_ARCHIVE" -C sdk
echo "Done extracting SDK from archive."
}
# TODO: this is probably not needed when the DDK becomes part of the core SDK.
update_sdk_from_local_build() {
SDK_OUT="$1"
PBM_OUT="$2"
if [[ ! -d "$SDK_OUT" ]]; then
echo "ERROR: cannot find a directory in $SDK_OUT"
exit 1
fi
if [[ ! -d "$PBM_OUT" ]]; then
echo "ERROR: cannot find a directory in $PBM_OUT"
exit 1
fi
PBM_FILES=(
$PBM_OUT/./gen/build/images/virtual_device.json
$PBM_OUT/./gen/build/images/emulator_flags.json.template
$PBM_OUT/./gen/build/images/product_bundle.json
$PBM_OUT/./obj/build/images/fuchsia/fuchsia/fvm.blk
$PBM_OUT/./multiboot.bin
$PBM_OUT/./fuchsia.zbi
$SDK_OUT/./amber-files
)
SDK_ARCHIVE="${SDK_OUT}/sdk/archive/core.tar.gz"
DDK_ARCHIVE="${SDK_OUT}/sdk/archive/ddk.tar.gz"
if [[ ! -f "$SDK_ARCHIVE" ]]; then
echo "ERROR: cannot find the core SDK archive, make sure your 'fx set' has"
echo " '--args=build_sdk_archives=true' and that you build"
echo " 'fx build :default sdk sdk:ddk': $SDK_ARCHIVE"
exit 1
fi
if [[ ! -f "$DDK_ARCHIVE" ]]; then
echo "ERROR: cannot find the DDK archive, make sure your 'fx set' has"
echo " '--args=build_sdk_archives=true' and that you build"
echo " 'fx build :default sdk sdk:ddk': $DDK_ARCHIVE"
exit 1
fi
for f in "${PBM_FILES[@]}"; do
if [[ ! -e "$f" ]]; then
echo "ERROR: cannot find file needed to run the emulator. Make sure your"
echo " 'fx set' does NOT have '--args=build_sdk_archives=true'"
echo " and that you build with 'fx build :default sdk': $f"
exit 1
fi
done
LOCAL_SDK_OUT=./sdk
if [[ -d "$LOCAL_SDK_OUT" ]]; then
echo "Removing old SDK in $LOCAL_SDK_OUT"
rm -Rf "${LOCAL_SDK_OUT}"
fi
echo "Syncing SDK from local build..."
mkdir "${LOCAL_SDK_OUT}"
tar xzf "${SDK_OUT}/sdk/archive/core.tar.gz" -C "${LOCAL_SDK_OUT}"
tar xzf "${SDK_OUT}/sdk/archive/ddk.tar.gz" -C "${LOCAL_SDK_OUT}" --transform='s/meta\/manifest.json/meta\/ddk_manifest.json/'
rsync -aR "${PBM_FILES[@]}" "${LOCAL_SDK_OUT}"
}
generate_rules_fuchsia() {
echo "Generating Fuchsia Bazel rules..."
if [[ ! -d "${REPO_ROOT}/sdk" ]]; then
echo "Warning! Cannot find directory 'sdk', please use argument --sdk-archive or --sdk-local and --product-bundle-local"
echo "Bazel rules will not be generated for the Fuchsia SDK. Please run bootstrap.sh again with the proper argument."
return
# TODO(mangini) exit on error here. Disabling error for now to avoid
# triggering CI/CQ, since we cannot run this in CI/CQ until the DDK is
# published in an accessible place.
# exit 1
fi
pushd "${REPO_ROOT}/sdk-integration/bazel_rules_generator/" > /dev/null || exit
./generate.py \
--directory "${REPO_ROOT}/sdk" \
--output "${REPO_ROOT}/bazel-rules"
popd > /dev/null || exit
echo "Done generating Fuchsia Bazel rules."
}
syntax() {
echo " $(basename $0)"
echo " assume that there is a directory 'sdk' with an SDK, DDK and"
echo " product bundle files for the emulator"
echo
echo " $(basename $0) --sdk-archive SDK_TARGZ"
echo " load an SDK and the emulator product bundle files from the"
echo " given tar.gz archive."
echo
echo " $(basename $0) --sdk-local FUCHSIA_OUT_SDK --product-bundle-local FUCHSIA_OUT_PRODUCT_BUNDLE"
echo " copy the SDK from a build directory of a local Fuchsia tree,"
echo " and copy the product bundle files for the emulator from another"
echo " build directory."
}
update_git_submodules() {
echo "Updating git submodules"
git submodule update --init --recursive
}
main() {
update_git_submodules
if [[ $# -gt 0 ]]; then
if [[ "$1" == "--sdk-archive" && $# -eq 2 ]]; then
update_sdk_from_archive "$2"
elif [[ $# -eq 4 && "$1" == "--sdk-local" && "$3" == "--product-bundle-local" ]]; then
update_sdk_from_local_build $2 $4
elif [[ $# -eq 4 && "$3" == "--sdk-local" && "$1" == "--product-bundle-local" ]]; then
update_sdk_from_local_build $4 $2
else
echo "Invalid syntax."
syntax
exit 1
fi
fi
create_bazel_symlink
create_tools_symlinks
generate_rules_fuchsia
}
main "$@"