-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·105 lines (91 loc) · 2.85 KB
/
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
#!/usr/bin/env bash
# TODO: https://emscripten.org/docs/porting/Debugging.html
set -xeu
PHP_VERSION=8.2.0RC5
PHP_PATH=php-$PHP_VERSION
AST_PATH=ast-1.1.0
PHAN_VERSION=5.4.1
PHAN_PATH=phan-$PHAN_VERSION.phar
if ! type emconfigure 2>/dev/null >/dev/null ; then
echo "emconfigure not found. Install emconfigure and add it to your path (e.g. source emsdk/emsdk_env.sh)"
exit 1
fi
echo "Get PHP source"
if [ ! -d $PHP_PATH ]; then
if [ ! -e $PHP_PATH.tar.xz ]; then
#wget https://www.php.net/distributions/$PHP_PATH.tar.xz
wget https://downloads.php.net/~pierrick/$PHP_PATH.tar.xz
fi
tar xf $PHP_PATH.tar.xz
fi
echo "Apply error handler patch"
cp main.c $PHP_PATH/main/main.c
echo "Get Phan phar"
if [ ! -e $PHAN_PATH ]; then
wget https://github.com/phan/phan/releases/download/$PHAN_VERSION/phan.phar -O $PHAN_PATH
fi
if [ ! -d "$PHP_PATH/ext/ast" ]; then
if [ ! -f "$AST_PATH.tgz" ]; then
wget https://pecl.php.net/get/$AST_PATH.tgz -O $AST_PATH.tgz
fi
tar zxf $AST_PATH.tgz
mv "$AST_PATH" "$PHP_PATH/ext/ast"
fi
# Check that the phar is not corrupt
php $PHAN_PATH --version || exit 1
cp $PHAN_PATH $PHP_PATH/
echo "Configure"
# https://emscripten.org/docs/porting/Debugging.html
# -g4 can be used to generate source maps for debugging C crashes
# NOTE: If -g4 is used, then firefox can require a lot of memory to load the resulting file.
export CFLAGS='-O3 -DZEND_MM_ERROR=0'
cd $PHP_PATH
# Configure this with a minimal set of extensions, statically compiling the third-party ast library.
# Run buildconf so that ast will a valid configure option
./buildconf --force
emconfigure ./configure \
--disable-all \
--disable-cgi \
--disable-cli \
--disable-rpath \
--disable-phpdbg \
--with-valgrind=no \
--without-pear \
--without-valgrind \
--without-pcre-jit \
--with-layout=GNU \
--enable-ast \
--enable-bcmath \
--enable-ctype \
--enable-embed=static \
--enable-filter \
--enable-json \
--enable-phar \
--enable-mbstring \
--disable-mbregex \
--disable-fiber-asm \
--enable-tokenizer
echo "Build"
# -j5 seems to work for parallel builds
emmake make clean
emmake make -j5
rm -rf out
mkdir -p out
emcc $CFLAGS -I . -I Zend -I main -I TSRM/ ../pib_eval.c -c -o pib_eval.o
# NOTE: If this crashes with code 16, ASSERTIONS=1 is useful
# -s IMPORTED_MEMORY=1 may help reduce memory if emscripten 3.0.10 is used?
emcc $CFLAGS \
--llvm-lto 2 \
-s ENVIRONMENT=web \
-s EXPORTED_FUNCTIONS='["_pib_eval", "_php_embed_init", "_zend_eval_string", "_php_embed_shutdown"]' \
-s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall"]' \
-s MODULARIZE=1 \
-s EXPORT_NAME="'PHP'" \
-s TOTAL_MEMORY=134217728 \
-s ASSERTIONS=0 \
-s INVOKE_RUN=0 \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 \
--preload-file $PHAN_PATH \
libs/libphp.a pib_eval.o -o out/php.js
cp out/php.wasm out/php.js out/php.data ..
echo "Done"