diff options
author | Bhushan Shah <bhush94@gmail.com> | 2022-01-22 19:20:06 +0530 |
---|---|---|
committer | Bhushan Shah <bshah@mykolab.com> | 2022-01-23 03:53:21 +0000 |
commit | abc3a698abc0dfea19040007a7c57d04c3bb6865 (patch) | |
tree | 4e91d1a3698b6856ea769f7733a5c856ea9514bf | |
parent | 675ca285769d6a5729c13c0eaad524355188027d (diff) | |
download | extra-cmake-modules-abc3a698abc0dfea19040007a7c57d04c3bb6865.tar.gz extra-cmake-modules-abc3a698abc0dfea19040007a7c57d04c3bb6865.tar.bz2 |
ECMEnableSanitizers: do not enable linker flags when not supported
If linker does not support the linker flag, do not enable it, on alpine
e.g. musl does not support the ASAN, if it is enabled unconditionally,
it will cause the linker to error out
-rw-r--r-- | modules/ECMEnableSanitizers.cmake | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/modules/ECMEnableSanitizers.cmake b/modules/ECMEnableSanitizers.cmake index 84f1819c..42d5fcb7 100644 --- a/modules/ECMEnableSanitizers.cmake +++ b/modules/ECMEnableSanitizers.cmake @@ -74,6 +74,7 @@ This is an example of usage:: Since 1.3.0. #]=======================================================================] +include(CheckLinkerFlag) # MACRO check_compiler_version #----------------------------- macro (check_compiler_version gcc_required_version clang_required_version msvc_required_version) @@ -147,18 +148,24 @@ if (ECM_ENABLE_SANITIZERS) string(TOLOWER ${CUR_SANITIZER} CUR_SANITIZER) # check option and enable appropriate flags enable_sanitizer_flags ( ${CUR_SANITIZER} ) - # TODO: GCC will not link pthread library if enabled ASan - if(CMAKE_C_COMPILER_ID MATCHES "Clang") - set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${XSAN_COMPILE_FLAGS}" ) - endif() - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${XSAN_COMPILE_FLAGS}" ) - if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") - link_libraries(${XSAN_LINKER_FLAGS}) - endif() - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + check_linker_flag(CXX "-l${XSAN_LINKER_FLAGS}" _HAVE_XSAN_LINKER_FLAGS) + if (_HAVE_XSAN_LINKER_FLAGS) + # TODO: GCC will not link pthread library if enabled ASan + if(CMAKE_C_COMPILER_ID MATCHES "Clang") + string(APPEND CMAKE_C_FLAGS " ${XSAN_COMPILE_FLAGS}") + endif() + string(APPEND CMAKE_CXX_FLAGS " ${XSAN_COMPILE_FLAGS}") + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + link_libraries(${XSAN_LINKER_FLAGS}) + endif() + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") string(REPLACE "-Wl,--no-undefined" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") string(REPLACE "-Wl,--no-undefined" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}") - endif () + endif () + else() + message(STATUS "Tried to enable sanitizers -DECM_ENABLE_SANITIZERS=${ECM_ENABLE_SANITIZERS}, \ +but linker does not have sanitizer support") + endif() endforeach() else() message(STATUS "Tried to enable sanitizers (-DECM_ENABLE_SANITIZERS=${ECM_ENABLE_SANITIZERS}), \ |