Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.sh text eol=lf
*.env text eol=lf
44 changes: 44 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ jobs:
path: out/*.tar.gz
if-no-files-found: error

build-windows:
runs-on: windows-2025
timeout-minutes: 30
steps:
- uses: actions/checkout@v7
- name: Build tools
shell: pwsh
run: |
choco install winflexbison3 --no-progress -y
python -m pip install --quiet meson ninja
python -c "import sysconfig; print(sysconfig.get_path('scripts'))" >> $env:GITHUB_PATH
- name: Build
shell: cmd
run: |
for /f "usebackq delims=" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath`) do call "%%i\VC\Auxiliary\Build\vcvars64.bat"
"%ProgramFiles%\Git\bin\bash.exe" scripts/build.sh win-x64 out
- uses: actions/upload-artifact@v7
with:
name: kcap-postgres-win-x64
path: out/*.tar.gz
if-no-files-found: error

# Smoke runs on a fresh machine, never the one that built the archive.
smoke-macos:
needs: build-macos
Expand Down Expand Up @@ -72,3 +94,25 @@ jobs:
archive=$(basename out/*.tar.gz)
docker run --rm -v "$PWD:/w:ro" debian:11-slim \
bash -c "useradd -m pg && su pg -c 'bash /w/scripts/smoke.sh /w/out/$archive'"

smoke-windows:
needs: build-windows
runs-on: windows-2025
timeout-minutes: 20
steps:
- uses: actions/checkout@v7
- uses: actions/download-artifact@v8
with:
name: kcap-postgres-win-x64
path: out
- name: Smoke on the runner
shell: bash
run: bash scripts/smoke.sh out/*.tar.gz
# The runner has the VC++ runtime installed; Server Core does not, so only the archive's own copy can serve.
- name: Smoke on a bare Windows Server Core
shell: pwsh
run: |
$archive = (Get-ChildItem out/*.tar.gz).Name
docker run --rm -v "${PWD}:C:\w:ro" -v "${env:ProgramFiles}\Git:C:\git:ro" mcr.microsoft.com/windows/servercore:ltsc2025 `
cmd /c "if exist C:\Windows\System32\vcruntime140.dll (echo the image has the VC++ runtime installed & exit /b 1) else C:\git\bin\bash.exe /c/w/scripts/smoke.sh /c/w/out/$archive"
if ($LASTEXITCODE) { exit $LASTEXITCODE }
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
pattern: kcap-postgres-*
merge-multiple: true
- name: Checksums
run: cd out && sha256sum ./*.tar.gz | sed 's| \./| |' | tee SHA256SUMS && [ "$(wc -l < SHA256SUMS)" -eq 3 ]
run: cd out && sha256sum ./*.tar.gz | sed 's| \./| |' | tee SHA256SUMS && [ "$(wc -l < SHA256SUMS)" -eq 4 ]
# Downstream pins an archive by digest, so a release is written once. A re-run after a failure
# before the upload finds no assets and carries on.
- name: Publish
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ It is a build input for Capacitor's server bundle, which signs it and ships it.
| `osx-arm64` | `macos-15` | macOS 15 or later, Apple silicon |
| `linux-x64` | `manylinux_2_28` | glibc 2.28 or later (RHEL 8, Debian 10, Ubuntu 18.10 and later) |
| `linux-arm64` | `manylinux_2_28` | the same, on aarch64 |
| `win-x64` | `windows-2025`, MSVC | Windows 10 version 1809 or later, Windows Server 2019 or later |

## What is in an archive

Expand All @@ -21,9 +22,11 @@ It is a build input for Capacitor's server bundle, which signs it and ships it.
* `share/`: catalog files, extension scripts, Postgres's own timezone data, and the PostgreSQL and pgvector licences under `share/licenses/`.
* `kcap-postgres.json`: the versions, the pgvector commit, the configure flags and the CI run that built it.

On Windows the programs are `.exe` files, the modules in `lib/` are DLLs, and `bin/` also holds `libpq.dll` and the parts of the VC++ runtime the programs import (`vcruntime140.dll`).

Every release also has a `SHA256SUMS` covering its archives.

The build leaves out ICU, libxml2, OpenSSL, readline, LLVM, GSSAPI and LDAP, because the server uses none of them. The archive depends only on the C library and zlib from the operating system. `scripts/check-deps.sh` enforces that on every build.
The build leaves out ICU, libxml2, OpenSSL, readline, LLVM, GSSAPI and LDAP, because the server uses none of them. On macOS and Linux the archive depends only on the C library and zlib from the operating system. Windows has no system zlib, so `win-x64` links zlib in. It carries its own VC++ runtime, so it needs no redistributable installed and no administrator rights, and depends only on Windows's own DLLs, the Universal C Runtime among them. `scripts/check-deps.sh` enforces this on every build.

## Running it

Expand All @@ -42,7 +45,7 @@ EOF
bin/pg_ctl -D data -l postgres.log -w start
```

Postgres refuses to run as root. A unix socket path is limited to 104 bytes on macOS and 108 on Linux.
Postgres refuses to run as root, or on Windows with administrator rights enabled; `pg_ctl` and `initdb` drop those rights themselves, `postgres` does not. A unix socket path is limited to 104 bytes on macOS and 108 on Linux and Windows.

## Building

Expand All @@ -52,7 +55,14 @@ bash scripts/build-in-container.sh linux-arm64 out # with Docker; linux-x64 run
bash scripts/smoke.sh out/kcap-postgres-*-osx-arm64.tar.gz
```

`build.sh` verifies the Postgres source digest and the pgvector commit from `versions.env` before building, and runs `check-deps.sh` on the result.
`win-x64` builds in Git Bash with Visual Studio's x64 environment loaded (`vcvars64.bat`), Strawberry Perl, `meson`, `ninja` and `win_bison`/`win_flex` (winflexbison) on `PATH`:

```bat
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
"C:\Program Files\Git\bin\bash.exe" scripts/build.sh win-x64 out
```

`build.sh` verifies the Postgres source digest, the pgvector commit and, on Windows, the zlib digest from `versions.env` before building, and runs `check-deps.sh` on the result.

## Releasing

Expand Down
98 changes: 76 additions & 22 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,72 +1,126 @@
#!/usr/bin/env bash
# Builds a relocatable Postgres with pg_trgm and pgvector for one runtime identifier, trims it to
# what the local bundle runs, makes every library reference relative, checks it and archives it.
# win-x64 runs in Git Bash with the MSVC x64 environment loaded (vcvars64), plus meson and win_bison.
set -euo pipefail

usage="usage: build.sh <osx-arm64|linux-x64|linux-arm64> <out-dir>"
usage="usage: build.sh <osx-arm64|linux-x64|linux-arm64|win-x64> <out-dir>"
repo=$(cd "$(dirname "$0")/.." && pwd)
# shellcheck source=versions.env
source "$repo/versions.env"
rid=${1:?$usage}
mkdir -p "${2:?$usage}" && out=$(cd "$2" && pwd)

host="$(uname -s)/$(uname -m)"
case "$(uname -s)" in MINGW*|MSYS*) os=Windows ;; *) os=$(uname -s) ;; esac
host="$os/$(uname -m)"
case "$rid" in
osx-arm64) expected_host=Darwin/arm64 ;;
linux-x64) expected_host=Linux/x86_64 ;;
linux-arm64) expected_host=Linux/aarch64 ;;
win-x64) expected_host=Windows/x86_64 ;;
*) echo "$usage" >&2; exit 2 ;;
esac
[[ $host == "$expected_host" ]] || { echo "build: $rid builds on $expected_host, not $host" >&2; exit 2; }

# Source tarballs carry no generated parsers since Postgres 17, so bison, flex and perl are needed.
tools=(curl git make cc tar bison flex perl file)
if [[ $rid == linux-* ]]; then tools+=(patchelf strip readelf objdump); else tools+=(install_name_tool codesign otool xcrun strip); fi
case "$rid" in
linux-*) tools=(curl git make cc tar bison flex perl file patchelf strip readelf objdump) ;;
osx-*) tools=(curl git make cc tar bison flex perl file install_name_tool codesign otool xcrun strip) ;;
win-*) tools=(curl git tar perl cygpath meson ninja cl nmake dumpbin) ;;
esac
for t in "${tools[@]}"; do command -v "$t" >/dev/null || { echo "build: missing tool $t" >&2; exit 2; }; done
if [[ $rid == win-* ]]; then
command -v bison >/dev/null || command -v win_bison >/dev/null || { echo "build: missing tool win_bison" >&2; exit 2; }
[[ -n ${VCToolsRedistDir:-} ]] || { echo "build: VCToolsRedistDir is unset; load vcvars64 first" >&2; exit 2; }
# Git Bash's /usr/bin has a link.exe of its own, which meson would take for the linker.
PATH="$(dirname "$(command -v cl)"):$PATH"
fi

tag="$PG_VERSION-$PACKAGE_REVISION"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
# Postgres installs a flat lib/ and share/ only when the prefix contains "postgres"; the random
# parent guarantees the prefix exists on no machine that later runs the archive.
prefix="$work/postgresql"
jobs=$(getconf _NPROCESSORS_ONLN)
sha256() { if command -v sha256sum >/dev/null; then sha256sum "$1"; else shasum -a 256 "$1"; fi | awk '{print $1}'; }

curl -fsSL --retry 3 -o "$work/pg.tar.gz" "https://ftp.postgresql.org/pub/source/v$PG_VERSION/postgresql-$PG_VERSION.tar.gz"
[[ $(sha256 "$work/pg.tar.gz") == "$PG_SHA256" ]] || { echo "build: postgresql-$PG_VERSION.tar.gz digest mismatch" >&2; exit 1; }
tar -xzf "$work/pg.tar.gz" -C "$work"
git clone -q --depth 1 --branch "v$PGVECTOR_VERSION" https://github.com/pgvector/pgvector.git "$work/pgvector" 2>/dev/null
[[ $(git -C "$work/pgvector" rev-parse HEAD) == "$PGVECTOR_COMMIT" ]] || { echo "build: pgvector v$PGVECTOR_VERSION is not $PGVECTOR_COMMIT" >&2; exit 1; }

configure=(--prefix="$prefix" --disable-rpath --without-icu --without-libxml --without-libxslt
--without-openssl --without-readline --without-llvm --without-gssapi --without-ldap --with-zlib)
if [[ $rid == osx-* ]]; then export MACOSX_DEPLOYMENT_TARGET; fi
src="$work/postgresql-$PG_VERSION"
(cd "$src" && ./configure "${configure[@]}" >"$work/configure.log") || { tail -30 "$work/configure.log" >&2; exit 1; }
make -C "$src" -j"$jobs" -s >/dev/null
make -C "$src" install -s >/dev/null
make -C "$src/contrib/pg_trgm" -j"$jobs" -s >/dev/null
make -C "$src/contrib/pg_trgm" install -s >/dev/null

vector_args=(PG_CONFIG="$prefix/bin/pg_config" OPTFLAGS=)
if [[ $rid == osx-* ]]; then vector_args+=(PG_SYSROOT="$(xcrun --show-sdk-path)"); fi
make -C "$work/pgvector" -j"$jobs" "${vector_args[@]}" >/dev/null
make -C "$work/pgvector" install "${vector_args[@]}" >/dev/null
if [[ $rid == win-* ]]; then
curl -fsSL --retry 3 -o "$work/zlib.tar.gz" "https://github.com/madler/zlib/releases/download/v$ZLIB_VERSION/zlib-$ZLIB_VERSION.tar.gz"
[[ $(sha256 "$work/zlib.tar.gz") == "$ZLIB_SHA256" ]] || { echo "build: zlib-$ZLIB_VERSION.tar.gz digest mismatch" >&2; exit 1; }
tar -xzf "$work/zlib.tar.gz" -C "$work"
(cd "$work/zlib-$ZLIB_VERSION" && nmake -nologo -f win32/Makefile.msc zlib.lib >"$work/zlib.log") \
|| { tail -30 "$work/zlib.log" >&2; exit 1; }
zlib_dir=$(cygpath -w "$work/zlib-$ZLIB_VERSION")
export INCLUDE="$zlib_dir;$INCLUDE" LIB="$zlib_dir;$LIB"

# Of contrib, only pg_trgm, as the make build does.
sed -i "/^subdir('/{/^subdir('pg_trgm')/!d}" "$src/contrib/meson.build"
# The build's perl scripts pass /-style flags to MSVC tools, which Git Bash's own perl rewrites as paths.
# Asked, not judged by path: on a runner Git's perl is on PATH as /c/Program Files/Git/usr/bin.
perl=
while read -r p; do
if "$p" -e 'exit($^O eq "MSWin32" ? 0 : 1)'; then perl=$p; break; fi
done < <(type -ap perl)
[[ -n $perl ]] || { echo "build: needs a native Windows perl such as Strawberry Perl on PATH" >&2; exit 2; }
flags=(--buildtype=release -Dauto_features=disabled -Dzlib=enabled -Dssl=none -Duuid=none)
meson setup "$work/build" "$src" --prefix="$(cygpath -w "$prefix")" "${flags[@]}" -DPERL="$(cygpath -w "$perl")" >"$work/configure.log" \
|| { tail -30 "$work/configure.log" >&2; exit 1; }
meson install -C "$work/build" --quiet >"$work/build.log" \
|| { grep -A20 -m3 '^FAILED' "$work/build.log" >&2 || tail -30 "$work/build.log" >&2; exit 1; }

(cd "$work/pgvector" && PGROOT=$(cygpath -w "$prefix") nmake -nologo -f Makefile.win >/dev/null \
&& PGROOT=$(cygpath -w "$prefix") nmake -nologo -f Makefile.win install >/dev/null)
exe=.exe
else
flags=(--disable-rpath --without-icu --without-libxml --without-libxslt
--without-openssl --without-readline --without-llvm --without-gssapi --without-ldap --with-zlib)
if [[ $rid == osx-* ]]; then export MACOSX_DEPLOYMENT_TARGET; fi
jobs=$(getconf _NPROCESSORS_ONLN)
(cd "$src" && ./configure --prefix="$prefix" "${flags[@]}" >"$work/configure.log") || { tail -30 "$work/configure.log" >&2; exit 1; }
make -C "$src" -j"$jobs" -s >/dev/null
make -C "$src" install -s >/dev/null
make -C "$src/contrib/pg_trgm" -j"$jobs" -s >/dev/null
make -C "$src/contrib/pg_trgm" install -s >/dev/null

vector_args=(PG_CONFIG="$prefix/bin/pg_config" OPTFLAGS=)
if [[ $rid == osx-* ]]; then vector_args+=(PG_SYSROOT="$(xcrun --show-sdk-path)"); fi
make -C "$work/pgvector" -j"$jobs" "${vector_args[@]}" >/dev/null
make -C "$work/pgvector" install "${vector_args[@]}" >/dev/null
exe=
fi

# Trim to what the local stack runs; pg_upgrade needs pg_dump, pg_dumpall, pg_restore, pg_resetwal.
pkg="$work/pkg"
mkdir -p "$pkg/bin" "$pkg/lib" "$pkg/share/licenses"
for b in postgres initdb pg_ctl pg_controldata pg_isready pg_dump pg_dumpall pg_restore pg_upgrade pg_resetwal psql; do
cp -p "$prefix/bin/$b" "$pkg/bin/"
cp -p "$prefix/bin/$b$exe" "$pkg/bin/"
done
(cd "$prefix/lib" && find . -maxdepth 1 \( -type f -o -type l \) \
! -name '*.a' ! -name 'libecpg*' ! -name 'libpgtypes*' -exec cp -P {} "$pkg/lib/" \;)
! -name '*.a' ! -name '*.lib' ! -name '*.pdb' ! -name 'libecpg*' ! -name 'libpgtypes*' -exec cp -P {} "$pkg/lib/" \;)
cp -R "$prefix/share/." "$pkg/share/"
cp "$src/COPYRIGHT" "$pkg/share/licenses/postgresql-COPYRIGHT"
cp "$work/pgvector/LICENSE" "$pkg/share/licenses/pgvector-LICENSE"

if [[ $rid == linux-* ]]; then
if [[ $rid == win-* ]]; then
# Windows looks for a DLL beside the executable first: libpq, and the VC++ runtime, which ships
# here rather than as a system install that would need administrator rights.
cp -p "$prefix/bin/libpq.dll" "$pkg/bin/"
cp "$work/zlib-$ZLIB_VERSION/LICENSE" "$pkg/share/licenses/zlib-LICENSE"
crt=$(compgen -G "$(cygpath -u "$VCToolsRedistDir")x64/Microsoft.VC*.CRT" | tail -1)
[[ -n $crt ]] || { echo "build: no VC++ runtime under $VCToolsRedistDir" >&2; exit 1; }
while read -r dll; do
if [[ -f $crt/$dll ]]; then cp "$crt/$dll" "$pkg/bin/$dll"; fi
done < <(find "$pkg/bin" "$pkg/lib" -type f \( -name '*.exe' -o -name '*.dll' \) -print0 \
| xargs -0 -n1 cygpath -w | xargs -d '\n' dumpbin -nologo -dependents \
| awk 'NF == 1 && tolower($1) ~ /\.dll$/ {print tolower($1)}' | sort -u)
elif [[ $rid == linux-* ]]; then
while IFS= read -r -d '' f; do
if readelf -h "$f" >/dev/null 2>&1; then strip --strip-unneeded "$f"; fi
done < <(find "$pkg/bin" "$pkg/lib" -type f -print0)
Expand Down Expand Up @@ -98,7 +152,7 @@ cat >"$pkg/kcap-postgres.json" <<EOF
"postgres": "$PG_VERSION",
"pgvector": "$PGVECTOR_VERSION",
"pgvector_commit": "$PGVECTOR_COMMIT",
"configure": "${configure[*]:1}",
"configure": "${flags[*]}",
"build_prefix": "$prefix",
"build_run": "${build_run:-local}"
}
Expand Down
13 changes: 13 additions & 0 deletions scripts/check-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,23 @@ check_macos() {
codesign -v "$f" 2>/dev/null || fail "${f#"$root"/} has no valid signature"
}

# Windows: a DLL the archive carries in bin/ (libpq, the VC++ runtime), the UCRT or one of Windows's own.
check_windows() {
local f=$1 dep deps
[[ $f == *.exe || $f == *.dll ]] || return 0
deps=$(dumpbin -nologo -dependents "$(cygpath -w "$f")") || { fail "${f#"$root"/} could not be read by dumpbin"; return 0; }
while read -r dep; do
[[ -e $root/bin/$dep ]] && continue
[[ $dep =~ ^(api-ms-win-crt-[a-z0-9-]+|kernel32|advapi32|ws2_32|secur32|shell32|user32|wldap32|bcrypt|crypt32|userenv)\.dll$ ]] \
|| fail "${f#"$root"/} needs $dep"
done < <(awk 'NF == 1 && tolower($1) ~ /\.(dll|exe)$/ {print tolower($1)}' <<<"$deps")
}

MIN_MACOS=$(sed -n 's/^MACOSX_DEPLOYMENT_TARGET=//p' "$(dirname "$0")/../versions.env")
case "$(uname -s)" in
Linux) checker=check_linux ;;
Darwin) checker=check_macos ;;
MINGW*|MSYS*) checker=check_windows ;;
*) echo "check-deps: unsupported OS $(uname -s)" >&2; exit 2 ;;
esac
while IFS= read -r -d '' f; do "$checker" "$f"; done < <(find "$root/bin" "$root/lib" -type f -print0)
Expand Down
7 changes: 6 additions & 1 deletion scripts/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ archive=$(cd "$(dirname "${1:?usage: smoke.sh <archive.tar.gz>}")" && pwd)/$(bas

# /tmp, not $TMPDIR: macOS's $TMPDIR is long enough to push the socket path past its 104-byte limit.
root=$(mktemp -d "/tmp/kcap pg smoke.XXXXXX")
# In Git Bash, Postgres reads paths from postgresql.conf as Windows paths, so use the C:/ form throughout.
windows=
if command -v cygpath >/dev/null; then root=$(cygpath -m "$root") windows=1; fi
pg="$root/install dir"
data="$root/data"
run="$root/run dir"
Expand All @@ -21,7 +24,9 @@ stop() {
}
trap stop EXIT

mkdir -p "$pg" && mkdir -m 700 "$run"
mkdir -p "$pg" "$run"
# Windows has no mode bits; the socket directory inherits its ACL.
[[ -n $windows ]] || chmod 700 "$run"
tar -xzf "$archive" -C "$pg"

build_prefix=$(sed -n 's/.*"build_prefix": *"\([^"]*\)".*/\1/p' "$pg/kcap-postgres.json")
Expand Down
3 changes: 3 additions & 0 deletions versions.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ PG_VERSION=18.6
PG_SHA256=983ee554ec53dbeb9b70797bef9fcf4e67e117e7e48ca1463cc80b3ff8e8ff3f
PGVECTOR_VERSION=0.8.5
PGVECTOR_COMMIT=159b79aaad5983fb7459c1e3df2897fbb2d11788
# Windows has no system zlib, so win-x64 links this one in.
ZLIB_VERSION=1.3.2
ZLIB_SHA256=bb329a0a2cd0274d05519d61c667c062e06990d72e125ee2dfa8de64f0119d16
PACKAGE_REVISION=1
# The desktop app's LSMinimumSystemVersion; the bundle cannot support an older macOS than the app.
MACOSX_DEPLOYMENT_TARGET=15.0
Loading