python-emscripten  Check-in [f4bef9c8ce]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:py2: 2.7.18 support
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:f4bef9c8ceffcd10e63ad01ee18ebca1175ffc44fdbd28a614e42264ca7ff8c0
User & Date: Beuc 2020-07-27 16:53:45
Context
2020-08-08
12:36
py2: drop obsolete work-around Leaf check-in: dba24a450f user: Beuc tags: trunk
2020-07-27
16:53
py2: 2.7.18 support check-in: f4bef9c8ce user: Beuc tags: trunk
16:49
Doc fixes / clean-ups check-in: 3166b2f00b user: Beuc tags: trunk
Changes

Changes to .fossil-settings/ignore-glob.

1
2
3
4
5
6
7
8
9


*/build/
*/crosspython-static/
*/crosspython-dynamic/
*/destdir
*/package/
*/Python-*.tgz
*/Python-*.tar.xz
*/setuptools-*.zip
*/t/





|





>
>
1
2
3
4
5
6
7
8
9
10
11
*/build/
*/crosspython-static/
*/crosspython-dynamic/
*/destdir*
*/package/
*/Python-*.tgz
*/Python-*.tar.xz
*/setuptools-*.zip
*/t/
*.bak
*~

Added 2.7.18/package-pythonhome.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
#!/bin/bash -e

# Creates a minimal Python file hierarchy at $PACKAGEDIR

# Copyright (C) 2018, 2019  Sylvain Beucler

# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

FILE_PACKAGER="python $(dirname $(which emcc))/tools/file_packager.py"

PREFIX=${PREFIX:-$(dirname $(readlink -f $0))/destdir}
PACKAGEDIR=${PACKAGEDIR:-$(dirname $(readlink -f $0))/package}
OUTDIR=${OUTDIR:-.}
CROSSPYTHON=$(dirname $(readlink -f $0))/crosspython-static/bin/python

# Python home

# optional lz4 compression, requires '-s LZ4=1'
LZ4=
if [ "$1" == "--lz4" ]; then LZ4="--lz4"; shift; fi

rm -rf $PACKAGEDIR/
mkdir -p $PACKAGEDIR

# Hard-coded modules: for 'print("hello, world.")'
# $@: additional, app-specific modules
for i in site.py os.py posixpath.py stat.py genericpath.py warnings.py linecache.py types.py UserDict.py _abcoll.py abc.py _weakrefset.py copy_reg.py traceback.py sysconfig.py re.py sre_compile.py sre_parse.py sre_constants.py encodings/__init__.py codecs.py encodings/aliases.py encodings/utf_8.py __future__.py ast.py copy.py weakref.py platform.py string.py io.py tempfile.py random.py hashlib.py struct.py dummy_thread.py collections.py keyword.py heapq.py argparse.py textwrap.py gettext.py locale.py functools.py importlib/__init__.py glob.py fnmatch.py pickle.py colorsys.py contextlib.py zipfile.py shutil.py json/__init__.py json/decoder.py json/scanner.py encodings/hex_codec.py json/encoder.py difflib.py inspect.py dis.py opcode.py tokenize.py token.py xml/__init__.py xml/etree/__init__.py xml/etree/ElementTree.py xml/etree/ElementPath.py encodings/zlib_codec.py tarfile.py urlparse.py StringIO.py encodings/latin_1.py encodings/ascii.py \
    "$@"; do
    if [ $PREFIX/lib/python2.7/$i -nt $PREFIX/lib/python2.7/${i%.py}.pyo ]; then
	(cd $PREFIX && $CROSSPYTHON -OO -m py_compile lib/python2.7/$i)
    fi
    mkdir -p $PACKAGEDIR/lib/python2.7/$(dirname $i)
    cp -au $PREFIX/lib/python2.7/${i%.py}.pyo $PACKAGEDIR/lib/python2.7/${i%.py}.pyo
done
# Large and leaks build paths, clean it:
echo 'build_time_vars = {}' > $PACKAGEDIR/lib/python2.7/_sysconfigdata.py
(cd $PACKAGEDIR && $CROSSPYTHON -OO -m py_compile lib/python2.7/_sysconfigdata.py)
rm -f $PACKAGEDIR/lib/python2.7/_sysconfigdata.py

