forked from pygame-web/pygbag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-loader.sh
executable file
·404 lines (283 loc) · 8.98 KB
/
build-loader.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
. scripts/vendoring.sh
. ${CONFIG:-$SDKROOT/config}
if [ -f vendor/vendor.sh ]
then
echo " vendor build"
if ${ABI3:-false}
then
echo " vendor build (abi3) $PYBUILD"
if echo $PYBUILD|grep -v -q 3.12$
then
echo "abi3 vendor build only, skipping $PYBUILD"
exit 0
fi
fi
fi
ln -s $(pwd)/src/pygbag $(pwd)/pygbag
pushd src/pygbag/support
cp -r _xterm_parser ${SDKROOT}/prebuilt/emsdk/common/site-packages/
#cp pygbag_*.py readline.py typing_extensions.py ${SDKROOT}/prebuilt/emsdk/common/site-packages/
cp typing_extensions.py ${SDKROOT}/prebuilt/emsdk/common/site-packages/
popd
DISTRO=cpython
# version independant modules
cp -rf ${SDKROOT}/prebuilt/emsdk/common/* ${SDKROOT}/prebuilt/emsdk/${PYBUILD}/
# pre populated site-packages
export REQUIREMENTS=${SDKROOT}/prebuilt/emsdk/${PYBUILD}/site-packages
# and wasm libraries
export DYNLOAD=${SDKROOT}/prebuilt/emsdk/${PYBUILD}/lib-dynload
. $SDKROOT/emsdk/emsdk_env.sh
echo "
* building loader $(pwd) for ${VENDOR} / ${PACKAGES}
PYBUILD=$PYBUILD python${PYMAJOR}${PYMINOR}
EMFLAVOUR=$EMFLAVOUR
EMSDK=$EMSDK
SDKROOT=$SDKROOT
PYTHONPYCACHEPREFIX=$PYTHONPYCACHEPREFIX
HPY=$HPY
LD_VENDOR=$LD_VENDOR
" 1>&2
EMPIC=/opt/python-wasm-sdk/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/pic
if echo -n $PYBUILD|grep -q 13$
then
MIMALLOC="-I/opt/python-wasm-sdk/emsdk/upstream/emscripten/system/lib/mimalloc/include"
else
MIMALLOC=""
fi
SUPPORT_FS=""
mkdir -p $DIST_DIR/${DISTRO}${PYMAJOR}${PYMINOR}
rm $DIST_DIR/${DISTRO}${PYMAJOR}${PYMINOR}/main.* 2>/dev/null
# git does not keep empty dirs
mkdir -p tests/assets tests/code
ALWAYS_CODE=$(realpath tests/code)
#ALWAYS_ASSETS=$(realpath tests/assets)
ALWAYS_ASSETS=$(realpath assets/cpython)
for asset in readline pyodide pygbag_app pygbag_fsm pygbag_host pygbag_ui pygbag_ux
do
[ -f ${ALWAYS_ASSETS}/${asset}.py ] || ln ./src/pygbag/support/${asset}.py ${ALWAYS_ASSETS}/${asset}.py
done
# crosstools, aio and simulator most likely from pygbag
if [ -d src/pygbag/support/cross ]
then
CROSS=$(realpath src/pygbag/support/cross)
SUPPORT_FS="$SUPPORT_FS --preload-file ${CROSS}@/data/data/org.python/assets/site-packages"
else
echo "
WARNING : no cross support lib found
maybe have a look at pygbag module support subfolder
"
fi
if [ -d /data/git/platform_wasm ]
then
cp -Rf /data/git/platform_wasm ./
else
if [ -d platform_wasm ]
then
pushd platform_wasm
git pull
popd
else
git clone https://github.com/pygame-web/platform_wasm
fi
fi
export PATCH_FS="--preload-file $(realpath platform_wasm/platform_wasm)@/data/data/org.python/assets/site-packages/platform_wasm"
# =2 will break pyodide module reuses
LOPTS="-sMAIN_MODULE=1"
# O0/g3 is much faster to build and easier to debug
echo " ************************************"
if [ -f dev ]
then
export COPTS="-O1 -g1 -fPIC"
echo " building DEBUG $COPTS"
LOPTS="$LOPTS -sASSERTIONS=0"
ALWAYS_FS="--preload-file ${ALWAYS_CODE}@/data/data/org.python/assets"
else
export COPTS="-Os -g0 -fPIC"
echo " building RELEASE $COPTS"
LOPTS="$LOPTS -sASSERTIONS=0 -sLZ4"
ALWAYS_FS=""
fi
echo " ************************************"
ALWAYS_FS="$ALWAYS_FS --preload-file ${ALWAYS_ASSETS}@/data/data/org.python/assets"
# pre populated site-packages given by env
# REQUIREMENTS
# DYNLOAD
# runtime patches on known modules for specific platform
# applies to prebuilt/emsdk/site-packages at preload stage.
PLATFORM=$(realpath support/__EMSCRIPTEN__)
echo "
site-packages=${PLATFORM}
crosstoosl=${CROSS}
COPTS=$COPTS
LOPTS=$LOPTS
ALWAYS_ASSETS=$ALWAYS_ASSETS
ALWAYS_CODE=$ALWAYS_CODE
REQUIREMENTS=$REQUIREMENTS
DYNLOAD=$DYNLOAD
"
if false
then
FINAL_OPTS="$COPTS --proxy-to-worker -s ENVIRONMENT=web,worker"
MODE="worker"
WORKER_STATUS="using worker"
else
# https://github.com/emscripten-core/emscripten/issues/10086
# EXPORT_NAME does not affect generated html
#
FINAL_OPTS="$COPTS"
MODE="main"
WORKER_STATUS="not using worker"
fi
if false
then
FINAL_OPTS="$FINAL_OPTS -s MODULARIZE=1"
FINAL_OPTS="$FINAL_OPTS -s EXPORT_NAME=\"${EXE}\""
FINAL_OPTS="$FINAL_OPTS -s EXPORTED_RUNTIME_METHODS=[\"FS\"]"
fi
# pack the minimal stdlib for current implicit requirements
# see inside ./scripts/build-rootfs.sh to view them
./scripts/build-rootfs.sh
PYDIR=${SDKROOT}/devices/emsdk/usr/include/python${PYBUILD}
# gnu99 not c99 for EM_ASM() js calls functions.
if $STATIC
then
echo "building static loader"
else
export PACKAGES=${BUILD_STATIC:-emsdk hpy _ctypes}
echo "building dynamic loader
with static parts : ${BUILD_STATIC}
"
fi
for lib in $PACKAGES
do
CPY_CFLAGS="$CPY_CFLAGS -DPYDK_$lib=1"
done
echo CPY_CFLAGS=$CPY_CFLAGS
#\
# -I/opt/python-wasm-sdk/emsdk/upstream/emscripten/cache/sysroot/include/freetype2 -lfreetype\
# -lopenal \
#\
if emcc -fPIC -std=gnu99 -D__PYDK__=1 -DNDEBUG $MIMALLOC $CPY_CFLAGS $CF_SDL $CPOPTS \
-c -fwrapv -Wall -Werror=implicit-function-declaration -fvisibility=hidden \
-I${PYDIR}/internal -I${PYDIR} -I./support -I./external/hpy/hpy/devel/include -DPy_BUILD_CORE\
-o build/${MODE}.o support/__EMSCRIPTEN__-pymain.c
then
STDLIBFS="--preload-file build/stdlib-rootfs/python${PYBUILD}@/usr/lib/python${PYBUILD}"
# \
# --preload-file /usr/share/terminfo/x/xterm@/usr/share/terminfo/x/xterm \
# --preload-file ${REQUIREMENTS}@/data/data/org.python/assets/site-packages \
# --preload-file ${ROOT}/support/xterm@/etc/termcap \
# TODO: test -sWEBGL2_BACKWARDS_COMPATIBILITY_EMULATION
#
LDFLAGS="-sUSE_GLFW=3 -sUSE_WEBGL2 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sOFFSCREENCANVAS_SUPPORT=1 -sFULL_ES2 -sFULL_ES3"
LDFLAGS="$LDFLAGS -lsqlite3"
LDFLAGS="-L${SDKROOT}/devices/emsdk/usr/lib $LDFLAGS -lssl -lcrypto -lffi -lbz2 -lz -ldl -lm"
LINKPYTHON="python mpdec expat"
if echo $PYBUILD|grep -q 3.12
then
LINKPYTHON="Hacl_Hash_SHA2 $LINKPYTHON"
else
if echo $PYBUILD|grep -q 3.13
then
LINKPYTHON="Hacl_Hash_SHA2 $LINKPYTHON"
fi
fi
for lib in $LINKPYTHON
do
cpylib=${SDKROOT}/prebuilt/emsdk/lib${lib}${PYBUILD}.a
if [ -f $cpylib ]
then
LDFLAGS="$LDFLAGS $cpylib"
fi
done
for lib in $PACKAGES
do
cpylib=${SDKROOT}/prebuilt/emsdk/lib${lib}${PYBUILD}.a
LDFLAGS="$LDFLAGS $cpylib"
done
echo "
LDFLAGS=$LDFLAGS
" 1>&2
# -std=gnu99 -std=c++23
# EXTRA_EXPORTED_RUNTIME_METHODS => EXPORTED_RUNTIME_METHODS after 3.1.52
cat > final_link.sh <<END
#!/bin/bash
emcc \\
$FINAL_OPTS \\
$LOPTS \\
-D__PYDK__=1 -DNDEBUG \\
-sTOTAL_MEMORY=256MB -sSTACK_SIZE=4MB -sALLOW_TABLE_GROWTH -sALLOW_MEMORY_GROWTH \\
-sEXPORTED_RUNTIME_METHODS=FS \\
$CF_SDL \\
--use-preload-plugins \\
$STDLIBFS \\
$ALWAYS_FS \\
$SUPPORT_FS \\
$PATCH_FS \\
--preload-file ${DYNLOAD}@/usr/lib/python${PYBUILD}/lib-dynload \\
--preload-file ${REQUIREMENTS}@/data/data/org.python/assets/site-packages \\
-o ${DIST_DIR}/${DISTRO}${PYMAJOR}${PYMINOR}/${MODE}.js build/${MODE}.o \\
$LDFLAGS -lembind
# -lfreetype
# --bind -fno-rtti
# -sERROR_ON_UNDEFINED_SYMBOLS=0
# -sGL_ENABLE_GET_PROC_ADDRESS
# --bind -fno-rtti
END
chmod +x ./final_link.sh
if ./final_link.sh
then
rm build/${MODE}.o
du -hs ${DIST_DIR}/*
echo Total
echo _________
if $CI
then
if [ -f /pp ]
then
USECP=false
else
USECP=true
fi
else
USECP=false
fi
if $USECP
then
cp -R static/* ${DIST_DIR}/
cp src/pygbag/support/cpythonrc.py ${DIST_DIR}/cpythonrc.py
# for simulator
cp src/pygbag/support/cpythonrc.py ${SDKROOT}/support/
else
[ -f ${DIST_DIR}/cpythonrc.py ] || ln src/pygbag/support/cpythonrc.py ${DIST_DIR}/cpythonrc.py
pushd static
for fn in *
do
if [ -f $fn ]
then
[ -f ${DIST_DIR}/$fn ] && continue
ln $fn ${DIST_DIR}/$fn
continue
fi
[ -L ${DIST_DIR}/$fn ] && continue
ln -s $(pwd)/$fn ${DIST_DIR}/$fn
done
popd
fi
#echo "
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# emsdk tot js gen temp fix
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#"
# sed -i 's/_glfwSetWindowContentScaleCallback_sig=iii/_glfwSetWindowContentScaleCallback_sig="iii"/g' \
# ${DIST_DIR}/python${PYMAJOR}${PYMINOR}/${MODE}.js
du -hs ${DIST_DIR}/*
else
echo "pymain+loader linking failed"
exit 178
fi
else
echo "pymain compilation failed"
exit 182
fi