Skip to content

Commit 8a8452f

Browse files
committed
add emscripten platform and CI
1 parent cee8db3 commit 8a8452f

File tree

2 files changed

+287
-0
lines changed

2 files changed

+287
-0
lines changed

.github/workflows/emsdk.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: WebAssembly build
2+
on: [push, workflow_dispatch]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-22.04
7+
env:
8+
SDK_VERSION: 3.1.57.2bi
9+
SDK_ARCHIVE: python3.12-wasm-sdk-Ubuntu-22.04.tar.lz4
10+
SDKROOT: /opt/python-wasm-sdk
11+
SYS_PYTHON: /usr/bin/python3
12+
13+
steps:
14+
- uses: actions/[email protected]
15+
with:
16+
fetch-depth: 1
17+
18+
- name: install python-wasm-sdk for wasi+prebuilts
19+
run: |
20+
sudo apt-get install -y lz4 wget pv bash
21+
echo https://github.com/pygame-web/python-wasm-sdk/releases/download/$SDK_VERSION/$SDK_ARCHIVE
22+
curl -sL --retry 5 https://github.com/pygame-web/python-wasm-sdk/releases/download/$SDK_VERSION/$SDK_ARCHIVE | tar xvP --use-compress-program=lz4 | pv -p -l -s 24400 >/dev/null
23+
curl https://wasmtime.dev/install.sh -sSf | bash
24+
mkdir -p /opt/python-wasm-sdk/wasisdk/bin
25+
mv -vf $(find $HOME/|grep /wasmtime$) /opt/python-wasm-sdk/wasisdk/bin/
26+
working-directory: /
27+
28+
- name: Build WASM with emsdk
29+
run: |
30+
chmod +x wasm-build.sh
31+
bash ./wasm-build.sh
32+

wasm-build.sh