# --no-heap-copy: suited for ALLOW_MEMORY_GROWTH=1
PACKAGEDIR_FULLPATH=$(readlink -f $PACKAGEDIR)
(
    cd $OUTDIR;  # use relative path in xxx-data.js
    $FILE_PACKAGER \
	pythonhome.data --js-output=pythonhome-data.js \
	--preload $PACKAGEDIR_FULLPATH@/ \
	--use-preload-cache --no-heap-copy $LZ4
)

Added 2.7.18/patches/python2-cross_compile.patch.



























































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
Description: Fix build system for Emscripten cross-compilation.
  Also adds ac_cv_func_dlopen=yes to support dynamic linking.
Forwarded: no
Author: Marat Dukhan <maratek@gmail.com>, Sylvain Beucler <beuc@beuc.net>
Origin: https://github.com/PeachPy/Python-2.7
Last-Update: 2020-07-27

commit 5b7a8e46d129e576ad9298055bb4e695aea29cb6
Author: Marat Dukhan <maratek@gmail.com>
Date:   Mon Oct 5 16:58:19 2015 -0400

    Make config.sub recognize asmjs-unknown-emscripten target

Index: Python-2.7.18/config.sub
===================================================================
--- Python-2.7.18.orig/config.sub
+++ Python-2.7.18/config.sub
@@ -2,7 +2,7 @@
 # Configuration validation subroutine script.
 #   Copyright 1992-2017 Free Software Foundation, Inc.
 
-timestamp='2017-04-02'
+timestamp='2020-07-27'
 
 # This file is free software; you can redistribute it and/or modify it
 # under the terms of the GNU General Public License as published by
@@ -118,7 +118,8 @@ case $maybe_os in
   linux-musl* | linux-uclibc* | uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | \
   knetbsd*-gnu* | netbsd*-gnu* | netbsd*-eabi* | \
   kopensolaris*-gnu* | cloudabi*-eabi* | \
-  storm-chaos* | os2-emx* | rtmk-nova*)
+  storm-chaos* | os2-emx* | rtmk-nova* | \
+  emscripten)
     os=-$maybe_os
     basic_machine=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'`
     ;;
@@ -253,6 +254,7 @@ case $basic_machine in
 	| am33_2.0 \
 	| arc | arceb \
 	| arm | arm[bl]e | arme[lb] | armv[2-8] | armv[3-8][lb] | armv7[arm] \
+	| asmjs \
 	| avr | avr32 \
 	| ba \
 	| be32 | be64 \
@@ -1547,6 +1549,8 @@ case $os in
 	-dicos*)
 		os=-dicos
 		;;
+	-emscripten)
+		;;
 	-nacl*)
 		;;
 	-ios)
Index: Python-2.7.18/configure
===================================================================
--- Python-2.7.18.orig/configure
+++ Python-2.7.18/configure
@@ -3287,6 +3287,9 @@ then
 	*-*-cygwin*)
 		ac_sys_system=Cygwin
 		;;
+	asmjs-*-*)
+		ac_sys_system=Emscripten
+		;;
 	*)
 		# for now, limit cross builds to known configurations
 		MACHDEP="unknown"
@@ -3333,6 +3336,9 @@ if test "$cross_compiling" = yes; then
 	*-*-cygwin*)
 		_host_cpu=
 		;;
+	asmjs-*-*)
+		_host_cpu=
+		;;
 	*)
 		# for now, limit cross builds to known configurations
 		MACHDEP="unknown"
Index: Python-2.7.18/configure.ac
===================================================================
--- Python-2.7.18.orig/configure.ac
+++ Python-2.7.18/configure.ac
@@ -340,6 +340,9 @@ then
 	*-*-cygwin*)
 		ac_sys_system=Cygwin
 		;;
+	asmjs-*-*)
+		ac_sys_system=Emscripten
+		;;
 	*)
 		# for now, limit cross builds to known configurations
 		MACHDEP="unknown"
@@ -386,6 +389,9 @@ if test "$cross_compiling" = yes; then
 	*-*-cygwin*)
 		_host_cpu=
 		;;
+	asmjs-*-*)
+		_host_cpu=
+		;;
 	*)
 		# for now, limit cross builds to known configurations
 		MACHDEP="unknown"
Index: Python-2.7.18/config.site
===================================================================
--- /dev/null
+++ Python-2.7.18/config.site
@@ -0,0 +1,3 @@
+ac_cv_file__dev_ptmx=no
+ac_cv_file__dev_ptc=no
+ac_cv_func_dlopen=yes

