Skip to content

Add support for C++23 std modules#27065

Merged
sbc100 merged 37 commits into
emscripten-core:mainfrom
Diyou:cxx23_stl_modules
Jun 15, 2026
Merged

Add support for C++23 std modules#27065
sbc100 merged 37 commits into
emscripten-core:mainfrom
Diyou:cxx23_stl_modules

Conversation

@Diyou

@Diyou Diyou commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

This adds the libcxx/modules folder from https://github.com/emscripten-core/llvm-project/tree/emscripten-libs-21 with its generated files in system/lib/libcxx/modules/prebuilt

  • Prebuilt module interface files installed in sysroot via update_libcxx.py + install_system_headers
  • Added support in cmake/Modules/Emscripten.cmake for CMake > 3.30
  • Added Test

Resolves #21143

Additional Note:

To use the std or std.compat modules without CMake one would need to precompile the module interface like:

em++ -std=c++23 -stdlib=libc++ \
  -Wno-reserved-module-identifier \
  --precompile <EMSCRIPTEN_SYSROOT>/share/libc++/v1/std.cppm \
  -o std.pcm

And then compile the source files with:

em++ -std=c++23 -stdlib=libc++ \
  -fmodule-file=std=std.pcm \
  main.cpp -o main.js

@sbc100 sbc100 left a comment

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.

Nice!

To be clear are these files taken from the emscripten-libs-21 branch?

We normally use the system/lib/update_libcxx.py script to update these files. Perhaps you could try using that (maybe it needs modifying to include the modules directory somehow?)

Comment thread cmake/Modules/Platform/Emscripten.cmake Outdated
Comment thread cmake/Modules/Platform/Emscripten.cmake Outdated
Diyou added 3 commits June 8, 2026 19:50
Also made copy_tree conditionally exclude files because we need the CMakeLists.txt for the modules

@sbc100 sbc100 left a comment

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.

lgtm!

But maybe we could add a test for this ?

Comment thread system/lib/update_libcxx.py Outdated
@Diyou

Diyou commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

But maybe we could add a test for this ?

Might need to get Clang-CXX-CXXImportStd working if the test environment doesn't support cmake 4.2 for CMAKE_CXX_STDLIB_MODULES_JSON .

In this case we'd need to patch emcc.py to allow using a relative search path for -print-file-name with CMAKE_CXX_COMPILER_ID_ARG1

Diyou added 2 commits June 8, 2026 22:10
This is explicitly used by CMake's Clang-CXX-CXXImportStd.cmake
@sbc100

sbc100 commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

But maybe we could add a test for this ?

Might need to get Clang-CXX-CXXImportStd working if the test environment doesn't support cmake 4.2 for CMAKE_CXX_STDLIB_MODULES_JSON .

In this case we'd need to patch emcc.py to allow using a relative search path for -print-file-name with CMAKE_CXX_COMPILER_ID_ARG1

If we can get cmake 4.2 installed on at least one of the CI machines would that address the issue?

I would be fine landing this change standalone and following up with a test too.

Comment thread emcc.py Outdated
Comment thread emcc.py Outdated
Comment thread cmake/Modules/Platform/Emscripten.cmake Outdated
Comment thread cmake/Modules/Platform/Emscripten.cmake Outdated
Comment thread system/lib/update_libcxx.py Outdated
Comment thread cmake/Modules/Platform/Emscripten.cmake Outdated
Diyou added 4 commits June 11, 2026 05:24
Also adjust the toolchain file to use those
Q: Shouldn't these rather be installed when building libc++ ?
Comment thread test/test_other.py Outdated
Comment thread test/test_other.py Outdated
Comment thread test/test_other.py Outdated
This creates 2 separate test:
- test_cxx_import_std23
- test_cxx_import_std26

@sbc100 sbc100 left a comment

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.

lgtm!

Comment thread test/test_other.py Outdated
Comment thread system/lib/update_libcxx.py Outdated

@sbc100 sbc100 left a comment

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.

lgtm! (with one last nit)

Thanks for working on this

Comment thread test/test_other.py Outdated
@Diyou

Diyou commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

Pleasure. Hope I can get clangd for this :D . Differing versions still have trouble reading the .pcm files.

@sbc100

sbc100 commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

I think you need to rebase or merge to fix the chrome tests.

@sbc100 sbc100 changed the title C++23 stl modules Add support for C++23 std modules Jun 14, 2026
endif()
endif()

set_property(SOURCE

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.

Does it make sense to keep this outside of the above condition?

@Diyou Diyou Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because cmake caches CMAKE_CXX_STDLIB_MODULES_JSON the above condition would not run on subsequent configuration runs and remove the flags

Comment thread system/lib/update_libcxx.py Outdated
if(CMAKE_VERSION VERSION_GREATER_EQUAL 4.3.0)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD 451f2fe2-a8a2-47c3-bc32-94786d8fc91b)
else()
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD d0edc3af-4c50-42ea-a356-e2862fe7a444)

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.

Are normal end users executed to write lines like this?

@Diyou Diyou Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For now yes. This is still an Opt-in feature and users might also not have the backported values and would look that up for their installation.
Ref: https://cmake.org/cmake/help/latest/manual/cmake-cxxmodules.7.html#import-std-support

Note
This support is provided only when experimental support for import std has been enabled by the CMAKE_EXPERIMENTAL_CXX_IMPORT_STD gate.

@sbc100 sbc100 merged commit d913170 into emscripten-core:main Jun 15, 2026
39 checks passed
Comment thread test/test_other.py
sbc100 pushed a commit that referenced this pull request Jun 19, 2026
Removes the dependency on python3-packaging in test_other.py which was
introduced in #27065
@aheejin

aheejin commented Jul 1, 2026

Copy link
Copy Markdown
Member

What version of LLVM was this taken from? We normally sync library updates in specific LLVM version and it's confusing to mix different versions within the same library. The last update was 21.1.8 (#26058).

@sbc100

sbc100 commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

I'm pretty sure it was taking from the emscripten-libs-21 branch.. or at least I hope it was and seem to remember point it out at some point.

@Diyou maybe you could confirm and also update the PR description to mention this branch name.

@Diyou

Diyou commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Yes I changed it to the emscripten-libs-21 branch in this commit after you pointed this out which fixed the compatibility issues we initially had.

These should stay in sync with the changes in update_libcxx.py

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.

C++ 23 module support & import std

4 participants