-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from gysit/update-sanitizer-module
Update the sanitizer CMake module
- Loading branch information
Showing
1 changed file
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# | ||
# Copyright (C) 2018 by George Cave - [email protected] | ||
# Original source: Copyright (C) 2018 by George Cave - [email protected] | ||
# Modified source: Copyright (C) 2020 by Jean-Michel Gorius | ||
# - [email protected] | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
|
@@ -12,6 +14,9 @@ | |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
# | ||
# Adapted by Jean-Michel Gorius to forward sanitizer flags to the linker. | ||
# | ||
|
||
set(USE_SANITIZER | ||
"" | ||
|
@@ -29,7 +34,9 @@ function(append value) | |
endfunction() | ||
|
||
if (USE_SANITIZER) | ||
append("-fno-omit-frame-pointer" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fno-omit-frame-pointer" | ||
CMAKE_C_FLAGS | ||
CMAKE_CXX_FLAGS) | ||
|
||
if (UNIX) | ||
|
||
|
@@ -40,45 +47,98 @@ if (USE_SANITIZER) | |
if (USE_SANITIZER MATCHES "([Aa]ddress);([Uu]ndefined)" | ||
OR USE_SANITIZER MATCHES "([Uu]ndefined);([Aa]ddress)") | ||
message(STATUS "Building with Address, Undefined sanitizers") | ||
append("-fsanitize=address,undefined" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize=address,undefined" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
elseif (USE_SANITIZER MATCHES "([Aa]ddress)") | ||
# Optional: -fno-optimize-sibling-calls -fsanitize-address-use-after-scope | ||
message(STATUS "Building with Address sanitizer") | ||
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize=address" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
elseif (USE_SANITIZER MATCHES "([Mm]emory([Ww]ith[Oo]rigins)?)") | ||
# Optional: -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2 | ||
append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize=memory" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
if (USE_SANITIZER MATCHES "([Mm]emory[Ww]ith[Oo]rigins)") | ||
message(STATUS "Building with MemoryWithOrigins sanitizer") | ||
append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize-memory-track-origins" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
else () | ||
message(STATUS "Building with Memory sanitizer") | ||
endif () | ||
elseif (USE_SANITIZER MATCHES "([Uu]ndefined)") | ||
message(STATUS "Building with Undefined sanitizer") | ||
append("-fsanitize=undefined" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize=undefined" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
if (EXISTS "${BLACKLIST_FILE}") | ||
append("-fsanitize-blacklist=${BLACKLIST_FILE}" CMAKE_C_FLAGS | ||
CMAKE_CXX_FLAGS) | ||
append("-fsanitize-blacklist=${BLACKLIST_FILE}" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
endif () | ||
elseif (USE_SANITIZER MATCHES "([Tt]hread)") | ||
message(STATUS "Building with Thread sanitizer") | ||
append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize=thread" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
elseif (USE_SANITIZER MATCHES "([Ll]eak)") | ||
message(STATUS "Building with Leak sanitizer") | ||
append("-fsanitize=leak" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize=leak" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
else () | ||
message( | ||
FATAL_ERROR "Unsupported value of USE_SANITIZER: ${USE_SANITIZER}") | ||
endif () | ||
elseif (MSVC) | ||
if (USE_SANITIZER MATCHES "([Aa]ddress)") | ||
message(STATUS "Building with Address sanitizer") | ||
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) | ||
append("-fsanitize=address" | ||
CMAKE_EXE_C_FLAGS | ||
CMAKE_EXE_CXX_FLAGS | ||
CMAKE_STATIC_C_FLAGS | ||
CMAKE_STATIC_CXX_FLAGS | ||
CMAKE_EXE_LINKER_FLAGS | ||
CMAKE_MODULE_LINKER_FLAGS) | ||
else () | ||
message( | ||
FATAL_ERROR | ||
"This sanitizer not yet supported in the MSVC environment: ${USE_SANITIZER}" | ||
"This sanitizer is not yet supported in the MSVC environment: ${USE_SANITIZER}" | ||
) | ||
endif () | ||
else () | ||
|