blob: 8e020a875880a1928230424c46975bf5f94ab242 [file] [log] [blame]
#!/bin/bash
set -eufo pipefail
usage() {
cat <<EOS
Usage: $0 [-h]
This script prepares for a fidlbolt deployment. It:
1. Builds needed targets in $FUCHSIA_DIR.
This assumes you have fuchsia, and nested repositories like topaz, in the
state you want them. It will warn if there are uncommitted changes or if the
commit has not been merged on the remote.
2. Copies binaries and other files into ./deployment.
This uses the following directory structure:
./deployment/bin binaries (fidlc, fidl-format, etc.)
./deployment/etc configuration files
./deployment/fuchsia parts of the fuchsia tree that have .fidl files
It preserves directories for FIDL libraries so that paths displayed in
fidlbolt (e.g. import tooltips) resemble Fuchsia source tree paths.
3. Writes ./deployment/etc/fidlbolt_deployment.json.
This includes hashes and timestamps for all the relevant repositories, and
version strings for other tools like rustfmt.
After running the script, you can:
1. Run fidlbolt using the deployment: make run DEPLOYMENT=1
2. Build a Docker image: docker image build -t fidlbolt .
EOS
}
die() {
echo "$0: $*" >&2
exit 1
}
step() {
printf "\x1b[32;1m* %s\x1b[0m\n" "$*"
}
warn() {
printf "\x1b[31;1mWARNING:\x1b[0m %s\n" "$*" >&2
}
while getopts "h" opt; do
case $opt in
h) usage; exit 0 ;;
*) exit 1 ;;
esac
done
shift $((OPTIND - 1))
if [[ $# -gt 0 ]]; then
die "unexpected arguments: $*"
fi
if [[ -z "$FUCHSIA_DIR" ]]; then
die "FUCHSIA_DIR not set"
fi
# Source fx-env.sh to get the fx tool on the PATH.
# Source vars.sh for FUCHSIA_BUILD_DIR, ZIRCON_TOOLS_DIR, and PREBUILT_RUST_DIR.
# (Reset shell options before sourcing since vars.sh does not expect them.)
set +ufo pipefail
source "$FUCHSIA_DIR/scripts/fx-env.sh" || exit $?
source "$FUCHSIA_DIR/tools/devshell/lib/vars.sh" || exit $?
fx-config-read
set -ufo pipefail
step "Building targets in $FUCHSIA_DIR"
fx build host_x64/{fidlc,fidlgen_{llcpp,hlcpp,rust,go,dart}} zircon/tools
step "Cleaning ./deployment"
cd "$(dirname "$0")"
rm -rf deployment/
mkdir -p deployment/{bin,etc,fuchsia/{sdk/fidl,zircon/system/fidl}}
step "Copying binaries"
cp \
"$FUCHSIA_BUILD_DIR/host_x64/"{fidlc,fidlgen_{llcpp,hlcpp,rust,go,dart}} \
"$ZIRCON_TOOLS_DIR/fidl-"{format,lint} \
"$PREBUILT_RUST_DIR/bin/rustfmt" \
deployment/bin
step "Copying FIDL libraries"
find "$FUCHSIA_DIR/sdk/fidl" -mindepth 1 -maxdepth 1 -type d -print0 \
| xargs -0 -I% cp -r % deployment/fuchsia/sdk/fidl
find "$FUCHSIA_DIR/zircon/system/fidl" -mindepth 1 -maxdepth 1 -type d -print0 \
| xargs -0 -I% cp -r % deployment/fuchsia/zircon/system/fidl
cp -r "$FUCHSIA_DIR/zircon/vdso" deployment/fuchsia/zircon/vdso
# Remove the non-fidl files. This is easier and less error-prone than
# manipulating paths to call mkdir -p and only copying the .fidl files.
find deployment/fuchsia -type f -not -name '*.fidl' -delete
find deployment/fuchsia -type d -empty -delete
step "Copying etc files"
cp \
"$FUCHSIA_DIR/rustfmt.toml" \
deployment/etc
hash() {
# Check for uncommitted changes https://stackoverflow.com/a/3879077
git update-index --refresh > /dev/null 2>&1
if ! git diff-index --quiet HEAD --; then
warn "fidlbolt_deployment.json is inaccurate due to uncommitted changes"
warn "$(pwd): uncommitted changes:"
git status -s -uno >&2
fi
if [[ -z "$(git branch -r --contains HEAD)" ]]; then
warn "fidlbolt will have broken links due to unpublished commits"
warn "$(pwd): HEAD has not been published:"
git log HEAD -n1 --oneline >&2
fi
git rev-parse --verify HEAD
}
timestamp() {
git show -s --format=%ct HEAD
}
rustfmt_version=$(
"$PREBUILT_RUST_DIR/bin/rustfmt" --version \
| awk 'NR==1{sub(/-.*/, "", $2); print $2}'
)
step "Writing fidlbolt_deployment.json"
cat <<EOS > deployment/etc/fidlbolt_deployment.json
{
"fidlbolt": {
"hash": "$(hash)",
"timestamp": $(timestamp)
},
"fuchsia": {
"hash": "$(cd "$FUCHSIA_DIR" && hash)",
"timestamp": $(cd "$FUCHSIA_DIR" && timestamp)
},
"topaz": {
"hash": "$(cd "$FUCHSIA_DIR/topaz" && hash)",
"timestamp": $(cd "$FUCHSIA_DIR/topaz" && timestamp)
},
"rustfmt": {
"version": "$rustfmt_version"
}
}
EOS