Reworked FindASan.cmake.
* removed build type ASAN. Targets should define ASan usage by sanitize_address function * compiler flags will be searched by compiler instead of per language * FindASan warns, if a target can't be sanitized because of incompatible compilers * added some helper functions
This commit is contained in:
parent
c96a15b46c
commit
584f137e70
@ -40,80 +40,62 @@ set(ASAN_FLAG_CANDIDATES
|
||||
set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
|
||||
set(CMAKE_REQUIRED_QUIET ${ASan_FIND_QUIETLY})
|
||||
|
||||
set(_ASAN_REQUIRED_VARS)
|
||||
|
||||
get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
|
||||
foreach (LANG ${ENABLED_LANGUAGES})
|
||||
if (CMAKE_${LANG}_COMPILER_LOADED)
|
||||
list(APPEND _ASAN_REQUIRED_VARS ASAN_${LANG}_FLAGS)
|
||||
# Sanitizer flags are not dependend on language, but the used compiler. So
|
||||
# instead of searching flags foreach language, search flags foreach compiler
|
||||
# used.
|
||||
set(COMPILER ${CMAKE_${LANG}_COMPILER_ID})
|
||||
if (NOT ASAN_${COMPILER}_FLAGS)
|
||||
foreach (FLAG ${ASAN_FLAG_CANDIDATES})
|
||||
if(NOT CMAKE_REQUIRED_QUIET)
|
||||
message(STATUS
|
||||
"Try ${COMPILER} AddressSanitizer flag = [${FLAG}]")
|
||||
endif()
|
||||
|
||||
# If flags for this compiler were already found, do not try to find them
|
||||
# again.
|
||||
if (NOT ASAN_${LANG}_FLAGS)
|
||||
foreach (FLAG ${ASAN_FLAG_CANDIDATES})
|
||||
if(NOT CMAKE_REQUIRED_QUIET)
|
||||
message(STATUS "Try Address sanitizer ${LANG} flag = [${FLAG}]")
|
||||
endif()
|
||||
set(CMAKE_REQUIRED_FLAGS "${FLAG}")
|
||||
unset(ASAN_FLAG_DETECTED CACHE)
|
||||
|
||||
set(CMAKE_REQUIRED_FLAGS "${FLAG}")
|
||||
unset(ASAN_FLAG_DETECTED CACHE)
|
||||
if (${LANG} STREQUAL "C")
|
||||
include(CheckCCompilerFlag)
|
||||
check_c_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
|
||||
|
||||
if (${LANG} STREQUAL "C")
|
||||
include(CheckCCompilerFlag)
|
||||
check_c_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
|
||||
elseif (${LANG} STREQUAL "CXX")
|
||||
include(CheckCXXCompilerFlag)
|
||||
check_cxx_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
|
||||
|
||||
elseif (${LANG} STREQUAL "CXX")
|
||||
include(CheckCXXCompilerFlag)
|
||||
check_cxx_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
|
||||
|
||||
elseif (${LANG} STREQUAL "Fortran")
|
||||
include(CheckFortranCompilerFlag)
|
||||
elseif (${LANG} STREQUAL "Fortran")
|
||||
# CheckFortranCompilerFlag was introduced in CMake 3.x. To be
|
||||
# compatible with older Cmake versions, we will check if this
|
||||
# module is present before we use it. Otherwise we will define
|
||||
# Fortran coverage support as not available.
|
||||
include(CheckFortranCompilerFlag OPTIONAL
|
||||
RESULT_VARIABLE INCLUDED)
|
||||
if (INCLUDED)
|
||||
check_fortran_compiler_flag("${FLAG}" ASAN_FLAG_DETECTED)
|
||||
endif()
|
||||
|
||||
if (ASAN_FLAG_DETECTED)
|
||||
set(ASAN_${LANG}_FLAGS "${FLAG}"
|
||||
CACHE STRING "${LANG} compiler flags for Address sanitizer")
|
||||
break()
|
||||
elseif (NOT CMAKE_REQUIRED_QUIET)
|
||||
message("-- Performing Test ASAN_FLAG_DETECTED")
|
||||
message("-- Performing Test ASAN_FLAG_DETECTED - Failed "
|
||||
"(Check not supported)")
|
||||
endif ()
|
||||
endforeach()
|
||||
endif ()
|
||||
endif()
|
||||
|
||||
if (ASAN_FLAG_DETECTED)
|
||||
set(ASAN_${COMPILER}_FLAGS "${FLAG}"
|
||||
CACHE STRING "${LANG} compiler flags for AddressSanitizer.")
|
||||
mark_as_advanced(ASAN_${COMPILER}_FLAGS)
|
||||
break()
|
||||
endif ()
|
||||
endforeach ()
|
||||
endif ()
|
||||
endforeach ()
|
||||
|
||||
set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
|
||||
|
||||
|
||||
if (_ASAN_REQUIRED_VARS)
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(ASan REQUIRED_VARS ${_ASAN_REQUIRED_VARS})
|
||||
mark_as_advanced(${_ASAN_REQUIRED_VARS})
|
||||
unset(_ASAN_REQUIRED_VARS)
|
||||
else()
|
||||
message(SEND_ERROR "FindASan requires C, CXX or Fortran language to be enabled")
|
||||
endif()
|
||||
|
||||
|
||||
# add build target ASan
|
||||
if (ASan_FOUND)
|
||||
get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
|
||||
foreach (LANG ${ENABLED_LANGUAGES})
|
||||
set(CMAKE_${LANG}_FLAGS_ASAN "${ASAN_${LANG}_FLAGS}" CACHE
|
||||
STRING "Flags used by the ${LANG} compiler during ASan builds.")
|
||||
mark_as_advanced(CMAKE_${LANG}_FLAGS_ASAN)
|
||||
endforeach ()
|
||||
|
||||
set(CMAKE_EXE_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
|
||||
STRING "Flags used for linking binaries during ASan builds.")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
|
||||
STRING "Flags used by the shared libraries linker during ASan builds.")
|
||||
set(CMAKE_MODULE_LINKER_FLAGS_ASAN "${ASAN_C_FLAGS}" CACHE
|
||||
STRING "Flags used by the module libraries linker during ASan builds.")
|
||||
mark_as_advanced(CMAKE_EXE_LINKER_FLAGS_ASAN
|
||||
CMAKE_SHARED_LINKER_FLAGS_ASAN
|
||||
CMAKE_MODULE_LINKER_FLAGS_ASAN)
|
||||
endif ()
|
||||
|
||||
include(sanitize-helpers)
|
||||
|
||||
|
||||
function (sanitize_address TARGET)
|
||||
@ -121,23 +103,25 @@ function (sanitize_address TARGET)
|
||||
return()
|
||||
endif ()
|
||||
|
||||
get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
|
||||
get_target_property(SOURCE_FILES ${TARGET} SOURCES)
|
||||
foreach (SOURCE_FILE ${SOURCE_FILES})
|
||||
foreach (LANG ${ENABLED_LANGUAGES})
|
||||
get_filename_component(FILE_EXT "${SOURCE_FILE}" EXT)
|
||||
string(TOLOWER "${FILE_EXT}" FILE_EXT)
|
||||
string(SUBSTRING "${FILE_EXT}" 1 -1 FILE_EXT)
|
||||
list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${FILE_EXT}" TEMP)
|
||||
if (NOT ${TEMP} EQUAL -1)
|
||||
if (DEFINED ASAN_${LANG}_FLAGS)
|
||||
set_property(SOURCE ${SOURCE_FILE} APPEND_STRING PROPERTY
|
||||
COMPILE_FLAGS " ${ASAN_${LANG}_FLAGS}")
|
||||
endif ()
|
||||
endif ()
|
||||
endforeach()
|
||||
endforeach (SOURCE_FILE)
|
||||
# Get list of compilers used by target and check, if target can be checked
|
||||
# by sanitizer.
|
||||
sanitizer_target_compilers(${TARGET} TARGET_COMPILER)
|
||||
list(LENGTH TARGET_COMPILER NUM_COMPILERS)
|
||||
if (NUM_COMPILERS GREATER 1)
|
||||
message(AUTHOR_WARNING "AddressSanitizer disabled for target ${TARGET} "
|
||||
"because it will be compiled by different compilers.")
|
||||
return()
|
||||
|
||||
set_property(TARGET ${TARGET} APPEND_STRING PROPERTY
|
||||
LINK_FLAGS " ${ASAN_C_FLAGS}")
|
||||
elseif ((NUM_COMPILERS EQUAL 0) OR
|
||||
(NOT DEFINED "ASAN_${TARGET_COMPILER}_FLAGS"))
|
||||
message(AUTHOR_WARNING "AddressSanitizer disabled for target ${TARGET} "
|
||||
"because there is no sanitizer available for target sources.")
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Set compile- and link-flags for target.
|
||||
set_property(TARGET ${TARGET} APPEND_STRING
|
||||
PROPERTY COMPILE_FLAGS " ${ASAN_${TARGET_COMPILER}_FLAGS}")
|
||||
set_property(TARGET ${TARGET} APPEND_STRING
|
||||
PROPERTY LINK_FLAGS " ${ASAN_${TARGET_COMPILER}_FLAGS}")
|
||||
endfunction ()
|
||||
|
66
cmake/sanitize-helpers.cmake
Normal file
66
cmake/sanitize-helpers.cmake
Normal file
@ -0,0 +1,66 @@
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c)
|
||||
# 2013 Matthew Arsenault
|
||||
# 2015-2016 RWTH Aachen University, Federal Republic of Germany
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
# Helper function to get the language of a source file.
|
||||
function (sanitizer_lang_of_source FILE RETURN_VAR)
|
||||
get_filename_component(FILE_EXT "${FILE}" EXT)
|
||||
string(TOLOWER "${FILE_EXT}" FILE_EXT)
|
||||
string(SUBSTRING "${FILE_EXT}" 1 -1 FILE_EXT)
|
||||
|
||||
get_property(ENABLED_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
|
||||
foreach (LANG ${ENABLED_LANGUAGES})
|
||||
list(FIND CMAKE_${LANG}_SOURCE_FILE_EXTENSIONS "${FILE_EXT}" TEMP)
|
||||
if (NOT ${TEMP} EQUAL -1)
|
||||
set(${RETURN_VAR} "${LANG}" PARENT_SCOPE)
|
||||
return()
|
||||
endif ()
|
||||
endforeach()
|
||||
|
||||
set(${RETURN_VAR} "" PARENT_SCOPE)
|
||||
endfunction ()
|
||||
|
||||
|
||||
# Helper function to get compilers used by a target.
|
||||
function (sanitizer_target_compilers TARGET RETURN_VAR)
|
||||
# Check if all sources for target use the same compiler. If a target uses
|
||||
# e.g. C and Fortran mixed and uses different compilers (e.g. clang and
|
||||
# gfortran) this can trigger huge problems, because different compilers may
|
||||
# use different implementations for sanitizers.
|
||||
set(BUFFER "")
|
||||
get_target_property(TSOURCES ${TARGET} SOURCES)
|
||||
foreach (FILE ${TSOURCES})
|
||||
# If expression was found, FILE is a generator-expression for an object
|
||||
# library. Object libraries will be ignored.
|
||||
string(REGEX MATCH "TARGET_OBJECTS:([^ >]+)" _file ${FILE})
|
||||
if ("${_file}" STREQUAL "")
|
||||
sanitizer_lang_of_source(${FILE} LANG)
|
||||
if (LANG)
|
||||
list(APPEND BUFFER ${CMAKE_${LANG}_COMPILER_ID})
|
||||
endif ()
|
||||
endif ()
|
||||
endforeach ()
|
||||
|
||||
list(REMOVE_DUPLICATES BUFFER)
|
||||
set(${RETURN_VAR} "${BUFFER}" PARENT_SCOPE)
|
||||
endfunction ()
|
Loading…
Reference in New Issue
Block a user