Skip to content

Commit b5b4504

Browse files
tiranemmatyping
andcommitted
bpo-40280: Add --with-emscripten-target to build for browser or node
Co-authored-by: Ethan Smith <[email protected]> Signed-off-by: Christian Heimes <[email protected]>
1 parent 607d8a8 commit b5b4504

File tree

5 files changed

+137
-14
lines changed

5 files changed

+137
-14
lines changed

Makefile.pre.in

+5-8
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ SRCDIRS= @SRCDIRS@
246246
# Other subdirectories
247247
SUBDIRSTOO= Include Lib Misc
248248

249+
# assets for Emscripten browser builds
250+
WASM_ASSETS_DIR=".$(prefix)"
251+
WASM_STDLIB="$(WASM_ASSETS_DIR)/local/lib/python$(VERSION)/os.py"
252+
249253
# Files and directories to be distributed
250254
CONFIGFILES= configure configure.ac acconfig.h pyconfig.h.in Makefile.pre.in
251255
DISTFILES= README.rst ChangeLog $(CONFIGFILES)
@@ -833,19 +837,12 @@ $(DLLLIBRARY) libpython$(LDVERSION).dll.a: $(LIBRARY_OBJS)
833837
# wasm32-emscripten build
834838
# wasm assets directory is relative to current build dir, e.g. "./usr/local".
835839
# --preload-file turns a relative asset path into an absolute path.
836-
WASM_ASSETS_DIR=".$(prefix)"
837-
WASM_STDLIB="$(WASM_ASSETS_DIR)/local/lib/python$(VERSION)/os.py"
838840

839841
$(WASM_STDLIB): $(srcdir)/Lib/*.py $(srcdir)/Lib/*/*.py \
840842
pybuilddir.txt $(srcdir)/Tools/wasm/wasm_assets.py
841843
$(PYTHON_FOR_BUILD) $(srcdir)/Tools/wasm/wasm_assets.py \
842844
--builddir . --prefix $(prefix)
843845

