Skip to content

Conversation

@kaladron
Copy link
Contributor

Add opt-in C++20 module wrappers for Glaze JSON functionality, providing cleaner imports via import glaze.json; and potentially faster compile times.

Changes:

  • Add include/glaze/module/glaze_json.cppm module wrapper that exports the glz namespace with JSON-specific functionality
  • Add glaze_BUILD_MODULES CMake option (OFF by default) to enable module builds, requiring CMake 3.28+ for FILE_SET CXX_MODULES support
  • Create glaze::json_module target for downstream consumption
  • Update cmake/install-rules.cmake to install module targets when enabled
  • Add module tests in tests/module_test/ using the ut test framework
  • Document module usage in docs/installation.md including requirements, CMake configuration, and example usage

Module requirements:

  • CMake 3.28+ with Ninja or Visual Studio generator
  • Compiler with C++20 module support (Clang 16+, GCC 14+, MSVC 19.34+)

Usage:
cmake -B build -G Ninja -Dglaze_BUILD_MODULES=ON

// In source code: import glaze.json; auto json = glz::write_json(my_object);

Add opt-in C++20 module wrappers for Glaze JSON functionality, providing
cleaner imports via `import glaze.json;` and potentially faster compile
times.

Changes:
- Add `include/glaze/module/glaze_json.cppm` module wrapper that exports
  the glz namespace with JSON-specific functionality
- Add `glaze_BUILD_MODULES` CMake option (OFF by default) to enable
  module builds, requiring CMake 3.28+ for FILE_SET CXX_MODULES support
- Create `glaze::json_module` target for downstream consumption
- Update `cmake/install-rules.cmake` to install module targets when enabled
- Add module tests in `tests/module_test/` using the ut test framework
- Document module usage in `docs/installation.md` including requirements,
  CMake configuration, and example usage

Module requirements:
- CMake 3.28+ with Ninja or Visual Studio generator
- Compiler with C++20 module support (Clang 16+, GCC 14+, MSVC 19.34+)

Usage:
  cmake -B build -G Ninja -Dglaze_BUILD_MODULES=ON

  // In source code:
  import glaze.json;
  auto json = glz::write_json(my_object);
@kaladron
Copy link
Contributor Author

Hi! I made this wrapper to use in my own code and it's working, so I figured I'd toss it to you for consideration.

I'll admit that the installation directions are largely untested - I've confirmed that they go into an install directory right, but I my code currently uses this patched into its own tree here: https://github.com/kaladron/galactic-bloodshed/blob/ec3fb48284bd6f1cde12ab98859230febdb4ab15/gb/repositories/gblib-repositories.cppm#L5

This begins to address #1586

@kaladron
Copy link
Contributor Author

I should also note that it's entirely possible to do this without the wrapper and just use some preprocessor defines to put the export statements on the right places in the header file. Then the module include just sets the preprocessor define and includes the header.

I thought that this might be a nice non-invasive first step.

@kaladron
Copy link
Contributor Author

The GCC test failure looks unrelated to my code.

@stephenberry
Copy link
Owner

The GCC test failure looks unrelated to my code.

Yeah, this should be fixed when we merge in main

@stephenberry
Copy link
Owner

This is awesome! I'll review it in more detail and likely merge it in soon.

@stephenberry
Copy link
Owner

@kaladron, were you able to get C++20 modules working with GCC? I added a CI test and you can see it failing here for GCC. Any thoughts?

@kaladron
Copy link
Contributor Author

@kaladron, were you able to get C++20 modules working with GCC? I added a CI test and you can see it failing here for GCC. Any thoughts?

Sorry for the delay. I want to try this test with GCC 15 instead of 14, but that doesn't seem to be easily installable on anything I have handy. I needed to build some new images anyway. I'll get back to you soon.

@JonasGilg
Copy link

This sadly causes an internal compiler error, when trying to build with MSVC:

> cd build

> cmake -GNinja -Dglaze_BUILD_MODULES=ON -Dglaze_DEVELOPER_MODE=Off ..
-- The CXX compiler identification is MSVC 19.44.35222.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Professional/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Building with C++20 module support
-- Configuring done (1.2s)
-- Generating done (0.0s)
-- Build files have been written to: C:/LocalData/dev/glaze/build

> cmake --build .
[1/4] Scanning C:\LocalData\dev\glaze\include\glaze\module\glaze_json.cppm for CXX dependencies
glaze_json.cppm
[3/4] Building CXX object CMakeFiles\glaze_json_module.dir\include\glaze\module\glaze_json.cppm.obj
FAILED: [code=2] CMakeFiles/glaze_json_module.dir/include/glaze/module/glaze_json.cppm.obj CMakeFiles/glaze_json_module.dir/glaze.json.ifc
C:\PROGRA~1\MIB055~1\2022\PROFES~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\cl.exe  /nologo /TP  -IC:\LocalData\dev\glaze\include /DWIN32 /D_WINDOWS /EHsc /Zi /Ob0 /Od /RTC1 -std:c++latest -MDd /Zc:preprocessor /permissive- /Zc:lambda /showIncludes @CMakeFiles\glaze_json_module.dir\include\glaze\module\glaze_json.cppm.obj.modmap /FoCMakeFiles\glaze_json_module.dir\include\glaze\module\glaze_json.cppm.obj /FdCMakeFiles\glaze_json_module.dir\glaze_json_module.pdb /FS -c C:\LocalData\dev\glaze\include\glaze\module\glaze_json.cppm
glaze_json.cppm
D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\module\writer.cpp:5942: the impossible happened
C:\LocalData\dev\glaze\include\glaze\module\glaze_json.cppm(80): fatal error C1001: Internal compiler error.
(compiler file 'D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\module\utilities.h', line 48)
 To work around this problem, try simplifying or changing the program near the locations listed above.
If possible please provide a repro here: https://developercommunity.visualstudio.com
Please choose the Technical Support command on the Visual C++
 Help menu, or open the Technical Support help file for more information
ninja: build stopped: subcommand failed.

@DockedFerret800
Copy link

DockedFerret800 commented Dec 18, 2025

@JonasGilg A fix for this is pending release for a few months now, so I hope we can expect it in the next Insider release soon. MSVC switched to a different release cadence recently, so GA is only expected in May.

UPD: Please see and upvote - https://developercommunity.visualstudio.com/t/Modules-ICE-with-glaze-constrained-templ/10822191?q=Glaze

@kaladron
Copy link
Contributor Author

@DockedFerret800 Eh, I'm actually happy that you took the same approach, though! That at least validates the method some =)

This time of year gets super busy for me so I haven't had a chance to cook up an image with GCC 15 or later yet. Hopefully over the holiday break.

@kaladron
Copy link
Contributor Author

OK, I finally got https://github.com/kaladron/cpp-image/pkgs/container/cpp-image%2Fdev-env/644873000?tag=latest built that has the latest cmake, llvm snapshot and gcc-15. So I'll try this shortly. Sorry for the delay.

(If you want to use that image, note that the snapshot puts the std.cppm in the wrong directory, so it's not plug-and-play yet, but I need to send fixes to the LLVM packages for that. A few symlinks and the image is good to go for LLVM)

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.

4 participants