Skip to content

Commit fea1d90

Browse files
Allow tomlplusplus to be built by C++20 modules (#266)
1 parent f5b3fb2 commit fea1d90

File tree

5 files changed

+134
-8
lines changed

5 files changed

+134
-8
lines changed

CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,26 @@ target_include_directories(
3030

3131
target_compile_features(tomlplusplus_tomlplusplus INTERFACE cxx_std_17)
3232

33-
# ---- Install rules, examples, and fuzzing ----
33+
# ---- Install rules ----
3434
if (tomlplusplus_INSTALL)
3535
include(cmake/install-rules.cmake)
3636
endif()
3737

38+
# ---- C++ Modules Support (optional) ----
39+
option(TOMLPLUSPLUS_BUILD_MODULES "Build C++ modules support" OFF)
40+
41+
if(TOMLPLUSPLUS_BUILD_MODULES)
42+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
43+
message(STATUS "Building C++ modules (CMake ${CMAKE_VERSION} supports modules)")
44+
add_subdirectory(src/modules)
45+
else()
46+
message(WARNING "Skipping C++ modules (requires CMake 3.28+, found ${CMAKE_VERSION})")
47+
endif()
48+
else()
49+
message(STATUS "C++ modules support is disabled. Enable with -DTOMLPLUSPLUS_BUILD_MODULES=ON")
50+
endif()
51+
52+
# ---- Examples and fuzzing ----
3853
if(PROJECT_IS_TOP_LEVEL)
3954
option(BUILD_EXAMPLES "Build examples tree." OFF)
4055
option(BUILD_FUZZER "Build fuzzer." OFF)

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
## Library features
2121

2222
- Header-only (optional!)
23+
- Module support
2324
- Supports the latest [TOML] release ([v1.0.0]), plus optional support for some unreleased TOML features
2425
- Passes all tests in the [toml-test](https://github.com/toml-lang/toml-test) suite
2526
- Supports serializing to JSON and YAML
@@ -110,8 +111,8 @@ You'll find some more code examples in the `examples` directory, and plenty more
110111
### 🍨️ Regular flavour
111112
112113
1. Clone the repository
113-
2. Add `tomlplusplus/include` to your include paths
114-
3. `#include <toml++/toml.hpp>`
114+
2. Add `tomlplusplus/include` to your include paths, or for optional module support add `tomlplusplus/modules` as well and enable `TOMLPLUSPLUS_BUILD_MODULES`
115+
3. `#include <toml++/toml.hpp>` (or `import tomlplusplus;`)
115116
116117
### Conan
117118
@@ -281,13 +282,16 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
281282

282283
### With thanks to:
283284

285+
- **[@a-is](https://github.com/a-is)** - Fixed a bug
284286
- **[@beastle9end](https://github.com/beastle9end)** - Made Windows.h include bypass
285287
- **[@bjadamson](https://github.com/bjadamson)** - Reported some bugs and helped design a new feature
286288
- **[@bobfang1992](https://github.com/bobfang1992)** - Reported a bug and created a [wrapper in python](https://github.com/bobfang1992/pytomlpp)
289+
- **[@capuanob](https://github.com/capuanob)** - Integrated this project into OSSFuzz
287290
- **[@GiulioRomualdi](https://github.com/GiulioRomualdi)** - Added cmake+meson support
288291
- **[@jonestristand](https://github.com/jonestristand)** - Designed and implemented the `toml::path`s feature
289292
- **[@kcsaul](https://github.com/kcsaul)** - Fixed a bug
290293
- **[@levicki](https://github.com/levicki)** - Helped design some new features
294+
- **[@mikomikotaishi](https://github.com/mikomikotaishi)** - Added support for C++20 modules
291295
- **[@moorereason](https://github.com/moorereason)** - Reported a whole bunch of bugs
292296
- **[@mosra](https://github.com/mosra)** - Created the awesome [m.css] used to generate the API docs
293297
- **[@N-Dekker](https://github.com/N-Dekker)** - Added a workaround for the legacy lambda processor of MSVC 2019/2022, added `get_line`
@@ -304,11 +308,9 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[
304308
- **[@std-any-emplace](https://github.com/std-any-emplace)** - Reported some bugs
305309
- **[@Tachi107](https://github.com/Tachi107)** - Made some tweaks to meson.build, added compile_library build option
306310
- **[@traversaro](https://github.com/traversaro)** - Added vcpkg support and reported a bunch of bugs
311+
- **[@tyler92](https://github.com/tyler92)** - Fixed stack overflow that occurred during fuzzing tests
307312
- **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions
308313
- **[@ximion](https://github.com/ximion)** - Added support for installation with meson
309-
- **[@a-is](https://github.com/a-is)** - Fixed a bug
310-
- **[@capuanob](https://github.com/capuanob)** - Integrated this project into OSSFuzz
311-
- **[@tyler92](https://github.com/tyler92)** - Fixed stack overflow that occurred during fuzzing tests
312314
<br>
313315

314316
## Contact

docs/pages/main_page.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
@section mainpage-features Features
88

99
- Header-only (optional!)
10+
- Module support
1011
- Supports the latest [TOML](https://toml.io/) release ([v1.0.0](https://toml.io/en/v1.0.0)), plus
1112
optional support for some unreleased TOML features
1213
- Passes all tests in the [toml-test](https://github.com/BurntSushi/toml-test) suite
@@ -37,6 +38,8 @@ Call toml::parse_file() and work with the toml::table you get back, or handle an
3738
@cpp
3839
#include <iostream>
3940
#include <toml++/toml.hpp>
41+
// or alternatively:
42+
import tomlplusplus; // if C++20 or later
4043

4144
int main(int argc, char\*\* argv)
4245
{
@@ -460,8 +463,8 @@ and [emoji sundae] Regular. The API is the same for both.
460463

461464
<h3>[emoji sundae] Regular flavour</h3>
462465
1. Clone \github{marzer/tomlplusplus, the repository} from GitHub
463-
2. Add `tomlplusplus/include` to your include paths
464-
3. `#include <toml++/toml.hpp>`
466+
2. Add `tomlplusplus/include` to your include paths, or for optional module support add `tomlplusplus/modules` as well and enable `TOMLPLUSPLUS_BUILD_MODULES`
467+
3. `#include <toml++/toml.hpp>` (or `import tomlplusplus;`)
465468

466469
<!-- --------------------------------------------------------------------------------------------------------------- -->
467470

src/modules/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
file(GLOB_RECURSE TOMLPLUSPLUS_MODULES *.cppm)
2+
3+
add_library(tomlplusplus_modules)
4+
5+
cmake_minimum_required(VERSION 3.28)
6+
7+
if(NOT COMMAND configure_cpp_module_target)
8+
function(configure_cpp_module_target target)
9+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
10+
target_sources(${target} PUBLIC FILE_SET CXX_MODULES FILES ${TOMLPLUSPLUS_MODULES})
11+
else()
12+
message(WARNING "C++ modules require CMake 3.28+. Using standard compilation.")
13+
target_sources(${target} PRIVATE ${TOMLPLUSPLUS_MODULES})
14+
endif()
15+
endfunction()
16+
endif()
17+
18+
configure_cpp_module_target(tomlplusplus_modules)
19+
20+
target_link_libraries(tomlplusplus_modules
21+
PUBLIC
22+
tomlplusplus::tomlplusplus
23+
)
24+
25+
target_include_directories(tomlplusplus_modules
26+
PRIVATE
27+
${tomlplusplus_SOURCE_DIR}/include
28+
)
29+
30+
target_compile_features(tomlplusplus_modules PUBLIC cxx_std_20)
31+
32+
if(TOMLPLUSPLUS_ENABLE_INSTALL)
33+
install(TARGETS tomlplusplus_modules
34+
EXPORT tomlplusplus-targets
35+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
36+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
37+
FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tomlplusplus/modules
38+
)
39+
endif()

src/modules/tomlpp.cppm

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @file tomlpp.cppm
3+
* @brief File containing the module declaration for toml++.
4+
*/
5+
6+
module;
7+
8+
#include <toml++/toml.hpp>
9+
10+
export module tomlplusplus;
11+
12+
/**
13+
* @namespace toml
14+
* @brief The toml++ namespace toml::
15+
*/
16+
export namespace toml {
17+
/**
18+
* @namespace literals
19+
* @brief The toml++ namespace toml::literals::
20+
*/
21+
inline namespace literals {
22+
using toml::literals::operator""_toml;
23+
using toml::literals::operator""_tpath;
24+
}
25+
26+
using toml::array;
27+
using toml::date;
28+
using toml::date_time;
29+
using toml::inserter;
30+
using toml::json_formatter;
31+
using toml::key;
32+
using toml::node;
33+
using toml::node_view;
34+
using toml::parse_error;
35+
using toml::parse_result;
36+
using toml::path;
37+
using toml::path_component;
38+
using toml::source_position;
39+
using toml::source_region;
40+
using toml::table;
41+
using toml::time;
42+
using toml::time_offset;
43+
using toml::toml_formatter;
44+
using toml::value;
45+
using toml::yaml_formatter;
46+
using toml::format_flags;
47+
using toml::node_type;
48+
using toml::path_component_type;
49+
using toml::value_flags;
50+
using toml::array_iterator;
51+
using toml::const_array_iterator;
52+
using toml::const_table_iterator;
53+
using toml::default_formatter;
54+
using toml::inserted_type_of;
55+
using toml::optional;
56+
using toml::source_index;
57+
using toml::source_path_ptr;
58+
using toml::table_iterator;
59+
60+
using toml::at_path;
61+
using toml::get_line;
62+
using toml::operator""_toml;
63+
using toml::operator""_tpath;
64+
using toml::operator<<;
65+
using toml::parse;
66+
using toml::parse_file;
67+
}

0 commit comments

Comments
 (0)