From 93ff61d350a5ca57b460e6a440ab3cff18b168f7 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 6 Aug 2025 15:49:44 +0200 Subject: [PATCH] add asan option to cmake --- CMakeLists.txt | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 517a064..1ef9343 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") option(SOLANACEAE_ECOSYSTEM_BUILD_TESTING "Build tests" ${BUILD_TESTING}) message("II SOLANACEAE_ECOSYSTEM_BUILD_TESTING " ${SOLANACEAE_ECOSYSTEM_BUILD_TESTING}) +option(SOLANACEAE_ECOSYSTEM_ASAN "Build eco with asan (gcc/clang/msvc)" OFF) # uggly, but it needs to be defined for all dependencies too # what if its always export? @@ -29,8 +30,33 @@ if (SOLANACEAE_ECOSYSTEM_BUILD_TESTING) include(CTest) endif() -#add_compile_options(-fsanitize=undefined) -#link_libraries(-fsanitize=undefined) +if (SOLANACEAE_ECOSYSTEM_ASAN) + if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") + if (NOT WIN32) # exclude mingw + add_compile_options(-fno-omit-frame-pointer) + add_compile_options(-fsanitize=address,undefined) + #add_compile_options(-fsanitize=address,undefined,pointer-compare,pointer-subtract) + #add_compile_options(-fhardened) + #add_compile_options(-D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -ftrivial-auto-var-init=zero -fPIE -pie -Wl,-z,relro,-z,now -fstack-protector-strong -fstack-clash-protection -fcf-protection=full) + + add_link_options(-fno-omit-frame-pointer) + add_link_options(-fsanitize=address,undefined) + #add_link_options(-fsanitize=address,undefined,pointer-compare,pointer-subtract) + #add_link_options(-fhardened) + #add_link_options(-D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -ftrivial-auto-var-init=zero -fPIE -pie -Wl,-z,relro,-z,now -fstack-protector-strong -fstack-clash-protection -fcf-protection=full) + + link_libraries(-static-libasan) # make it "work" on nix + message("II enabled ASAN") + else() + message("!! can not enable ASAN on this platform (gcc/clang + win)") + endif() + elseif (MSVC) + add_compile_options("/fsanitize=address") + message("II enabled ASAN") + else() + message("!! can not enable ASAN on this platform") + endif() +endif() # external libs add_subdirectory(./external) # before increasing warn levels, sad :(