Added 2.7.18/patches/python2-no_popen.patch.















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Description: Fix popen-related issues with Emscripten
  warning: unresolved symbol: popen  [emcc]
  missing function: popen  [_popen->abort at runtime]
Forwarded: no
Author: Sylvain Beucler <beuc@beuc.net>
Last-Update: 2018-10-28


Index: Python-2.7.18/Modules/posixmodule.c
===================================================================
--- Python-2.7.18.orig/Modules/posixmodule.c
+++ Python-2.7.18/Modules/posixmodule.c
@@ -156,8 +156,10 @@ corresponding Unix manual entries for mo
 #define HAVE_OPENDIR    1
 #define HAVE_PIPE       1
 #ifndef __rtems__
+#ifndef __EMSCRIPTEN__
 #define HAVE_POPEN      1
 #endif
+#endif
 #define HAVE_SYSTEM     1
 #define HAVE_WAIT       1
 #define HAVE_TTYNAME    1

Added 2.7.18/patches/python2-webbrowser.patch.







































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
Description: Support opening new browser tabs from Python/Emscripten
Forwarded: no
Author: Sylvain Beucler <beuc@beuc.net>
Last-Update: 2018-10-28

Index: Python-2.7.18/Lib/webbrowser.py
===================================================================
--- Python-2.7.18.orig/Lib/webbrowser.py
+++ Python-2.7.18/Lib/webbrowser.py
@@ -6,7 +6,7 @@ import os
 import shlex
 import sys
 import stat
-import subprocess
+#import subprocess
 import time
 
 __all__ = ["Error", "open", "open_new", "open_new_tab", "get", "register"]
@@ -656,6 +656,16 @@ if sys.platform[:3] == "os2" and _iscomm
              GenericBrowser(["start", "netscape", "%s"]), -1)
 
 
+class EmscriptenBrowser(BaseBrowser):
+    def open(self, url, new=0, autoraise=True):
+        import emscripten
+        emscripten.run_script("""try { window.open('%s', '_blank'); } catch (e) { console.log(e); }""" % url.replace("'", "%27"))
+        return True
+
+if sys.platform == 'emscripten':
+    register("emscripten", None, EmscriptenBrowser())
+
+
 # OK, now that we know what the default preference orders for each
 # platform are, allow user to override them with the BROWSER variable.
 if "BROWSER" in os.environ:

Added 2.7.18/patches/series.







>
>
>
1
2
3
python2-cross_compile.patch
python2-no_popen.patch
python2-webbrowser.patch

Added 2.7.18/python.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
#!/bin/bash -ex

# Compile minimal Python for Emscripten and native local testing

# Copyright (C) 2018, 2019, 2020  Sylvain Beucler

# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

VERSION=2.7.18  # for end-of-life Python2, support Ren'Py 7.x version only
SCRIPTDIR=$(dirname $(readlink -f $0))
DESTDIR=${DESTDIR:-$SCRIPTDIR/destdir}
SETUPLOCAL=${SETUPLOCAL:-'/dev/null'}

CACHEROOT=$SCRIPTDIR
BUILD=$SCRIPTDIR/build
export QUILT_PATCHES=$(dirname $(readlink -f $0))/patches

WGET=${WGET:-wget}

unpack () {
    $WGET -c https://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz -P $CACHEROOT/
    mkdir -p $BUILD
    cd $BUILD/
    rm -rf Python-$VERSION/
    tar xf $CACHEROOT/Python-$VERSION.tgz
    cd Python-$VERSION/
    quilt push -a
}

# use cases:
# - python/.pyo for emscripten() below
# - common basis for crosspython below
hostpython () {
    cd $BUILD/Python-$VERSION/
    mkdir -p native
    (
        cd native/
        if [ ! -e config.status ]; then
	    # --without-pymalloc: prevents segfault during 'make' (sysconfigdata)
            ../configure \
                --prefix='' \
                --without-pymalloc
        fi

        make -j$(nproc)
        make install DESTDIR=$BUILD/hostpython
    )

    # setuptools for 3rd-party module installers
    wget -c https://files.pythonhosted.org/packages/11/0a/7f13ef5cd932a107cd4c0f3ebc9d831d9b78e1a0e8c98a098ca17b1d7d97/setuptools-41.6.0.zip -P $CACHEROOT/
    cd $BUILD/hostpython/
    rm -rf setuptools-41.6.0/
    unzip $CACHEROOT/setuptools-41.6.0.zip
    cd setuptools-41.6.0/
    ../bin/python setup.py install
}

