-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_utils.cmake
98 lines (85 loc) · 3.19 KB
/
build_utils.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Macro is used to set variables for compiler flags.
# Macros scope populates inside the scope of the caller
# -D_FORITIFY_SOURCE=2 : Detect runtime buffer overflow
# -fpie, -Wl,-pie : full ASLR
# -fpic -shared : Disable text relocations for shared libraries
MACRO(set_compiler_flags)
# base flags for detecting errors
set(base_exceptions
"-Wall"
"-Werror"
"-Wshadow"
"-Wconversion"
"-Wpedantic"
"-Wformat"
"-Wvla"
"-Wfloat-equal"
"-D_FORTIFY_SOURCE=2"
"-fpie"
"-Wl,-pie"
"-shared"
"-fPIC"
)
# Base flags for static analysis. This should be added to both the
# compiler and linker options
set(base_static_analysis
"-fsanitize=address"
"-fno-omit-frame-pointer"
"-fsanitize=undefined"
"-fno-sanitize-recover=all"
"-fsanitize=float-divide-by-zero"
"-fsanitize=float-cast-overflow"
"-fno-sanitize=null"
"-fno-sanitize=alignment"
)
# Add debuging symbols if in debug mode
IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(debug_flag "-g3")
ENDIF()
# Create a base flags variable to be linked
set(base_flags ${base_exceptions} ${base_static_analysis} ${debug_flag})
ENDMACRO()
#
# set_project_properties the include directory using the relocatable generator
# syntax which allows the --prefix syntax to be used without breaking where
# the header files are located. Additionally, the function sets the compiler
# flags for the target
#
FUNCTION(set_project_properties target_name target_include_dir)
# Run macro to get the variables set
set_compiler_flags()
# Separates the area of concern when it comes to the build files and the
# installation files by using the build in generator expression for relocation
target_include_directories(
${target_name} PUBLIC
"$<BUILD_INTERFACE:${target_include_dir}>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>"
)
# set additional target properties to include flags for static analysis
set_target_properties(
${target_name} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
COMPILE_OPTIONS "${base_flags}"
LINK_OPTIONS "${base_static_analysis}"
)
ENDFUNCTION()
#
# GTest_add_target sets the build path and flags for the gtest executable
# and places the result in the ${CMAKE_BINARY_DIR}/test_bin for easy retrival
#
FUNCTION(GTest_add_target target_name)
# Include populates the scope of the caller
include(GoogleTest)
# run the flags macro
set_compiler_flags()
# Set flags and set the output of the binaries to a the test_bin
set_target_properties(
${target_name} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test_bin
COMPILE_OPTIONS "${base_flags}"
LINK_OPTIONS "${base_static_analysis}"
)
target_link_libraries(${target_name} PRIVATE gtest_main)
gtest_discover_tests(${target_name})
ENDFUNCTION()