Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-40280: Add wasm cross build targets (GH-29771) #29771

Merged
merged 3 commits into from
Nov 25, 2021

Conversation

tiran
Copy link
Member

@tiran tiran commented Nov 25, 2021

CPython uses the same target triplet convention as Rust:

$ rustc --print target-list | grep wasm
wasm32-unknown-emscripten
wasm32-unknown-unknown
wasm32-wasi
wasm64-unknown-unknown

Signed-off-by: Christian Heimes [email protected]

https://bugs.python.org/issue40280

@emmatyping
Copy link
Contributor

FWIW, this is enough to allow me to compile CPython with emscripten with the following settings:

# assuming emsdk, llvm, etc is installed...
READELF=llvm-readelf-12 emconfigure ./configure ac_cv_func_memfd_create=no ac_cv_func_eventfd=no ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no cross_compiling=yes --with-pydebug --with-suffix=.wasm --with-ensurepip=no --disable-ipv6 --host=wasm32-unknown-emscripten --build=x86_64-unknown-linux-gnu

emmake make FREEZE_MODULE=../cpython-build/Programs/_freeze_module

However it seems the built python.wasm won't run with any of the webassembly runtimes I tried, I think the finall emcc call would need to be tweaked...

@tiran
Copy link
Member Author

tiran commented Nov 25, 2021

I'm facing the same issue, e.g. wasmtime complaints about missing clock_gettime.

I fixed the sigset_t issue in GH-29770 and the wrong CC for cross compilation in GH-29752. Pyodide and similar projects have more downstream patches.

Did you know that you can put all custom autoconf overrides into a config.site file? I'm using this config.site file

# config.site-wasm
ac_cv_buggy_getaddrinfo=no
ac_cv_file__dev_ptmx=yes
ac_cv_file__dev_ptc=no
# undefined symbols in wasm:
ac_cv_func_posix_spawn=no
ac_cv_func_posix_spawnp=no
ac_cv_func_eventfd=no

to run CONFIG_SITE=config.site-wasm emconfigure ./configure ....

Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if the current solution will work with wasm32-wasi. We should also make sure to support wasm32-unknown-wasi.

@bedevere-bot
Copy link

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@brettcannon
Copy link
Member

Do we want to record all the incantations we are trying to make builds work somewhere? E.g. https://bugs.python.org/issue40280 or somewhere else like a GitHub org/repo (I'm happy to create a cpython-wasm repo and give you both write access to write down what does and doesn't work)?

@ethanhs while I think --with-suffix .wasm should trigger STANDALONE_WASM=1, I'm not sure, so you may need to set that environment variable to force emscripten to do a WASI build over an emscripten/browser/JS build.

@tiran
Copy link
Member Author

tiran commented Nov 25, 2021

configure runs the host and build strings through config.sub, which converts any short forms into long canonical form:

$ emconfigure ../../configure -C --host=wasm32-wasi --build=x86_64-unknown-linux-gnu
configure: ../../configure -C --host=wasm32-wasi --build=x86_64-unknown-linux-gnu
configure: loading site script /usr/local/etc/config.site
configure: loading cache config.cache
checking for git... found
checking build system type... x86_64-unknown-linux-gnu
checking host system type... wasm32-unknown-wasi

--host=wasm32-wasi -> wasm32-unknown-wasi

For more information see:

CPython uses the same target triplet convention as Rust:

```
$ rustc --print target-list | grep wasm
wasm32-unknown-emscripten
wasm32-unknown-unknown
wasm32-wasi
wasm64-unknown-unknown
```

Signed-off-by: Christian Heimes <[email protected]>
@tiran tiran changed the title bpo-40280: Add wasm cross build targets bpo-40280: Add wasm cross build targets (GH-29771) Nov 25, 2021
@tiran tiran merged commit 1052a39 into python:main Nov 25, 2021
@tiran tiran deleted the bpo-40280-wasm branch November 25, 2021 20:24
@emmatyping
Copy link
Contributor

Do we want to record all the incantations we are trying to make builds work somewhere? E.g. https://bugs.python.org/issue40280 or somewhere else like a GitHub org/repo (I'm happy to create a cpython-wasm repo and give you both write access to write down what does and doesn't work)?

This seems like a great idea! Plus there will definitely need to be more patches for WASI, and probably Emscripten as well, so we can coordinate there if needed.

while I think --with-suffix .wasm should trigger STANDALONE_WASM=1, I'm not sure, so you may need to set that environment variable to force emscripten to do a WASI build over an emscripten/browser/JS build.

Certainly could! I was mostly seeing if I could get things to build at all before I went to bed. I'll probably play with things more tomorrow (and hopefully have something to show for it).

@tiran
Copy link
Member Author

tiran commented Nov 25, 2021

+1 for Brett's proposal.

Progress! I'm now getting a different error from wasmtime. My emscripten version may not support pthread.

# wasmtime ./python.wasm
Error: failed to run main module `./python.wasm`

Caused by:
    0: failed to instantiate "./python.wasm"
    1: unknown import: `env::pthread_create` has not been defined

@emmatyping
Copy link
Contributor

Progress! I'm now getting a different error from wasmtime. My emscripten version may not support pthread.

Great! Unfortunately I don't think wasmtime supports pthreads. The only WASM runtime that I could find that did was https://github.com/bytecodealliance/wasm-micro-runtime

Wasmer seems to stub pthread out https://github.com/wasmerio/wasmer/blob/master/lib/emscripten/src/pthread.rs, but maybe it just passes those through?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants