-
Notifications
You must be signed in to change notification settings - Fork 35
Add sanitizers #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sanitizers #121
Changes from all commits
7c69d89
a60c7fb
345a280
3743f2e
e17e7ae
77e6aaf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
# Copyright (C) 2023 Intel Corporation | ||
# Copyright (C) 2023-2024 Intel Corporation | ||
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
# | ||
# helpers.cmake -- helper functions for top-level CMakeLists.txt | ||
# | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls bump dates (in all files) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not done in the top-level CMakeLists There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done: #150 |
||
|
||
# CMake modules that check whether the C/C++ compiler supports a given flag | ||
include(CheckCCompilerFlag) | ||
include(CheckCXXCompilerFlag) | ||
|
||
# Sets ${ret} to version of program specified by ${name} in major.minor format | ||
function(get_program_version_major_minor name ret) | ||
execute_process(COMMAND ${name} --version | ||
|
@@ -104,3 +108,48 @@ function(add_umf_library) | |
add_umf_target_compile_options(${ARG_NAME}) | ||
add_umf_target_link_options(${ARG_NAME}) | ||
endfunction() | ||
|
||
# Add sanitizer ${flag}, if it is supported, for both C and C++ compiler | ||
macro(add_sanitizer_flag flag) | ||
# Save current 'CMAKE_REQUIRED_LIBRARIES' state and temporarily extend it with | ||
# '-fsanitize=${flag}'. It is required by CMake to check the compiler for | ||
# availability of provided sanitizer ${flag}. | ||
if(WINDOWS) | ||
set(SANITIZER_FLAG "/fsanitize=${flag}") | ||
set(SANITIZER_ARGS "") | ||
else() | ||
set(SANITIZER_FLAG "-fsanitize=${flag}") | ||
set(SANITIZER_ARGS "-fno-sanitize-recover=all") | ||
endif() | ||
|
||
set(SAVED_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) | ||
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} ${SANITIZER_FLAG}") | ||
|
||
if(${flag} STREQUAL "address") | ||
lukaszstolarczuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
set(check_name "HAS_ASAN") | ||
elseif(${flag} STREQUAL "undefined") | ||
set(check_name "HAS_UBSAN") | ||
elseif(${flag} STREQUAL "thread") | ||
set(check_name "HAS_TSAN") | ||
elseif(${flag} STREQUAL "memory") | ||
set(check_name "HAS_MSAN") | ||
endif() | ||
|
||
# Check C and CXX compilers for given sanitizer flag. | ||
check_c_compiler_flag("${SANITIZER_FLAG}" "C_${check_name}") | ||
check_cxx_compiler_flag("${SANITIZER_FLAG}" "CXX_${check_name}") | ||
if (${C_${check_name}} OR ${CXX_${check_name}}) | ||
# Set appropriate linker flags for building executables. | ||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}") | ||
if (${C_${check_name}}) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}") | ||
endif() | ||
if (${CXX_${check_name}}) | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAG} ${SANITIZER_ARGS}") | ||
endif() | ||
else() | ||
message(FATAL_ERROR "${flag} sanitizer is not supported (neither by C nor CXX compiler)") | ||
endif() | ||
|
||
set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES}) | ||
endmacro() |
Uh oh!
There was an error while loading. Please reload this page.