Skip to content

Commit 412fa5f

Browse files
committed
cleanup cmake
- cleaned up main CMakeLists.txt - created new cmake files - adapted CMake structure to new CMake template from jason turner
1 parent 3670701 commit 412fa5f

12 files changed

Lines changed: 370 additions & 88 deletions

CMakeLists.txt

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CMAKE_MINIMUM_REQUIRED(VERSION 3.16...3.23)
1+
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
22

33
# strongly encouraged to enable this globally to avoid conflicts between
44
# -Wpedantic being enabled and -std=c++20 and -std=gnu++20 for example
@@ -18,51 +18,25 @@ ENDIF()
1818

1919
SET(CMAKE_CXX_STANDARD ${CXX_STANDARD})
2020

21-
GET_PROPERTY(BUILDING_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
22-
IF(BUILDING_MULTI_CONFIG)
23-
IF(NOT CMAKE_BUILD_TYPE)
24-
# Make sure that all supported configuration types have their
25-
# associated conan packages available. You can reduce this
26-
# list to only the configuration types you use, but only if one
27-
# is not forced-set on the command line for VS
28-
MESSAGE(TRACE "Setting up multi-config build types")
29-
SET(CMAKE_CONFIGURATION_TYPES
30-
Debug
31-
Release
32-
RelWithDebInfo
33-
MinSizeRel
34-
CACHE STRING "Enabled build types" FORCE)
35-
ELSE()
36-
MESSAGE(TRACE "User chose a specific build type, so we are using that")
37-
SET(CMAKE_CONFIGURATION_TYPES
38-
${CMAKE_BUILD_TYPE}
39-
CACHE STRING "Enabled build types" FORCE)
40-
ENDIF()
41-
ENDIF()
21+
INCLUDE(cmake/BuildingConfig.cmake)
22+
SETUP_MULTI_CONFIG()
4223

4324
INCLUDE(cmake/StandardProjectSettings.cmake)
4425
INCLUDE(cmake/PreventInSourceBuilds.cmake)
4526
INCLUDE(cmake/CodeFormat.cmake)
27+
INCLUDE(cmake/InterproceduralOptimization.cmake)
28+
ENABLE_IPO()
4629

47-
EXECUTE_PROCESS(
48-
COMMAND git log -1 --format=%h
49-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
50-
OUTPUT_VARIABLE GIT_HASH
51-
OUTPUT_STRIP_TRAILING_WHITESPACE
52-
)
30+
# fetch git hash information for configure version template file
31+
INCLUDE(cmake/GitInformation.cmake)
32+
GET_GIT_HASH()
5333

5434
CONFIGURE_FILE("templates/version.hpp.in" "${CMAKE_BINARY_DIR}/generated/include/version.hpp" ESCAPE_QUOTES)
5535

5636
# Link this 'library' to set the c++ standard / compile-time options requested
5737
ADD_LIBRARY(project_options INTERFACE)
5838
TARGET_COMPILE_FEATURES(project_options INTERFACE cxx_std_${CXX_STANDARD})
5939

60-
IF(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
61-
IF(ENABLE_BUILD_WITH_TIME_TRACE)
62-
TARGET_COMPILE_OPTIONS(project_options INTERFACE -ftime-trace)
63-
ENDIF()
64-
ENDIF()
65-
6640
# Link this 'library' to use the warnings specified in CompilerWarnings.cmake
6741
ADD_LIBRARY(project_warnings INTERFACE)
6842

@@ -84,18 +58,9 @@ ENABLE_DOXYGEN()
8458
# allow for static analysis options
8559
INCLUDE(cmake/StaticAnalyzers.cmake)
8660

87-
IF(ENABLE_PCH)
88-
# This sets a global PCH parameter, each project will build its own PCH, which is a good idea if any #define's change
89-
#
90-
# consider breaking this out per project as necessary
91-
TARGET_PRECOMPILE_HEADERS(
92-
project_options
93-
INTERFACE
94-
<vector>
95-
<string>
96-
<map>
97-
<utility>)
98-
ENDIF()
61+
# enabled precompiled headers
62+
INCLUDE(cmake/PrecompiledHeader.cmake)
63+
ENABLE_PCH()
9964

10065
INCLUDE(cmake/Conan.cmake)
10166
RUN_CONAN()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Ongoing project of the Zühlke Germany **Modern C++ Topic Group**.
88
With this project, we want to provide an example and starting point for C++ projects (embedded and otherwise), especially regarding tooling.
99

1010
The project has initially been forked/copied from [Jason Turner's cpp_starter_project](https://github.com/lefticus/cpp_starter_project) and is customised by Zühlke members and adapted to [Jason Turner's cmake_conan_boilerplate_template](https://github.com/cpp-best-practices/cmake_conan_boilerplate_template).
11-
11+
It also uses CMake structure from [Jason Turner's cmake_template](https://github.com/cpp-best-practices/cmake_template) repository.
1212

1313
## Build Status
1414

cmake/BuildingConfig.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FUNCTION(SETUP_MULTI_CONFIG)
2+
GET_PROPERTY(BUILDING_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
3+
IF(BUILDING_MULTI_CONFIG)
4+
IF(NOT CMAKE_BUILD_TYPE)
5+
# Make sure that all supported configuration types have their
6+
# associated conan packages available. You can reduce this
7+
# list to only the configuration types you use, but only if one
8+
# is not forced-set on the command line for VS
9+
MESSAGE(TRACE "Setting up multi-config build types")
10+
SET(CMAKE_CONFIGURATION_TYPES
11+
Debug
12+
Release
13+
RelWithDebInfo
14+
MinSizeRel
15+
CACHE STRING "Enabled build types" FORCE)
16+
ELSE()
17+
MESSAGE(TRACE "User chose a specific build type, so we are using that")
18+
SET(CMAKE_CONFIGURATION_TYPES
19+
${CMAKE_BUILD_TYPE}
20+
CACHE STRING "Enabled build types" FORCE)
21+
ENDIF()
22+
ENDIF()
23+
ENDFUNCTION()

cmake/CompilerWarnings.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md
44

55
FUNCTION(SET_PROJECT_WARNINGS project_name)
6-
OPTION(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE)
76

87
SET(MSVC_WARNINGS
98
/W4 # Baseline reasonable warnings
@@ -47,6 +46,7 @@ FUNCTION(SET_PROJECT_WARNINGS project_name)
4746
-Wnull-dereference # warn if a null dereference is detected
4847
-Wdouble-promotion # warn if float is implicit promoted to double
4948
-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
5050
)
5151

5252
IF(WARNINGS_AS_ERRORS)

cmake/Doxygen.cmake

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
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()
313
SET(DOXYGEN_CALLER_GRAPH YES)
414
SET(DOXYGEN_CALL_GRAPH YES)
515
SET(DOXYGEN_EXTRACT_ALL YES)
6-
FIND_PACKAGE(Doxygen REQUIRED dot)
7-
DOXYGEN_ADD_DOCS(doxygen-docs ${PROJECT_SOURCE_DIR})
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")
854

955
ENDIF()
1056
ENDFUNCTION()

cmake/GitInformation.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FUNCTION(GET_GIT_HASH)
2+
EXECUTE_PROCESS(
3+
COMMAND git log -1 --format=%h
4+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
5+
OUTPUT_VARIABLE HASH
6+
OUTPUT_STRIP_TRAILING_WHITESPACE
7+
)
8+
9+
SET(GIT_HASH ${HASH} PARENT_SCOPE)
10+
ENDFUNCTION()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FUNCTION(ENABLE_IPO)
2+
IF(ENABLE_IPO)
3+
INCLUDE(CheckIPOSupported)
4+
CHECK_IPO_SUPPORTED(RESULT result OUTPUT output)
5+
IF(result)
6+
SET(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
7+
ELSE()
8+
MESSAGE(SEND_ERROR "IPO is not supported: ${output}")
9+
ENDIF()
10+
ENDIF()
11+
ENDFUNCTION()

cmake/Options.cmake

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,36 @@ IF(NOT MSVC)
22
OPTION(ENABLE_BUILD_WITH_TIME_TRACE "Enable -ftime-trace to generate time tracing .json files on clang" OFF)
33
ENDIF()
44

5-
OPTION(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF)
6-
OPTION(ENABLE_TESTING "Enable Test Builds" ON)
7-
OPTION(ENABLE_FUZZING "Enable Fuzzing Builds" OFF)
8-
95
# Very basic PCH example
106
OPTION(ENABLE_PCH "Enable Precompiled Headers" OFF)
117

12-
option(ENABLE_CPPCHECK "Enable static analysis with cppcheck" OFF)
13-
option(ENABLE_CLANG_TIDY "Enable static analysis with clang-tidy" OFF)
14-
option(ENABLE_INCLUDE_WHAT_YOU_USE "Enable static analysis with include-what-you-use" OFF)
8+
# static analyzers
9+
OPTION(ENABLE_CPPCHECK "Enable static analysis with cppcheck" OFF)
10+
OPTION(ENABLE_CLANG_TIDY "Enable static analysis with clang-tidy" OFF)
11+
OPTION(ENABLE_INCLUDE_WHAT_YOU_USE "Enable static analysis with include-what-you-use" OFF)
1512

13+
# tooling
1614
OPTION(ENABLE_CACHE "Enable cache if available" ON)
17-
option(ENABLE_DOXYGEN "Enable doxygen doc builds of source" OFF)
15+
OPTION(ENABLE_DOXYGEN "Enable doxygen doc builds of source" OFF)
1816

1917
# Sanitizers
20-
OPTION(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" FALSE)
21-
OPTION(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" FALSE)
22-
OPTION(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" FALSE)
23-
OPTION(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" FALSE)
24-
OPTION(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" FALSE)
25-
26-
OPTION(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" FALSE)
27-
18+
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)
21+
OPTION(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" OFF)
22+
OPTION(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" OFF)
23+
24+
# others
25+
OPTION(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" OFF)
2826
OPTION(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" OFF)
27+
OPTION(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" ON)
28+
OPTION(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF)
29+
OPTION(ENABLE_TESTING "Enable Test Builds" ON)
30+
OPTION(ENABLE_FUZZING "Enable Fuzzing Builds" OFF)
2931

3032
# examples
3133
OPTION(CPP_STARTER_USE_SML "Enable compilation of SML sample" OFF)
3234
OPTION(CPP_STARTER_USE_BOOST_BEAST "Enable compilation of boost beast sample" OFF)
3335
OPTION(CPP_STARTER_USE_CROW "Enable compilation of crow sample" OFF)
3436
OPTION(CPP_STARTER_USE_CPPZMQ_PROTO "Enable compilation of protobuf and cppzmq sample" OFF)
3537
OPTION(CPP_STARTER_USE_EMBEDDED_TOOLCHAIN "Enable compilation of an example cortex m4 project" OFF)
36-
37-
# Note: by default ENABLE_DEVELOPER_MODE is True
38-
# This means that all analysis (sanitizers, static analysis)
39-
# is enabled and all warnings are treated as errors
40-
# if you want to switch this behavior, change TRUE to FALSE
41-
SET(ENABLE_DEVELOPER_MODE
42-
TRUE
43-
CACHE BOOL "Enable 'developer mode'")

cmake/PrecompiledHeader.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FUNCTION(ENABLE_PCH)
2+
IF(ENABLE_PCH)
3+
# This sets a global PCH parameter, each project will build its own PCH, which is a good idea if any #define's change
4+
#
5+
# consider breaking this out per project as necessary
6+
TARGET_PRECOMPILE_HEADERS(
7+
project_options
8+
INTERFACE
9+
<vector>
10+
<string>
11+
<map>
12+
<utility>)
13+
ENDIF()
14+
ENDFUNCTION()

cmake/StandardProjectSettings.cmake

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,32 @@ 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-
IF(ENABLE_IPO)
21-
INCLUDE(CheckIPOSupported)
22-
CHECK_IPO_SUPPORTED(
23-
RESULT
24-
result
25-
OUTPUT
26-
output)
27-
IF(result)
28-
SET(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
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()
25+
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>)
2929
ELSE()
30-
MESSAGE(SEND_ERROR "IPO is not supported: ${output}")
30+
ADD_COMPILE_OPTIONS(-fcolor-diagnostics)
3131
ENDIF()
32-
ENDIF()
33-
IF(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
34-
ADD_COMPILE_OPTIONS(-fcolor-diagnostics)
3532
ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
36-
ADD_COMPILE_OPTIONS(-fdiagnostics-color=always)
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)
3742
ELSE()
3843
MESSAGE(STATUS "No colored compiler diagnostic set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
3944
ENDIF()
45+
46+
# run vcvarsall when msvc is used
47+
INCLUDE("${CMAKE_CURRENT_LIST_DIR}/VCEnvironment.cmake")
48+
RUN_VCVARSALL()

0 commit comments

Comments
 (0)