From c41a9e801403c5fefd6abb7b7c9e3415bc84e031 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Wed, 27 Mar 2024 22:18:36 +0800 Subject: [PATCH 1/5] Disable gcc sanitizer on macos by default According to https://github.com/orgs/Homebrew/discussions/3384#discussioncomment-6264292, gcc now dosen't support libsanitizer on macOS with arm64 and macOS Ventuna or later with x86_64. --- src/DynamicProjectOptions.cmake | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/DynamicProjectOptions.cmake b/src/DynamicProjectOptions.cmake index 8f6c8b92..4cf62520 100644 --- a/src/DynamicProjectOptions.cmake +++ b/src/DynamicProjectOptions.cmake @@ -101,20 +101,14 @@ macro(dynamic_project_options) ) endif() - if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") - AND NOT WIN32 + if(((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*") AND (NOT WIN32)) + OR ((CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND (NOT WIN32) AND (NOT APPLE)) ) set(SUPPORTS_UBSAN ON) + set(SUPPORTS_ASAN ON) else() set(SUPPORTS_UBSAN OFF) - endif() - - if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") - AND WIN32 - ) set(SUPPORTS_ASAN OFF) - else() - set(SUPPORTS_ASAN ON) endif() # ccache, clang-tidy, cppcheck are only supported with Ninja and Makefile based generators From f5e7f3aff31d7d1f4dbd480e959bced64dc8eef5 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 16 Apr 2024 16:43:18 +0800 Subject: [PATCH 2/5] Add gcc test on macos-13 --- .github/workflows/ci.yml | 6 +++++- src/Utilities.cmake | 9 +++++++++ tests/install/CMakeLists.txt | 32 ++++++++++++++++++++++++++------ tests/myproj/CMakeLists.txt | 13 ++++++++++++- tests/myproj/Taskfile.yml | 4 ++-- 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 94d247c6..879e6435 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,10 @@ jobs: compiler: "gcc" cmake: true vcvarsall: true + - os: "macos-13" + compiler: "gcc" + cmake: true + vcvarsall: true exclude: # fails with an internal error - os: "macos-12" @@ -80,7 +84,7 @@ jobs: cppcheck: true clangtidy: true task: true - doxygen: ${{ !contains(matrix.os, 'macos-11') }} + doxygen: ${{ !contains(matrix.os, 'macos-11') && !contains(matrix.os, 'macos-13') }} - name: Test if: ${{ !cancelled() }} diff --git a/src/Utilities.cmake b/src/Utilities.cmake index 25086238..0e539f56 100644 --- a/src/Utilities.cmake +++ b/src/Utilities.cmake @@ -125,6 +125,15 @@ function(detect_architecture arch) endif() endfunction() +function(detect_macos_version version) + find_program(SW_VERS_EXECUTABLE sw_vers) + execute_process( + COMMAND "${SW_VERS_EXECUTABLE}" -productVersion + OUTPUT_VARIABLE MACOS_VERSION + ) + set(${version} "${MACOS_VERSION}" PARENT_SCOPE) +endfunction() + # convert semicolons in generator expression to $ function(convert_genex_semicolons genex output) set(result) diff --git a/tests/install/CMakeLists.txt b/tests/install/CMakeLists.txt index e4a78b9d..ba222bc0 100644 --- a/tests/install/CMakeLists.txt +++ b/tests/install/CMakeLists.txt @@ -8,6 +8,26 @@ run_conan() project(anotherproj VERSION 0.1.0 LANGUAGES CXX C) +option(FEATURE_TESTS "Enable the tests" OFF) +if(FEATURE_TESTS) + # Enable sanitizers and static analyzers when running the tests + check_sanitizers_support( + ENABLE_SANITIZER_ADDRESS ENABLE_SANITIZER_UNDEFINED_BEHAVIOR ENABLE_SANITIZER_LEAK + ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY + ) +endif() + +set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "ENABLE_INTERPROCEDURAL_OPTIMIZATION") +if(APPLE) + detect_macos_version(apple_version) + if(apple_version VERSION_GREATER_EQUAL 13) + # workaround for linkage error as described in https://github.com/Homebrew/homebrew-core/issues/145991 + # although fixed, this problem still exists in github actions + add_link_options(-Wl,-ld_classic) + set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "") + endif() +endif() + # Initialize project_options project_options( ENABLE_CACHE @@ -23,14 +43,14 @@ project_options( DOXYGEN_THEME "${CMAKE_CURRENT_LIST_DIR}/css/my_custom_theme.css" "${CMAKE_CURRENT_LIST_DIR}/css/my_custom_theme_extra.css" - ENABLE_INTERPROCEDURAL_OPTIMIZATION + ${ENABLE_INTERPROCEDURAL_OPTIMIZATION} # ENABLE_BUILD_WITH_TIME_TRACE # ENABLE_UNITY - # ENABLE_SANITIZER_ADDRESS - # ENABLE_SANITIZER_LEAK - # ENABLE_SANITIZER_UNDEFINED_BEHAVIOR - # ENABLE_SANITIZER_THREAD - # ENABLE_SANITIZER_MEMORY + ${ENABLE_SANITIZER_ADDRESS} + # ${ENABLE_SANITIZER_LEAK} + ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + # ${ENABLE_SANITIZER_THREAD} + # ${ENABLE_SANITIZER_MEMORY} ) # add src, tests, etc here: diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index 1a29ddbf..769de3d3 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -49,6 +49,17 @@ if(NOT MSVC) #message(STATUS "Detect Linker: ${LINKER}") endif() +set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "ENABLE_INTERPROCEDURAL_OPTIMIZATION") +if(APPLE) + detect_macos_version(apple_version) + if(apple_version VERSION_GREATER_EQUAL 13) + # workaround for linkage error as described in https://github.com/Homebrew/homebrew-core/issues/145991 + # although fixed, this problem still exists in github actions + add_link_options(-Wl,-ld_classic) + set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "") + endif() +endif() + # Initialize project_options # uncomment the options to enable them project_options( @@ -65,7 +76,7 @@ project_options( # PCH_HEADERS # ${PCH_HEADERS} ENABLE_DOXYGEN - ENABLE_INTERPROCEDURAL_OPTIMIZATION + ${ENABLE_INTERPROCEDURAL_OPTIMIZATION} ENABLE_NATIVE_OPTIMIZATION # ENABLE_BUILD_WITH_TIME_TRACE # ENABLE_UNITY diff --git a/tests/myproj/Taskfile.yml b/tests/myproj/Taskfile.yml index 787a6d63..31dd2357 100644 --- a/tests/myproj/Taskfile.yml +++ b/tests/myproj/Taskfile.yml @@ -24,13 +24,13 @@ tasks: cmds: - task: build vars: - CMAKE_ARGS: -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} + CMAKE_ARGS: -DFEATURE_TESTS:BOOL=ON -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} build.mingw.release: cmds: - task: build.release vars: - CMAKE_ARGS: -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} + CMAKE_ARGS: -DFEATURE_TESTS:BOOL=ON -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} lint: - ~/vcpkg/vcpkg format-manifest ./vcpkg.json From 216dfd93c86bfac9a800dc23c485659e05908fe0 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 16 Apr 2024 17:01:00 +0800 Subject: [PATCH 3/5] Test if sanitizers are enabled correctly --- tests/myproj/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index 769de3d3..104e9c8e 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -43,6 +43,9 @@ if(FEATURE_TESTS) ) endif() +include(CMakePrintHelpers) +cmake_print_variables(ENABLE_SANITIZER_ADDRESS ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) + # Detect custom linker if(NOT MSVC) find_linker(LINKER) From 19169997e7834056c8e95b4bb210fd9f45c5c514 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 16 Apr 2024 17:09:59 +0800 Subject: [PATCH 4/5] Enable sanitizers on macos gcc test --- tests/install/CMakeLists.txt | 2 +- tests/myproj/CMakeLists.txt | 5 +---- tests/myproj/Taskfile.yml | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/install/CMakeLists.txt b/tests/install/CMakeLists.txt index ba222bc0..8983ce29 100644 --- a/tests/install/CMakeLists.txt +++ b/tests/install/CMakeLists.txt @@ -8,7 +8,7 @@ run_conan() project(anotherproj VERSION 0.1.0 LANGUAGES CXX C) -option(FEATURE_TESTS "Enable the tests" OFF) +option(FEATURE_TESTS "Enable the tests" ON) if(FEATURE_TESTS) # Enable sanitizers and static analyzers when running the tests check_sanitizers_support( diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index 104e9c8e..5c4b88e2 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -33,7 +33,7 @@ set(PCH_HEADERS ) -option(FEATURE_TESTS "Enable the tests" OFF) +option(FEATURE_TESTS "Enable the tests" ON) if(FEATURE_TESTS) # Enable sanitizers and static analyzers when running the tests @@ -43,9 +43,6 @@ if(FEATURE_TESTS) ) endif() -include(CMakePrintHelpers) -cmake_print_variables(ENABLE_SANITIZER_ADDRESS ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) - # Detect custom linker if(NOT MSVC) find_linker(LINKER) diff --git a/tests/myproj/Taskfile.yml b/tests/myproj/Taskfile.yml index 31dd2357..787a6d63 100644 --- a/tests/myproj/Taskfile.yml +++ b/tests/myproj/Taskfile.yml @@ -24,13 +24,13 @@ tasks: cmds: - task: build vars: - CMAKE_ARGS: -DFEATURE_TESTS:BOOL=ON -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} + CMAKE_ARGS: -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} build.mingw.release: cmds: - task: build.release vars: - CMAKE_ARGS: -DFEATURE_TESTS:BOOL=ON -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} + CMAKE_ARGS: -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER={{.CROSS_CC | default "x86_64-w64-mingw32-gcc"}} -DCMAKE_CXX_COMPILER={{.CROSS_CXX | default "x86_64-w64-mingw32-g++"}} lint: - ~/vcpkg/vcpkg format-manifest ./vcpkg.json From 6f25c7f942e2e53a50b2fd8d1d49e7c9c675e876 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 16 Apr 2024 19:17:25 +0800 Subject: [PATCH 5/5] Make sanitizer enabling consistent - Make sanitizer enabling in dynamic_project_options consistent with detect_sanitizers_support. - detect MacOS OS version to keep gcc sanitizers enable on x86_64 macOS Monterey or older. --- src/DynamicProjectOptions.cmake | 20 ++++++++++++++++---- src/Sanitizers.cmake | 29 ++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/DynamicProjectOptions.cmake b/src/DynamicProjectOptions.cmake index 4cf62520..bd7cec7e 100644 --- a/src/DynamicProjectOptions.cmake +++ b/src/DynamicProjectOptions.cmake @@ -1,5 +1,7 @@ include_guard() +include("${CMAKE_CURRENT_LIST_DIR}/Sanitizers.cmake") + #[[.rst: ``dynamic_project_options`` @@ -101,16 +103,26 @@ macro(dynamic_project_options) ) endif() - if(((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*") AND (NOT WIN32)) - OR ((CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND (NOT WIN32) AND (NOT APPLE)) + check_sanitizers_support( + ENABLE_SANITIZER_ADDRESS + ENABLE_SANITIZER_UNDEFINED_BEHAVIOR + ENABLE_SANITIZER_LEAK + ENABLE_SANITIZER_THREAD + ENABLE_SANITIZER_MEMORY ) - set(SUPPORTS_UBSAN ON) + + if(ENABLE_SANITIZER_ADDRESS) set(SUPPORTS_ASAN ON) else() - set(SUPPORTS_UBSAN OFF) set(SUPPORTS_ASAN OFF) endif() + if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) + set(SUPPORTS_UBSAN ON) + else() + set(SUPPORTS_UBSAN OFF) + endif() + # ccache, clang-tidy, cppcheck are only supported with Ninja and Makefile based generators # note that it is possible to use Ninja with cl, so this still allows clang-tidy on Windows # with CL. diff --git a/src/Sanitizers.cmake b/src/Sanitizers.cmake index 830f209e..df927a9b 100644 --- a/src/Sanitizers.cmake +++ b/src/Sanitizers.cmake @@ -1,5 +1,7 @@ include_guard() +include("${CMAKE_CURRENT_LIST_DIR}/Utilities.cmake") + # Enable the sanitizers for the given project function( enable_sanitizers @@ -133,11 +135,28 @@ function( ) set(SANITIZERS "") if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") - list(APPEND SANITIZERS "address") - list(APPEND SANITIZERS "undefined") - list(APPEND SANITIZERS "leak") - list(APPEND SANITIZERS "thread") - list(APPEND SANITIZERS "memory") + set(HAS_SANITIZER_SUPPORT ON) + + # Disable gcc sanitizer on some macos according to https://github.com/orgs/Homebrew/discussions/3384#discussioncomment-6264292 + if((CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND APPLE) + detect_macos_version(MACOS_VERSION) + if(MACOS_VERSION VERSION_GREATER_EQUAL 13) + set(HAS_SANITIZER_SUPPORT OFF) + endif() + + detect_architecture(ARCHITECTURE) + if(ARCHITECTURE STREQUAL "arm64") + set(HAS_SANITIZER_SUPPORT OFF) + endif() + endif() + + if (HAS_SANITIZER_SUPPORT) + list(APPEND SANITIZERS "address") + list(APPEND SANITIZERS "undefined") + list(APPEND SANITIZERS "leak") + list(APPEND SANITIZERS "thread") + list(APPEND SANITIZERS "memory") + endif() elseif(MSVC) # or it is MSVC and has run vcvarsall string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)