emscripten () {
    cd $BUILD/Python-$VERSION/
    mkdir -p emscripten
    (
        cd emscripten/
        # OPT=-Oz: TODO
        # CONFIG_SITE: deals with cross-compilation https://bugs.python.org/msg136962
        # not needed as emcc has a single arch: BASECFLAGS=-m32 LDFLAGS=-m32
        # PATH: detect our Python, beware of conflict with emcc's python3
        #       don't use PYTHON_FOR_BUILD which is high-level / lots of options
        # --without-threads: pthreads experimental as of 2020-05
        #   cf. https://emscripten.org/docs/porting/pthreads.html
        # --without-signal-module: no process signals in Emscripten
        # --without-pymalloc: ?
        # --disable-ipv6: browser-side networking
        # --disable-shared: compile statically for Emscripten perfs + incomplete PIC support
        if [ ! -e config.status ]; then
            CONFIG_SITE=../config.site \
                BASECFLAGS='-s USE_ZLIB=1' LDFLAGS='-s USE_ZLIB=1' \
                PATH=$BUILD/Python-$VERSION/native:$PATH \
                emconfigure ../configure \
                --host=asmjs-unknown-emscripten --build=$(../config.guess) \
                --prefix='' \
                --without-threads --without-pymalloc --without-signal-module --disable-ipv6 \
                --disable-shared
        fi

        # Modules/Setup.local
        echo '*static*' > Modules/Setup.local
        cat $SETUPLOCAL >> Modules/Setup.local
        # drop -I/-L/-lz, we USE_ZLIB=1 (keep it in SETUPLOCAL for mock)
        sed -i -e 's/^\(zlib zlibmodule.c\).*/\1/' Modules/Setup.local
        emmake make Makefile
        # decrease .pyo size by dropping docstrings
        sed -i -e '/compileall.py/ s/ -O / -OO /' Makefile
    
        (
            export PATH=$BUILD/Python-$VERSION/native:$PATH
            emmake make -j$(nproc)
            # setup.py install_lib doesn't respect DESTDIR
            #echo -e 'sharedinstall:\n\ttrue' >> Makefile
            emmake make install DESTDIR=$DESTDIR
        )

        # Basic trimming
        # Disabled for now, better cherry-pick the files we need
        #emmake make install DESTDIR=$(pwd)/destdir
        #find destdir/ -name "*.py" -print0 | xargs -r0 rm
        #find destdir/ -name "*.pyo" -print0 | xargs -r0 rm  # only keep .pyc, .pyo apparently don't work
        #find destdir/ -name "*.so" -print0 | xargs -r0 rm
        #rm -rf destdir/usr/local/bin/
        #rm -rf destdir/usr/local/share/man/
        #rm -rf destdir/usr/local/include/
        #rm -rf destdir/usr/local/lib/*.a
        #rm -rf destdir/usr/local/lib/pkgconfig/
        #rm -rf destdir/usr/local/lib/python2.7/test/
        # Ditch .so for now, they cause an abort() with dynamic
        # linking unless we recompile all of them as SIDE_MODULE-s
        #rm -rf $DESTDIR/lib/python2.7/lib-dynload/
    )
}

# For mock-ing emscripten environment through static desktop python
mock () {
    cd $BUILD/Python-$VERSION/
    mkdir -p native
    (
        cd native/
        if [ ! -e config.status ]; then
            ../configure \
                --prefix=$BUILD/hostpython/ \
                --without-threads --without-pymalloc --without-signal-module --disable-ipv6 \
                --disable-shared
        fi
        echo '*static*' > Modules/Setup.local
        cat $SETUPLOCAL >> Modules/Setup.local

        make -j$(nproc) Parser/pgen python
    
        make -j$(nproc)
        DESTDIR= make install
    )
}