+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
#!/bin/bash
2+
reset
3+
PGROOT=/pgdata
4+
PREFIX=$PGROOT
5+
PGDATA=${PGROOT}/base
6+
7+
echo "
8+
9+
PREFIX=$PREFIX
10+
PGDATA=$PGDATA
11+
12+
"
13+
14+
if echo "$@"|grep clean
15+
then
16+
make clean
17+
rm $(find |grep \\.js$) $(find |grep \\.wasm$)
18+
if echo "$@"|grep distclean
19+
then
20+
exit 0
21+
fi
22+
fi
23+
24+
if $CI
25+
then
26+
echo "
27+
28+
29+
applying patches
30+
31+
32+
"
33+
wget -O- https://github.com/pmp-p/postgres/commit/23a4a59d484ab18847f1848d29f1287551958c9e.diff | patch -p1
34+
35+
sudo mkdir /pgdata
36+
sudo chmod 777 /pgdata
37+
38+
fi
39+
40+
41+
mkdir -p ${PREFIX}
42+
43+
44+
cat > ${PREFIX}/config.site <<END
45+
END
46+
47+
# --with-blocksize=8 --with-segsize=1 --with-segsize-blocks=0 --with-wal-blocksize=8 \
48+
#
49+
50+
51+
52+
CNF="./configure --prefix=${PREFIX} \
53+
--disable-spinlocks --disable-atomics --without-zlib --disable-largefile --without-llvm \
54+
--without-readline --without-icu \
55+
--without-pam --disable-largefile --without-zlib --with-openssl=no \
56+
--enable-debug"
57+
58+
59+
60+
if echo "$@" |grep native
61+
then
62+
63+
make distclean
64+
echo " =============== building host native =================== "
65+
CONFIG_SITE=${PREFIX}/config.site CC="clang" CXX="clang++" CFLAGS="-m32" $CNF --with-template=linux && make && make install
66+
67+
if pushd ${PREFIX}
68+
then
69+
cp bin/postgres bin/postgres.native
70+
71+
cat > ${PREFIX}/bin/postgres <<END
72+
#!/bin/bash
73+
unset LD_PRELOAD
74+
PGDATA=${PGDATA}
75+
echo "# \$@" >> ${PREFIX}/journal.sql
76+
tee -a ${PREFIX}/journal.sql | ${PREFIX}/bin/postgres.native \$@
77+
END
78+
79+
chmod +x ${PREFIX}/bin/postgres
80+
81+
# 32bits build run initdb
82+
unset LD_PRELOAD
83+
84+
rm -rf base/
85+
> ${PREFIX}/journal.sql
86+
87+
TZ=UTC ${PREFIX}/bin/initdb -g -N -U postgres --pwfile=${PREFIX}/password -E UTF8 --locale=C --locale-provider=libc --pgdata=${PGDATA}
88+
89+
mkdir -p base-native/ base-wasm/
90+
cp -rf base/* base-native/
91+
popd
92+
fi
93+
sync
94+
echo "initdb-native done"
95+
make distclean 2>&1 >/dev/null
96+
exit 0
97+
fi
98+
99+
100+
if echo "$@" |grep wasi
101+
then
102+
. /opt/python-wasm-sdk/wasisdk/wasisdk_env.sh
103+
echo " =============== building wasi =================== "
104+
#make distclean
105+
106+
CONFIG_SITE=${PREFIX}/config.site CC="wasi-c" CXX="wasi-c++" $CNF --cache-file=../config.wasi && make
107+
# && make install
108+
109+
exit 0
110+
fi
111+
112+
113+
114+
115+
116+
echo " =============== building wasm =================== "
117+
118+
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
119+
# was erased, default pfx is sdk dir
120+
export PREFIX=$PGROOT
121+
122+
123+
# -lwebsocket.js -sPROXY_POSIX_SOCKETS -pthread -sPROXY_TO_PTHREAD
124+
# CONFIG_SITE=$(pwd)/config.site EMCC_CFLAGS="--oformat=html" \
125+
126+
# --disable-shared is not supported
127+
128+
CONFIG_SITE==${PGDATA}/config.site emconfigure $CNF --with-template=emscripten --cache-file=../config.emsdk $@
129+
130+
131+
sed -i 's|ZIC= ./zic|ZIC= zic|g' ./src/timezone/Makefile
132+
133+
mkdir bin
134+
135+
cat > bin/zic <<END
136+
#!/bin/bash
137+
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
138+
node $(pwd)/src/timezone/zic \$@
139+
END
140+
141+
> /tmp/disable-shared.log
142+
143+
# --disable-shared not supported so use a fake linker
144+
145+
cat > bin/disable-shared <<END
146+
#!/bin/bash
147+
echo "[\$(pwd)] $0 \$@" >> /tmp/disable-shared.log
148+
for arg do
149+
shift
150+
if [ "\$arg" = "-o" ]
151+
then
152+
continue
153+
fi
154+
if echo "\$arg" | grep -q ^-
155+
then
156+
continue
157+
fi
158+
if echo "\$arg" | grep -q \\\\.o$
159+
then
160+
continue
161+
fi
162+
set -- "\$@" "\$arg"
163+
done
164+
touch \$@
165+
END
166+
167+
# FIXME: workaround for /conversion_procs/ make
168+
cp bin/disable-shared bin/o
169+
170+
chmod +x bin/zic bin/disable-shared bin/o
171+
172+
rm /srv/www/html/pygbag/pg/libpq.so /srv/www/html/pygbag/pg/libpq.so.map
173+
174+
if [ -f $(pwd)/wasmfix.h ]
175+
then
176+
export EMCC_CFLAGS="-include $(pwd)/wasmfix.h -sNODERAWFS"
177+
else
178+
export EMCC_CFLAGS="-sNODERAWFS"
179+
fi
180+
181+
rm ./src/backend/postgres.wasm
182+
183+
# for zic and disable-shared
184+
PATH=$(pwd)/bin:$PATH
185+
186+
if emmake make -j 6
187+
then
188+
189+
if emmake make install
190+
then
191+
192+
mv -vf ./src/bin/initdb/initdb.wasm ./src/backend/postgres.wasm ./src/backend/postgres.map ${PREFIX}/bin/
193+
mv -vf ./src/bin/initdb/initdb ${PREFIX}/bin/initdb.js
194+
mv -vf ./src/backend/postgres ${PREFIX}/bin/postgres.js
195+
196+
197+
cat > ${PREFIX}/bin/postgres <<END
198+
#!/bin/bash
199+
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
200+
PGDATA=${PGDATA} node ${PREFIX}/bin/postgres.js \$@
201+
END
202+
203+
cat > ${PREFIX}/bin/initdb <<END
204+
#!/bin/bash
205+
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
206+
node ${PREFIX}/bin/initdb.js \$@
207+
END
208+
209+
chmod +x ${PREFIX}/bin/postgres ${PREFIX}/bin/initdb
210+
211+
echo "initdb for PGDATA=${PGDATA} "
212+
213+
fi
214+
215+
cat >$PREFIX/initdb.sh <<END
216+
#!/bin/bash
217+
rm -rf ${PGDATA} /tmp/initdb-*.log
218+
${PREFIX}/initdb -k -g -N -U postgres --pwfile=${PREFIX}/password --locale=C --locale-provider=libc --pgdata=${PGDATA} 2> /tmp/initdb-\$\$.log
219+
echo "Ready to run sql command through ${PREFIX}/postgres"
220+
read
221+
tail -n +3 /tmp/initdb-\$\$.log | head -n -2 > /tmp/initdb-\$\$
222+
rm /tmp/initdb-\$\$.log
223+
${PREFIX}/postgres --boot -d 1 -c log_checkpoints=false -X 16777216 -k < /tmp/initdb-\$\$ 2>&1 | grep -v 'bootstrap>'
224+
rm /tmp/initdb-\$\$
225+
END
226+
227+
chmod +x $PREFIX/*.sh
228+
229+
$PREFIX/initdb.sh
230+
echo "initdb done, now init sql default database"
231+
232+
if [ -f ${PGDATA}/postmaster.pid ]
233+
then
234+
cat > $PREFIX/initsql.sh <<END
235+
cat $(realpath ../initdb.sql) | ${PREFIX}/postgres --single -F -O -j -c search_path=pg_catalog -c exit_on_error=true -c log_checkpoints=false template1
236+
END
237+
238+
chmod +x $PREFIX/*.sh
239+
240+
241+
read
242+
243+
$PREFIX/initsql.sh
244+
rm $PGDATA/postmaster.pid
245+
246+
emcc -shared -o /srv/www/html/pygbag/pg/libpq.so \
247+
./src/interfaces/libpq/libpq.a \
248+
./src/port/libpgport.a \
249+
./src/common/libpgcommon.a
250+
251+
fi
252+
# TZ=UTC node ./src/bin/initdb/initdb -k -g -N -U postgres --pwfile=/date/git/pg/pw --locale=C --locale-provider=libc --pgdata=${PGDATA}
253+
else
254+
echo build failed
255+
fi

0 commit comments

Comments
 (0)