forked from jprendes/emception
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-cpython.sh
executable file
·122 lines (97 loc) · 3.14 KB
/
build-cpython.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
#!/bin/bash
set -e
export $(cat .env | xargs)
if [ "$(uname)" == "Darwin" ]; then
alias nproc="sysctl -n hw.ncpu"
fi
SRC=$(dirname $0)
BUILD="$1"
CPYTHON_SRC="$2"
if [ "$CPYTHON_SRC" == "" ]; then
CPYTHON_SRC=$(pwd)/upstream/cpython
fi
if [ "$BUILD" == "" ]; then
BUILD=$(pwd)/build
fi
# to be safe and guarantee the folder exists
mkdir -p "$BUILD"
SRC=$(realpath "$SRC")
BUILD=$(realpath "$BUILD")
CPYTHON_BUILD=$BUILD/cpython
CPYTHON_NATIVE=$BUILD/cpython-native
# If we don't have a copy of cpython, make one
if [ ! -d $CPYTHON_SRC/ ]; then
git clone https://github.com/python/cpython.git "$CPYTHON_SRC/"
pushd $CPYTHON_SRC/
echo "Checking out $CPYTHON_COMMIT"
git fetch origin "$CPYTHON_COMMIT"
echo "Resetting to $CPYTHON_COMMIT"
git reset --hard "$CPYTHON_COMMIT"
git clean -f -d
echo "Applying patch"
git apply $SRC/patches/cpython.patch
popd
fi
# todo: create a way to reconfigure if the folder exists
if [ ! -d $CPYTHON_NATIVE/ ]; then
# Rever the cpython patch in case this runs after the wasm version reconfigures it.
pushd $CPYTHON_SRC/
git fetch origin $CPYTHON_COMMIT
git reset --hard $CPYTHON_COMMIT
git clean -f -d
git apply $SRC/patches/cpython.patch
autoreconf -i
popd
mkdir -p $CPYTHON_NATIVE/
pushd $CPYTHON_NATIVE/
$CPYTHON_SRC/configure -C --host=x86_64-pc-linux-gnu --build=$($CPYTHON_SRC/config.guess) --with-suffix=""
make -j$(nproc)
popd
fi
# todo: create a way to reconfigure if the folder exists
if [ ! -d $CPYTHON_BUILD/ ]; then
# Patch cpython to add a module to evaluate JS code.
pushd $CPYTHON_SRC/
git fetch origin $CPYTHON_COMMIT
git reset --hard $CPYTHON_COMMIT
git clean -f -d
git apply $SRC/patches/cpython.patch
autoreconf -i
popd
mkdir -p $CPYTHON_BUILD/
pushd $CPYTHON_BUILD/
# for compatibility reasons check if filesystem is case sensitive. if it is case sensitive set PYTHONEXE to python, if not set it to python.exe
touch filename fileName
if [ $(du -a file* | wc -l | xargs) -eq 2 ]; then
PYTHONEXE=python
else
PYTHONEXE=python.exe
fi
rm -rf filename fileName
echo $PYTHONEXE
# Build cpython with asyncify support.
# Disable sqlite3, zlib and bzip2, which cpython enables by default
CONFIG_SITE=$CPYTHON_SRC/Tools/wasm/config.site-wasm32-emscripten \
LIBSQLITE3_CFLAGS=" " \
BZIP2_CFLAGS=" " \
LDFLAGS="\
-s ALLOW_MEMORY_GROWTH=1 \
-s EXPORTED_FUNCTIONS=_main,_free,_malloc \
-s EXPORTED_RUNTIME_METHODS=FS,PROXYFS,ERRNO_CODES,allocateUTF8 \
-lproxyfs.js \
--js-library=$SRC/emlib/fsroot.js \
" emconfigure $CPYTHON_SRC/configure -C \
--host=wasm32-unknown-emscripten \
--build=$($CPYTHON_SRC/config.guess) \
--with-emscripten-target=browser \
--disable-wasm-dynamic-linking \
--with-suffix=".mjs" \
--disable-wasm-preload \
--enable-wasm-js-module \
--with-build-python=$CPYTHON_NATIVE/$PYTHONEXE \
emmake make -j$(nproc)
popd
fi
pushd $CPYTHON_BUILD/
emmake make -j$(nproc)
popd