Skip to content

Commit 15f43b7

Browse files
authored
initial c++ module support (#24)
* c++ module export only public interface, hide private detail namespace * split addon headers for glaze and fmt from simple enum main include and module * add enum_name_at_index, enum_size, enum_value_at_index to public interface --------- Co-authored-by: Artur Bać <[email protected]>
1 parent 9c54bcc commit 15f43b7

27 files changed

+459
-156
lines changed

.github/workflows/ci.yml

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,58 @@ jobs:
3131

3232
- name: ci-gcc-c++26
3333
run: cmake --workflow --preset="ci-gcc-c++26"
34-
35-
build-and-test-clang:
34+
35+
build-and-test-clang-20:
36+
runs-on: ubuntu-24.04
37+
steps:
38+
- uses: actions/checkout@v3
39+
- name: Add LLVM Repository
40+
run: |
41+
wget https://apt.llvm.org/llvm.sh
42+
chmod +x llvm.sh
43+
sudo ./llvm.sh 20
44+
45+
- name: Install dependencies
46+
run: |
47+
sudo apt-get update
48+
sudo apt-get install -y cmake g++-14 ninja-build clang-20 libfmt-dev libc++-20-dev libc++abi-20-dev clang-tools-20
49+
50+
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-20 200
51+
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-20 200
52+
sudo update-alternatives --install /usr/bin/clang-scan-deps clang-scan-deps /usr/bin/clang-scan-deps-20 200
53+
54+
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 140
55+
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 140
56+
57+
sudo update-alternatives --set clang /usr/bin/clang-20
58+
sudo update-alternatives --set clang++ /usr/bin/clang++-20
59+
sudo update-alternatives --set clang-scan-deps /usr/bin/clang-scan-deps-20
60+
61+
sudo update-alternatives --set gcc /usr/bin/gcc-14
62+
sudo update-alternatives --set g++ /usr/bin/g++-14
63+
64+
clang++ --version
65+
g++ --version
66+
67+
- name: ci-clang-libstdc++
68+
run: cmake --workflow --preset="ci-clang-libstdc++"
69+
70+
- name: ci-module-clang-libstdc++
71+
run: cmake --workflow --preset="ci-module-clang-libstdc++"
72+
73+
- name: ci-clang-libc++
74+
run: cmake --workflow --preset="ci-clang-libc++"
75+
76+
- name: ci-module-clang-libc++
77+
run: cmake --workflow --preset="ci-module-clang-libc++"
78+
79+
- name: ci-clang-libstdc++-c++26
80+
run: cmake --workflow --preset="ci-clang-libstdc++-c++26"
81+
82+
- name: ci-clang-libc++-c++26
83+
run: cmake --workflow --preset="ci-clang-libc++-c++26"
84+
85+
build-and-test-clang-19:
3686
runs-on: ubuntu-24.04
3787
steps:
3888
- uses: actions/checkout@v3
@@ -45,40 +95,43 @@ jobs:
4595
- name: Install dependencies
4696
run: |
4797
sudo apt-get update
48-
sudo apt-get install -y cmake g++-14 ninja-build clang-19 libfmt-dev libc++-19-dev libc++abi-19-dev
98+
sudo apt-get install -y cmake g++-14 ninja-build clang-19 libfmt-dev libc++-19-dev libc++abi-19-dev clang-tools-19
99+
49100
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-19 190
50101
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-19 190
102+
sudo update-alternatives --install /usr/bin/clang-scan-deps clang-scan-deps /usr/bin/clang-scan-deps-19 190
51103
52104
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 140
53105
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 140
54106
55107
sudo update-alternatives --set clang /usr/bin/clang-19
56108
sudo update-alternatives --set clang++ /usr/bin/clang++-19
109+
sudo update-alternatives --set clang-scan-deps /usr/bin/clang-scan-deps-19
57110
58111
sudo update-alternatives --set gcc /usr/bin/gcc-14
59112
sudo update-alternatives --set g++ /usr/bin/g++-14
60113
61114
clang++ --version
62115
g++ --version
116+
63117
- name: ci-clang-libstdc++
64118
run: cmake --workflow --preset="ci-clang-libstdc++"
119+
65120
- name: ci-clang-libc++
66121
run: cmake --workflow --preset="ci-clang-libc++"
67122

68-
- name: ci-clang-libstdc++-c++26
69-
run: cmake --workflow --preset="ci-clang-libstdc++-c++26"
70-
- name: ci-clang-libc++-c++26
71-
run: cmake --workflow --preset="ci-clang-libc++-c++26"
123+
- name: ci-module-clang-libc++
124+
run: cmake --workflow --preset="ci-module-clang-libc++"
72125

73126
build-and-test-msvc:
74127
runs-on: windows-latest
75128
steps:
76129
- uses: actions/checkout@v3
77-
- name: Configure MSVC with glaze 4.x support
78-
run: cmake -B ${{github.workspace}}/build/msvc -DCMAKE_BUILD_TYPE=Release -DSIMPLE_ENUM_USE_GLAZE_3_1=OFF -DSIMPLE_ENUM_USE_GLAZE_4_0=ON
79-
- name: Build MSVC with glaze 4.x support
130+
- name: Configure MSVC
131+
run: cmake -B ${{github.workspace}}/build/msvc -DCMAKE_BUILD_TYPE=Release
132+
- name: Build MSVC
80133
run: cmake --build ${{github.workspace}}/build/msvc
81-
- name: Test MSVC with glaze 4.x support
134+
- name: Test MSVC
82135
working-directory: ${{github.workspace}}/build/msvc
83136
run: ctest -C Debug
84137

CMakeLists.txt

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
cmake_minimum_required(VERSION 3.21)
2-
cmake_policy(
3-
SET
4-
CMP0167
5-
NEW)
6-
cmake_policy(
7-
SET
8-
CMP0175
9-
NEW)
1+
cmake_minimum_required(VERSION 3.28..3.31)
102

113
include(${CMAKE_CURRENT_LIST_DIR}/cmake/extract_version.cmake)
124

@@ -51,30 +43,80 @@ option(
5143
"Enable cmake targets"
5244
ON)
5345

54-
add_library(simple_enum INTERFACE)
46+
option(
47+
SIMPLE_ENUM_ENABLE_MODULE
48+
"Build C++23 module instead of INTERFACE library"
49+
OFF)
50+
51+
if(SIMPLE_ENUM_ENABLE_MODULE)
52+
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
53+
54+
add_library(simple_enum STATIC)
55+
target_compile_definitions(simple_enum PUBLIC SIMPLE_ENUM_CXX_MODULE)
56+
target_sources(
57+
simple_enum
58+
PRIVATE FILE_SET
59+
HEADERS
60+
BASE_DIRS
61+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
62+
PUBLIC FILE_SET
63+
ext_support
64+
TYPE
65+
HEADERS
66+
BASE_DIRS
67+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ext_support>
68+
$<INSTALL_INTERFACE:include>
69+
PUBLIC FILE_SET
70+
CXX_MODULES
71+
FILES
72+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/module/simple_enum.cxx>
73+
$<INSTALL_INTERFACE:include>)
74+
75+
else()
76+
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
77+
add_library(simple_enum INTERFACE)
78+
target_sources(
79+
simple_enum
80+
INTERFACE FILE_SET
81+
HEADERS
82+
BASE_DIRS
83+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
84+
$<INSTALL_INTERFACE:include>
85+
INTERFACE FILE_SET
86+
ext_support
87+
TYPE
88+
HEADERS
89+
BASE_DIRS
90+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ext_support>
91+
$<INSTALL_INTERFACE:include>)
92+
endif()
5593

56-
target_compile_features(simple_enum INTERFACE cxx_std_23)
94+
set_target_properties(
95+
simple_enum
96+
PROPERTIES CXX_STANDARD 23
97+
CXX_STANDARD_REQUIRED YES
98+
CXX_EXTENSIONS OFF
99+
CXX_SCAN_FOR_MODULES ${SIMPLE_ENUM_ENABLE_MODULE})
57100

58101
if(SIMPLE_ENUM_OPT_IN_STATIC_ASSERTS)
59102
target_compile_definitions(simple_enum INTERFACE SIMPLE_ENUM_OPT_IN_STATIC_ASSERTS=1)
60103
endif()
61104

62-
target_sources(
63-
simple_enum
64-
INTERFACE FILE_SET
65-
HEADERS
66-
BASE_DIRS
67-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
68-
$<INSTALL_INTERFACE:include>)
69-
70105
install(
71106
TARGETS simple_enum
72107
EXPORT simple_enum_targets
73108
INCLUDES
74109
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
75-
FILE_SET HEADERS)
110+
FILE_SET
111+
HEADERS
112+
FILE_SET
113+
ext_support
114+
FILE_SET
115+
CXX_MODULES
116+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/modules)
76117

