-
Notifications
You must be signed in to change notification settings - Fork 209
Add experimental C++20 module support for JSON #2100
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
base: main
Are you sure you want to change the base?
Conversation
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);
|
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 |
|
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. |
|
The GCC test failure looks unrelated to my code. |
Yeah, this should be fixed when we merge in |
|
This is awesome! I'll review it in more detail and likely merge it in soon. |
|
@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. |
|
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. |
|
@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 |
|
@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. |
|
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) |
Add opt-in C++20 module wrappers for Glaze JSON functionality, providing cleaner imports via
import glaze.json;and potentially faster compile times.Changes:
include/glaze/module/glaze_json.cppmmodule wrapper that exports the glz namespace with JSON-specific functionalityglaze_BUILD_MODULESCMake option (OFF by default) to enable module builds, requiring CMake 3.28+ for FILE_SET CXX_MODULES supportglaze::json_moduletarget for downstream consumptioncmake/install-rules.cmaketo install module targets when enabledtests/module_test/using the ut test frameworkdocs/installation.mdincluding requirements, CMake configuration, and example usageModule requirements:
Usage:
cmake -B build -G Ninja -Dglaze_BUILD_MODULES=ON
// In source code: import glaze.json; auto json = glz::write_json(my_object);