forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwasm-build.sh
executable file
·272 lines (193 loc) · 5.99 KB
/
wasm-build.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
#!/bin/bash
reset
PGROOT=/pgdata
PREFIX=$PGROOT
PGDATA=${PGROOT}/base
echo "
PREFIX=$PREFIX
PGDATA=$PGDATA
"
if echo "$@"|grep -q clean
then
make clean
rm $(find |grep \\.js$) $(find |grep \\.wasm$)
if echo "$@"|grep distclean
then
exit 0
fi
fi
if echo "$@"|grep -q patchwork
then
echo "
applying patchwork from https://github.com/pmp-p/postgres-patchwork/issues?q=is%3Aissue+is%3Aopen+label%3Apatch
"
wget -O- https://patch-diff.githubusercontent.com/raw/pmp-p/postgres-patchwork/pull/2.diff | patch -p1
wget -O- https://patch-diff.githubusercontent.com/raw/pmp-p/postgres-patchwork/pull/5.diff | patch -p1
wget -O- https://patch-diff.githubusercontent.com/raw/pmp-p/postgres-patchwork/pull/7.diff | patch -p1
sudo mkdir /pgdata
sudo chmod 777 /pgdata
exit 0
fi
mkdir -p ${PREFIX}
cat > ${PREFIX}/config.site <<END
END
# --with-blocksize=8 --with-segsize=1 --with-segsize-blocks=0 --with-wal-blocksize=8 \
#
CNF="./configure --prefix=${PREFIX} \
--disable-spinlocks --disable-atomics --without-zlib --disable-largefile --without-llvm \
--without-readline --without-icu \
--without-pam --disable-largefile --without-zlib --with-openssl=no \
--enable-debug"
if echo "$@" |grep native
then
make distclean
echo " =============== building host native =================== "
CONFIG_SITE=${PREFIX}/config.site CC="clang" CXX="clang++" CFLAGS="-m32" $CNF --with-template=linux && make && make install
if pushd ${PREFIX}
then
cp bin/postgres bin/postgres.native
cat > ${PREFIX}/bin/postgres <<END
#!/bin/bash
unset LD_PRELOAD
PGDATA=${PGDATA}
echo "# \$@" >> ${PREFIX}/journal.sql
tee -a ${PREFIX}/journal.sql | ${PREFIX}/bin/postgres.native \$@
END
chmod +x ${PREFIX}/bin/postgres
# 32bits build run initdb
unset LD_PRELOAD
rm -rf base/
> ${PREFIX}/journal.sql
TZ=UTC ${PREFIX}/bin/initdb -g -N -U postgres --pwfile=${PREFIX}/password -E UTF8 --locale=C --locale-provider=libc --pgdata=${PGDATA}
mkdir -p base-native/ base-wasm/
cp -rf base/* base-native/
popd
fi
sync
echo "initdb-native done"
make distclean 2>&1 >/dev/null
exit 0
fi
if echo "$@" |grep wasi
then
. /opt/python-wasm-sdk/wasisdk/wasisdk_env.sh
echo " =============== building wasi =================== "
#make distclean
CONFIG_SITE=${PREFIX}/config.site CC="wasi-c" CXX="wasi-c++" $CNF --cache-file=../config.wasi && make
# && make install
exit 0
fi
echo " =============== building wasm =================== "
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
# was erased, default pfx is sdk dir
export PREFIX=$PGROOT
# -lwebsocket.js -sPROXY_POSIX_SOCKETS -pthread -sPROXY_TO_PTHREAD
# CONFIG_SITE=$(pwd)/config.site EMCC_CFLAGS="--oformat=html" \
# --disable-shared is not supported
CONFIG_SITE==${PGDATA}/config.site emconfigure $CNF --with-template=emscripten --cache-file=../config.emsdk $@
sed -i 's|ZIC= ./zic|ZIC= zic|g' ./src/timezone/Makefile
mkdir bin
cat > bin/zic <<END
#!/bin/bash
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
node $(pwd)/src/timezone/zic \$@
END
> /tmp/disable-shared.log
# --disable-shared not supported so use a fake linker
cat > bin/disable-shared <<END
#!/bin/bash
echo "[\$(pwd)] $0 \$@" >> /tmp/disable-shared.log
for arg do
shift
if [ "\$arg" = "-o" ]
then
continue
fi
if echo "\$arg" | grep -q ^-
then
continue
fi
if echo "\$arg" | grep -q \\\\.o$
then
continue
fi
set -- "\$@" "\$arg"
done
touch \$@
END
# FIXME: workaround for /conversion_procs/ make
cp bin/disable-shared bin/o
chmod +x bin/zic bin/disable-shared bin/o
rm /srv/www/html/pygbag/pg/libpq.so /srv/www/html/pygbag/pg/libpq.so.map
if [ -f $(pwd)/wasmfix.h ]
then
export EMCC_CFLAGS="-include $(pwd)/wasmfix.h -sNODERAWFS"
else
export EMCC_CFLAGS="-sNODERAWFS"
fi
rm ./src/backend/postgres.wasm
# for zic and disable-shared
PATH=$(pwd)/bin:$PATH
if emmake make -j 6
then
if emmake make install
then
mv -vf ./src/bin/initdb/initdb.wasm ./src/backend/postgres.wasm ./src/backend/postgres.map ${PREFIX}/bin/
mv -vf ./src/bin/initdb/initdb ${PREFIX}/bin/initdb.js
mv -vf ./src/backend/postgres ${PREFIX}/bin/postgres.js
cat > ${PREFIX}/bin/postgres <<END
#!/bin/bash
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
PGDATA=${PGDATA} node ${PREFIX}/bin/postgres.js \$@
END
cat > ${PREFIX}/bin/initdb <<END
#!/bin/bash
. /opt/python-wasm-sdk/wasm32-bi-emscripten-shell.sh
node ${PREFIX}/bin/initdb.js \$@
END
chmod +x ${PREFIX}/bin/postgres ${PREFIX}/bin/initdb
echo "initdb for PGDATA=${PGDATA} "
fi
cat >$PREFIX/initdb.sh <<END
#!/bin/bash
rm -rf ${PGDATA} /tmp/initdb-*.log
TZ=UTC
${PREFIX}/initdb -k -g -N -U postgres --pwfile=${PREFIX}/password --locale=C --locale-provider=libc --pgdata=${PGDATA} 2> /tmp/initdb-\$\$.log
echo "Ready to run sql command through ${PREFIX}/postgres"
grep -v ^initdb.js /tmp/initdb-\$\$.log \\
| tail -n +4 \\
| head -n -1 \\
> /tmp/initdb-\$\$.sql
md5sum /tmp/initdb-\$\$.sql
${PREFIX}/postgres --boot -d 1 -c log_checkpoints=false -X 16777216 -k < /tmp/initdb-\$\$.sql 2>&1 | grep -v 'bootstrap>'
echo cleaning up sql journal
rm /tmp/initdb-\$\$.log /tmp/initdb-\$\$.sql
END
chmod +x $PREFIX/*.sh
$PREFIX/initdb.sh
echo "initdb done, now init sql default database"
if [ -f ${PGDATA}/postmaster.pid ]
then
cat > $PREFIX/initsql.sh <<END
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
END
chmod +x $PREFIX/*.sh
read
$PREFIX/initsql.sh
rm $PGDATA/postmaster.pid
fi
mkdir -p ${PREFIX}/lib
rm ${PREFIX}/lib/lib*.so.* ${PREFIX}/lib/libpq.so
emcc -shared -o ${PREFIX}/lib/libpq.so \
./src/interfaces/libpq/libpq.a \
./src/port/libpgport.a \
./src/common/libpgcommon.a
if [ -f /data/git/pg/local.sh ]
then
. /data/git/pg/local.sh
fi
echo "========================================================="
file ${PREFIX}/lib/lib*.so
else
echo build failed
fi