77118
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
119+
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ext_support/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
78120

79121
configure_package_config_file(cmake/simple_enumConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/simple_enumConfig.cmake
80122
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/simple_enum)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# simple_enum
66

77
## Features
8-
8+
- **Initial c++ module support** build mode, tested with clang 19 libc++ and clang 20 libc++,libstdc++
99
- **Bounded Enum Views**: Provides `enum_view` for iterating over bounded enumerations, leveraging `std::ranges::views`.
1010
- **Enum to String and Back**: Supports conversion from enum to `std::string_view` and vice versa with minimal compile-time overhead.
1111
- **Enumeration Indexing**: Offers `enum_index`, allowing for index retrieval of enum values based on compile-time metadata.

cmake/preset-build.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@
3232
{
3333
"name": "ci-gcc",
3434
"configurePreset": "ci-gcc"
35+
},
36+
37+
{
38+
"name": "ci-module-clang-libstdc++",
39+
"configurePreset": "ci-module-clang-libstdc++"
40+
},
41+
{
42+
"name": "ci-module-clang-libc++",
43+
"configurePreset": "ci-module-clang-libc++"
44+
},
45+
{
46+
"name": "ci-module-gcc",
47+
"configurePreset": "ci-module-gcc"
3548
}
3649
]
3750
}

