blob: 1dfbb08aa4df3cc5a8b5e91fc89a8d19df607423 [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"
readonly OVERRIDE_SDK="${REPO_ROOT}/fuchsia_sdk.tar.gz"
readonly WORKSPACE_NAME="$(basename "$REPO_ROOT")"
readonly SDK_WORKSPACE_DIR="${REPO_ROOT}/bazel-${WORKSPACE_NAME}/external/fuchsia_sdk"
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 "${SDK_WORKSPACE_DIR}/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
echo "Copying SDK archive to ${OVERRIDE_SDK}..."
rsync --checksum "$SDK_ARCHIVE" "$OVERRIDE_SDK"
echo "Done copying SDK 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
if [[ -d "$OVERRIDE_SDK" ]]; then
echo "Removing old SDK in $OVERRIDE_SDK"
rm -f "$OVERRIDE_SDK"
fi
echo "Syncing SDK from local build..."
TMP_CORE_DIR="$(mktemp -d)"
TMP_DDK_DIR="$(mktemp -d)"
tar xzf "${SDK_OUT}/sdk/archive/core.tar.gz" -C "${TMP_CORE_DIR}"
tar xzf "${SDK_OUT}/sdk/archive/ddk.tar.gz" -C "${TMP_DDK_DIR}"
mv "${TMP_DDK_DIR}/meta/manifest.json" "${TMP_DDK_DIR}/meta/ddk_manifest.json"
rsync -aR "${PBM_FILES[@]}" "$TMP_CORE_DIR"
tar czvf "$OVERRIDE_SDK" -C "$TMP_CORE_DIR" . -C "$TMP_DDK_DIR" .
rm -Rf "$TMP_CORE_DIR" "$TMP_DDK_DIR"
echo "Done syncing SDK from local build."
}
syntax() {
echo " $(basename $0)"
echo " fetch from CIPD the SDK defined in WORKSPACE.bazel or use"
echo " ${OVERRIDE_SDK##REPO_ROOT/} if one is present."
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. See README.md on how to generate this 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. See README.md for more information."
}
main() {
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
# symlink to compile commands
ln -f -s "${REPO_ROOT}/bazel-bin/src/compile_commands.json" "${REPO_ROOT}/compile_commands.json"
}
main "$@"