# python aimed at compiling third-party Python modules to WASM
# - building static/dynamic wasm modules
# - compiling .pyo files, in emscripten() and package-xxx.sh
# Uses hostpython; detects its PYTHONHOME through ../lib AFAICS, no need to recompile
# Usage:
# .../crosspython-static/bin/python  setup.py xxx --root=.../destdir/ --prefix=''
# .../crosspython-dynamic/bin/python setup.py xxx --root=.../destdir/ --prefix=''
# .../crosspython-static/bin/python -OO -m py_compile xxx.py
# Note: maybe create and patch two virtualenv instead?  but harder to
#   use with dual static/dynamic Makefile; doesn't handle root=destdir
crosspython () {
    cd $SCRIPTDIR
    # Copy-link hostpython except for include/ and
    # lib/python2.7/_sysconfigdata.py
    for variant in static dynamic; do
	rm -rf crosspython-$variant
	mkdir crosspython-$variant
	(
	    cd crosspython-$variant
	    for i in $(cd ../build/hostpython && ls -A); do
		ln -s ../build/hostpython/$i $i
	    done
	    rm include lib
	    mkdir lib
	    for i in $(cd ../build/hostpython/lib && ls -A); do
		ln -s ../../build/hostpython/lib/$i lib/$i
	    done
	    rm lib/python2.7
	    mkdir lib/python2.7
	    for i in $(cd ../build/hostpython/lib/python2.7 && ls -A); do
		ln -s ../../../build/hostpython/lib/python2.7/$i lib/python2.7/$i
	    done
	    
	    # Use Python.h configured for WASM
	    ln -s $DESTDIR/include include
	    
	    # Use compiler settings configured for WASM
	    rm lib/python2.7/_sysconfigdata.*
	    cp -a $DESTDIR/lib/python2.7/_sysconfigdata.* lib/python2.7/
	)
    done
    # 'CCSHARED': 'xxx',
    sed -i -e "s/'CCSHARED': .*/'CCSHARED': '-fPIC -s SIDE_MODULE=1',/" \
	crosspython-dynamic/lib/python2.7/_sysconfigdata.py
}

case "$1" in
    unpack|hostpython|emscripten|mock|crosspython)
        "$1"
        ;;
    '')
        unpack
        hostpython
        emscripten
        crosspython
        ;;
    *)
        echo "Usage: $0 unpack|hostpython|emscripten|mock|crosspython"
        exit 1
        ;;
esac

Added 2.7.18/webprompt.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
#!/bin/bash -ex

# Simple Python prompt for the browser, for smoke testing

# Copyright (C) 2019, 2020  Sylvain Beucler

# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

# Alternatively: use Emscripten's old binary:
# emscripten/tests/python/python.bc -s ERROR_ON_UNDEFINED_SYMBOLS=0

INSTALLDIR=${INSTALLDIR:-$(dirname $(readlink -f $0))/destdir}
BUILD=t

mkdir -p $BUILD

cython -2 ../emscripten.pyx -o $BUILD/emscripten.c
cython -2 ../emscripten_fetch.pyx -o $BUILD/emscripten_fetch.c
# utf_32_be: support Unicode characters e.g. u'é'
PREFIX=$INSTALLDIR OUTDIR=$BUILD ./package-pythonhome.sh \
    encodings/utf_32_be.py

FLAGS='-O3'
while (( $# )); do
    case "$1" in
	debug) FLAGS='-s ASSERTIONS=1 -g -s FETCH_DEBUG=1';;
	async) ASYNC='-s ASYNCIFY=1 -O3';;
    esac
    shift
done
emcc -o $BUILD/index.html \
  ../webprompt-main.c $BUILD/emscripten.c $BUILD/emscripten_fetch.c \
  $FLAGS \
  -I$INSTALLDIR/include/python2.7 -L$INSTALLDIR/lib -lpython2.7 \
  -s EMULATE_FUNCTION_POINTER_CASTS=1 \
  -s USE_ZLIB=1 \
  -s FETCH=1 \
  -s ALLOW_MEMORY_GROWTH=1 \
  -s FORCE_FILESYSTEM=1 -s RETAIN_COMPILER_SETTINGS=1 \
  $ASYNC \
  --shell-file ../webprompt-shell.html -s MINIFY_HTML=0 \
  -s EXPORTED_FUNCTIONS='[_main, _Py_Initialize, _PyRun_SimpleString, _pyruni]' \
  -s EXTRA_EXPORTED_RUNTIME_METHODS='[ccall, cwrap]'

# emrun --serve_after_close t/index.html

# cython -2 ../mock/emscripten.pyx -o t/mock.c
# cython -2 ../mock/emscripten_fetch.pyx -o t/mock2.c
# gcc -g -I build/hostpython/include/python2.7 -L build/hostpython/lib/ t/mock.c t/mock2.c ../webprompt-main.c -lpython2.7 -ldl -lm -lutil -lz -lpthread
# PYTHONHOME=build/hostpython/ ./a.out