Skip to content

[Web] Include emscripten headers by default #105800

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adamscott
Copy link
Member

@adamscott adamscott commented Apr 26, 2025

Currently, developing using Emscripten for the Web platform is kinda a pain. As Emscripten uses by default it's own patched and new headers, the IDEs usually don't know what to do with Emscripten code because including manually those headers is not required.

Capture d’écran, le 2025-04-26 à 13 59 12

This code compiles with Emscripten even if clangd complains.

So this PR adds a new option: emscripten_include_path. When added, this path will be included as an include path in the compilation.

Edit: This PR now detects and adds the include path automatically, based on the detected binary.

Capture d’écran, le 2025-04-26 à 14 09 15

See how great it looks?

Note

If you're using emsdk:
scons platform=web emscripten_include_path=<emsdk path>/upstream/emscripten/cache/sysroot/include (not needed anymore)

If you're using directly emscripten:
scons platform=web emscripten_include_path=<emscripten path>/cache/sysroot/include (not needed anymore)

@dsnopek
Copy link
Contributor

dsnopek commented Apr 28, 2025

So, the goal is to get this include path into the compile_commands.json?

I wonder if there's a way to get Emscripten to give us the its include paths without the user having to specify them?

ChatGPT gave me this which seems to work:

emcc -v -E -x c++ - < /dev/null 2>&1 | awk '/#include <...> search starts here:/,/End of search list./ {if ($0 !~ /(#include|End of search)/) print $1}'

@akien-mga
Copy link
Member

Yeah I agree we should set this automatically and not require passing it on the command line.

A correct (current) Emscripten setup should have EMSDK in the environment I believe, so you can just append $EMSDK/upstream/emscripten/cache/sysroot/include/ to CPPPATH.

@adamscott
Copy link
Member Author

Yeah I agree we should set this automatically and not require passing it on the command line.

A correct (current) Emscripten setup should have EMSDK in the environment I believe, so you can just append $EMSDK/upstream/emscripten/cache/sysroot/include/ to CPPPATH.

Not necessarily. You can setup it to work directly from the emscripten-core/emscripten repo.

@adamscott adamscott force-pushed the add-emscripten-include-path branch from 0e40f5b to 657945b Compare April 29, 2025 22:30
@adamscott adamscott changed the title [Web] Add emscripten_include_path option [Web] Include emscripten headers by default Apr 29, 2025
Comment on lines +114 to +120
## Add Emscripten to the included paths (for compile_commands.json completion)
emcc_path = Path(str(WhereIs("emcc")))
while emcc_path.is_symlink():
# For some reason, mypy trips on `Path.readlink` not being defined, somehow.
emcc_path = emcc_path.readlink() # type: ignore[attr-defined]
emscripten_include_path = emcc_path.parent.joinpath("cache", "sysroot", "include")
env.Append(CPPPATH=[emscripten_include_path])
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the new logic, in order to include automatically the right headers to the build.

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

Successfully merging this pull request may close these issues.

3 participants