Skip to content

Commit e6e880b

Browse files
authored
Merge pull request #76 from wusatosi/toolchain
Introduce CMake toolchain
2 parents 4d9f05a + 553fc12 commit e6e880b

File tree

6 files changed

+177
-59
lines changed

6 files changed

+177
-59
lines changed

.github/workflows/ci_tests.yml

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,62 +49,45 @@ jobs:
4949
fail-fast: false
5050
matrix:
5151
platform:
52-
- description: "Ubuntu GCC"
53-
cpp: g++
54-
c: gcc
52+
- description: "Ubuntu GNU"
5553
os: ubuntu-latest
56-
- description: "Ubuntu Clang"
57-
cpp: clang++
58-
c: clang
54+
toolchain: "cmake/gnu-toolchain.cmake"
55+
- description: "Ubuntu LLVM"
5956
os: ubuntu-latest
57+
toolchain: "cmake/llvm-toolchain.cmake"
58+
- description: "Windows MSVC"
59+
os: windows-latest
60+
toolchain: "cmake/msvc-toolchain.cmake"
6061
cpp_version: [17, 20, 23, 26]
6162
cmake_args:
6263
- description: "Default"
63-
args: ""
6464
- description: "TSan"
65-
args: "-DCMAKE_CXX_FLAGS=-fsanitize=thread"
66-
- description: "ASan"
67-
args: "-DCMAKE_CXX_FLAGS='-fsanitize=address -fsanitize=undefined'"
65+
args: "-DBEMAN_BUILDSYS_SANITIZER=TSan"
66+
- description: "MaxSan"
67+
args: "-DBEMAN_BUILDSYS_SANITIZER=MaxSan"
6868
include:
6969
- platform:
7070
description: "Ubuntu GCC"
71-
cpp: g++
72-
c: gcc
7371
os: ubuntu-latest
72+
toolchain: "cmake/gnu-toolchain.cmake"
7473
cpp_version: 17
7574
cmake_args:
7675
description: "Werror"
7776
args: "-DCMAKE_CXX_FLAGS='-Werror=all -Werror=extra'"
7877
- platform:
7978
description: "Ubuntu GCC"
80-
cpp: g++
81-
c: gcc
8279
os: ubuntu-latest
80+
toolchain: "cmake/gnu-toolchain.cmake"
8381
cpp_version: 17
8482
cmake_args:
8583
description: "Dynamic"
8684
args: "-DBUILD_SHARED_LIBS=on"
85+
exclude:
86+
# MSVC does not support thread sanitizer
8787
- platform:
8888
description: "Windows MSVC"
89-
cpp: cl
90-
c: cl
91-
os: windows-latest
92-
cpp_version: 17
93-
cmake_args:
94-
description: "Default"
95-
args: ""
96-
- platform:
97-
description: "Windows MSVC"
98-
cpp: cl
99-
c: cl
100-
os: windows-latest
101-
cpp_version: 17
10289
cmake_args:
103-
description: "ASan"
104-
# Debug infomation needed to avoid cl: C5072
105-
# https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-c5072?view=msvc-170
106-
args: "-DCMAKE_CXX_FLAGS='/fsanitize=address /Zi'"
107-
90+
description: "TSan"
10891

10992
name: "Unit: ${{ matrix.platform.description }} ${{ matrix.cpp_version }} ${{ matrix.cmake_args.description }}"
11093
runs-on: ${{ matrix.platform.os }}
@@ -123,26 +106,13 @@ jobs:
123106
- name: Print installed softwares
124107
shell: bash
125108
run: |
126-
echo "Compiler:"
127-
128-
# cl does not have a --version option
129-
if [ "${{ matrix.platform.cpp }}" != "cl" ]; then
130-
${{ matrix.platform.cpp }} --version
131-
${{ matrix.platform.c }} --version
132-
else
133-
${{ matrix.platform.cpp }}
134-
${{ matrix.platform.c }}
135-
fi
136-
137109
echo "Build system:"
138110
cmake --version
139111
ninja --version
140112
- name: Configure CMake
141113
run: |
142-
cmake -B build -S . -DCMAKE_CXX_STANDARD=${{ matrix.cpp_version }} ${{ matrix.cmake_args.args }}
114+
cmake -B build -S . -DCMAKE_CXX_STANDARD=${{ matrix.cpp_version }} -DCMAKE_TOOLCHAIN_FILE="${{ matrix.platform.toolchain }}" ${{ matrix.cmake_args.args }}
143115
env:
144-
CC: ${{ matrix.platform.c }}
145-
CXX: ${{ matrix.platform.cpp }}
146116
CMAKE_GENERATOR: "Ninja Multi-Config"
147117
- name: Build Release
148118
run: |

