blob: 482b424824d5f1925f09374364d6ef8734a56b02 [file] [edit]
#!/bin/bash
# Copyright 2026 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=Other
### query the fuchsia codebase to support IDE features
## usage: fx ide-query [--dev] [args...]
##
## --dev Always recompile ide-query from source.
# shellcheck source=/dev/null
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
function main {
# Parse standard switches like --help.
fx-standard-switches "$@"
# The design calls for a --dev flag that forces a recompilation.
local dev=false
# Filter out --dev from the arguments passed to the final binary.
local filtered_args=()
for arg in "$@"; do
if [[ "$arg" == "--dev" ]]; then
dev=true
else
filtered_args+=("$arg")
fi
done
# Path to the cached executable and the revision it was built at.
readonly exe_path="${FX_CACHE_DIR}/ide-query.bin"
readonly revision_file="${FX_CACHE_DIR}/ide-query.revision"
local current_revision
current_revision="$(git --no-optional-locks -C "$FUCHSIA_DIR" rev-parse HEAD)"
# Rebuild if requested, or if the binary is missing or at the wrong revision.
local should_rebuild=true
if ! $dev && [[ -f "$exe_path" && -f "$revision_file" ]]; then
local stored_revision
stored_revision="$(cat "$revision_file")"
if [[ "$stored_revision" == "$current_revision" ]]; then
should_rebuild=false
fi
fi
if $should_rebuild; then
fx-info "Building ide-query..."
local BUILD_DIR
BUILD_DIR=$(mktemp -d)
trap "rm -rf '$BUILD_DIR'" EXIT
# Mirror the build setup used by other core fx tools.
pushd "$BUILD_DIR" >/dev/null
for target in go.{mod,sum} vendor; do
ln -s "$FUCHSIA_DIR"/third_party/golibs/"${target}" .
done
# ide-query depends on source files in scripts/ and potentially tools/.
ln -s "$FUCHSIA_DIR"/tools .
ln -s "$FUCHSIA_DIR"/scripts .
# Build the binary using the prebuilt Go toolchain.
# - GOPROXY=off enforces that we only use vendored dependencies.
# - GO111MODULE=on ensures module mode for consistency.
# - CGO_ENABLED=0 produces a statically linked binary.
if ! CGO_ENABLED=0 GOPROXY=off GO111MODULE=on GOCACHE="${FX_CACHE_DIR}/gocache" \
fx-command-run go build -buildvcs=false -o "$exe_path" ./scripts/cog/ide_query; then
fx-error "Failed to build ide-query."
return 1
fi
popd >/dev/null
# Store the revision so we can do a lazy build next time.
echo "$current_revision" > "$revision_file"
fi
# Execute the binary with any remaining arguments.
"$exe_path" --fuchsia-dir "$FUCHSIA_DIR" "${filtered_args[@]}"
}
main "$@"