844-
python.html: Programs/python.o $(LIBRARY_DEPS) $(WASM_STDLIB)
845-
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o \
846-
$(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) \
847-
-s ASSERTIONS=1 --preload-file $(WASM_ASSETS_DIR)
848-
849846
##########################################################################
850847
# Build static libmpdec.a
851848
LIBMPDEC_CFLAGS=$(PY_STDMODULE_CFLAGS) $(CCSHARED) @LIBMPDEC_CFLAGS@
@@ -2396,7 +2393,7 @@ clean-retain-profile: pycremoval
23962393
-rm -f pybuilddir.txt
23972394
-rm -f Lib/lib2to3/*Grammar*.pickle
23982395
-rm -f _bootstrap_python
2399-
-rm -f python.html python.js python.data
2396+
-rm -f python.html python*.js python.data
24002397
-rm -f Programs/_testembed Programs/_freeze_module
24012398
-rm -f Python/deepfreeze/*.[co]
24022399
-rm -f Python/frozen_modules/*.h

Modules/socketmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7931,7 +7931,7 @@ PyInit__socket(void)
79317931
#ifdef IPPROTO_VRRP
79327932
PyModule_AddIntMacro(m, IPPROTO_VRRP);
79337933
#endif
7934-
#ifdef IPPROTO_SCTP
7934+
#if defined(IPPROTO_SCTP) && !defined(__EMSCRIPTEN__)
79357935
PyModule_AddIntMacro(m, IPPROTO_SCTP);
79367936
#endif
79377937
#ifdef IPPROTO_BIP

Tools/wasm/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ embuilder build zlib
2727

2828
### Cross compile to wasm32-emscripten
2929

30+
For browser:
31+
3032
```shell
3133
mkdir -p builddir/emscripten
3234
pushd builddir/emscripten
@@ -35,11 +37,25 @@ CONFIG_SITE=../../Tools/wasm/config.site-wasm32-emscripten \
3537
emconfigure ../../configure -C \
3638
--host=wasm32-unknown-emscripten \
3739
--build=$(../../config.guess) \
40+
--with-emscripten-target=browser \
3841
--with-build-python=$(pwd)/../build/python
3942

4043
emmake make -j$(nproc) python.html
4144
```
4245

46+
For node:
47+
48+
```
49+
CONFIG_SITE=../../Tools/wasm/config.site-wasm32-emscripten \
50+
emconfigure ../../configure -C \
51+
--host=wasm32-unknown-emscripten \
52+
--build=$(../../config.guess) \
53+
--with-emscripten-target=node \
54+
--with-build-python=$(pwd)/../build/python
55+
56+
emmake make -j$(nproc) python.js
57+
```
58+
4359
### Test in browser
4460

4561
Serve `python.html` with a local webserver and open the file in a browser.

configure

+74-3
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,8 @@ SHLIB_SUFFIX
846846
LIBTOOL_CRUFT
847847
OTHER_LIBTOOL_OPT
848848
UNIVERSAL_ARCH_FLAGS
849+
WASM_STDLIB
850+
WASM_ASSETS_DIR
849851
LDFLAGS_NOLTO
850852
LDFLAGS_NODIST
851853
CFLAGS_NODIST
@@ -1002,6 +1004,7 @@ with_universal_archs
10021004
with_framework_name
10031005
enable_framework
10041006
with_cxx_main
1007+
with_emscripten_target
10051008
with_suffix
10061009
enable_shared
10071010
enable_profiling
@@ -1751,6 +1754,8 @@ Optional Packages:
17511754
--with-cxx-main[=COMPILER]
17521755
compile main() and link Python executable with C++
17531756
compiler specified in COMPILER (default is $CXX)
1757+
--with-emscripten-target=[browser|node]
1758+
Emscripten platform
17541759
--with-suffix=SUFFIX set executable suffix to SUFFIX (default is empty,
17551760
yes is mapped to '.exe')
17561761
--with-pydebug build with Py_DEBUG defined (default is no)
@@ -6205,6 +6210,42 @@ case $ac_sys_system/$ac_sys_release in #(
62056210
;;
62066211
esac
62076212

6213+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-emscripten-target" >&5
6214+
$as_echo_n "checking for --with-emscripten-target... " >&6; }
6215+
6216+
# Check whether --with-emscripten-target was given.
6217+
if test "${with_emscripten_target+set}" = set; then :
6218+
withval=$with_emscripten_target;
6219+
if test "x$ac_sys_system" = xEmscripten; then :
6220+
6221+
case $with_emscripten_target in #(
6222+
browser) :
6223+
ac_sys_emscripten_target=browser ;; #(
6224+
node) :
6225+
ac_sys_emscripten_target=node ;; #(
6226+
*) :
6227+
as_fn_error $? "Invalid argument: --with-emscripten-target=browser|node" "$LINENO" 5
6228+
;;
6229+
esac
6230+
6231+
else
6232+
6233+
as_fn_error $? "--with-emscripten-target only applies to Emscripten" "$LINENO" 5
6234+
6235+
fi
6236+
6237+
else
6238+
6239+
eval as_val=\$ac_sys_system, Emscripten
6240+
if test "x$as_val" = xac_sys_emscripten_target=browser; then :
6241+
6242+
fi
6243+
6244+
fi
6245+
6246+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_sys_emscripten_target" >&5
6247+
$as_echo "$ac_sys_emscripten_target" >&6; }
6248+
62086249
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-suffix" >&5
62096250
$as_echo_n "checking for --with-suffix... " >&6; }
62106251

@@ -6223,9 +6264,13 @@ esac
62236264

62246265
else
62256266

6226-
case $ac_sys_system in #(
6227-
Emscripten) :
6228-
EXEEXT=.wasm ;; #(
6267+
case $ac_sys_system/$ac_sys_emscripten_target in #(
6268+
Emscripten/browser) :
6269+
EXEEXT=.html ;; #(
6270+
Emscripten/node) :
6271+
EXEEXT=.js ;; #(
6272+
wasi/*) :
6273+
EXEEXT=.js ;; #(
62296274
*) :
62306275
EXEEXT=
62316276
;;
@@ -7567,6 +7612,25 @@ then
75677612
esac
75687613
fi
75697614

7615+
# WASM flags
7616+
case $ac_sys_system/$ac_sys_emscripten_target in #(
7617+
Emscripten/browser) :
7618+
7619+
LDFLAGS_NODIST="$(LDFLAGS_NODIST) -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 --preload-file \$(WASM_ASSETS_DIR)"
7620+
WASM_ASSETS_DIR=".\$(prefix)"
7621+
WASM_STDLIB="\$(WASM_ASSETS_DIR)/local/lib/python\$(VERSION)/os.py"
7622+
;; #(
7623+
Emscripten/node) :
7624+
7625+
LDFLAGS_NODIST="$(LDFLAGS_NODIST) -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1 -s USE_PTHREADS -s PROXY_TO_PTHREAD"
7626+
CFLAGS_NODIST="$(CFLAGS_NODIST) -pthread"
7627+
;; #(
7628+
*) :
7629+
;;
7630+
esac
7631+
7632+
7633+
75707634

75717635

75727636

@@ -21169,6 +21233,13 @@ if test "$PY_ENABLE_SHARED" = 1 || test "$enable_framework" ; then
2116921233
else
2117021234
LIBRARY_DEPS="\$(LIBRARY) $LIBRARY_DEPS"
2117121235
fi
21236+
case $ac_sys_system/$ac_sys_emscripten_target in #(
21237+
Emscripten/browser) :
21238+
LIBRARY_DEPS="$LIBRARY_DEPS \$(WASM_STDLIB)" ;; #(
21239+
*) :
21240+
;;
21241+
esac
21242+
2117221243

2117321244

2117421245

configure.ac

+41-2
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,24 @@ AS_CASE([$ac_sys_system/$ac_sys_release],
10621062
]
10631063
)
10641064

1065+
AC_MSG_CHECKING([for --with-emscripten-target])
1066+
AC_ARG_WITH([emscripten-target],
1067+
[AS_HELP_STRING([--with-emscripten-target=@<:@browser|node@:>@], [Emscripten platform])],
1068+
[
1069+
AS_VAR_IF([ac_sys_system], [Emscripten], [
1070+
AS_CASE([$with_emscripten_target],
1071+
[browser], [ac_sys_emscripten_target=browser],
1072+
[node], [ac_sys_emscripten_target=node],
1073+
[AC_MSG_ERROR([Invalid argument: --with-emscripten-target=browser|node])]
1074+
)
1075+
], [
1076+
AC_MSG_ERROR([--with-emscripten-target only applies to Emscripten])
1077+
])
1078+
], [
1079+
AS_VAR_IF([ac_sys_system, Emscripten], [ac_sys_emscripten_target=browser])
1080+
])
1081+
AC_MSG_RESULT([$ac_sys_emscripten_target])
1082+
10651083
AC_MSG_CHECKING([for --with-suffix])
10661084
AC_ARG_WITH([suffix],
10671085
[AS_HELP_STRING([--with-suffix=SUFFIX], [set executable suffix to SUFFIX (default is empty, yes is mapped to '.exe')])],
@@ -1072,8 +1090,10 @@ AC_ARG_WITH([suffix],
10721090
[EXEEXT=$with_suffix]
10731091
)
10741092
], [
1075-
AS_CASE([$ac_sys_system],
1076-
[Emscripten], [EXEEXT=.wasm],
1093+
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
1094+
[Emscripten/browser], [EXEEXT=.html],
1095+
[Emscripten/node], [EXEEXT=.js],
1096+
[wasi/*], [EXEEXT=.js],
10771097
[EXEEXT=]
10781098
)
10791099
])
@@ -1769,10 +1789,25 @@ then
17691789
esac
17701790
fi
17711791

1792+
# WASM flags
1793+
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
1794+
[Emscripten/browser], [
1795+
LDFLAGS_NODIST="$(LDFLAGS_NODIST) -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 --preload-file \$(WASM_ASSETS_DIR)"
1796+
WASM_ASSETS_DIR=".\$(prefix)"
1797+
WASM_STDLIB="\$(WASM_ASSETS_DIR)/local/lib/python\$(VERSION)/os.py"
1798+
],
1799+
[Emscripten/node], [
1800+
LDFLAGS_NODIST="$(LDFLAGS_NODIST) -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1 -s USE_PTHREADS -s PROXY_TO_PTHREAD"
1801+
CFLAGS_NODIST="$(CFLAGS_NODIST) -pthread"
1802+
],
1803+
)
1804+
17721805
AC_SUBST(BASECFLAGS)
17731806
AC_SUBST(CFLAGS_NODIST)
17741807
AC_SUBST(LDFLAGS_NODIST)
17751808
AC_SUBST(LDFLAGS_NOLTO)
1809+
AC_SUBST([WASM_ASSETS_DIR])
1810+
AC_SUBST([WASM_STDLIB])
17761811

17771812
# The -arch flags for universal builds on macOS
17781813
UNIVERSAL_ARCH_FLAGS=
@@ -6252,6 +6287,10 @@ if test "$PY_ENABLE_SHARED" = 1 || test "$enable_framework" ; then
62526287
else
62536288
LIBRARY_DEPS="\$(LIBRARY) $LIBRARY_DEPS"
62546289
fi
6290+
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
6291+
[Emscripten/browser], [LIBRARY_DEPS="$LIBRARY_DEPS \$(WASM_STDLIB)"],
6292+
)
6293+
62556294
AC_SUBST(STATIC_LIBPYTHON)
62566295
AC_SUBST(LIBRARY_DEPS)
62576296

0 commit comments

Comments
 (0)