CMakePresets.json

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"name": "_debug-base",
1515
"hidden": true,
1616
"cacheVariables": {
17-
"CMAKE_BUILD_TYPE": "Debug"
17+
"CMAKE_BUILD_TYPE": "Debug",
18+
"BEMAN_BUILDSYS_SANITIZER": "MaxSan"
1819
}
1920
},
2021
{
@@ -32,8 +33,7 @@
3233
"_debug-base"
3334
],
3435
"cacheVariables": {
35-
"CMAKE_CXX_COMPILER": "g++",
36-
"CMAKE_CXX_FLAGS": "-fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=leak -fsanitize=undefined"
36+
"CMAKE_TOOLCHAIN_FILE": "cmake/gnu-toolchain.cmake"
3737
}
3838
},
3939
{
@@ -44,8 +44,7 @@
4444
"_release-base"
4545
],
4646
"cacheVariables": {
47-
"CMAKE_CXX_COMPILER": "g++",
48-
"CMAKE_CXX_FLAGS": "-O3"
47+
"CMAKE_TOOLCHAIN_FILE": "cmake/gnu-toolchain.cmake"
4948
}
5049
},
5150
{
@@ -56,8 +55,7 @@
5655
"_debug-base"
5756
],
5857
"cacheVariables": {
59-
"CMAKE_CXX_COMPILER": "clang++",
60-
"CMAKE_CXX_FLAGS": "-fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined"
58+
"CMAKE_TOOLCHAIN_FILE": "cmake/appleclang-toolchain.cmake"
6159
}
6260
},
6361
{
@@ -68,8 +66,7 @@
6866
"_release-base"
6967
],
7068
"cacheVariables": {
71-
"CMAKE_CXX_COMPILER": "clang++",
72-
"CMAKE_CXX_FLAGS": "-O3"
69+
"CMAKE_TOOLCHAIN_FILE": "cmake/appleclang-toolchain.cmake"
7370
}
7471
},
7572
{
@@ -80,8 +77,7 @@
8077
"_debug-base"
8178
],
8279
"cacheVariables": {
83-
"CMAKE_CXX_COMPILER": "cl",
84-
"CMAKE_CXX_FLAGS": "/EHsc /permissive- /fsanitize=address /Zi"
80+
"CMAKE_TOOLCHAIN_FILE": "cmake/msvc-toolchain.cmake"
8581
}
8682
},
8783
{
@@ -92,8 +88,7 @@
9288
"_release-base"
9389
],
9490
"cacheVariables": {
95-
"CMAKE_CXX_COMPILER": "cl",
96-
"CMAKE_CXX_FLAGS": "/EHsc /permissive- /O2"
91+
"CMAKE_TOOLCHAIN_FILE": "cmake/msvc-toolchain.cmake"
9792
}
9893
}
9994
],

cmake/appleclang-toolchain.cmake

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
# This toolchain file is not meant to be used directly,
4+
# but to be invoked by CMake preset and GitHub CI.
5+
#
6+
# This toolchain file configures for apple clang family of compiler.
7+
# Note this is different from LLVM toolchain.
8+
#
9+
# BEMAN_BUILDSYS_SANITIZER:
10+
# This optional CMake parameter is not meant for public use and is subject to
11+
# change.
12+
# Possible values:
13+
# - MaxSan: configures clang and clang++ to use all available non-conflicting
14+
# sanitizers. Note that apple clang does not support leak sanitizer.
15+
# - TSan: configures clang and clang++ to enable the use of thread sanitizer.
16+
17+
include_guard(GLOBAL)
18+
19+
set(CMAKE_C_COMPILER clang)
20+
set(CMAKE_CXX_COMPILER clang++)
21+
22+
if(BEMAN_BUILDSYS_SANITIZER STREQUAL "MaxSan")
23+
set(SANITIZER_FLAGS
24+
"-fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined"
25+
)
26+
elseif(BEMAN_BUILDSYS_SANITIZER STREQUAL "TSan")
27+
set(SANITIZER_FLAGS "-fsanitize=thread")
28+
endif()
29+
30+
set(CMAKE_C_FLAGS_DEBUG_INIT "${SANITIZER_FLAGS}")
31+
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${SANITIZER_FLAGS}")
32+
33+
set(RELEASE_FLAGS "-O3 ${SANITIZER_FLAGS}")
34+
35+
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
36+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
37+
38+
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
39+
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")

