Skip to content

Commit 5a0488a

Browse files
Add options for enabling sanitisers, Partially replaces #68
1 parent f56d4eb commit 5a0488a

File tree

4 files changed

+82
-39
lines changed

4 files changed

+82
-39
lines changed

CMakeLists.txt

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ project(
3030
)
3131

3232
# We use additional modules that cmake needs to know about
33-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
33+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/" "${CMAKE_SOURCE_DIR}/cmake/Modules/")
3434

3535
# Output the compilation database
3636
set(CMAKE_EXPORT_COMPILE_COMMANDS
@@ -63,38 +63,13 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
6363
set(MASTER_PROJECT ON)
6464
endif()
6565

66-
if(MSVC)
67-
add_compile_options(/W4)
68-
else()
69-
add_compile_options(-Wall -Wextra -pedantic)
70-
endif(MSVC)
71-
7266
# If this option is set we are building using continous integration
7367
option(CI_BUILD "Enable build options for building in the CI server" OFF)
7468

75-
# Default not to run the clang-tidy checks, default to whatever our CI_BUILD is
76-
option(ENABLE_CLANG_TIDY "Enable building with clang-tidy checks.")
77-
if(ENABLE_CLANG_TIDY)
78-
# Search for clang-tidy first as this is the version installed in CI
79-
find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy)
80-
message(STATUS "Found clang-tidy: ${CLANG_TIDY_EXECUTABLE}")
81-
if(NOT CLANG_TIDY_EXECUTABLE)
82-
message(FATAL_ERROR "clang-tidy not found.")
83-
endif()
84-
85-
# Report clang-tidy executable details
86-
execute_process(COMMAND "${CLANG_TIDY_EXECUTABLE}" "--version" OUTPUT_VARIABLE CLANG_TIDY_VERSION)
87-
string(REGEX REPLACE ".*LLVM version ([0-9.]*).*" "\\1" CLANG_TIDY_VERSION "${CLANG_TIDY_VERSION}")
88-
message(STATUS "Found clang-tidy: ${CLANG_TIDY_EXECUTABLE} ${CLANG_TIDY_VERSION}")
89-
90-
# Build clang-tidy command line
91-
set(CLANG_TIDY_ARGS "${CLANG_TIDY_EXECUTABLE}" "--use-color" "--config-file=${PROJECT_SOURCE_DIR}/.clang-tidy")
92-
if(CI_BUILD)
93-
set(CLANG_TIDY_ARGS "${CLANG_TIDY_EXECUTABLE}" "-warnings-as-errors=*")
94-
endif(CI_BUILD)
95-
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_ARGS}" "--extra-arg=-std=c++14")
96-
set(CMAKE_C_CLANG_TIDY "${CLANG_TIDY_ARGS}" "--extra-arg=-std=c99")
97-
endif(ENABLE_CLANG_TIDY)
69+
# Include files to set up the compiler options
70+
include(ClangTidy)
71+
include(CompilerOptions)
72+
include(Sanitizers)
9873

9974
# If we are doing a CI build then we want to enable -Werror when compiling warnings are bad. We will also make it fail
10075
# if clang-tidy has an error
@@ -106,19 +81,11 @@ if(CI_BUILD)
10681
endif()
10782
endif(CI_BUILD)
10883

109-
# Make the compiler display colours always (even when we build with ninja)
110-
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
111-
add_compile_options(-fdiagnostics-color=always)
112-
endif()
113-
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
114-
add_compile_options(-fcolor-diagnostics)
115-
endif()
116-
11784
# Add the src directory
11885
add_subdirectory(src)
11986

12087
# Add the tests directory
121-
option(BUILD_TESTS "Builds all of the NUClear unit tests." TRUE)
88+
option(BUILD_TESTS "Builds all of the NUClear unit tests." OFF)
12289
if(BUILD_TESTS)
12390
enable_testing()
12491
add_subdirectory(tests)

