Skip to content

Commit 29c1c3d

Browse files
committed
Feat: Prepare CXX_MODULES build support
1 parent 4cb202c commit 29c1c3d

File tree

11 files changed

+160
-28
lines changed

11 files changed

+160
-28
lines changed

.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: 57 additions & 2 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!")
@@ -19,6 +36,44 @@ set(TARGET_ALIAS ${TARGET_NAMESPACE}::${TARGET_NAME})
1936
set(TARGET_PACKAGE_NAME ${PROJECT_NAME}-config)
2037
set(TARGETS_EXPORT_NAME ${PROJECT_NAME}-targets)
2138

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+
#===============================================================================
76+
2277
option(
2378
BEMAN_EXECUTION_ENABLE_TESTING
2479
"Enable building tests and test infrastructure. Values: { ON, OFF }."

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: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,37 @@ CXX_FLAGS ?= -g
3434
SANITIZER ?= default
3535
SOURCEDIR = $(CURDIR)
3636
BUILDROOT = build
37-
SYSTEM = $(shell uname -s)
38-
BUILD = $(BUILDROOT)/$(SYSTEM)/$(SANITIZER)
37+
export hostSystemName:=$(shell uname -s)
38+
BUILD = $(BUILDROOT)/$(hostSystemName)/$(SANITIZER)
3939
EXAMPLE = beman.execution.examples.stop_token
4040

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

4369
ifeq ($(SANITIZER),release)
4470
CXX_FLAGS = -O3 -Wpedantic -Wall -Wextra -Wno-shadow -Werror
@@ -80,10 +106,12 @@ doc:
80106
# $(MAKE) SANITIZER=$@
81107

82108
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 \
109+
cmake --fresh -G Ninja -S $(SOURCEDIR) -B $(BUILD) $(TOOLCHAIN) $(SYSROOT) \
110+
-D CMAKE_EXPORT_COMPILE_COMMANDS=ON \
111+
-D CMAKE_SKIP_INSTALL_RULES=ON \
86112
-D CMAKE_CXX_STANDARD=23 \
113+
-D CMAKE_CXX_EXTENSIONS=ON \
114+
-D CMAKE_CXX_STANDARD_REQUIRED=ON \
87115
-D CMAKE_CXX_COMPILER=$(CXX) # XXX -D CMAKE_CXX_FLAGS="$(CXX_FLAGS) $(SAN_FLAGS)"
88116
cmake --build $(BUILD)
89117

@@ -94,15 +122,17 @@ test: build
94122
install: test
95123
cmake --install $(BUILD) --prefix /opt/local
96124

97-
CMakeUserPresets.json: cmake/CMakeUserPresets.json
125+
CMakeUserPresets.json:: cmake/CMakeUserPresets.json
98126
ln -s $< $@
99127

100128
release: CMakeUserPresets.json
101129
cmake --preset $@ --fresh --log-level=TRACE
130+
ln -fs $(BUILDROOT)/$@/compile_commands.json .
102131
cmake --workflow --preset $@
103132

104133
debug: CMakeUserPresets.json
105134
cmake --preset $@ --fresh --log-level=TRACE
135+
ln -fs $(BUILDROOT)build/$@/compile_commands.json .
106136
cmake --workflow --preset $@
107137

108138
ce:
@@ -123,18 +153,21 @@ check:
123153
build/$(SANITIZER)/compile_commands.json: $(SANITIZER)
124154

125155
clang-tidy: $(BUILD)/compile_commands.json
156+
ln -fs $< .
126157
run-clang-tidy -p $(BUILD) tests examples
127158

128159
codespell:
129-
codespell -L statics,snd,copyable,cancelled
160+
pre-commit run $@
130161

131-
format: cmake-format clang-format
162+
format:
163+
pre-commit run --all
132164

133165
cmake-format:
134-
pre-commit run --all
166+
pre-commit run gersemi
135167

136168
clang-format:
137-
git clang-format main
169+
pre-commit run $@
170+
# XXX TBD: git clang-format main
138171

139172
todo:
140173
bin/mk-todo.py
@@ -147,8 +180,16 @@ clean-doc:
147180
$(RM) -r docs/html docs/latex
148181

149182
clean: clean-doc
150-
cmake --build $(BUILD) --target clean
183+
-cmake --build $(BUILD) --target clean
151184
$(RM) mkerr olderr *~
152185

153186
distclean: clean
154187
$(RM) -r $(BUILDROOT) stagedir
188+
189+
Makefile :: ;
190+
*.txt :: ;
191+
*.json :: ;
192+
193+
# Anything we don't know how to build will use this rule.
194+
% ::
195+
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: 2 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

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[tool.black]
2+
line-length = 88
3+
4+
[tool.isort]
5+
profile = "black"
6+
line_length = 88

0 commit comments

Comments
 (0)