diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a11ad55..3a9af151 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.13) - +# CMP0025: Compiler id for Apple Clang is now AppleClang. +# https://cmake.org/cmake/help/v3.17/policy/CMP0025.html +cmake_policy(SET CMP0025 NEW) project(simdjson DESCRIPTION "Parsing gigabytes of JSON per second" LANGUAGES CXX C diff --git a/cmake/simdjson-flags.cmake b/cmake/simdjson-flags.cmake index ecffc65d..13a266a7 100644 --- a/cmake/simdjson-flags.cmake +++ b/cmake/simdjson-flags.cmake @@ -16,18 +16,34 @@ add_library(simdjson-flags INTERFACE) add_library(simdjson-internal-flags INTERFACE) target_link_libraries(simdjson-internal-flags INTERFACE simdjson-flags) +option(SIMDJSON_SANITIZE_UNDEFINED "Sanitize undefined behavior" OFF) +if(SIMDJSON_SANITIZE_UNDEFINED) + target_compile_options(simdjson-flags INTERFACE -fsanitize=undefined -fno-sanitize-recover=all) + target_link_libraries(simdjson-flags INTERFACE -fsanitize=undefined -fno-sanitize-recover=all) +endif() + option(SIMDJSON_SANITIZE "Sanitize addresses" OFF) if(SIMDJSON_SANITIZE) - target_compile_options(simdjson-flags INTERFACE -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all) - target_link_libraries(simdjson-flags INTERFACE -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all) - + if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + message(STATUS "The address sanitizer under Apple's clang appears to be incompatible with the undefined-behavior sanitizer.") + message(STATUS "You may set SIMDJSON_SANITIZE_UNDEFINED to sanitize undefined behavior.") + target_compile_options(simdjson-flags INTERFACE -fsanitize=address -fno-omit-frame-pointer -fno-sanitize-recover=all) + target_compile_definitions(simdjson-flags INTERFACE ASAN_OPTIONS=detect_leaks=1) + target_link_libraries(simdjson-flags INTERFACE -fsanitize=address -fno-omit-frame-pointer -fno-sanitize-recover=all) + else() + message(STATUS "Setting both the address sanitizer and the undefined sanitizer.") + target_compile_options(simdjson-flags INTERFACE -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all) + target_link_libraries(simdjson-flags INTERFACE -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all) + endif() # Ubuntu bug for GCC 5.0+ (safe for all versions) if (CMAKE_COMPILER_IS_GNUCC) target_link_libraries(simdjson-flags INTERFACE -fuse-ld=gold) endif() endif() + if(SIMDJSON_SANITIZE_THREADS) + message(STATUS "Setting both the thread sanitizer and the undefined-behavior sanitizer.") target_compile_options(simdjson-flags INTERFACE -fsanitize=thread -fsanitize=undefined -fno-sanitize-recover=all) target_link_libraries(simdjson-flags INTERFACE -fsanitize=thread -fsanitize=undefined -fno-sanitize-recover=all) @@ -38,10 +54,13 @@ if(SIMDJSON_SANITIZE_THREADS) endif() if (NOT CMAKE_BUILD_TYPE) - message(STATUS "No build type selected, default to Release") - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) - if(SIMDJSON_SANITIZE) - message(WARNING "No build type selected and you have enabled the sanitizer. Consider setting CMAKE_BUILD_TYPE to Debug to help identify the eventual problems.") + # Deliberately not including SIMDJSON_SANITIZE_THREADS since thread behavior depends on the build type. + if(SIMDJSON_SANITIZE OR SIMDJSON_SANITIZE_UNDEFINED) + message(STATUS "No build type selected and you have enabled the sanitizer, default to Debug. Consider setting CMAKE_BUILD_TYPE.") + set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE) + else() + message(STATUS "No build type selected, default to Release") + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) endif() endif()