#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" PGRUST_REPOSITORY="${PGRUST_REPOSITORY:-https://github.com/malisper/pgrust.git}" PGRUST_REF="${PGRUST_REF:-c9aecbd05348e52a8ba21c688eec6258e8e8282f}" PGRUST_SOURCE_DIR="${PGRUST_SOURCE_DIR:-$ROOT_DIR/target/pgrust-src}" PGRUST_TARGET_DIR="${PGRUST_TARGET_DIR:-$ROOT_DIR/target/pgrust-build}" PGRUST_OUTPUT_DIR="${PGRUST_OUTPUT_DIR:-$ROOT_DIR/dist/pgrust}" PGRUST_SIZE_OPTIMIZED="${PGRUST_SIZE_OPTIMIZED:-0}" for command in cargo git pkg-config; do command -v "$command" >/dev/null || { echo "missing required command: $command" >&2 exit 1 } done for package in icu-uc openssl libxml-2.0; do pkg-config --exists "$package" || { echo "missing native development package for $package" >&2 exit 1 } done if [ ! -d "$PGRUST_SOURCE_DIR/.git" ]; then rm -rf "$PGRUST_SOURCE_DIR" git clone --no-checkout "$PGRUST_REPOSITORY" "$PGRUST_SOURCE_DIR" fi minimal_auth_patch="$ROOT_DIR/patches/pgrust-minimal-auth.patch" if git -C "$PGRUST_SOURCE_DIR" apply --check "$minimal_auth_patch" 2>/dev/null; then git -C "$PGRUST_SOURCE_DIR" apply "$minimal_auth_patch" elif ! git -C "$PGRUST_SOURCE_DIR" apply --reverse --check "$minimal_auth_patch" 2>/dev/null; then echo "pgrust minimal auth patch does not apply cleanly to $PGRUST_REF" >&2 exit 1 fi if [ "$(git -C "$PGRUST_SOURCE_DIR" rev-parse HEAD 2>/dev/null || true)" != "$PGRUST_REF" ]; then git -C "$PGRUST_SOURCE_DIR" fetch --depth 1 origin "$PGRUST_REF" git -C "$PGRUST_SOURCE_DIR" checkout --detach FETCH_HEAD fi if rustc -vV | grep -q 'host: .*musl'; then patch_file="$ROOT_DIR/patches/pgrust-musl-sem.patch" if git -C "$PGRUST_SOURCE_DIR" apply --check "$patch_file" 2>/dev/null; then git -C "$PGRUST_SOURCE_DIR" apply "$patch_file" elif ! git -C "$PGRUST_SOURCE_DIR" apply --reverse --check "$patch_file" 2>/dev/null; then echo "pgrust musl patch does not apply cleanly to $PGRUST_REF" >&2 exit 1 fi fi share_source="$PGRUST_SOURCE_DIR/vendor/postgres-18.3/share" test -f "$share_source/postgres.bki" || { echo "pgrust PostgreSQL 18.3 share tree is incomplete" >&2 exit 1 } if [ "$PGRUST_SIZE_OPTIMIZED" = "1" ]; then export CARGO_PROFILE_RELEASE_OPT_LEVEL=s export CARGO_PROFILE_RELEASE_LTO=thin fi PGRUST_PGBINDIR=/opt/boosapi/pgrust/bin \ PGRUST_PGSHAREDIR=/opt/boosapi/pgrust/share \ CARGO_TARGET_DIR="$PGRUST_TARGET_DIR" \ cargo build \ --manifest-path "$PGRUST_SOURCE_DIR/Cargo.toml" \ --release --locked --bin postgres staging="$PGRUST_OUTPUT_DIR.tmp-$$" rm -rf "$staging" mkdir -p "$staging/bin" "$staging/share" cp "$PGRUST_TARGET_DIR/release/postgres" "$staging/bin/postgres" cp -a "$share_source/." "$staging/share/" cp "$PGRUST_SOURCE_DIR/LICENSE" "$staging/LICENSE.pgrust-AGPL-3.0" printf '%s\n' \ "Repository: $PGRUST_REPOSITORY" \ "Commit: $PGRUST_REF" \ "License: AGPL-3.0" \ >"$staging/SOURCE" chmod 755 "$staging/bin/postgres" find "$staging/share" -type d -exec chmod 755 {} + find "$staging/share" -type f -exec chmod 644 {} + rm -rf "$PGRUST_OUTPUT_DIR" mv "$staging" "$PGRUST_OUTPUT_DIR" echo "pgrust runtime built at $PGRUST_OUTPUT_DIR"