summaryrefslogtreecommitdiffstats
path: root/cmake
diff options
context:
space:
mode:
Diffstat (limited to 'cmake')
-rw-r--r--cmake/modules/FindASan.cmake59
-rw-r--r--cmake/modules/FindMSan.cmake57
-rw-r--r--cmake/modules/FindSanitizers.cmake87
-rw-r--r--cmake/modules/FindTSan.cmake65
-rw-r--r--cmake/modules/FindUBSan.cmake46
-rwxr-xr-xcmake/modules/asan-wrapper55
-rw-r--r--cmake/modules/sanitize-helpers.cmake170
7 files changed, 539 insertions, 0 deletions
diff --git a/cmake/modules/FindASan.cmake b/cmake/modules/FindASan.cmake
new file mode 100644
index 00000000..98ea7cb3
--- /dev/null
+++ b/cmake/modules/FindASan.cmake
@@ -0,0 +1,59 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_ADDRESS "Enable AddressSanitizer for sanitized targets." Off)
26
27set(FLAG_CANDIDATES
28 # Clang 3.2+ use this version. The no-omit-frame-pointer option is optional.
29 "-g -fsanitize=address -fno-omit-frame-pointer"
30 "-g -fsanitize=address"
31
32 # Older deprecated flag for ASan
33 "-g -faddress-sanitizer"
34)
35
36
37if (SANITIZE_ADDRESS AND (SANITIZE_THREAD OR SANITIZE_MEMORY))
38 message(FATAL_ERROR "AddressSanitizer is not compatible with "
39 "ThreadSanitizer or MemorySanitizer.")
40endif ()
41
42
43include(sanitize-helpers)
44
45if (SANITIZE_ADDRESS)
46 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}" "AddressSanitizer"
47 "ASan")
48
49 find_program(ASan_WRAPPER "asan-wrapper" PATHS ${CMAKE_MODULE_PATH})
50 mark_as_advanced(ASan_WRAPPER)
51endif ()
52
53function (add_sanitize_address TARGET)
54 if (NOT SANITIZE_ADDRESS)
55 return()
56 endif ()
57
58 sanitizer_add_flags(${TARGET} "AddressSanitizer" "ASan")
59endfunction ()
diff --git a/cmake/modules/FindMSan.cmake b/cmake/modules/FindMSan.cmake
new file mode 100644
index 00000000..22d0050e
--- /dev/null
+++ b/cmake/modules/FindMSan.cmake
@@ -0,0 +1,57 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_MEMORY "Enable MemorySanitizer for sanitized targets." Off)
26
27set(FLAG_CANDIDATES
28 "-g -fsanitize=memory"
29)
30
31
32include(sanitize-helpers)
33
34if (SANITIZE_MEMORY)
35 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
36 message(WARNING "MemorySanitizer disabled for target ${TARGET} because "
37 "MemorySanitizer is supported for Linux systems only.")
38 set(SANITIZE_MEMORY Off CACHE BOOL
39 "Enable MemorySanitizer for sanitized targets." FORCE)
40 elseif (NOT ${CMAKE_SIZEOF_VOID_P} EQUAL 8)
41 message(WARNING "MemorySanitizer disabled for target ${TARGET} because "
42 "MemorySanitizer is supported for 64bit systems only.")
43 set(SANITIZE_MEMORY Off CACHE BOOL
44 "Enable MemorySanitizer for sanitized targets." FORCE)
45 else ()
46 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}" "MemorySanitizer"
47 "MSan")
48 endif ()
49endif ()
50
51function (add_sanitize_memory TARGET)
52 if (NOT SANITIZE_MEMORY)
53 return()
54 endif ()
55
56 sanitizer_add_flags(${TARGET} "MemorySanitizer" "MSan")
57endfunction ()
diff --git a/cmake/modules/FindSanitizers.cmake b/cmake/modules/FindSanitizers.cmake
new file mode 100644
index 00000000..4f586a34
--- /dev/null
+++ b/cmake/modules/FindSanitizers.cmake
@@ -0,0 +1,87 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25# If any of the used compiler is a GNU compiler, add a second option to static
26# link against the sanitizers.
27option(SANITIZE_LINK_STATIC "Try to link static against sanitizers." Off)
28
29
30
31
32set(FIND_QUIETLY_FLAG "")
33if (DEFINED Sanitizers_FIND_QUIETLY)
34 set(FIND_QUIETLY_FLAG "QUIET")
35endif ()
36
37find_package(ASan ${FIND_QUIETLY_FLAG})
38find_package(TSan ${FIND_QUIETLY_FLAG})
39find_package(MSan ${FIND_QUIETLY_FLAG})
40find_package(UBSan ${FIND_QUIETLY_FLAG})
41
42
43
44
45function(sanitizer_add_blacklist_file FILE)
46 if(NOT IS_ABSOLUTE ${FILE})
47 set(FILE "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}")
48 endif()
49 get_filename_component(FILE "${FILE}" REALPATH)
50
51 sanitizer_check_compiler_flags("-fsanitize-blacklist=${FILE}"
52 "SanitizerBlacklist" "SanBlist")
53endfunction()
54
55function(add_sanitizers ...)
56 # If no sanitizer is enabled, return immediately.
57 if (NOT (SANITIZE_ADDRESS OR SANITIZE_MEMORY OR SANITIZE_THREAD OR
58 SANITIZE_UNDEFINED))
59 return()
60 endif ()
61
62 foreach (TARGET ${ARGV})
63 # Check if this target will be compiled by exactly one compiler. Other-
64 # wise sanitizers can't be used and a warning should be printed once.
65 sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
66 list(LENGTH TARGET_COMPILER NUM_COMPILERS)
67 if (NUM_COMPILERS GREATER 1)
68 message(WARNING "Can't use any sanitizers for target ${TARGET}, "
69 "because it will be compiled by incompatible compilers. "
70 "Target will be compiled without sanitizers.")
71 return()
72
73 # If the target is compiled by no known compiler, ignore it.
74 elseif (NUM_COMPILERS EQUAL 0)
75 message(WARNING "Can't use any sanitizers for target ${TARGET}, "
76 "because it uses an unknown compiler. Target will be "
77 "compiled without sanitizers.")
78 return()
79 endif ()
80
81 # Add sanitizers for target.
82 add_sanitize_address(${TARGET})
83 add_sanitize_thread(${TARGET})
84 add_sanitize_memory(${TARGET})
85 add_sanitize_undefined(${TARGET})
86 endforeach ()
87endfunction(add_sanitizers)
diff --git a/cmake/modules/FindTSan.cmake b/cmake/modules/FindTSan.cmake
new file mode 100644
index 00000000..3cba3c03
--- /dev/null
+++ b/cmake/modules/FindTSan.cmake
@@ -0,0 +1,65 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_THREAD "Enable ThreadSanitizer for sanitized targets." Off)
26
27set(FLAG_CANDIDATES
28 "-g -fsanitize=thread"
29)
30
31
32# ThreadSanitizer is not compatible with MemorySanitizer.
33if (SANITIZE_THREAD AND SANITIZE_MEMORY)
34 message(FATAL_ERROR "ThreadSanitizer is not compatible with "
35 "MemorySanitizer.")
36endif ()
37
38
39include(sanitize-helpers)
40
41if (SANITIZE_THREAD)
42 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND
43 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
44 message(WARNING "ThreadSanitizer disabled for target ${TARGET} because "
45 "ThreadSanitizer is supported for Linux systems and macOS only.")
46 set(SANITIZE_THREAD Off CACHE BOOL
47 "Enable ThreadSanitizer for sanitized targets." FORCE)
48 elseif (NOT ${CMAKE_SIZEOF_VOID_P} EQUAL 8)
49 message(WARNING "ThreadSanitizer disabled for target ${TARGET} because "
50 "ThreadSanitizer is supported for 64bit systems only.")
51 set(SANITIZE_THREAD Off CACHE BOOL
52 "Enable ThreadSanitizer for sanitized targets." FORCE)
53 else ()
54 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}" "ThreadSanitizer"
55 "TSan")
56 endif ()
57endif ()
58
59function (add_sanitize_thread TARGET)
60 if (NOT SANITIZE_THREAD)
61 return()
62 endif ()
63
64 sanitizer_add_flags(${TARGET} "ThreadSanitizer" "TSan")
65endfunction ()
diff --git a/cmake/modules/FindUBSan.cmake b/cmake/modules/FindUBSan.cmake
new file mode 100644
index 00000000..ae103f71
--- /dev/null
+++ b/cmake/modules/FindUBSan.cmake
@@ -0,0 +1,46 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25option(SANITIZE_UNDEFINED
26 "Enable UndefinedBehaviorSanitizer for sanitized targets." Off)
27
28set(FLAG_CANDIDATES
29 "-g -fsanitize=undefined"
30)
31
32
33include(sanitize-helpers)
34
35if (SANITIZE_UNDEFINED)
36 sanitizer_check_compiler_flags("${FLAG_CANDIDATES}"
37 "UndefinedBehaviorSanitizer" "UBSan")
38endif ()
39
40function (add_sanitize_undefined TARGET)
41 if (NOT SANITIZE_UNDEFINED)
42 return()
43 endif ()
44
45 sanitizer_add_flags(${TARGET} "UndefinedBehaviorSanitizer" "UBSan")
46endfunction ()
diff --git a/cmake/modules/asan-wrapper b/cmake/modules/asan-wrapper
new file mode 100755
index 00000000..5d541033
--- /dev/null
+++ b/cmake/modules/asan-wrapper
@@ -0,0 +1,55 @@
1#!/bin/sh
2
3# The MIT License (MIT)
4#
5# Copyright (c)
6# 2013 Matthew Arsenault
7# 2015-2016 RWTH Aachen University, Federal Republic of Germany
8#
9# Permission is hereby granted, free of charge, to any person obtaining a copy
10# of this software and associated documentation files (the "Software"), to deal
11# in the Software without restriction, including without limitation the rights
12# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13# copies of the Software, and to permit persons to whom the Software is
14# furnished to do so, subject to the following conditions:
15#
16# The above copyright notice and this permission notice shall be included in all
17# copies or substantial portions of the Software.
18#
19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25# SOFTWARE.
26
27# This script is a wrapper for AddressSanitizer. In some special cases you need
28# to preload AddressSanitizer to avoid error messages - e.g. if you're
29# preloading another library to your application. At the moment this script will
30# only do something, if we're running on a Linux platform. OSX might not be
31# affected.
32
33
34# Exit immediately, if platform is not Linux.
35if [ "$(uname)" != "Linux" ]
36then
37 exec $@
38fi
39
40
41# Get the used libasan of the application ($1). If a libasan was found, it will
42# be prepended to LD_PRELOAD.
43libasan=$(ldd $1 | grep libasan | sed "s/^[[:space:]]//" | cut -d' ' -f1)
44if [ -n "$libasan" ]
45then
46 if [ -n "$LD_PRELOAD" ]
47 then
48 export LD_PRELOAD="$libasan:$LD_PRELOAD"
49 else
50 export LD_PRELOAD="$libasan"
51 fi
52fi
53
54# Execute the application.
55exec $@
diff --git a/cmake/modules/sanitize-helpers.cmake b/cmake/modules/sanitize-helpers.cmake
new file mode 100644
index 00000000..c51ee6af
--- /dev/null
+++ b/cmake/modules/sanitize-helpers.cmake
@@ -0,0 +1,170 @@
1# The MIT License (MIT)
2#
3# Copyright (c)
4# 2013 Matthew Arsenault
5# 2015-2016 RWTH Aachen University, Federal Republic of Germany
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included in all
15# copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24
25# Helper function to get the language of a source file.
26function (sanitizer_lang_of_source FILE RETURN_VAR)
27 get_filename_component(FILE_EXT "${FILE}" EXT)
28 string(TOLOWER "${FILE_EXT}" FILE_EXT)
29 string(SUBSTRING "${FILE_EXT}" 1 -1 FILE_EXT)
30
31 get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
32 foreach (LANG ${ENABLED_LANGUAGES})
33 list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${FILE_EXT}" TEMP)
34 if (NOT ${TEMP} EQUAL -1)
35 set(${RETURN_VAR} "${LANG}" PARENT_SCOPE)
36 return()
37 endif ()
38 endforeach()
39
40 set(${RETURN_VAR} "" PARENT_SCOPE)
41endfunction ()
42
43
44# Helper function to get compilers used by a target.
45function (sanitizer_target_compilers TARGET RETURN_VAR)
46 # Check if all sources for target use the same compiler. If a target uses
47 # e.g. C and Fortran mixed and uses different compilers (e.g. clang and
48 # gfortran) this can trigger huge problems, because different compilers may
49 # use different implementations for sanitizers.
50 set(BUFFER "")
51 get_target_property(TSOURCES ${TARGET} SOURCES)
52 foreach (FILE ${TSOURCES})
53 # If expression was found, FILE is a generator-expression for an object
54 # library. Object libraries will be ignored.
55 string(REGEX MATCH "TARGET_OBJECTS:([^ >]+)" _file ${FILE})
56 if ("${_file}" STREQUAL "")
57 sanitizer_lang_of_source(${FILE} LANG)
58 if (LANG)
59 list(APPEND BUFFER ${CMAKE_${LANG}_COMPILER_ID})
60 endif ()
61 endif ()
62 endforeach ()
63
64 list(REMOVE_DUPLICATES BUFFER)
65 set(${RETURN_VAR} "${BUFFER}" PARENT_SCOPE)
66endfunction ()
67
68
69# Helper function to check compiler flags for language compiler.
70function (sanitizer_check_compiler_flag FLAG LANG VARIABLE)
71 if (${LANG} STREQUAL "C")
72 include(CheckCCompilerFlag)
73 check_c_compiler_flag("${FLAG}" ${VARIABLE})
74
75 elseif (${LANG} STREQUAL "CXX")
76 include(CheckCXXCompilerFlag)
77 check_cxx_compiler_flag("${FLAG}" ${VARIABLE})
78
79 elseif (${LANG} STREQUAL "Fortran")
80 # CheckFortranCompilerFlag was introduced in CMake 3.x. To be compatible
81 # with older Cmake versions, we will check if this module is present
82 # before we use it. Otherwise we will define Fortran coverage support as
83 # not available.
84 include(CheckFortranCompilerFlag OPTIONAL RESULT_VARIABLE INCLUDED)
85 if (INCLUDED)
86 check_fortran_compiler_flag("${FLAG}" ${VARIABLE})
87 elseif (NOT CMAKE_REQUIRED_QUIET)
88 message(STATUS "Performing Test ${VARIABLE}")
89 message(STATUS "Performing Test ${VARIABLE}"
90 " - Failed (Check not supported)")
91 endif ()
92 endif()
93endfunction ()
94
95
96# Helper function to test compiler flags.
97function (sanitizer_check_compiler_flags FLAG_CANDIDATES NAME PREFIX)
98 set(CMAKE_REQUIRED_QUIET ${${PREFIX}_FIND_QUIETLY})
99
100 get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
101 foreach (LANG ${ENABLED_LANGUAGES})
102 # Sanitizer flags are not dependend on language, but the used compiler.
103 # So instead of searching flags foreach language, search flags foreach
104 # compiler used.
105 set(COMPILER ${CMAKE_${LANG}_COMPILER_ID})
106 if (NOT DEFINED ${PREFIX}_${COMPILER}_FLAGS)
107 foreach (FLAG ${FLAG_CANDIDATES})
108 if(NOT CMAKE_REQUIRED_QUIET)
109 message(STATUS "Try ${COMPILER} ${NAME} flag = [${FLAG}]")
110 endif()
111
112 set(CMAKE_REQUIRED_FLAGS "${FLAG}")
113 unset(${PREFIX}_FLAG_DETECTED CACHE)
114 sanitizer_check_compiler_flag("${FLAG}" ${LANG}
115 ${PREFIX}_FLAG_DETECTED)
116
117 if (${PREFIX}_FLAG_DETECTED)
118 # If compiler is a GNU compiler, search for static flag, if
119 # SANITIZE_LINK_STATIC is enabled.
120 if (SANITIZE_LINK_STATIC AND (${COMPILER} STREQUAL "GNU"))
121 string(TOLOWER ${PREFIX} PREFIX_lower)
122 sanitizer_check_compiler_flag(
123 "-static-lib${PREFIX_lower}" ${LANG}
124 ${PREFIX}_STATIC_FLAG_DETECTED)
125
126 if (${PREFIX}_STATIC_FLAG_DETECTED)
127 set(FLAG "-static-lib${PREFIX_lower} ${FLAG}")
128 endif ()
129 endif ()
130
131 set(${PREFIX}_${COMPILER}_FLAGS "${FLAG}" CACHE STRING
132 "${NAME} flags for ${COMPILER} compiler.")
133 mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
134 break()
135 endif ()
136 endforeach ()
137
138 if (NOT ${PREFIX}_FLAG_DETECTED)
139 set(${PREFIX}_${COMPILER}_FLAGS "" CACHE STRING
140 "${NAME} flags for ${COMPILER} compiler.")
141 mark_as_advanced(${PREFIX}_${COMPILER}_FLAGS)
142
143 message(WARNING "${NAME} is not available for ${COMPILER} "
144 "compiler. Targets using this compiler will be "
145 "compiled without ${NAME}.")
146 endif ()
147 endif ()
148 endforeach ()
149endfunction ()
150
151
152# Helper to assign sanitizer flags for TARGET.
153function (sanitizer_add_flags TARGET NAME PREFIX)
154 # Get list of compilers used by target and check, if sanitizer is available
155 # for this target. Other compiler checks like check for conflicting
156 # compilers will be done in add_sanitizers function.
157 sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
158 list(LENGTH TARGET_COMPILER NUM_COMPILERS)
159 if ("${${PREFIX}_${TARGET_COMPILER}_FLAGS}" STREQUAL "")
160 return()
161 endif()
162
163 # Set compile- and link-flags for target.
164 set_property(TARGET ${TARGET} APPEND_STRING
165 PROPERTY COMPILE_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
166 set_property(TARGET ${TARGET} APPEND_STRING
167 PROPERTY COMPILE_FLAGS " ${SanBlist_${TARGET_COMPILER}_FLAGS}")
168 set_property(TARGET ${TARGET} APPEND_STRING
169 PROPERTY LINK_FLAGS " ${${PREFIX}_${TARGET_COMPILER}_FLAGS}")
170endfunction ()