Skip to content

Commit 6a0bc94

Browse files
Feature/prepare cxx modules support (#194)
* Feat: pre-commit autoupdate and apply * Feat: Prepare CXX_MODULES build support * Make distclean better * Add the execution.cppm module again * Remove codespell hook from black section --------- Co-authored-by: Dietmar Kühl <[email protected]> Co-authored-by: Dietmar Kühl <[email protected]>
1 parent deecebc commit 6a0bc94

18 files changed

+701
-59
lines changed

.cmake-format

Lines changed: 0 additions & 18 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ CMakeUserPresets.json
3737
build
3838
.DS_store
3939
.vs
40+
.cache
4041
.vscode
42+
compile_commands.json
4143
stagedir
4244

4345
# In-source builds are not allowed
4446
CMakeCache.txt
4547
CMakeFiles/
4648

49+
*.log
4750
docs/html
4851
docs/latex

.pre-commit-config.yaml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ repos:
1111
exclude: ^\.clang-(format|tidy)$
1212
- id: check-added-large-files
1313

14+
# Config file: .codespellrc
15+
- repo: https://github.com/codespell-project/codespell
16+
rev: v2.4.1
17+
hooks:
18+
- id: codespell
19+
files: ^.*\.(cmake|cpp|hpp|txt|md|json|in|yaml|yml|py|toml)$
20+
args: ["--write", "--ignore-words", ".codespellignore" ]
21+
1422
# Clang-format for C++
1523
# This brings in a portable version of clang-format.
1624
# See also: https://github.com/ssciwr/clang-format-wheel
25+
# Config file: .clang-format
1726
- repo: https://github.com/pre-commit/mirrors-clang-format
1827
rev: v21.1.7
1928
hooks:
@@ -36,11 +45,18 @@ repos:
3645
# hooks:
3746
# - id: markdownlint
3847

39-
- repo: https://github.com/codespell-project/codespell
40-
rev: v2.4.1
48+
# Config file: pyproject.toml
49+
# first Python sort imports
50+
- repo: https://github.com/pycqa/isort
51+
rev: 7.0.0
4152
hooks:
42-
- id: codespell
43-
files: ^.*\.(cmake|cpp|hpp|txt|md|json|in|yaml|yml)$
44-
args: ["-w", "--ignore-words", ".codespellignore" ]
53+
- id: isort
54+
55+
# Config file: pyproject.toml
56+
# second Python code formatting
57+
- repo: https://github.com/psf/black
58+
rev: 25.11.0
59+
hooks:
60+
- id: black
4561

4662
exclude: 'infra/'

CMakeLists.txt

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
# cmake-format: off
1+
# gersemi: off
22
# /CMakeLists.txt -*-makefile-*-
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4-
# cmake-format: on
4+
# gersemi: on
55

66
cmake_minimum_required(VERSION 3.25...4.2)
77

8+
#========================== pre project settings ===============================
9+
# gersemi: off
10+
if(CMAKE_VERSION VERSION_EQUAL 4.2)
11+
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")
12+
13+
if(CMAKE_CXX_STDLIB_MODULES_JSON)
14+
message(
15+
STATUS
16+
"CMAKE_CXX_STDLIB_MODULES_JSON=${CMAKE_CXX_STDLIB_MODULES_JSON}"
17+
)
18+
endif()
19+
endif()
20+
# gersemi: on
21+
#===============================================================================
22+
23+
#===================================================
824
project(beman_execution VERSION 0.0.1 LANGUAGES CXX)
25+
#===================================================
926

1027
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
1128
message(FATAL_ERROR "In-source builds are not allowed!")
@@ -17,7 +34,45 @@ set(TARGET_PREFIX ${TARGET_NAMESPACE}.${TARGET_NAME})
1734
set(TARGET_LIBRARY ${PROJECT_NAME})
1835
set(TARGET_ALIAS ${TARGET_NAMESPACE}::${TARGET_NAME})
1936
set(TARGET_PACKAGE_NAME ${PROJECT_NAME}-config)
20-
set(TARGETS_EXPORT_NAME ${PROJECT_NAME}-targets)
37+
set(TARGETS_EXPORT_NAME ${PROJECT_NAME}-config-targets)
38+
39+
#========================== post project settings ==============================
40+
# Tell CMake that we explicitly want `import std`.
41+
# This will initialize the property on all targets declared after this to 1
42+
message(STATUS "CMAKE_CXX_COMPILER_IMPORT_STD=${CMAKE_CXX_COMPILER_IMPORT_STD}")
43+
if(${CMAKE_CXX_STANDARD} IN_LIST CMAKE_CXX_COMPILER_IMPORT_STD)
44+
set(CMAKE_CXX_MODULE_STD ON)
45+
message(STATUS "CMAKE_CXX_MODULE_STD=${CMAKE_CXX_MODULE_STD}")
46+
endif()
47+
48+
if(CMAKE_CXX_SCAN_FOR_MODULES AND ${CMAKE_GENERATOR} STREQUAL Ninja)
49+
set(BEMAN_USE_MODULES ON)
50+
message(STATUS "BEMAN_USE_MODULES=${BEMAN_USE_MODULES}")
51+
else()
52+
message(WARNING "Missing support for CMAKE_CXX_SCAN_FOR_MODULES!")
53+
endif()
54+
55+
# gersemi: off
56+
if(CMAKE_EXPORT_COMPILE_COMMANDS)
57+
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
58+
message(
59+
STATUS
60+
"CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}"
61+
)
62+
endif()
63+
# gersemi: on
64+
65+
# CMake requires the language standard to be specified as compile feature
66+
# when a target provides C++23 modules and the target will be installed
67+
add_library(${TARGET_NAME} STATIC)
68+
target_compile_features(${TARGET_NAME} PUBLIC cxx_std_23)
69+
70+
if(BEMAN_USE_MODULES AND CMAKE_CXX_MODULE_STD)
71+
target_compile_definitions(${TARGET_NAME} PUBLIC BEMAN_HAS_IMPORT_STD)
72+
else()
73+
message(WARNING "Missing support for CMAKE_CXX_MODULE_STD!")
74+
endif()
75+
#===============================================================================
2176

2277
option(
2378
BEMAN_EXECUTION_ENABLE_TESTING

CMakePresets.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
"binaryDir": "${sourceDir}/build/${presetName}",
99
"cacheVariables": {
1010
"CMAKE_CXX_STANDARD": "23",
11-
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
12-
"CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "./infra/cmake/use-fetch-content.cmake"
11+
"CMAKE_CXX_EXTENSIONS": true,
12+
"CMAKE_CXX_SCAN_FOR_MODULES": false,
13+
"CMAKE_CXX_STANDARD_REQUIRED": true,
14+
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
15+
"CMAKE_SKIP_TEST_ALL_DEPENDENCY": false,
16+
"CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "infra/cmake/use-fetch-content.cmake"
1317
}
1418
},
1519
{

Makefile

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,42 @@ endif
3131
LDFLAGS ?=
3232
SAN_FLAGS ?=
3333
CXX_FLAGS ?= -g
34+
# TODO: SANITIZER := release
3435
SANITIZER ?= default
3536
SOURCEDIR = $(CURDIR)
3637
BUILDROOT = build
37-
SYSTEM = $(shell uname -s)
38-
BUILD = $(BUILDROOT)/$(SYSTEM)/$(SANITIZER)
38+
export hostSystemName:=$(shell uname -s)
39+
# TODO BUILD := $(BUILDROOT)/$(SANITIZER)
40+
BUILD ?= $(BUILDROOT)/$(hostSystemName)/$(SANITIZER)
3941
EXAMPLE = beman.execution.examples.stop_token
4042

41-
export CXX=$(COMPILER)
43+
################################################
44+
ifeq (${hostSystemName},Darwin)
45+
export LLVM_PREFIX:=$(shell brew --prefix llvm)
46+
export LLVM_DIR:=$(shell realpath ${LLVM_PREFIX})
47+
export PATH:=${LLVM_DIR}/bin:${PATH}
48+
49+
# export CMAKE_CXX_STDLIB_MODULES_JSON=${LLVM_DIR}/lib/c++/libc++.modules.json
50+
# export CXX=clang++
51+
# export LDFLAGS=-L$(LLVM_DIR)/lib/c++ -lc++abi -lc++ # -lc++experimental
52+
# export GCOV="llvm-cov gcov"
53+
54+
### TODO: to test g++-15:
55+
export GCC_PREFIX:=$(shell brew --prefix gcc)
56+
export GCC_DIR:=$(shell realpath ${GCC_PREFIX})
57+
58+
export CMAKE_CXX_STDLIB_MODULES_JSON=${GCC_DIR}/lib/gcc/current/libstdc++.modules.json
59+
export CXX:=g++-15
60+
export CXXFLAGS:=-stdlib=libstdc++
61+
export GCOV="gcov"
62+
else ifeq (${hostSystemName},Linux)
63+
export LLVM_DIR=/usr/lib/llvm-20
64+
export PATH:=${LLVM_DIR}/bin:${PATH}
65+
export CXX=clang++-20
66+
else
67+
export CXX=$(COMPILER)
68+
endif
69+
################################################
4270

4371
ifeq ($(SANITIZER),release)
4472
CXX_FLAGS = -O3 -Wpedantic -Wall -Wextra -Wno-shadow -Werror
@@ -66,6 +94,9 @@ ifeq ($(SANITIZER),lsan)
6694
LDFLAGS = $(SAN_FLAGS)
6795
endif
6896

97+
# TODO: beman.execution.examples.modules
98+
# FIXME: beman.execution.execution-module.test beman.execution.stop-token-module.test
99+
69100
default: test
70101

71102
all: $(SANITIZERS)
@@ -80,29 +111,33 @@ doc:
80111
# $(MAKE) SANITIZER=$@
81112

82113
build:
83-
cmake --fresh -G Ninja -S $(SOURCEDIR) -B $(BUILD) $(TOOLCHAIN) $(SYSROOT) \
84-
-D CMAKE_EXPORT_COMPILE_COMMANDS=1 \
85-
-D CMAKE_SKIP_INSTALL_RULES=1 \
114+
cmake --fresh -G Ninja -S $(SOURCEDIR) -B $(BUILD) $(TOOLCHAIN) $(SYSROOT) \
115+
-D CMAKE_EXPORT_COMPILE_COMMANDS=ON \
116+
-D CMAKE_SKIP_INSTALL_RULES=ON \
86117
-D CMAKE_CXX_STANDARD=23 \
118+
-D CMAKE_CXX_EXTENSIONS=ON \
119+
-D CMAKE_CXX_STANDARD_REQUIRED=ON \
87120
-D CMAKE_CXX_COMPILER=$(CXX) # XXX -D CMAKE_CXX_FLAGS="$(CXX_FLAGS) $(SAN_FLAGS)"
88121
cmake --build $(BUILD)
89122

90-
# NOTE: without install! CK
123+
# NOTE: without install, see CMAKE_SKIP_INSTALL_RULES! CK
91124
test: build
92125
ctest --test-dir $(BUILD) --rerun-failed --output-on-failure
93126

94127
install: test
95128
cmake --install $(BUILD) --prefix /opt/local
96129

97-
CMakeUserPresets.json: cmake/CMakeUserPresets.json
130+
CMakeUserPresets.json:: cmake/CMakeUserPresets.json
98131
ln -s $< $@
99132

100133
release: CMakeUserPresets.json
101-
cmake --preset $@ --fresh --log-level=TRACE
134+
cmake --preset $@ --log-level=TRACE # XXX --fresh
135+
ln -fs $(BUILDROOT)/$@/compile_commands.json .
102136
cmake --workflow --preset $@
103137

104138
debug: CMakeUserPresets.json
105-
cmake --preset $@ --fresh --log-level=TRACE
139+
cmake --preset $@ --log-level=TRACE # XXX --fresh
140+
ln -fs $(BUILDROOT)build/$@/compile_commands.json .
106141
cmake --workflow --preset $@
107142

108143
ce:
@@ -123,18 +158,21 @@ check:
123158
build/$(SANITIZER)/compile_commands.json: $(SANITIZER)
124159

125160
clang-tidy: $(BUILD)/compile_commands.json
161+
ln -fs $< .
126162
run-clang-tidy -p $(BUILD) tests examples
127163

128164
codespell:
129-
codespell -L statics,snd,copyable,cancelled
165+
pre-commit run $@
130166

131-
format: cmake-format clang-format
167+
format:
168+
pre-commit run --all
132169

133170
cmake-format:
134-
pre-commit run --all
171+
pre-commit run gersemi
135172

136173
clang-format:
137-
git clang-format main
174+
pre-commit run $@
175+
# XXX TBD: git clang-format main
138176

139177
todo:
140178
bin/mk-todo.py
@@ -147,8 +185,17 @@ clean-doc:
147185
$(RM) -r docs/html docs/latex
148186

149187
clean: clean-doc
150-
cmake --build $(BUILD) --target clean
151-
$(RM) mkerr olderr *~
188+
-cmake --build $(BUILD) --target clean
189+
$(RM) mkerr olderr compile_commands.json
152190

153191
distclean: clean
154-
$(RM) -r $(BUILDROOT) stagedir
192+
$(RM) -r $(BUILDROOT) stagedir CMakeUserPresets.json
193+
find . -name '*~' -delete
194+
195+
Makefile :: ;
196+
*.txt :: ;
197+
*.json :: ;
198+
199+
# Anything we don't know how to build will use this rule.
200+
% ::
201+
ninja -C $(BUILD) $(@)

cmake/presets/CMakeDarwinPresets.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": 6,
2+
"version": 9,
33
"include": [
44
"CMakeGenericPresets.json"
55
],
@@ -8,6 +8,7 @@
88
"name": "debug-base-Darwin",
99
"hidden": true,
1010
"cacheVariables": {
11+
"CMAKE_CXX_STDLIB_MODULES_JSON": "$env{CMAKE_CXX_STDLIB_MODULES_JSON}",
1112
"CMAKE_BUILD_TYPE": "Debug"
1213
},
1314
"condition": {
@@ -20,6 +21,7 @@
2021
"name": "release-base-Darwin",
2122
"hidden": true,
2223
"cacheVariables": {
24+
"CMAKE_CXX_STDLIB_MODULES_JSON": "$env{CMAKE_CXX_STDLIB_MODULES_JSON}",
2325
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
2426
},
2527
"condition": {

cmake/presets/CMakeGenericPresets.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
"type": "path",
1313
"value": "${sourceDir}/stagedir"
1414
},
15-
"CMAKE_CXX_EXTENSIONS": true,
1615
"CMAKE_CXX_STANDARD": "23",
16+
"CMAKE_CXX_EXTENSIONS": true,
17+
"CMAKE_CXX_SCAN_FOR_MODULES": true,
1718
"CMAKE_CXX_STANDARD_REQUIRED": true,
1819
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
1920
"CMAKE_SKIP_TEST_ALL_DEPENDENCY": false

cmake/presets/CMakeLinuxPresets.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"name": "debug-base-Linux",
99
"hidden": true,
1010
"cacheVariables": {
11+
"CMAKE_CXX_STDLIB_MODULES_JSON": "$env{CMAKE_CXX_STDLIB_MODULES_JSON}",
1112
"CMAKE_BUILD_TYPE": "Debug"
1213
},
1314
"condition": {
@@ -20,6 +21,7 @@
2021
"name": "release-base-Linux",
2122
"hidden": true,
2223
"cacheVariables": {
24+
"CMAKE_CXX_STDLIB_MODULES_JSON": "$env{CMAKE_CXX_STDLIB_MODULES_JSON}",
2325
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
2426
},
2527
"condition": {

examples/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# cmake-format: off
1+
# gersemi: off
22
# examples/CMakeLists.txt -*-makefile-*-
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4-
# cmake-format: on
4+
# gersemi: on
55

66
list(
77
APPEND EXAMPLES
@@ -21,6 +21,10 @@ list(
2121
doc-just_stopped
2222
)
2323

24+
if(BEMAN_USE_MODULES)
25+
list(APPEND EXAMPLES modules) # modules.cpp
26+
endif()
27+
2428
foreach(EXAMPLE ${EXAMPLES})
2529
set(EXAMPLE_TARGET ${TARGET_PREFIX}.examples.${EXAMPLE})
2630
add_executable(${EXAMPLE_TARGET})

0 commit comments

Comments
 (0)