cmake/gnu-toolchain.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
# This toolchain file is not meant to be used directly,
4+
# but to be invoked by CMake preset and GitHub CI.
5+
#
6+
# This toolchain file configures for GNU family of compiler.
7+
#
8+
# BEMAN_BUILDSYS_SANITIZER:
9+
# This optional CMake parameter is not meant for public use and is subject to
10+
# change.
11+
# Possible values:
12+
# - MaxSan: configures gcc and g++ to use all available non-conflicting
13+
# sanitizers.
14+
# - TSan: configures gcc and g++ to enable the use of thread sanitizer
15+
16+
include_guard(GLOBAL)
17+
18+
set(CMAKE_C_COMPILER gcc)
19+
set(CMAKE_CXX_COMPILER g++)
20+
21+
if(BEMAN_BUILDSYS_SANITIZER STREQUAL "MaxSan")
22+
set(SANITIZER_FLAGS
23+
"-fsanitize=address -fsanitize=leak -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined"
24+
)
25+
elseif(BEMAN_BUILDSYS_SANITIZER STREQUAL "TSan")
26+
set(SANITIZER_FLAGS "-fsanitize=thread")
27+
endif()
28+
29+
set(CMAKE_C_FLAGS_DEBUG_INIT "${SANITIZER_FLAGS}")
30+
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${SANITIZER_FLAGS}")
31+
32+
set(RELEASE_FLAGS "-O3 ${SANITIZER_FLAGS}")
33+
34+
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
35+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
36+
37+
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
38+
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")

cmake/llvm-toolchain.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
# This toolchain file is not meant to be used directly,
4+
# but to be invoked by CMake preset and GitHub CI.
5+
#
6+
# This toolchain file configures for LLVM family of compiler.
7+
#
8+
# BEMAN_BUILDSYS_SANITIZER:
9+
# This optional CMake parameter is not meant for public use and is subject to
10+
# change.
11+
# Possible values:
12+
# - MaxSan: configures clang and clang++ to use all available non-conflicting
13+
# sanitizers.
14+
# - TSan: configures clang and clang++ to enable the use of thread sanitizer.
15+
16+
include_guard(GLOBAL)
17+
18+
set(CMAKE_C_COMPILER clang)
19+
set(CMAKE_CXX_COMPILER clang++)
20+
21+
if(BEMAN_BUILDSYS_SANITIZER STREQUAL "MaxSan")
22+
set(SANITIZER_FLAGS
23+
"-fsanitize=address -fsanitize=leak -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined"
24+
)
25+
elseif(BEMAN_BUILDSYS_SANITIZER STREQUAL "TSan")
26+
set(SANITIZER_FLAGS "-fsanitize=thread")
27+
endif()
28+
29+
set(CMAKE_C_FLAGS_DEBUG_INIT "${SANITIZER_FLAGS}")
30+
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${SANITIZER_FLAGS}")
31+
32+
set(RELEASE_FLAGS "-O3 ${SANITIZER_FLAGS}")
33+
34+
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
35+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
36+
37+
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
38+
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")

cmake/msvc-toolchain.cmake

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
# This toolchain file is not meant to be used directly,
4+
# but to be invoked by CMake preset and GitHub CI.
5+
#
6+
# This toolchain file configures for MSVC family of compiler.
7+
#
8+
# BEMAN_BUILDSYS_SANITIZER:
9+
# This optional CMake parameter is not meant for public use and is subject to
10+
# change.
11+
# Possible values:
12+
# - MaxSan: configures cl to use all available non-conflicting sanitizers.
13+
#
14+
# Note that in other toolchain files, TSan is also a possible value for
15+
# BEMAN_BUILDSYS_SANITIZER, however, MSVC does not support thread sanitizer,
16+
# thus this value is omitted.
17+
18+
include_guard(GLOBAL)
19+
20+
set(CMAKE_C_COMPILER cl)
21+
set(CMAKE_CXX_COMPILER cl)
22+
23+
if(BEMAN_BUILDSYS_SANITIZER STREQUAL "MaxSan")
24+
# /Zi flag (add debug symbol) is needed when using address sanitizer
25+
# See C5072: https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-c5072
26+
set(SANITIZER_FLAGS "/fsanitize=address /Zi")
27+
endif()
28+
29+
set(CMAKE_CXX_FLAGS_DEBUG_INIT "/EHsc /permissive- ${SANITIZER_FLAGS}")
30+
set(CMAKE_C_FLAGS_DEBUG_INIT "/EHsc /permissive- ${SANITIZER_FLAGS}")
31+
32+
set(RELEASE_FLAGS "/EHsc /permissive- /O2 ${SANITIZER_FLAGS}")
33+
34+
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
35+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
36+
37+
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
38+
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")

0 commit comments

Comments
 (0)