Skip to content

Commit a783e8e

Browse files
committed
adapted and changed cmake files
1 parent 405b9f7 commit a783e8e

9 files changed

Lines changed: 90 additions & 122 deletions

CMakeLists.txt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
1+
CMAKE_MINIMUM_REQUIRED(VERSION 3.25)
22

33
INCLUDE(cmake/Conan.cmake)
4+
INCLUDE(cmake/ProjectSettings.cmake)
45

56
# strongly encouraged to enable this globally to avoid conflicts between
67
# -Wpedantic being enabled and -std=c++20 and -std=gnu++20 for example
@@ -10,15 +11,9 @@ SET(CMAKE_CXX_EXTENSIONS OFF)
1011
INCLUDE(cmake/Options.cmake)
1112

1213
# Set the project name to your project name, my project isn't very descriptive
13-
PROJECT(myproject
14+
PROJECT(${PROJECT_NAME}
1415
LANGUAGES CXX
15-
VERSION 0.0.1)
16-
17-
IF(NOT DEFINED CXX_STANDARD)
18-
SET(CXX_STANDARD 20)
19-
ENDIF()
20-
21-
SET(CMAKE_CXX_STANDARD ${CXX_STANDARD})
16+
VERSION ${VERSION})
2217

2318
INCLUDE(cmake/BuildingConfig.cmake)
2419
SETUP_MULTI_CONFIG()
@@ -37,7 +32,7 @@ CONFIGURE_FILE("templates/version.hpp.in" "${CMAKE_BINARY_DIR}/generated/include
3732

3833
# Link this 'library' to set the c++ standard / compile-time options requested
3934
ADD_LIBRARY(project_options INTERFACE)
40-
TARGET_COMPILE_FEATURES(project_options INTERFACE cxx_std_${CXX_STANDARD})
35+
TARGET_COMPILE_FEATURES(project_options INTERFACE cxx_std_${CMAKE_CXX_STANDARD})
4136

4237
# Link this 'library' to use the warnings specified in CompilerWarnings.cmake
4338
ADD_LIBRARY(project_warnings INTERFACE)

cmake/CodeFormat.cmake

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
FIND_PACKAGE(Python COMPONENTS Interpreter REQUIRED)
2-
31
# To exclude directories from the format check, add corresponding clang-format config files into those directories.
2+
FIND_PROGRAM(CLANG_FORMAT_BINARY NAMES clang-format REQUIRED)
3+
FIND_PROGRAM(CLANG_TIDY_BINARY NAMES clang-tidy REQUIRED)
4+
45
ADD_CUSTOM_TARGET(clang-format-check
56
USES_TERMINAL
6-
COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run-clang-format.py -warnings-as-errors
7-
)
7+
COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run-clang-format.py
8+
-clang-format-binary ${CLANG_FORMAT_BINARY}
9+
-warnings-as-errors
10+
)
811

9-
ADD_CUSTOM_TARGET(clang-format-check-fix
12+
ADD_CUSTOM_TARGET(clang-format-fix
1013
USES_TERMINAL
11-
COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run-clang-format.py -fix
12-
)
14+
COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run-clang-format.py
15+
-clang-format-binary ${CLANG_FORMAT_BINARY}
16+
-fix
17+
)
1318

1419
ADD_CUSTOM_TARGET(clang-tidy-check
1520
USES_TERMINAL
21+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
1622
COMMAND ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run-clang-tidy.py
23+
-clang-tidy-binary ${CLANG_TIDY_BINARY}
24+
-p ${CMAKE_BINARY_DIR}
1725
)
1826

