Skip to content

Commit c026976

Browse files
tdrztudor
andauthored
Switch to vanilla emsdk for building pglite (#48)
* first pass * skip postgresql tests as usual * preloaded files; pglite.data file autotools handling * new pglite and install-pglite Makefile targets; workflow succesful; * add pglite.{data,js,html} to gitignore * remove the need for emsdk-shared script * add password and PGPASSFILE static files * use system zic from /usr/sbin/zic for timezone * use static files from repo when preloading password and PGPASSFILE * add empty file * builder dockerfile; external deps source code * use -sMAIN_MODULE=2 which reduces size code * building with -sMAIN_MODULE=2 to reduce size * use file containing needed EXPORTED_FUNCTIONS - tested on data generated from the *.o files of plpgsql + the already predefined ones; -sDYLINK_DEBUG=2 flag is very useful in debugging emscripten's dynamic linking; generating the EXPORTED_FUNCTIONS needs to be automated * automate creation of imports needed by plpgsql; automate creation of EXPORTED_FUNCTIONS emscripten parameter; * infrastructure for contrib extensions * more excluded imports * link pglite.wasm against more libs so the exports are visible for emscripten * more EXPORTED_FUNCTIONS excludes * more EXPORTED_FUNCTIONS includes * extended docker image to contain the exports of the prebuilt libraries * change the exports format of shared libs * extend backend makefile to better handle EXPORTED_FUNCTIONS depending on shared libs' exports * git: ignore .map files * added pg_ivm as submodule; added Makefile to build such submodules * added pgvector as submodule and updated the Makefile to build it * use env var to point to llvm-nm for exports and imports defs * exclude libossp-uuid exports and dont link libossp-uuid to the exe * git ignore excluded.imports file * new simple bash script to build everything; allow debug builds * cleanup * added bashs script to clean everything * unset DEBUG var if it is false - there is a bug somewhere and until we fix that, this works; bug fix in passing PGLITE_CFLAGS * further simplify build-with-docker2.sh script * build pglite script remove SOURCE_MAP_PREFIXES * comments * rename script * add 2 more exported functions; sort the always included imports * separate dockerfiles for different archs - this is needed because emsdk does not publish different archs under the same tag; hopefully this will be fixed in the future * contrib extensions packaging; integration into the build of the said extensions * better error message when building/disting contribs * change custom build root for pglite contrib * add LDFLAGS_SL="-sSIDE_MODULE=1" when building contrib extensions * renamed pgvector submodule to vector * dist extra extensions insted of install (packaging pgvector and pg_ivm into tar.gz) * use pushed docker image * build only using native build platform, since wasm is the same on all platforms * remove unused dockerfiles * cleanup src/backend/Makefile * remove wasm-builder folder * added comment to other extensions Makefile * comments * remove binaries used for pglite-builder * remove config.site since the param that it contains is already passed in the command line * update clean-pglite.sh script --------- Co-authored-by: tudor <[email protected]>
1 parent 7dbc9f6 commit c026976

File tree

55 files changed

+968
-100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+968
-100
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ lib*.pc
4444
/tmp_install/
4545
/portlock/
4646
/dist
47+
48+
# emscripten build excludes
49+
*.cjs
50+
*.wasm
51+
pglite.data
52+
pglite.js
53+
pglite.html
54+
*.map
55+
pglite-wasm/excluded.imports

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "pglite/pg_ivm"]
2+
path = pglite/pg_ivm
3+
url = https://github.com/sraoss/pg_ivm.git
4+
[submodule "pglite/pgvector"]
5+
path = pglite/vector
6+
url = https://github.com/pgvector/pgvector.git