cmake/ClangTidy.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Default not to run the clang-tidy checks, default to whatever our CI_BUILD is
2+
option(ENABLE_CLANG_TIDY "Enable building with clang-tidy checks.")
3+
if(ENABLE_CLANG_TIDY)
4+
# Search for clang-tidy-15 first as this is the version installed in CI
5+
find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy-15 clang-tidy)
6+
if(NOT CLANG_TIDY_EXECUTABLE)
7+
message(FATAL_ERROR "clang-tidy-15 not found.")
8+
endif()
9+
10+
# Report clang-tidy executable details
11+
execute_process(COMMAND "${CLANG_TIDY_EXECUTABLE}" "--version" OUTPUT_VARIABLE CLANG_TIDY_VERSION)
12+
string(REGEX REPLACE ".*LLVM version ([0-9.]*).*" "\\1" CLANG_TIDY_VERSION "${CLANG_TIDY_VERSION}")
13+
message(STATUS "Found clang-tidy: ${CLANG_TIDY_EXECUTABLE} ${CLANG_TIDY_VERSION}")
14+
15+
# Build clang-tidy command line
16+
set(CLANG_TIDY_ARGS "${CLANG_TIDY_EXECUTABLE}" "--use-color" "--config-file=${PROJECT_SOURCE_DIR}/.clang-tidy")
17+
if(CI_BUILD)
18+
set(CLANG_TIDY_ARGS "${CLANG_TIDY_EXECUTABLE}" "-warnings-as-errors=*")
19+
endif(CI_BUILD)
20+
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_ARGS}" "--extra-arg=-std=c++14")
21+
set(CMAKE_C_CLANG_TIDY "${CLANG_TIDY_ARGS}" "--extra-arg=-std=c99")
22+
endif(ENABLE_CLANG_TIDY)

cmake/CompilerOptions.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Make the compiler display colours always (even when we build with ninja)
2+
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
3+
add_compile_options(-fdiagnostics-color=always)
4+
endif()
5+
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
6+
add_compile_options(-fcolor-diagnostics)
7+
endif()

cmake/Sanitizers.cmake

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
###############################################################################
2+
### Options for enabling different sanitisers. ###
3+
### ###
4+
### User beware: ###
5+
### Not all sanitisers can be enabled at the same time. ###
6+
###############################################################################
7+
option(USE_ASAN "Enable address sanitization" OFF)
8+
if(USE_ASAN)
9+
add_compile_options(-fsanitize=address -fno-omit-frame-pointer -U_FORTIFY_SOURCE -fno-common)
10+
add_link_options(-fsanitize=address)
11+
link_libraries(asan)
12+
endif(USE_ASAN)
13+
14+
option(USE_LSAN "Enable leak sanitization" OFF)
15+
if(USE_LSAN)
16+
add_compile_options(-fsanitize=leak -fno-omit-frame-pointer -U_FORTIFY_SOURCE -fno-common)
17+
add_link_options(-fsanitize=leak)
18+
link_libraries(lsan)
19+
endif(USE_LSAN)
20+
21+
option(USE_TSAN "Enable thread sanitization" OFF)
22+
if(USE_TSAN)
23+
add_compile_options(-fsanitize=thread -fno-omit-frame-pointer -U_FORTIFY_SOURCE -fno-common)
24+
add_link_options(-fsanitize=thread)
25+
link_libraries(tsan)
26+
endif(USE_TSAN)
27+
28+
option(USE_UBSAN "Enable undefined behaviour sanitization" OFF)
29+
if(USE_UBSAN)
30+
add_compile_options(-fsanitize=undefined -fno-omit-frame-pointer -U_FORTIFY_SOURCE -fno-common)
31+
add_link_options(-fsanitize=undefined)
32+
link_libraries(ubsan)
33+
endif(USE_UBSAN)
34+
35+
# Option for enabling code profiling. Disabled by default
36+
option(ENABLE_PROFILING "Compile with profiling support enabled." OFF)
37+
if(ENABLE_PROFILING)
38+
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
39+
message(
40+
WARNING
41+
"Profiling is enabled but no debugging symbols will be kept in the compiled binaries. This may cause fine-grained profilling data to be lost."
42+
)
43+
endif()
44+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg -fprofile-arcs")
45+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -fprofile-arcs")
46+
set(CMAKE_LINKER "${CMAKE_LINKER_FLAGS} -pg -fprofile-arcs")
47+
endif(ENABLE_PROFILING)

0 commit comments

Comments
 (0)