-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
python-wasm-sdk.sh
executable file
·359 lines (267 loc) · 9.37 KB
/
python-wasm-sdk.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
reset
# TODO: check how dbg tools work with default settings
# https://developer.chrome.com/blog/wasm-debugging-2020/
mkdir -p /tmp/sdk-bin
export PATH=/tmp/sdk-bin:$PATH
which command || cat > /tmp/sdk-bin/command << END
#!/bin/bash
shift
which $1
END
chmod +x /tmp/sdk-bin/command
if which python3
then
SYS_PYTHON=$(which python3)
else
SYS_PYTHON=$(which python)
fi
DISTRIB_RELEASE=${DISTRIB_RELEASE:-any}
# is it linux enough ?
if [ -f /etc/lsb-release ]
then
. /etc/lsb-release
export PLATFORM=linux
else
# or not
export DISTRIB_ID=$($SYS_PYTHON -E -c "print(__import__('sysconfig').get_config_var('HOST_GNU_TYPE'))")
export PLATFORM=$($SYS_PYTHON -E -c "print(__import__('sys').platform)")
echo no /etc/lsb-release found, please identify platform $PLATFORM : \"${DISTRIB_ID}-${DISTRIB_RELEASE}\" or hit enter to continue
read
fi
export DISTRIB="${DISTRIB_ID}-${DISTRIB_RELEASE}"
export SDKROOT=${SDKROOT:-/tmp/sdk}
# default is behave like a CI
export CI={CI:-true}
# maybe have ci flavours later
export CIVER=${CIVER:-$DISTRIB}
if echo $0|grep -q python-wasm-sdk\.sh
then
echo " * adding emsdk to wasm-sdk"
emsdk=true
wasisdk=${wasisdk:-false}
nimsdk=${nimsdk:-false}
else
emsdk=false
BUILDS=3.13
wasisdk=true
gosdk=${gosdk:-false}
rustsdk=${rustdsk:-false}
nimsdk=${nimsdk:-false}
fi
for lang in wasisdk gosdk rustsdk nimsdk
do
if ${!lang:-false}
then
echo " * adding ${lang} to wasm-sdk"
fi
done
if mkdir -p ${SDKROOT}
then
echo "Assuming destination $SDKROOT is ready"
else
sudo mkdir -p ${SDKROOT}
sudo chmod 777 ${SDKROOT}
fi
ORIGIN=$(pwd)
BUILDS=${BUILDS:-"3.12 3.13"}
for PYBUILD in $BUILDS
do
cd "$ORIGIN"
if [ -f ${SDKROOT}/dev ]
then
echo "${SDKROOT}/dev found : using build cache"
else
echo "doing a clean build"
if [ -d ${SDKROOT}/go ]
then
chown -R u+rwx ${SDKROOT}/build ${SDKROOT}/go
fi
rm -rf ${SDKROOT}/* ${SDKROOT}/.??*
fi
cp -Rf * ${SDKROOT}/
chmod +x ${SDKROOT}/scripts/*
if cd ${SDKROOT}/
then
mkdir -p build/pycache
export PYTHONDONTWRITEBYTECODE=1
# make install cpython will force bytecode generation
export PYTHONPYCACHEPREFIX="$(realpath build/pycache)"
# reset config
unset CONFIG_ENV
. ${CONFIG:-config}
cd ${SDKROOT}
if [ ${PYMINOR} -ge 13 ]
then
GILOPT=true
if ${Py_GIL_DISABLED:-false}
then
GIL="--disable-gil --with-mimalloc --disable-experimental-jit"
else
Py_GIL_DISABLED=false
GIL="--without-mimalloc --disable-experimental-jit"
fi
else
GILOPT=false
GIL=""
fi
export GILOPT
export Py_GIL_DISABLED
if [ -f $HPY ]
then
echo " re-using host python HPY=$HPY"
else
cd ${SDKROOT}
. scripts/cpython-fetch.sh
cd ${SDKROOT}
# generic wasm patchwork
. support/__EMSCRIPTEN__.sh
. scripts/cpython-build-host.sh 2>&1 >/tmp/python-wasm-sdk.log
[ -f $HPY ] || exit 100
. scripts/cpython-build-host-deps.sh > /dev/null
fi
[ -f $HPY ] || exit 106
cat > /opt/python-wasm-sdk/devices/$(arch)/usr/bin/py <<END
#!/bin/bash
export XDG_SESSION_TYPE=x11
export SDKROOT=${SDKROOT}
export PYTHONPATH=/data/git/pygbag/src:/data/git/platform_wasm:${PYTHONPATH}
export PYTHONPYCACHEPREFIX=$PYTHONPYCACHEPREFIX
export HOME=${SDKROOT}
export PATH=${SDKROOT}/devices/$(arch)/usr/bin:\$PATH
export LD_LIBRARY_PATH=${SDKROOT}/devices/$(arch)/usr/lib:${SDKROOT}/devices/$(arch)/usr/lib64:$LD_LIBRARY_PATH
${SDKROOT}/devices/$(arch)/usr/bin/python\${PYBUILD:-$PYBUILD} \$@
END
chmod +x /opt/python-wasm-sdk/devices/$(arch)/usr/bin/py
# always install wasmtime because wasm-objdump needs it.
if [ -f ${SDKROOT}/devices/$(arch)/usr/bin/wastime ]
then
echo "keeping installed wasmtime and wasi binaries"
else
#wget https://github.com/bytecodealliance/wasmtime/releases/download/v22.0.0/wasmtime-v22.0.0-x86_64-linux.tar.xz
# wget https://github.com/bytecodealliance/wasmtime/releases/download/v26.0.1/wasmtime-v26.0.1-$(arch)-$(PLATFORM).tar.xz \
# TODO: window only has a zip archive, better use wasmtime-py instead.
wget https://github.com/bytecodealliance/wasmtime/releases/download/v27.0.0/wasmtime-v27.0.0-$(arch)-${PLATFORM}.tar.xz \
-O-|xzcat|tar xfv -
mv -vf $(find wasmtime*|grep /wasmtime$) ${SDKROOT}/devices/$(arch)/usr/bin
fi
if $emsdk
then
cd ${SDKROOT}
export TARGET=emsdk
mkdir -p src build ${SDKROOT}/devices/${TARGET} ${SDKROOT}/prebuilt/${TARGET}
if [ -f /tmp/emsdk.tar ]
then
echo "
===========================================================================
Using emsdk cache from :
$(cat /tmp/sdk/emsdk.version)
===========================================================================
"
pushd /
tar xfp /tmp/emsdk.tar
mkdir -p ${SDKROOT}/src ${SDKROOT}/build
popd
fi
# use ./ or emsdk will pollute env
./scripts/emsdk-fetch.sh > /dev/null
echo " ---------- building cpython wasm support ${PYBUILD} ${CIVER} -----------" 1>&2
if [ -f /tmp/emsdk.tar ]
then
echo " using cached cpython-build-emsdk-deps"
else
if ./scripts/cpython-build-${TARGET}-deps.sh
then
# if $CI
if false
then
pushd /
tar \
--exclude=${SDKROOT}/devices/*/usr/bin/*3.1* \
--exclude=${SDKROOT}/devices/*/usr/lib/python3.1? \
--exclude=${SDKROOT}/devices/*/usr/include/python3.1? \
--exclude=${SDKROOT}/config \
--exclude=${SDKROOT}/python-was?-sdk.sh \
--exclude=${SDKROOT}/python3-was? \
--exclude=${SDKROOT}/scripts/* \
--exclude=${SDKROOT}/sources.* \
--exclude=${SDKROOT}/build \
--exclude=${SDKROOT}/src \
-cpR $SDKROOT > /tmp/emsdk.tar
date "+%d-%m-%4Y" > /tmp/sdk/emsdk.version
popd
fi
else
echo " cpython-build-emsdk-deps failed" 1>&2
exit 213
fi
fi
echo " ------------ building cpython wasm ${PYBUILD} ${CIVER} ----------------" 1>&2
if ./scripts/cpython-build-${TARGET}.sh > /dev/null
then
echo " --------- adding some usefull pkg ${PYBUILD} ${CIVER} ---------" 1>&2
./scripts/cpython-build-${TARGET}-prebuilt.sh || exit 223
# experimental stuff
chmod +x sources.plus/*.sh
for extra in sources.plus/*.sh
do
./$extra
done
echo "
==========================================================
stripping emsdk ${PYBUILD} ${CIVER}
========================================================== " 1>&2
rm -rf ${SDKROOT}/emsdk/upstream/emscripten/cache/ports*
rm -rf ${SDKROOT}/emsdk/upstream/emscripten/tests
else
echo " cpython-build-emsdk failed" 1>&2
exit 239
fi
fi
# compile wasi last because of configure patches
if $wasisdk
then
cd ${SDKROOT}
export TARGET=wasi
mkdir -p src build ${SDKROOT}/devices/wasisdk ${SDKROOT}/prebuilt/wasisdk
# do not source to protect env
./scripts/cpython-build-wasisdk.sh
> ${SDKROOT}/python3-${TARGET}
# ROOT=/opt/python-wasm-sdk SDKROOT=/opt/python-wasm-sdk
# HOST_PREFIX=/opt/python-wasm-sdk/devices/$(arch)/usr
> ${SDKROOT}/wasm32-${TARGET}-shell.sh
CPU=wasm32
CPU=$CPU TARGET=$TARGET PYDK_PYTHON_HOST_PLATFORM=${CPU}-${TARGET} \
PYDK_SYSCONFIG_PLATFORM=${CPU}-${TARGET} \
PREFIX=/opt/python-wasm-sdk/devices/${TARGET}sdk/usr \
./scripts/make-shells.sh
cat >> $ROOT/${CPU}-${TARGET}-shell.sh <<END
#!/bin/bash
. ${WASISDK}/wasisdk_env.sh
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="[PyDK:${TARGET}] \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]\$ "
END
chmod +x ${SDKROOT}/python3-${TARGET} ${SDKROOT}/wasm32-${TARGET}-shell.sh
fi
if $nimsdk
then
${SDKROOT}/lang/nimsdk.sh
fi
mkdir -p /tmp/sdk
# pack extra build scripts
pushd /
tar -cpPRz \
${SDKROOT}/scripts/emsdk-extra.sh \
${SDKROOT}/scripts/emsdk-fetch.sh \
${SDKROOT}/sources.extra/* > /tmp/sdk/sdk-extra.tar.gz
# pack sdl as minimal prebuilt tar, and use lz4 compression on it
. ${SDKROOT}/scripts/pack-sdk.sh
popd
else
echo "cd failed" 1>&2
exit 208
fi
done
exit 0