1927
ADD_CUSTOM_TARGET(clang-tidy-diff-check
2028
USES_TERMINAL
2129
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
22-
COMMAND git diff -U0 HEAD --no-prefix | ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run-clang-tidy-diff.py -path ${CMAKE_BINARY_DIR}
30+
COMMAND git diff -U0 HEAD --no-prefix | ${Python_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/run-clang-tidy-diff.py
31+
-clang-tidy-binary ${CLANG_TIDY_BINARY}
32+
-path ${CMAKE_BINARY_DIR}
2333
)

cmake/CompilerWarnings.cmake

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ FUNCTION(SET_PROJECT_WARNINGS project_name)
2020
/w14546 # function call before comma missing argument list
2121
/w14547 # 'operator': operator before comma has no effect; expected operator with side-effect
2222
/w14549 # 'operator': operator before comma has no effect; did you intend 'operator'?
23-
/w14555 # expression has no effect; expected expression with side- effect
23+
/w14555 # expression has no effect; expected expression with side-effect
2424
/w14619 # pragma warning: there is no warning number 'number'
2525
/w14640 # Enable warning on thread un-safe static member initialization
2626
/w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may cause unexpected runtime behavior.
@@ -46,21 +46,17 @@ FUNCTION(SET_PROJECT_WARNINGS project_name)
4646
-Wnull-dereference # warn if a null dereference is detected
4747
-Wdouble-promotion # warn if float is implicit promoted to double
4848
-Wformat=2 # warn on security issues around functions that format output (ie printf)
49-
-Wimplicit-fallthrough # warn on statements that fallthrough without an explicit annotation
49+
-Wimplicit-fallthrough # warn on implicit fallthrough in switch statements
5050
)
5151

52-
IF(WARNINGS_AS_ERRORS)
53-
SET(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror)
54-
SET(MSVC_WARNINGS ${MSVC_WARNINGS} /WX)
55-
ENDIF()
56-
5752
SET(GCC_WARNINGS
5853
${CLANG_WARNINGS}
5954
-Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist
6055
-Wduplicated-cond # warn if if / else chain has duplicated conditions
6156
-Wduplicated-branches # warn if if / else branches have duplicated code
6257
-Wlogical-op # warn about logical operations being used where bitwise were probably wanted
6358
-Wuseless-cast # warn if you perform a cast to the same type
59+
-Wno-interference-size # suppress noisy C++17/20 ABI warning on GCC >= 12
6460
)
6561

