Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmake/Modules/Platform/Emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ if (NOT _emcache_result EQUAL 0)
message(FATAL_ERROR "Failed to find emscripten cache directory with command \"'${EMSCRIPTEN_ROOT_PATH}/em-config${EMCC_SUFFIX}' CACHE\"! Process returned with error code ${_emcache_result}.")
endif()
file(TO_CMAKE_PATH "${_emcache_output}" _emcache_output)

# You **should** use CMAKE_SYSROOT, the one below is kept for
# compat for whichever soul used that variable...
set(CMAKE_SYSROOT "${_emcache_output}/sysroot")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the reason we don't do this is that emcc on it own already automatically includes the sysroot.

i.e. you never need to do emcc --sysroot=/path/to/emscripten/sysroot since emcc already knows exactly where its sysroot is. Its not something we expect user to pass.

I think we don't really want cmake to inject and additional --sysroot since it would be redundant.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If we set the compiler to clang then it would make sense to pass things like --sysroot and --target=wasm32-unknown-emscripten, but when we use the emcc wrapper around clang they are not needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'm aware emcc itself uses --sysroot..

HOWEVER
many projects use ${CMAKE_SYSROOT} for other miscellany purpouses, most notably:

  • pkg search
  • pkgconfig
  • find package
  • checks
  • unholy usages of "absolute paths" that depend on ${CMAKE_SYSROOT}

For example shadPS4 uses CMAKE_SYSROOT for checks and stuff, is it bad design? No! As per documentation CMAKE_SYSROOT MUST point to a valid sysroot, otherwise it's not a valid toolchain.

@sbc100 sbc100 Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For find package we set CMAKE_FIND_ROOT_PATH.

For pkg_config we set PKG_CONFIG_LIBDIR.

As I said, I'm to to keem on the idea of seeing --sysroot=/path/to/default/sysroot added to every emcc command line that cmake generates.

@bradking, sorry to loop you in again here, but I'm not sure what the best approach is here. We do use a sysroot, but its location is already know the emcc. I guess I'm looking for way to tell cmake about the sysroot without injecting superfluous/redundant compiler flags. Is that something is possible? Are we wrong to not set CMAKE_SYSROOT?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe something like DEFAULT_SYSROOT which would allow cmake to be aware or it without having to also inject --sysroot?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CMAKE_SYSROOT is meant for passing --sysroot to the compiler, and is also used for find_package and such. If one only wants the find_package part, CMAKE_FIND_ROOT_PATH should be sufficient.

@xinitrcn1 xinitrcn1 Jun 11, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Is it really?
I'm pretty sure by this point most projects [that worry about CMAKE_SYSROOT] have used it as an ad-hoc solution to get the sysroot they're living on (for whichever reason).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CMake documentation for CMAKE_SYSROOT states:

passed to the compiler in the --sysroot flag...also used to prefix paths searched by the find_* commands.

If projects are referencing CMAKE_SYSROOT for their own purposes, that's their own business, and they should be able to deal with it not being set.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hm, Of which it says

This variable may only be set in a toolchain file specified by the CMAKE_TOOLCHAIN_FILE variable.

I mean the docs of CMake w.r.t to CMAKE_SYSROOT mostly just imply that "it's a path", and yes, used for "find_*", but, if we were left to guess what the intention was, well, many people guessed that CMAKE_SYSROOT was a safe path to use to get the sysroot of the system when cross compiling. And now it's in various projects.

Was this correct to do? Eh... well that's how it's used nowadays (aside from the use outlined on CMake itself)

Here is varying examples of CMAKE_SYSROOT, all of them using it as ""intended"" https://man.freebsd.org/cgi/man.cgi?query=cmake-toolchains&sektion=7&manpath=FreeBSD+13.2-RELEASE+and+Ports
Except, annoyingly, " Cross Compiling using Clang" which doesn't. Allegedly because it assumes the toolchain itself already sets the sysroot by itself(?). But the whole document implies that a sysroot should be used.

Also wasi does it as well https://github.com/WebAssembly/wasi-sdk/blob/5faf80805397ae2a96ab224d1f103798af06dd92/cmake/wasi-sdk-sysroot.cmake#L67

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think with wasi-sdk the --sysroot argument is actually requires since the compiler it doesn't always know how to find it (unlike emcc).

I do understand your frustration here though, and I agree that we should somehow weight the costs and benefits here of redundantly injecting --sysroot.

set(EMSCRIPTEN_SYSROOT "${_emcache_output}/sysroot")

# Allow skipping of CMake compiler autodetection. On by default since this is
Expand Down