cmake/preset-configure.json

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"name": "clang-debug",
1818
"inherits": [ "cfg-clang", "cfg-c++23", "cfg-debug" ]
1919
},
20+
2021
{
2122
"name": "clang-debug-c++26",
2223
"inherits": [ "cfg-clang", "cfg-c++26", "cfg-debug" ]
@@ -45,29 +46,56 @@
4546
"name": "clang-release-test-asan",
4647
"inherits": [ "cfg-clang", "cfg-libc++", "cfg-release", "cfg-asan", "cfg-build-dir" ]
4748
},
49+
50+
{
51+
"name": "module-clang-debug",
52+
"inherits": [ "clang-debug", "cfg-module" ]
53+
},
54+
{
55+
"name": "module-clang-debug-libstdc++",
56+
"inherits": [ "clang-debug-libstdc++", "cfg-module" ]
57+
},
58+
{
59+
"name": "module-gcc-debug",
60+
"inherits": [ "gcc-debug", "cfg-module" ]
61+
},
62+
4863
{
4964
"name": "ci-clang-libstdc++",
50-
"inherits": [ "cfg-clang", "cfg-build-dir", "cfg-glaze", "cfg-c++23" ]
65+
"inherits": [ "cfg-clang", "cfg-build-dir", "cfg-c++23" ]
5166
},
5267
{
5368
"name": "ci-clang-libc++",
54-
"inherits": [ "cfg-clang", "cfg-libc++", "cfg-build-dir", "cfg-glaze", "cfg-c++23" ]
69+
"inherits": [ "cfg-clang", "cfg-libc++", "cfg-build-dir", "cfg-c++23" ]
5570
},
5671
{
5772
"name": "ci-gcc",
58-
"inherits": [ "cfg-gcc", "cfg-build-dir", "cfg-glaze", "cfg-c++23" ]
73+
"inherits": [ "cfg-gcc", "cfg-build-dir", "cfg-c++23" ]
5974
},
6075
{
6176
"name": "ci-clang-libstdc++-c++26",
62-
"inherits": [ "cfg-clang", "cfg-build-dir", "cfg-glaze", "cfg-c++26" ]
77+
"inherits": [ "cfg-clang", "cfg-build-dir", "cfg-c++26" ]
6378
},
6479
{
6580
"name": "ci-clang-libc++-c++26",
66-
"inherits": [ "cfg-clang", "cfg-libc++", "cfg-build-dir", "cfg-glaze", "cfg-c++26" ]
81+
"inherits": [ "cfg-clang", "cfg-libc++", "cfg-build-dir", "cfg-c++26" ]
6782
},
6883
{
6984
"name": "ci-gcc-c++26",
70-
"inherits": [ "cfg-gcc", "cfg-build-dir", "cfg-glaze", "cfg-c++26" ]
85+
"inherits": [ "cfg-gcc", "cfg-build-dir", "cfg-c++26" ]
86+
},
87+
88+
{
89+
"name": "ci-module-clang-libstdc++",
90+
"inherits": [ "ci-clang-libstdc++", "cfg-module" ]
91+
},
92+
{
93+
"name": "ci-module-clang-libc++",
94+
"inherits": [ "ci-clang-libc++", "cfg-module" ]
95+
},
96+
{
97+
"name": "ci-module-gcc",
98+
"inherits": [ "ci-gcc", "cfg-module" ]
7199
}
72100
]
73101
}

cmake/preset-options.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
"patch": 0
77
},
88
"configurePresets": [
9+
{
10+
"name": "cfg-module",
11+
"hidden": true,
12+
"cacheVariables": { "SIMPLE_ENUM_ENABLE_MODULE": "ON" }
13+
},
914
{
1015
"name": "cfg-c++23",
1116
"hidden": true,
@@ -71,14 +76,6 @@
7176
}
7277
},
7378
{
74-
"name": "cfg-glaze",
75-
"hidden": true,
76-
"cacheVariables": {
77-
"SIMPLE_ENUM_USE_GLAZE_3_1" : "OFF",
78-
"SIMPLE_ENUM_USE_GLAZE_4_0" : "ON"
79-
}
80-
},
81-
{
8279
"name": "cfg-build-dir",
8380
"hidden": true,
8481
"binaryDir": "${sourceDir}/build/${presetName}"

cmake/preset-test.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@
5050
"output": {
5151
"outputOnFailure": true
5252
}
53+
},
54+
55+
{
56+
"name": "ci-module-clang-libstdc++",
57+
"configurePreset": "ci-module-clang-libstdc++",
58+
"output": {
59+
"outputOnFailure": true
60+
}
61+
},
62+
{
63+
"name": "ci-module-clang-libc++",
64+
"configurePreset": "ci-module-clang-libc++",
65+
"output": {
66+
"outputOnFailure": true
67+
}
68+
},
69+
{
70+
"name": "ci-module-gcc",
71+
"configurePreset": "ci-module-gcc",
72+
"output": {
73+
"outputOnFailure": true
74+
}
5375
}
5476
]
5577
}

0 commit comments

Comments
 (0)