6662
IF(MSVC)
@@ -73,6 +69,11 @@ FUNCTION(SET_PROJECT_WARNINGS project_name)
7369
MESSAGE(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
7470
ENDIF()
7571

76-
TARGET_COMPILE_OPTIONS(${project_name} INTERFACE ${PROJECT_WARNINGS})
72+
IF(WARNINGS_AS_ERRORS)
73+
LIST(APPEND PROJECT_WARNINGS $<IF:$<CXX_COMPILER_ID:MSVC>,/WX,-Werror>)
74+
ENDIF()
75+
76+
TARGET_COMPILE_OPTIONS(${project_name} INTERFACE
77+
$<$<COMPILE_LANGUAGE:CXX>:${PROJECT_WARNINGS}>)
7778

7879
ENDFUNCTION()

cmake/Doxygen.cmake

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,10 @@
11
FUNCTION(ENABLE_DOXYGEN)
22
IF(ENABLE_DOXYGEN)
3-
# If not specified, use the top readme file as the first page
4-
IF((NOT DOXYGEN_USE_MDFILE_AS_MAINPAGE) AND EXISTS "${PROJECT_SOURCE_DIR}/README.md")
5-
SET(DOXYGEN_USE_MDFILE_AS_MAINPAGE "${PROJECT_SOURCE_DIR}/README.md")
6-
ENDIF()
7-
8-
# set better defaults for doxygen
9-
IS_VERBOSE(_is_verbose)
10-
IF(NOT ${_is_verbose})
11-
SET(DOXYGEN_QUIET YES)
12-
ENDIF()
133
SET(DOXYGEN_CALLER_GRAPH YES)
144
SET(DOXYGEN_CALL_GRAPH YES)
155
SET(DOXYGEN_EXTRACT_ALL YES)
16-
SET(DOXYGEN_GENERATE_TREEVIEW YES)
17-
# svg files are much smaller than jpeg and png, and yet they have higher quality
18-
SET(DOXYGEN_DOT_IMAGE_FORMAT svg)
19-
SET(DOXYGEN_DOT_TRANSPARENT YES)
20-
21-
# If not specified, exclude the vcpkg files and the files CMake downloads under _deps (like project_options)
22-
IF(NOT DOXYGEN_EXCLUDE_PATTERNS)
23-
SET(DOXYGEN_EXCLUDE_PATTERNS "${CMAKE_CURRENT_BINARY_DIR}/vcpkg_installed/*" "${CMAKE_CURRENT_BINARY_DIR}/_deps/*")
24-
ENDIF()
25-
26-
IF("${DOXYGEN_THEME}" STREQUAL "")
27-
SET(DOXYGEN_THEME "awesome-sidebar")
28-
ENDIF()
29-
30-
IF("${DOXYGEN_THEME}" STREQUAL "awesome" OR "${DOXYGEN_THEME}" STREQUAL "awesome-sidebar")
31-
# use a modern doxygen theme
32-
# https://github.com/jothepro/doxygen-awesome-css v2.0.0
33-
FETCHCONTENT_DECLARE(_doxygen_theme
34-
URL https://github.com/jothepro/doxygen-awesome-css/archive/refs/tags/v2.2.0.zip)
35-
FETCHCONTENT_MAKEAVAILABLE(_doxygen_theme)
36-
IF("${DOXYGEN_THEME}" STREQUAL "awesome" OR "${DOXYGEN_THEME}" STREQUAL "awesome-sidebar")
37-
SET(DOXYGEN_HTML_EXTRA_STYLESHEET "${_doxygen_theme_SOURCE_DIR}/doxygen-awesome.css")
38-
ENDIF()
39-
IF("${DOXYGEN_THEME}" STREQUAL "awesome-sidebar")
40-
SET(DOXYGEN_HTML_EXTRA_STYLESHEET ${DOXYGEN_HTML_EXTRA_STYLESHEET}
41-
"${_doxygen_theme_SOURCE_DIR}/doxygen-awesome-sidebar-only.css")
42-
ENDIF()
43-
ELSE()
44-
# use the original doxygen theme
45-
ENDIF()
46-
47-
# find doxygen and dot if available
48-
FIND_PACKAGE(Doxygen REQUIRED OPTIONAL_COMPONENTS dot)
49-
50-
# add doxygen-docs target
51-
MESSAGE(STATUS "Adding `doxygen-docs` target that builds the documentation.")
52-
DOXYGEN_ADD_DOCS(doxygen-docs ALL ${PROJECT_SOURCE_DIR}
53-
COMMENT "Generating documentation - entry file: ${CMAKE_CURRENT_BINARY_DIR}/html/index.html")
54-
6+
SET(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/docs/doxygen")
7+
FIND_PACKAGE(Doxygen REQUIRED dot)
8+
DOXYGEN_ADD_DOCS(doxygen-docs ${PROJECT_SOURCE_DIR})
559
ENDIF()
5610
ENDFUNCTION()

cmake/Options.cmake

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
IF(NOT MSVC)
2-
OPTION(ENABLE_BUILD_WITH_TIME_TRACE "Enable -ftime-trace to generate time tracing .json files on clang" OFF)
3-
ENDIF()
1+
INCLUDE(CMakeDependentOption)
2+
cmake_dependent_option(ENABLE_BUILD_WITH_TIME_TRACE
3+
"Enable -ftime-trace to generate time tracing .json files on clang"
4+
OFF "NOT MSVC" OFF)
45

56
# Very basic PCH example
67
OPTION(ENABLE_PCH "Enable Precompiled Headers" OFF)
@@ -16,8 +17,6 @@ OPTION(ENABLE_DOXYGEN "Enable doxygen doc builds of source" OFF)
1617

1718
# Sanitizers
1819
OPTION(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" OFF)
19-
OPTION(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" OFF)
20-
OPTION(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" OFF)
2120
OPTION(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" OFF)
2221
OPTION(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" OFF)
2322

@@ -29,6 +28,14 @@ OPTION(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF)
2928
OPTION(ENABLE_TESTING "Enable Test Builds" ON)
3029
OPTION(ENABLE_FUZZING "Enable Fuzzing Builds" OFF)
3130

31+
# Sanitizer mutual-exclusion guards
32+
cmake_dependent_option(ENABLE_SANITIZER_THREAD
33+
"Enable thread sanitizer"
34+
OFF "NOT ENABLE_SANITIZER_ADDRESS;NOT ENABLE_SANITIZER_LEAK" OFF)
35+
cmake_dependent_option(ENABLE_SANITIZER_MEMORY
36+
"Enable memory sanitizer (Clang only)"
37+
OFF "NOT ENABLE_SANITIZER_ADDRESS;NOT ENABLE_SANITIZER_THREAD;NOT ENABLE_SANITIZER_LEAK" OFF)
38+
3239
# examples
3340
OPTION(CPP_STARTER_USE_SML "Enable compilation of SML sample" OFF)
3441
OPTION(CPP_STARTER_USE_BOOST_BEAST "Enable compilation of boost beast sample" OFF)
@@ -38,6 +45,8 @@ OPTION(CPP_STARTER_USE_EMBEDDED_TOOLCHAIN "Enable compilation of an example cort
3845
OPTION(CPP_STARTER_USE_QT "Enable compilation of an example QT project" OFF)
3946
OPTION(CPP_STARTER_USE_OPEN62541PP "Enable compilation of an example open62541pp wrapper project" OFF)
4047
OPTION(CPP_STARTER_USE_OPEN62541 "Enable compilation of an example open62541 project" OFF)
48+
OPTION(CPP_STARTER_USE_SLINT "Enable compilation of an example slint project" OFF)
49+
OPTION(CPP_STARTER_USE_IMGUI "Enable compilation of an example imgui project" OFF)
4150

4251
# test frameworks
4352
OPTION(CPP_STARTER_USE_CATCH2 "Enable compilation of an example test project using catch2" ON)

cmake/ProjectSettings.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SET(PROJECT_NAME myproject)
2+
SET(VERSION 0.0.1)
3+
4+
# C++ standard — override at configure time with -DCXX_STANDARD=23 etc.
5+
IF(NOT DEFINED CXX_STANDARD)
6+
SET(CXX_STANDARD 20)
7+
ENDIF()
8+
SET(CMAKE_CXX_STANDARD ${CXX_STANDARD} CACHE STRING "C++ standard to use" FORCE)
9+
SET_PROPERTY(CACHE CMAKE_CXX_STANDARD PROPERTY STRINGS 17 20 23 26)

cmake/Sanitizers.cmake

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ FUNCTION(ENABLE_SANITIZERS project_name)
33
IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
44

55
IF(ENABLE_COVERAGE)
6-
TARGET_COMPILE_OPTIONS(${project_name} INTERFACE --coverage -O0 -g)
6+
TARGET_COMPILE_OPTIONS(${project_name} INTERFACE
7+
$<$<COMPILE_LANGUAGE:CXX>:--coverage>
8+
$<$<COMPILE_LANGUAGE:CXX>:-O0>
9+
$<$<COMPILE_LANGUAGE:CXX>:-g>)
710
TARGET_LINK_LIBRARIES(${project_name} INTERFACE --coverage)
811
ENDIF()
912

@@ -39,22 +42,25 @@ FUNCTION(ENABLE_SANITIZERS project_name)
3942
ENDIF()
4043
ENDIF()
4144

42-
LIST(
43-
JOIN
44-
SANITIZERS
45-
","
46-
LIST_OF_SANITIZERS)
45+
LIST(JOIN SANITIZERS "," LIST_OF_SANITIZERS)
4746

48-
ENDIF()
49-
50-
IF(LIST_OF_SANITIZERS)
51-
IF(NOT
52-
"${LIST_OF_SANITIZERS}"
53-
STREQUAL
54-
"")
47+
IF(LIST_OF_SANITIZERS)
5548
TARGET_COMPILE_OPTIONS(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
5649
TARGET_LINK_OPTIONS(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS})
5750
ENDIF()
51+
52+
ELSEIF(MSVC)
53+
54+
IF(ENABLE_SANITIZER_ADDRESS)
55+
MESSAGE(STATUS "Enabling MSVC AddressSanitizer (/fsanitize=address)")
56+
TARGET_COMPILE_OPTIONS(${project_name} INTERFACE /fsanitize=address)
57+
TARGET_LINK_OPTIONS(${project_name} INTERFACE /INCREMENTAL:NO)
58+
ENDIF()
59+
IF(ENABLE_SANITIZER_LEAK OR ENABLE_SANITIZER_UNDEFINED_BEHAVIOR OR
60+
ENABLE_SANITIZER_THREAD OR ENABLE_SANITIZER_MEMORY)
61+
MESSAGE(WARNING "MSVC only supports AddressSanitizer; other sanitizer options are ignored.")
62+
ENDIF()
63+
5864
ENDIF()
5965

6066
ENDFUNCTION()

cmake/StandardProjectSettings.cmake

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,15 @@ ENDIF()
1717
# Generate compile_commands.json to make it easier to work with clang based tools
1818
SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1919

20-
# Enhance error reporting and compiler messages
21-
IF(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
22-
IF(ENABLE_BUILD_WITH_TIME_TRACE)
23-
TARGET_COMPILE_OPTIONS(project_options INTERFACE -ftime-trace)
24-
ENDIF()
2520

26-
IF(WIN32)
27-
# On Windows cuda nvcc uses cl and not clang
28-
ADD_COMPILE_OPTIONS($<$<COMPILE_LANGUAGE:C>:-fcolor-diagnostics> $<$<COMPILE_LANGUAGE:CXX>:-fcolor-diagnostics>)
29-
ELSE()
30-
ADD_COMPILE_OPTIONS(-fcolor-diagnostics)
31-
ENDIF()
21+
IF(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
22+
ADD_COMPILE_OPTIONS($<$<COMPILE_LANGUAGE:CXX>:-fcolor-diagnostics>)
3223
ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
33-
IF(WIN32)
34-
# On Windows cuda nvcc uses cl and not gcc
35-
ADD_COMPILE_OPTIONS($<$<COMPILE_LANGUAGE:C>:-fdiagnostics-color=always>
36-
$<$<COMPILE_LANGUAGE:CXX>:-fdiagnostics-color=always>)
37-
ELSE()
38-
ADD_COMPILE_OPTIONS(-fdiagnostics-color=always)
39-
ENDIF()
40-
ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER 1900)
41-
ADD_COMPILE_OPTIONS(/diagnostics:column)
24+
ADD_COMPILE_OPTIONS($<$<COMPILE_LANGUAGE:CXX>:-fdiagnostics-color=always>)
4225
ELSE()
4326
MESSAGE(STATUS "No colored compiler diagnostic set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
4427
ENDIF()
4528

46-
# run vcvarsall when msvc is used
47-
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/VCEnvironment.cmake")
48-
RUN_VCVARSALL()
29+
IF(MSVC)
30+
SET(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
31+
ENDIF()

cmake/StaticAnalyzers.cmake

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ IF(ENABLE_CPPCHECK)
66
--suppress=missingInclude
77
--enable=all
88
--inline-suppr
9-
--inconclusive
10-
-i
11-
${CMAKE_SOURCE_DIR}/imgui/lib)
9+
--inconclusive)
1210
ELSE()
1311
MESSAGE(SEND_ERROR "cppcheck requested but executable not found")
1412
ENDIF()
1513
ENDIF()
1614

1715
IF(ENABLE_CLANG_TIDY)
18-
FIND_PROGRAM(CLANGTIDY clang-tidy)
19-
IF(CLANGTIDY)
20-
SET(CMAKE_CXX_CLANG_TIDY ${CLANGTIDY} -extra-arg=-Wno-unknown-warning-option)
16+
FIND_PROGRAM(CLANG_TIDY_BINARY clang-tidy)
17+
IF(CLANG_TIDY_BINARY)
18+
SET(CMAKE_CXX_CLANG_TIDY
19+
${CLANG_TIDY_BINARY}
20+
-extra-arg=-Wno-unknown-warning-option
21+
-p=${CMAKE_BINARY_DIR})
2122
ELSE()
2223
MESSAGE(SEND_ERROR "clang-tidy requested but executable not found")
2324
ENDIF()

0 commit comments

Comments
 (0)