forked from electric-sql/pglite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgbuild.sh
342 lines (251 loc) · 9.02 KB
/
pgbuild.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
echo "
pgbuild:begin
CC_PGLITE=$CC_PGLITE
"
mkdir -p build/postgres
pushd build/postgres
# create empty package.json to avoid emsdk node conflicts
# with root package.json of project
echo "{}" > package.json
if $CI
then
echo "CI : using build cache"
else
if [ -f Makefile ]
then
echo "Cleaning up previous build ..."
make distclean 2>&1 > /dev/null
fi
fi
# TODO: --with-libxml xml2 >= 2.6.23
# TODO: --with-libxslt add to sdk
# --disable-atomics https://github.com/WebAssembly/threads/pull/147 "Allow atomic operations on unshared memories"
if ${WASI}
then
echo "WASI BUILD: turning off xml/xslt support"
XML2=""
UUID=""
BUILD=wasi
export MAIN_MODULE="-lwasi-emulated-getpid -lwasi-emulated-mman -lwasi-emulated-signal -lwasi-emulated-process-clocks"
else
if $CI
then
# do not build obsolete ext xml2 on CI
XML2="--with-zlib --with-libxml"
else
XML2="--with-zlib --with-libxml --with-libxslt"
fi
UUID="--with-uuid=ossp"
BUILD=emscripten
export MAIN_MODULE="-sMAIN_MODULE=1"
fi
# --with-libxml does not fit with --without-zlib
export XML2_CONFIG=$PREFIX/bin/xml2-config
export ZIC=$(pwd)/bin/zic
cp ${PGSRC}/./src/include/port/wasm_common.h /tmp/pglite/include/wasm_common.h
CNF="${PGSRC}/configure --prefix=${PGROOT} \
--cache-file=${PGROOT}/config.cache.${BUILD} \
--disable-spinlocks --disable-largefile --without-llvm \
--without-pam --disable-largefile --with-openssl=no \
--without-readline --without-icu \
${UUID} ${XML2} ${PGDEBUG}"
echo " ==== building wasm MVP:$MVP Debug=${PGDEBUG} with opts : $@ == "
# -lwebsocket.js -sPROXY_POSIX_SOCKETS -pthread -sPROXY_TO_PTHREAD
# CONFIG_SITE=$(pwd)/config.site EMCC_CFLAGS="--oformat=html" \
# crash clang CFLAGS=-Wno-error=implicit-function-declaration
mkdir -p bin
if $WASI
then
export EXT=wasi
cat > ${PGROOT}/config.site <<END
ac_cv_exeext=.wasi
END
cat > bin/zic <<END
#!/bin/bash
#. /opt/python-wasm-sdk/wasm32-wasi-shell.sh
TZ=UTC PGTZ=UTC wasi-run $(pwd)/src/timezone/zic.wasi \$@
END
else
export EXT=wasm
cat > ${PGROOT}/config.site <<END
ac_cv_exeext=.cjs
END
cat > bin/zic <<END
#!/bin/bash
#. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
TZ=UTC PGTZ=UTC node $(pwd)/src/timezone/zic.cjs \$@
END
fi
if EM_PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig CONFIG_SITE=${PGROOT}/config.site emconfigure $CNF --with-template=$BUILD
then
echo configure ok
else
echo configure failed
exit 76
fi
if grep -q MAIN_MODULE ${PGSRC}/src/backend/Makefile
then
echo "dyld server patch ok"
else
echo "missing server dyld patch"
exit 273
fi
if $WASI
then
sed -i 's|-pthread||g' ./src/Makefile.global
fi
# --disable-shared not supported so be able to use a fake linker
> /tmp/disable-shared.log
cat > bin/emsdk-shared <<END
#!/bin/bash
echo "[\$(pwd)] $0 \$@" >> /tmp/disable-shared.log
# shared build
\${PG_LINK:-emcc} -L${PREFIX}/lib -DPREFIX=${PGROOT} -shared -sSIDE_MODULE=1 \$@ -Wno-unused-function
END
cat > bin/wasi-shared <<END
#!/bin/bash
echo "[\$(pwd)] $0 \$@" >> /tmp/disable-shared.log
# shared build
echo ===================================================================================
wasi-c -L${PREFIX}/lib -DPREFIX=${PGROOT} -shared \$@ -Wno-unused-function
echo ===================================================================================
END
chmod +x bin/zic bin/wasi-shared bin/emsdk-shared
# for zic and emsdk-shared/wasi-shared called from makefile
export PATH=$(pwd)/bin:$PATH
EMCC_NODE="-sEXIT_RUNTIME=1 -DEXIT_RUNTIME -sNODERAWFS -sENVIRONMENT=node"
# -lwebsocket.js"
# -sWEBSOCKET_SUBPROTOCOL=binary -sWEBSOCKET_URL=ws://127.0.0.1:25432"
# -lwebsocket.js
# -sPROXY_POSIX_SOCKETS -pthread -sPROXY_TO_PTHREAD $EMCC_CFLAGS"
# -sWASMFS
EMCC_ENV="${EMCC_NODE} -sFORCE_FILESYSTEM=0"
EMCC_ENV="${EMCC_NODE} -sERROR_ON_UNDEFINED_SYMBOLS"
# only required for static initdb
EMCC_CFLAGS="-sERROR_ON_UNDEFINED_SYMBOLS=1 ${CC_PGLITE}"
EMCC_CFLAGS="${EMCC_CFLAGS} -sTOTAL_MEMORY=${TOTAL_MEMORY} -sSTACK_SIZE=5MB -sALLOW_TABLE_GROWTH -sALLOW_MEMORY_GROWTH -sGLOBAL_BASE=${CMA_MB}MB"
EMCC_CFLAGS="${EMCC_CFLAGS} -DPREFIX=${PGROOT}"
EMCC_CFLAGS="${EMCC_CFLAGS} -Wno-macro-redefined -Wno-unused-function"
WASI_CFLAGS="${CC_PGLITE} -DPREFIX=${PGROOT} -DPYDK=1 -Wno-declaration-after-statement -Wno-macro-redefined -Wno-unused-function -Wno-missing-prototypes -Wno-incompatible-pointer-types"
ZIC=${ZIC:-$(realpath bin/zic)}
if EMCC_CFLAGS="${EMCC_ENV} ${EMCC_CFLAGS}" WASI_CFLAGS="$WASI_CFLAGS" emmake make $BUILD=1 -j $(nproc) 2>&1 > /tmp/build.log
then
echo build ok
if $WASI
then
echo "will need to link postgres to extensions statically"
cp -vf src/backend/postgres src/backend/postgres.wasi
else
cp -vf src/backend/postgres src/backend/postgres.cjs
fi
# if running a 32bits zic from current build
unset LD_PRELOAD
if EMCC_CFLAGS="${EMCC_ENV} ${EMCC_CFLAGS}" WASI_CFLAGS="$WASI_CFLAGS" emmake make $BUILD=1 install 2>&1 > /tmp/install.log
then
echo install ok
if $WASI
then
# remove unlinked server
rm src/backend/postgres src/backend/postgres.wasi $PGROOT/bin/postgres $PGROOT/bin/postgres.wasi
pushd ../..
chmod +x ./cibuild/linkwasi.sh
WASI_CFLAGS="$WASI_CFLAGS" ./cibuild/linkwasi.sh || exit 190
popd
cp src/backend/postgres.wasi $PGROOT/bin/ || exit 205
fi
pushd ${PGROOT}
find . -type f | grep -v plpgsql > ${PGROOT}/pg.installed
popd
goback=$(pwd)
popd
python3 cibuild/pack_extension.py builtin
pushd $goback
pushd ${PGROOT}
find . -type f > ${PGROOT}/pg.installed
popd
else
cat /tmp/install.log
echo "install failed"
exit 225
fi
else
cat /tmp/build.log
echo "build failed"
exit 230
fi
# wip
mv -vf ./src/bin/psql/psql.$EXT ./src/bin/pg_config/pg_config.$EXT ${PGROOT}/bin/
mv -vf ./src/bin/pg_dump/pg_restore.$EXT ./src/bin/pg_dump/pg_dump.$EXT ./src/bin/pg_dump/pg_dumpall.$EXT ${PGROOT}/bin/
mv -vf ./src/bin/pg_resetwal/pg_resetwal.$EXT ./src/bin/initdb/initdb.$EXT ./src/backend/postgres.$EXT ${PGROOT}/bin/
python3 > ${PGROOT}/PGPASSFILE <<END
USER="${PGPASS:-postgres}"
PASS="${PGUSER:-postgres}"
md5pass = "md5" + __import__('hashlib').md5(USER.encode() + PASS.encode()).hexdigest()
print(f"localhost:5432:postgres:{USER}:{md5pass}")
USER="login"
PASS="password"
md5pass = "md5" + __import__('hashlib').md5(USER.encode() + PASS.encode()).hexdigest()
print(f"localhost:5432:postgres:{USER}:{md5pass}")
END
if [ -f $PGROOT/bin/pg_config.$EXT ]
then
echo pg_config installed
else
echo "pg_config build failed"; exit 243
fi
cat > ${PGROOT}/bin/pg_config <<END
#!/bin/bash
$(which node) ${PGROOT}/bin/pg_config.cjs \$@
END
cat > ${PGROOT}/postgres <<END
#!/bin/bash
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
TZ=UTC PGTZ=UTC PGDATA=${PGDATA} $(which node) ${PGROOT}/bin/postgres.cjs \$@
END
# remove the abort but stall prompt
# 2>&1 | grep --line-buffered -v ^var\\ Module
# force node wasm version
cp -vf ${PGROOT}/postgres ${PGROOT}/bin/postgres
cat > ${PGROOT}/initdb <<END
#!/bin/bash
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
TZ=UTC PGTZ=UTC $(which node) ${PGROOT}/bin/initdb.cjs \$@
END
chmod +x ${PGROOT}/postgres ${PGROOT}/bin/postgres
chmod +x ${PGROOT}/initdb ${PGROOT}/bin/initdb
# for extensions building
chmod +x ${PGROOT}/bin/pg_config
echo "initdb for PGDATA=${PGDATA} "
# create empty db hack
cat >$PGROOT/initdb.sh <<END
#!/bin/bash
rm -rf ${PGDATA} /tmp/initdb-* ${PGROOT}/wal/*
export TZ=UTC
export PGTZ=UTC
SQL=/tmp/initdb-\$\$
# TODO: --waldir=${PGROOT}/wal
> /tmp/initdb.txt
${PGROOT}/initdb --no-clean --wal-segsize=1 -g $LANG $CRED --pgdata=${PGDATA}
mv /tmp/initdb.boot.txt \${SQL}.boot.sql
mv /tmp/initdb.single.txt \${SQL}.single.sql
if \${CI:-false}
then
cp -vf \$SQL ${PGROOT}/\$(md5sum \$SQL|cut -c1-32).sql
fi
# --wal-segsize=1 -> -X 1048576
# CKSUM_B -k --data-checksums
# 2024-04-24 05:53:28.121 GMT [42] WARNING: page verification failed, calculated checksum 5487 but expected 0
# 2024-04-24 05:53:28.121 GMT [42] FATAL: invalid page in block 0 of relation base/1/1259
CMD="${PGROOT}/postgres --boot -D ${PGDATA} -d 3 $PGOPTS -X 1048576"
echo "\$CMD < \$SQL.boot.sql"
\$CMD < \$SQL.boot.sql 2>&1 \\
| grep -v --line-buffered 'bootstrap> boot' \\
| grep -v --line-buffered 'index'
echo "
\$(md5sum /tmp/initdb-\$\$.*.sql)
boot done
"
END
popd
echo "pgbuild:end
"