build-pglite.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
### NOTES ###
4+
# $INSTALL_PREFIX is expected to point to the installation folder of various libraries built to wasm (see pglite-builder)
5+
#############
6+
7+
# final output folder
8+
INSTALL_FOLDER=${INSTALL_FOLDER:-"/install/pglite"}
9+
10+
# build with optimizations by default aka release
11+
PGLITE_CFLAGS="-O2"
12+
if [ "$DEBUG" = true ]
13+
then
14+
echo "pglite: building debug version."
15+
PGLITE_CFLAGS="-g -gsource-map --no-wasm-opt"
16+
else
17+
echo "pglite: building release version."
18+
# we shouldn't need to do this, but there's a bug somewhere that prevents a successful build if this is set
19+
unset DEBUG
20+
fi
21+
22+
echo "pglite: PGLITE_CFLAGS=$PGLITE_CFLAGS"
23+
24+
# Step 1: configure the project
25+
LDFLAGS="-sWASM_BIGINT -sUSE_PTHREADS=0" CFLAGS="${PGLITE_CFLAGS} -sWASM_BIGINT -fpic -sENVIRONMENT=node,web,worker -sSUPPORT_LONGJMP=emscripten -DPYDK=1 -DCMA_MB=12 -Wno-declaration-after-statement -Wno-macro-redefined -Wno-unused-function -Wno-missing-prototypes -Wno-incompatible-pointer-types" emconfigure ./configure ac_cv_exeext=.cjs --disable-spinlocks --disable-largefile --without-llvm --without-pam --disable-largefile --with-openssl=no --without-readline --without-icu --with-includes=$INSTALL_PREFIX/include:$INSTALL_PREFIX/include/libxml2 --with-libraries=$INSTALL_PREFIX/lib --with-uuid=ossp --with-zlib --with-libxml --with-libxslt --with-template=emscripten --prefix=$INSTALL_FOLDER || { echo 'error: emconfigure failed' ; exit 11; }
26+
27+
# Step 2: make and install all except pglite
28+
emmake make PORTNAME=emscripten -j || { echo 'error: emmake make PORTNAME=emscripten -j' ; exit 21; }
29+
emmake make PORTNAME=emscripten install || { echo 'error: emmake make PORTNAME=emscripten install' ; exit 22; }
30+
31+
# Step 3.1: make all contrib extensions - do not install
32+
emmake make PORTNAME=emscripten LDFLAGS_SL="-sSIDE_MODULE=1" -C contrib/ -j || { echo 'error: emmake make PORTNAME=emscripten -C contrib/ -j' ; exit 31; }
33+
# Step 3.2: make dist contrib extensions - this will create an archive for each extension
34+
emmake make PORTNAME=emscripten -C contrib/ dist || { echo 'error: emmake make PORTNAME=emscripten -C contrib/ dist' ; exit 32; }
35+
# the above will also create a file with the imports that each extension needs - we pass these as input in the next step for emscripten to keep alive
36+
37+
# Step 4: make and dist other extensions
38+
SAVE_PATH=$PATH
39+
PATH=$PATH:$INSTALL_FOLDER/bin
40+
emmake make OPTFLAGS="" PORTNAME=emscripten -j -C pglite || { echo 'error: emmake make OPTFLAGS="" PORTNAME=emscripten -j -C pglite' ; exit 41; }
41+
emmake make OPTFLAGS="" PORTNAME=emscripten LDFLAGS_SL="-sSIDE_MODULE=1" -C pglite/ dist || { echo 'error: make OPTFLAGS="" PORTNAME=emscripten LDFLAGS_SL="-sSIDE_MODULE=1" -C pglite/ dist ' ; exit 42; }
42+
PATH=$SAVE_PATH
43+
44+
# Step 5: make and install pglite
45+
# Building pglite itself needs to be the last step because of the PRELOAD_FILES parameter (a list of files and folders) need to be available.
46+
PGLITE_CFLAGS=$PGLITE_CFLAGS emmake make PORTNAME=emscripten -j -C src/backend/ install-pglite || { echo 'emmake make OPTFLAGS="" PORTNAME=emscripten -j -C pglite' ; exit 51; }

build-with-docker.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# although we could use any path inside docker, using the same path as on the host
2+
# allows the DWARF info (when building in DEBUG) to contain the correct file paths
3+
DOCKER_WORKSPACE=$(pwd)
4+
5+
docker run $@ \
6+
--rm \
7+
-e DEBUG=${DEBUG:-false} \
8+
--workdir=${DOCKER_WORKSPACE} \
9+
-v .:${DOCKER_WORKSPACE}:rw \
10+
-v ./dist:/install/pglite:rw \
11+
electricsql/pglite-builder:3.1.74_1 \
12+
./build-pglite.sh
13+

clean-pglite.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
emmake make -C src/backend uninstall; emmake make -C src/backend clean;
4+
emmake make -C pglite/ clean; emmake make -C pglite/ uninstall;
5+
emmake make -C contrib/ clean; emmake make -C contrib/ uninstall; emmake make -C pglite clean; emmake make -C pglite uninstall;
6+
emmake make clean; emmake make uninstal

contrib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ endif
9090
# Missing:
9191
# start-scripts \ (does not have a makefile)
9292

93+
include ./dist.mk
9394

9495
$(recurse)
9596
$(recurse_always)

contrib/dist.mk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# contrib/dist.mk
2+
#
3+
# Package each contrib extension into its own .tar.gz archive
4+
5+
prefix ?= /install/pglite
6+
CONTRIB_BUILD_ROOT := /tmp/extensions/build
7+
ARCHIVE_DIR := /install/pglite/extensions
8+
9+
CONTRIBS := $(SUBDIRS)
10+
11+
# Default target: build tarballs for all contribs
12+
dist: $(addsuffix .tar.gz,$(CONTRIBS))
13+
14+
# Pattern rule: build $(EXT).tar.gz for each contrib
15+
%.tar.gz:
16+
@echo "=== Staging $* ==="
17+
rm -rf $(CONTRIB_BUILD_ROOT)/$*
18+
bash -c 'mkdir -p $(CONTRIB_BUILD_ROOT)/$*/$(prefix)/{bin,lib,share/extension,share/doc,share/postgresql/extension,share/postgresql/tsearch_data,include}'
19+
$(MAKE) -C $* install DESTDIR=$(CONTRIB_BUILD_ROOT)/$*
20+
@echo "=== Packaging $* ==="
21+
mkdir -p $(ARCHIVE_DIR)
22+
cd $(CONTRIB_BUILD_ROOT)/$*/$(prefix) && \
23+
files=$$(find . -type f -o -type l | sed 's|^\./||') && \
24+
tar -czf $(ARCHIVE_DIR)/$*.tar.gz $$files
25+
# tar -C $(CONTRIB_BUILD_ROOT)/$*/$(prefix) -czf $(ARCHIVE_DIR)/$*.tar.gz .
26+
27+
.PHONY: dist

other/PGPASSFILE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
localhost:5432:postgres:password:md532e12f215ba27cb750c9e093ce4b5127
2+
localhost:5432:postgres:postgres:md53175bce1d3201d16594cebf9d7eb3f9d
3+
localhost:5432:postgres:login:md5d5745f9425eceb269f9fe01d0bef06ff

other/empty

Whitespace-only changes.

other/password

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
password

0 commit comments

Comments
 (0)