#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" DIST_DIR="${DIST_DIR:-"$ROOT_DIR/dist/field"}" TARGETS="${TARGETS:-}" APPS="${APPS:-"edge node"}" CGO_ENABLED="${CGO_ENABLED:-0}" GOFLAGS="${GOFLAGS:-"-trimpath"}" if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then cat <<'USAGE' Build field-deployable IOP edge/node binaries. Usage: bin/build/field-binaries.sh Environment: APPS="edge node" apps to build TARGETS="linux/amd64 linux/arm64" GOOS/GOARCH target list DIST_DIR=dist/field output directory VERSION= release version label COMMIT= commit label BUILD_DATE= build date label ARTIFACT_BASE_URL= default bootstrap artifact base URL BOOTSTRAP_EDGE_ADDR= default Edge address written to node.yaml BOOTSTRAP_NODE_TOKEN= optional fallback token baked into bootstrap script CGO_ENABLED=0 Go CGO setting GOFLAGS="-trimpath" extra go build flags LDFLAGS="-s -w" linker flags USAGE exit 0 fi VERSION="${VERSION:-"$(git -C "$ROOT_DIR" describe --tags --always --dirty 2>/dev/null || echo dev)"}" COMMIT="${COMMIT:-"$(git -C "$ROOT_DIR" rev-parse --short=12 HEAD 2>/dev/null || echo unknown)"}" BUILD_DATE="${BUILD_DATE:-"$(date -u +%Y%m%dT%H%M%SZ)"}" LDFLAGS="${LDFLAGS:-"-s -w"}" if [[ -z "$TARGETS" ]]; then TARGETS="$(go env GOOS)/$(go env GOARCH)" fi checksum() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$@" return fi shasum -a 256 "$@" } app_enabled() { local wanted="$1" local app for app in $APPS; do if [[ "$app" == "$wanted" ]]; then return 0 fi done return 1 } build_one() { local app="$1" local goos="$2" local goarch="$3" local output_dir="$DIST_DIR/$VERSION/$goos-$goarch" local output="$output_dir/iop-$app" mkdir -p "$output_dir" echo "building $app for $goos/$goarch -> $output" ( cd "$ROOT_DIR" GOOS="$goos" GOARCH="$goarch" CGO_ENABLED="$CGO_ENABLED" \ go build $GOFLAGS -ldflags "$LDFLAGS" -o "$output" "./apps/$app/cmd/$app" ) ( cd "$output_dir" checksum "iop-$app" > "iop-$app.sha256" ) } write_node_bootstrap() { local goos="$1" local goarch="$2" local target_dir="$goos-$goarch" local bootstrap_dir="$DIST_DIR/$VERSION/bootstrap" local script="$bootstrap_dir/node-$target_dir.sh" local default_base_url="${ARTIFACT_BASE_URL:-"http://toki-labs.com:18080"}" local default_edge_addr="${BOOTSTRAP_EDGE_ADDR:-"toki-labs.com:19090"}" local default_node_token="${BOOTSTRAP_NODE_TOKEN:-}" mkdir -p "$bootstrap_dir" cat > "$script" <&2 echo "[iop-bootstrap] usage: curl -fsSL /bootstrap/node-\$TARGET.sh | bash -s TOKEN" >&2 exit 2 fi echo "[iop-bootstrap] version=\$VERSION target=\$TARGET" echo "[iop-bootstrap] install_dir=\$INSTALL_DIR" mkdir -p "\$INSTALL_DIR" curl -fsSL "\$ARTIFACT_BASE_URL/\$TARGET/iop-node" -o "\$TMP_DIR/iop-node" curl -fsSL "\$ARTIFACT_BASE_URL/\$TARGET/SHA256SUMS" -o "\$TMP_DIR/SHA256SUMS" if command -v sha256sum >/dev/null 2>&1; then (cd "\$TMP_DIR" && grep -E '[ *]iop-node$' SHA256SUMS | sha256sum -c -) elif command -v shasum >/dev/null 2>&1; then (cd "\$TMP_DIR" && grep -E '[ *]iop-node$' SHA256SUMS | shasum -a 256 -c -) else echo "[iop-bootstrap] warning: no sha256 checker found; skipping checksum verification" >&2 fi cp "\$TMP_DIR/iop-node" "\$BIN_FILE" chmod +x "\$BIN_FILE" if command -v xattr >/dev/null 2>&1; then xattr -d com.apple.quarantine "\$BIN_FILE" 2>/dev/null || true fi cat > "\$CONFIG_FILE" <&2 exit 1 fi for app in $APPS; do case "$app" in edge|node) build_one "$app" "$goos" "$goarch" ;; *) echo "unsupported app '$app'; expected edge or node" >&2 exit 1 ;; esac done output_dir="$DIST_DIR/$VERSION/$goos-$goarch" { echo "version=$VERSION" echo "commit=$COMMIT" echo "build_date=$BUILD_DATE" echo "target=$goos/$goarch" echo "apps=$APPS" } > "$output_dir/manifest.env" ( cd "$output_dir" checksum iop-* | grep -v '\.sha256$' > SHA256SUMS ) if app_enabled node; then write_node_bootstrap "$goos" "$goarch" fi done echo "field binaries written to $DIST_DIR/$VERSION"