Skip to content

Commit 124e371

Browse files
authored
add c++20 module support (#157)
* add constexpr alternatives for const macros * add condy.cppm module interface * add condy_module target and module-hello example * add docs for module support * add module ci check
1 parent e46d00a commit 124e371

10 files changed

Lines changed: 345 additions & 2 deletions

File tree

‎.github/workflows/ci-toolchain.yml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,23 @@ jobs:
5757
timeout-minutes: 5
5858
working-directory: ${{github.workspace}}/build
5959
run: ctest --verbose
60+
61+
- name: Build module-hello
62+
# liburing < 2.13 exposes TU-local entities that break GCC modules
63+
# https://github.com/axboe/liburing/issues/1457
64+
if: >
65+
matrix.generator == 'Ninja'
66+
&& !(matrix.compiler.cc == 'gcc' && matrix.liburing-version == '2.3')
67+
run: |
68+
cmake -B ${{github.workspace}}/build_module -G Ninja \
69+
-DBUILD_MODULE=ON \
70+
-DBUILD_EXAMPLES=ON \
71+
-DCMAKE_C_COMPILER=${{ matrix.compiler.cc }} \
72+
-DCMAKE_CXX_COMPILER=${{ matrix.compiler.cxx }}
73+
ninja -C ${{github.workspace}}/build_module module-hello
74+
75+
- name: Run module-hello
76+
if: >
77+
matrix.generator == 'Ninja'
78+
&& !(matrix.compiler.cc == 'gcc' && matrix.liburing-version == '2.3')
79+
run: ${{github.workspace}}/build_module/examples/module-hello

‎CMakeLists.txt‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project(condy LANGUAGES CXX)
55
option(BUILD_EXAMPLES "Build examples" OFF)
66
option(BUILD_BENCHMARKS "Build benchmarks" OFF)
77
option(BUILD_TESTS "Build tests" OFF)
8+
option(BUILD_MODULE "Build C++ module interface" OFF)
89
option(ENABLE_CLANG_TIDY "Enable clang-tidy static analysis" OFF)
910
option(TESTS_STATIC_LINK "Use static linking for tests" OFF)
1011
option(TESTS_TSAN "Enable thread sanitizer for tests" OFF)
@@ -25,7 +26,7 @@ add_library(condy INTERFACE)
2526
target_include_directories(condy INTERFACE include)
2627
target_compile_features(condy INTERFACE cxx_std_20)
2728

28-
if(BUILD_EXAMPLES OR BUILD_BENCHMARKS OR BUILD_TESTS OR LINK_LIBURING)
29+
if(BUILD_EXAMPLES OR BUILD_BENCHMARKS OR BUILD_TESTS OR LINK_LIBURING OR BUILD_MODULE)
2930
enable_language(C)
3031
include(ExternalProject)
3132

@@ -60,6 +61,12 @@ if(LINK_LIBURING)
6061
target_link_libraries(condy INTERFACE uring)
6162
endif()
6263

64+
if(BUILD_MODULE)
65+
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
66+
message(STATUS "Configuring C++ module interface in ${CMAKE_CURRENT_SOURCE_DIR}/module")
67+
add_subdirectory(module)
68+
endif()
69+
6370
# Warnings as errors
6471
add_compile_options(-Wall -Wextra -Werror)
6572
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")

‎docs/build.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,42 @@ ctest --test-dir build
6868
./build/examples/link-cp from.bin to.bin
6969
```
7070

71+
## C++20 Module Support
72+
73+
Condy provides an optional C++20 module interface as an alternative to `#include <condy.hpp>`.
74+
75+
```cpp
76+
import condy;
77+
```
78+
79+
### Requirements
80+
81+
- **CMake 3.28+** with **Ninja** generator
82+
- **GCC 14+** or **Clang 16+**
83+
84+
### Building the Example
85+
86+
```bash
87+
cmake -B build -S . -G Ninja \
88+
-DBUILD_MODULE=ON \
89+
-DBUILD_EXAMPLES=ON
90+
ninja -C build module-hello
91+
92+
./build/examples/module-hello
93+
# Hello, Condy (via module)!
94+
```
95+
96+
### Using in Your Project
97+
98+
The module target is `condy_module`:
99+
100+
```cmake
101+
set(BUILD_MODULE ON CACHE BOOL "")
102+
add_subdirectory(third_party/condy)
103+
add_executable(my_app src/main.cpp)
104+
target_link_libraries(my_app PRIVATE condy_module)
105+
```
106+
71107
## Version Compatibility
72108

73109
Condy supports **liburing ≥ 2.3**. When building with a specific version of liburing, Condy assumes that all related interfaces provided by that version are already supported by your Linux kernel.

‎docs/examples.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919

2020
- [queue-kernel-futex.cpp](queue-kernel-futex_8cpp_source.html)
2121
Builds a producer-consumer queue through asynchronous futex syscalls (`condy::async_futex_wait()`), implementing an asynchronous semaphore for synchronization.
22+
23+
- [module-hello.cpp](module-hello_8cpp_source.html)
24+
Demonstrates using Condy as a C++20 module via `import condy;`. Requires CMake 3.28+, Ninja, and GCC 14+ or Clang 16+. Build with `-DBUILD_MODULE=ON`.

‎examples/CMakeLists.txt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ add_executable(queue-kernel-futex queue-kernel-futex.cpp)
1414
target_link_libraries(queue-kernel-futex PRIVATE condy uring)
1515

1616
add_executable(queue-condy-futex queue-condy-futex.cpp)
17-
target_link_libraries(queue-condy-futex PRIVATE condy uring)
17+
target_link_libraries(queue-condy-futex PRIVATE condy uring)
18+
19+
if(BUILD_MODULE)
20+
add_executable(module-hello module-hello.cpp)
21+
target_link_libraries(module-hello PRIVATE condy_module uring)
22+
endif()

‎examples/module-hello.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <coroutine>
2+
#include <string>
3+
#include <unistd.h>
4+
5+
import condy;
6+
7+
condy::Coro<> co_main() {
8+
std::string msg = "Hello, Condy (via module)!\n";
9+
co_await condy::async_write(STDOUT_FILENO, condy::buffer(msg), 0);
10+
}
11+
12+
int main() noexcept(false) { condy::sync_wait(co_main()); }

‎include/condy/helpers.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626

2727
namespace condy {
2828

29+
/**
30+
* @brief Placeholder to let io_uring allocate a direct file descriptor.
31+
*/
32+
inline constexpr unsigned file_index_alloc = CONDY_FILE_INDEX_ALLOC;
33+
2934
/**
3035
* @brief Helper to build an invocable that spawns a coroutine on invocation.
3136
* @tparam CoroFunc Type of the coroutine function.

‎include/condy/version.hpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,26 @@
44

55
#pragma once
66

7+
/**
8+
* @brief Condy major version.
9+
*/
710
#define CONDY_VERSION_MAJOR 1
11+
12+
/**
13+
* @brief Condy minor version.
14+
*/
815
#define CONDY_VERSION_MINOR 8
16+
17+
namespace condy {
18+
19+
/**
20+
* @brief Condy major version.
21+
*/
22+
inline constexpr int version_major = CONDY_VERSION_MAJOR;
23+
24+
/**
25+
* @brief Condy minor version.
26+
*/
27+
inline constexpr int version_minor = CONDY_VERSION_MINOR;
28+
29+
} // namespace condy

‎module/CMakeLists.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
3+
add_library(condy_module)
4+
target_sources(condy_module
5+
PUBLIC FILE_SET CXX_MODULES
6+
FILES condy.cppm
7+
)
8+
target_compile_features(condy_module PUBLIC cxx_std_20)
9+
target_link_libraries(condy_module PUBLIC condy)

0 commit comments

Comments
 (0)