Skip to content

Commit

Permalink
gh-127629: Add ctypes to the Emscripten build
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Dec 6, 2024
1 parent 6cf7794 commit d9b6e9b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
31 changes: 29 additions & 2 deletions Tools/wasm/emscripten/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ def make_build_python(context, working_dir):
print(f"🎉 {binary} {version}")


@subdir(HOST_DIR, clean_ok=True)
def make_emscripten_libffi(context, working_dir):
shutil.rmtree(HOST_DIR/"libffi", ignore_errors=True)

call([
"git",
"clone",
"https://github.com/libffi/libffi",
"--depth=1"
],
quiet=context.quiet,
)
call(
[EMSCRIPTEN_DIR / "make_libffi.sh"],
env=updated_env({"PREFIX": HOST_DIR}),
cwd=HOST_DIR/"libffi",
quiet=context.quiet,
)


@subdir(HOST_DIR, clean_ok=True)
def configure_emscripten_python(context, working_dir):
"""Configure the emscripten/host build."""
Expand All @@ -183,12 +203,14 @@ def configure_emscripten_python(context, working_dir):
sysconfig_data += "-pydebug"

host_runner = context.host_runner
env_additions = {"CONFIG_SITE": config_site, "HOSTRUNNER": host_runner}
pkg_config_path_dir = (working_dir / "lib/pkgconfig/").resolve()
env_additions = {"CONFIG_SITE": config_site, "HOSTRUNNER": host_runner, "EM_PKG_CONFIG_PATH": str(pkg_config_path_dir)}
build_python = os.fsdecode(build_python_path())
configure = [
"emconfigure",
os.path.relpath(CHECKOUT / "configure", working_dir),
"CFLAGS=-DPY_CALL_TRAMPOLINE -sUSE_BZIP2",
'PKG_CONFIG=pkg-config',
f"--host={HOST_TRIPLE}",
f"--build={build_platform()}",
f"--with-build-python={build_python}",
Expand Down Expand Up @@ -264,6 +286,7 @@ def build_all(context):
steps = [
configure_build_python,
make_build_python,
make_emscripten_libffi,
configure_emscripten_python,
make_emscripten_python,
]
Expand Down Expand Up @@ -292,6 +315,9 @@ def main():
configure_build = subcommands.add_parser(
"configure-build-python", help="Run `configure` for the " "build Python"
)
make_libffi_cmd = subcommands.add_parser(
"make-libffi", help="Clone libffi repo, configure and build it for emscripten"
)
make_build = subcommands.add_parser(
"make-build-python", help="Run `make` for the build Python"
)
Expand All @@ -303,7 +329,7 @@ def main():
clean = subcommands.add_parser(
"clean", help="Delete files and directories created by this script"
)
for subcommand in build, configure_build, make_build, configure_host, make_host:
for subcommand in build, configure_build, make_libffi_cmd, make_build, configure_host, make_host:
subcommand.add_argument(
"--quiet",
action="store_true",
Expand Down Expand Up @@ -336,6 +362,7 @@ def main():
context = parser.parse_args()

dispatch = {
"make-libffi": make_emscripten_libffi,
"configure-build-python": configure_build_python,
"make-build-python": make_build_python,
"configure-host": configure_emscripten_python,
Expand Down
22 changes: 22 additions & 0 deletions Tools/wasm/emscripten/make_libffi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set +e

export CFLAGS="-O2 -fPIC -DWASM_BIGINT"
export CXXFLAGS="$CFLAGS"

# Build paths
export CPATH="$PREFIX/include"
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"
export EM_PKG_CONFIG_PATH="$PKG_CONFIG_PATH"

# Specific variables for cross-compilation
export CHOST="wasm32-unknown-linux" # wasm32-unknown-emscripten

autoreconf -fi
emconfigure ./configure --host=$CHOST --prefix="$PREFIX" --enable-static --disable-shared --disable-dependency-tracking \
--disable-builddir --disable-multi-os-directory --disable-raw-api --disable-docs

make install
# Some forgotten headers?
cp fficonfig.h $PREFIX/include/
cp include/ffi_common.h $PREFIX/include/

0 comments on commit d9b6e9b

Please sign in to comment.