summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2018-10-24 19:35:58 -0700
committerGuy Harris <guy@alum.mit.edu>2018-10-24 19:35:58 -0700
commit392914eaf691f7cb0a80d59bffe090cffbab8b93 (patch)
treeba89c78c080603fbd48f13de41a51f7ebe2f5832
parent1116e7b442ad19dd7166313f8863235c50226ae4 (diff)
downloadlibpcap-392914eaf691f7cb0a80d59bffe090cffbab8b93.tar.gz
Add an ENABLE_SANITIZERS list variable to enable various sanitizers.
If empty, no sanitizers are enabled; otherwise, it's a (semicolon-separated) list of sanitizers, e.g. "address;undefined", and it will attempt to enable all the sanitizers in the list. If any aren't supported by the compiler, we fail. Fix another issue we found along the way - to print a fatal error, do message(FATAL_ERROR "message") not message(ERROR "message")
-rw-r--r--CMakeLists.txt93
1 files changed, 92 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 06276f24..82d4c3a5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,6 +9,13 @@ if(POLICY CMP0042)
cmake_policy(SET CMP0042 OLD)
endif()
+#
+# Squelch noise about quoted strings in if() statements.
+# WE KNOW WHAT WE'RE DOING, WE'RE DOING EVERYTHING THE WAY THAT NEWER
+# VERSIONS OF CMAKE EXPECT BY DEFAULT, DON'T WASTE OUR TIME WITH NOISE.
+#
+cmake_policy(SET CMP0054 NEW)
+
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
project(pcap)
@@ -257,6 +264,90 @@ else (USE_STATIC_RT)
message(STATUS "Use DYNAMIC runtime")
endif(USE_STATIC_RT)
+#
+# Based on
+#
+# https://github.com/commonmark/cmark/blob/master/FindAsan.cmake
+#
+# The MIT License (MIT)
+#
+# Copyright (c) 2013 Matthew Arsenault
+#
+# 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.
+#
+# Test if the each of the sanitizers in the ENABLE_SANITIZERS list are
+# supported by the compiler, and, if so, adds the appropriate flags to
+# CMAKE_C_FLAGS, CMAKE_CXX_FLAGS, CMAKE_EXE_LINKER_FLAGS, and
+# CMAKE_SHARED_LINKER_FLAGS. If not, it fails.
+#
+set(SANITIZER_FLAGS "")
+foreach(sanitizer IN LISTS ENABLE_SANITIZERS)
+ # Set -Werror to catch "argument unused during compilation" warnings
+
+ message(STATUS "Checking sanitizer ${sanitizer}")
+ set(sanitizer_variable "sanitize_${sanitizer}")
+ set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=${sanitizer}")
+ check_c_compiler_flag("-fsanitize=${sanitizer}" ${sanitizer_variable})
+ if(${${sanitizer_variable}})
+ set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fsanitize=${sanitizer}")
+ message(STATUS "${sanitizer} sanitizer supported using -fsanitizer=${sanitizer}")
+ else()
+ #
+ # Try the versions supported prior to Clang 3.2.
+ # If the sanitizer is "address", try -fsanitize-address.
+ # If it's "undefined", try -fcatch-undefined-behavior.
+ # Otherwise, give up.
+ #
+ set(sanitizer_variable "OLD_${sanitizer_variable}")
+ if ("${sanitizer}" STREQUAL "address")
+ set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize-address")
+ check_c_compiler_flag("-fsanitize-address" ${sanitizer_variable})
+ if(${${sanitizer_variable}})
+ set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fsanitize-address")
+ message(STATUS "${sanitizer} sanitizer supported using -fsanitize-address")
+ else()
+ message(FATAL_ERROR "${sanitizer} isn't a supported sanitizer")
+ endif()
+ elseif("${sanitizer}" STREQUAL "undefined")
+ set(CMAKE_REQUIRED_FLAGS "-Werror -fcatch-undefined-behavior")
+ check_c_compiler_flag("-fcatch-undefined-behavior" ${sanitizer_variable})
+ if(${${sanitizer_variable}})
+ set(SANITIZER_FLAGS "${SANITIZER_FLAGS} -fcatch-undefined-behavior")
+ message(STATUS "${sanitizer} sanitizer supported using catch-undefined-behavior")
+ else()
+ message(FATAL_ERROR "${sanitizer} isn't a supported sanitizer")
+ endif()
+ else()
+ message(FATAL_ERROR "${sanitizer} isn't a supported sanitizer")
+ endif()
+ endif()
+
+ unset(CMAKE_REQUIRED_FLAGS)
+endforeach()
+
+if(NOT "${SANITIZER_FLAGS}" STREQUAL "")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -g ${SANITIZER_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -g ${SANITIZER_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls")
+ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAGS}")
+ set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${SANITIZER_FLAGS}")
+endif()
+
###################################################################
# Detect available platform features
###################################################################
@@ -964,7 +1055,7 @@ if(WIN32)
set(PCAP_LINK_LIBRARIES ${PACKET_LIBRARIES} ${PCAP_LINK_LIBRARIES})
elseif(PCAP_TYPE STREQUAL "null")
else()
- message(ERROR "${PCAP_TYPE} is not a valid pcap type")
+ message(FATAL_ERROR "${PCAP_TYPE} is not a valid pcap type")
endif()
else(WIN32)
if(PCAP_TYPE STREQUAL "dlpi")