Skip to content

Compile postgres#107

Open
rennergade wants to merge 11 commits intomainfrom
compile-postgres
Open

Compile postgres#107
rennergade wants to merge 11 commits intomainfrom
compile-postgres

Conversation

@rennergade
Copy link
Contributor

Closes #65

Summary

  • Cross-compile the PostgreSQL backend (postgres) and initdb to wasm32-wasi using a two-pass build strategy (native pass for flex/bison/Perl codegen, then WASM cross-compile)
  • Add make postgres / make install-postgres targets to the top-level Makefile
  • Produce postgres.wasm → postgres.cwasm (and initdb.wasm → initdb.cwasm when initdb links successfully)

Notable / Weird Build Workarounds

  • Two-pass build (like bash): PostgreSQL's build generates C/H files with flex, bison, and Perl scripts that must run natively. Pass 1 builds these on the host, then we clean .o files while preserving the generated sources and re-configure for WASM in Pass 2.
  • Config cache (config.cache.wasi): Cross-compile configure can't run test programs, so we pre-populate a cache with type sizes, alignments, spinlock support, and function availability for the wasm32 ILP32 target.
  • Makefile.shlib patched to disable shared libraries: wasm-ld cannot build .so files (no --version-script, --shared incompatible with --export-memory, missing crt1-reactor.o). We comment out the all-shared-lib dependency so only static libpq.a is produced.
  • libpq/Makefile patched: libpq-refs-stamp normally depends on the shared lib and runs a Perl symbol check. We remove that dependency since there’s no .so to check.
  • pg_config.h patched post-configure: Force-disable EXEC_BACKEND (no fork), all semaphore types (stubbed), and ensure HAVE_SPINLOCKS is set (GCC atomic builtins work on WASM).
  • Root-user check patched out in main.c: Lind sandbox’s geteuid() returns 0, but the security concern does not apply inside the sandbox.
  • wasm_compat.h force-included: Maps old st_atime / st_mtime / st_ctime stat field names to WASI’s st_atim.tv_sec / st_mtim.tv_sec / st_ctim.tv_sec.
  • wasi_stubs.c: Stubs _Unwind_Backtrace, _Unwind_GetIP, _Unwind_GetGR, _Unwind_GetCFA. These are pulled in by glibc’s backtrace.o, but WASI has no stack unwinder.
  • initdb link flags manually specified: The compile script overrides LIBS on the make command line (to inject wasi_stubs.o -lm), which wipes out -lpgcommon -lpgport that configure normally adds. We re-add them along with explicit -L paths since the LDFLAGS override also drops LDFLAGS_INTERNAL.
  • 256 MB --max-memory: PostgreSQL allocates shared buffers even in single-user mode and needs more than the default.
  • Best-effort throughout: Backend, libpq, fe_utils, and initdb builds all tolerate errors and continue.

New Files

  • postgres/compile_postgres.sh — main build orchestrator (527 lines)
  • postgres/wasi_stubs.c_Unwind_* no-op stubs
  • postgres/wasm_compat.h — stat field name compatibility macros

Test Plan

  • make postgres completes and produces postgres.cwasm in build/bin/postgres/wasm32-wasi/
  • initdb.cwasm is also produced (or at least link errors are diagnosed and documented)
  • lind-boot can load and start the postgres binary in single-user mode

@rennergade
Copy link
Contributor Author

This runs but basically doesn't do anything, a lot to debug here.

lind@894575b27c0f:~/lind-wasm/lindfs$ lind-wasm bin/postgres
LOG:  invalid binary "bin/postgres": Permission denied
LOG:  invalid binary "bin/postgres": Permission denied
FATAL:  bin/postgres: could not locate my own executable path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build Postgres

1 participant