From 84a09071cb1e6a98fb930d9f77f474706a47c8b0 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Mon, 13 Jan 2025 15:51:29 -0800 Subject: [PATCH] [CMake] Globally set sanitizer options Swift targets For C/CXX targets, sanitizer options are set by 'CMAKE_{C|CXX}_FLAGS' in HandleLLVMComfig.cmake. However for Swift targets, they are set for each target. That caused some mismatch issues. Instead set them globally for Swift targets too. rdar://142516855 --- CMakeLists.txt | 3 ++ cmake/modules/AddPureSwift.cmake | 1 - cmake/modules/AddSwift.cmake | 32 ---------------------- cmake/modules/SwiftSharedCMakeConfig.cmake | 32 ++++++++++++++++++++++ 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ded05a6626e..3519842e9ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1087,6 +1087,9 @@ endif() # swift_common_cxx_warnings() +# Set sanitizer options for Swift compiler. +swift_common_sanitizer_config() + # Check if we're build with MSVC or Clang-cl, as these compilers have similar command line arguments. if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC") set(SWIFT_COMPILER_IS_MSVC_LIKE TRUE) diff --git a/cmake/modules/AddPureSwift.cmake b/cmake/modules/AddPureSwift.cmake index 28e496a1ba4..a40689bf862 100644 --- a/cmake/modules/AddPureSwift.cmake +++ b/cmake/modules/AddPureSwift.cmake @@ -69,7 +69,6 @@ function(_add_host_swift_compile_options name) target_compile_options(${name} PRIVATE $<$:-tools-directory;${tools_path};>) endif() endif() - _add_host_variant_swift_sanitizer_flags(${name}) target_compile_options(${name} PRIVATE $<$:-color-diagnostics> diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 909a8fb8499..b8e9a9d41d1 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -82,36 +82,6 @@ function(_set_target_prefix_and_suffix target kind sdk) endif() endfunction() -function(_add_host_variant_swift_sanitizer_flags target) - if(LLVM_USE_SANITIZER) - if(LLVM_USE_SANITIZER STREQUAL "Address") - set(_Swift_SANITIZER_FLAGS "-sanitize=address" "-Xclang-linker" "-fsanitize=address") - elseif(LLVM_USE_SANITIZER STREQUAL "HWAddress") - # Not supported? - elseif(LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") - # Not supported - if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") - # Not supported - endif() - elseif(LLVM_USE_SANITIZER STREQUAL "Undefined") - set(_Swift_SANITIZER_FLAGS "-sanitize=undefined" "-Xclang-linker" "-fsanitize=undefined") - elseif(LLVM_USE_SANITIZER STREQUAL "Thread") - set(_Swift_SANITIZER_FLAGS "-sanitize=thread" "-Xclang-linker" "-fsanitize=thread") - elseif(LLVM_USE_SANITIZER STREQUAL "DataFlow") - # Not supported - elseif(LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR - LLVM_USE_SANITIZER STREQUAL "Undefined;Address") - set(_Swift_SANITIZER_FLAGS "-sanitize=address" "-sanitize=undefined" "-Xclang-linker" "-fsanitize=address" "-Xclang-linker" "-fsanitize=undefined") - elseif(LLVM_USE_SANITIZER STREQUAL "Leaks") - # Not supported - else() - message(SEND_ERROR "unsupported value for LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") - endif() - - target_compile_options(${name} PRIVATE $<$:${_Swift_SANITIZER_FLAGS}>) - endif() -endfunction() - function(swift_get_host_triple out_var) if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}") @@ -145,8 +115,6 @@ function(_add_host_variant_c_compile_link_flags name) if (CMAKE_Swift_COMPILER) target_compile_options(${name} PRIVATE $<$:-target;${SWIFT_HOST_TRIPLE}>) - - _add_host_variant_swift_sanitizer_flags(${name}) endif() set(_sysroot diff --git a/cmake/modules/SwiftSharedCMakeConfig.cmake b/cmake/modules/SwiftSharedCMakeConfig.cmake index 057a48d6bdc..edade93050e 100644 --- a/cmake/modules/SwiftSharedCMakeConfig.cmake +++ b/cmake/modules/SwiftSharedCMakeConfig.cmake @@ -369,3 +369,35 @@ function(swift_common_llvm_config target) llvm_config("${target}" ${ARGN}) endif() endfunction() + +# Set sanitizer options to all Swift compiler flags. Similar options are added to C/CXX compiler in 'HandleLLVMOptions' +macro(swift_common_sanitizer_config) + if(LLVM_USE_SANITIZER) + if(LLVM_USE_SANITIZER STREQUAL "Address") + set(_Swift_SANITIZER_FLAGS "-sanitize=address -Xclang-linker -fsanitize=address") + elseif(LLVM_USE_SANITIZER STREQUAL "HWAddress") + # Not supported? + elseif(LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") + # Not supported + if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") + # Not supported + endif() + elseif(LLVM_USE_SANITIZER STREQUAL "Undefined") + set(_Swift_SANITIZER_FLAGS "-sanitize=undefined -Xclang-linker -fsanitize=undefined") + elseif(LLVM_USE_SANITIZER STREQUAL "Thread") + set(_Swift_SANITIZER_FLAGS "-sanitize=thread -Xclang-linker -fsanitize=thread") + elseif(LLVM_USE_SANITIZER STREQUAL "DataFlow") + # Not supported + elseif(LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR + LLVM_USE_SANITIZER STREQUAL "Undefined;Address") + set(_Swift_SANITIZER_FLAGS "-sanitize=address -sanitize=undefined -Xclang-linker -fsanitize=address -Xclang-linker -fsanitize=undefined") + elseif(LLVM_USE_SANITIZER STREQUAL "Leaks") + # Not supported + else() + message(SEND_ERROR "unsupported value for LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") + endif() + + set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS} ${_Swift_SANITIZER_FLAGS}") + + endif() +endmacro()