#!/usr/bin/env bash # Runs one Cargo command with one synchronized HCIE build-number increment. set -euo pipefail SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd -- "${SCRIPT_DIR}/.." && pwd)" LOCK_DIR="${ROOT_DIR}/.build-id.lock" COUNTER_FILE="${ROOT_DIR}/build.id" cleanup() { rm -rf -- "${LOCK_DIR}" } acquire_lock() { while ! mkdir -- "${LOCK_DIR}" 2>/dev/null; do if [[ -f "${LOCK_DIR}/pid" ]]; then local owner owner="$(<"${LOCK_DIR}/pid")" if [[ "${owner}" =~ ^[0-9]+$ ]] && ! kill -0 "${owner}" 2>/dev/null; then rm -rf -- "${LOCK_DIR}" continue fi fi sleep 0.05 done printf '%s\n' "$$" > "${LOCK_DIR}/pid" trap cleanup EXIT INT TERM } write_counter() { local target="$1" local value="$2" local temporary="${target}.tmp.$$" printf '%s\n' "${value}" > "${temporary}" mv -f -- "${temporary}" "${target}" } if [[ $# -eq 0 ]]; then echo "usage: scripts/cargo-with-build-id.sh [arguments...]" >&2 exit 2 fi acquire_lock if [[ -f "${COUNTER_FILE}" ]]; then current="$(tr -d '[:space:]' < "${COUNTER_FILE}")" else current="0" fi if [[ ! "${current}" =~ ^[0-9]+$ ]]; then echo "build.id must contain one unsigned integer, found: ${current}" >&2 exit 1 fi if (( current >= 9223372036854775807 )); then echo "build.id reached the supported integer limit" >&2 exit 1 fi next=$((current + 1)) write_counter "${COUNTER_FILE}" "${next}" write_counter "${ROOT_DIR}/hcie-egui-app/build.id" "${next}" write_counter "${ROOT_DIR}/hcie-egui-app/crates/hcie-gui-egui/build.id" "${next}" export HCIE_BUILD_ID_OVERRIDE="${next}" echo "HCIE build ${next}: cargo $*" cd -- "${ROOT_DIR}" cargo "$@"