Skip to content

Commit 84a0907

Browse files
committed
[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
1 parent b718c6d commit 84a0907

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,9 @@ endif()
10871087
#
10881088
swift_common_cxx_warnings()
10891089

1090+
# Set sanitizer options for Swift compiler.
1091+
swift_common_sanitizer_config()
1092+
10901093
# Check if we're build with MSVC or Clang-cl, as these compilers have similar command line arguments.
10911094
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
10921095
set(SWIFT_COMPILER_IS_MSVC_LIKE TRUE)

cmake/modules/AddPureSwift.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ function(_add_host_swift_compile_options name)
6969
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-tools-directory;${tools_path};>)
7070
endif()
7171
endif()
72-
_add_host_variant_swift_sanitizer_flags(${name})
7372

7473
target_compile_options(${name} PRIVATE
7574
$<$<COMPILE_LANGUAGE:Swift>:-color-diagnostics>

cmake/modules/AddSwift.cmake

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,6 @@ function(_set_target_prefix_and_suffix target kind sdk)
8282
endif()
8383
endfunction()
8484

85-
function(_add_host_variant_swift_sanitizer_flags target)
86-
if(LLVM_USE_SANITIZER)
87-
if(LLVM_USE_SANITIZER STREQUAL "Address")
88-
set(_Swift_SANITIZER_FLAGS "-sanitize=address" "-Xclang-linker" "-fsanitize=address")
89-
elseif(LLVM_USE_SANITIZER STREQUAL "HWAddress")
90-
# Not supported?
91-
elseif(LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
92-
# Not supported
93-
if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
94-
# Not supported
95-
endif()
96-
elseif(LLVM_USE_SANITIZER STREQUAL "Undefined")
97-
set(_Swift_SANITIZER_FLAGS "-sanitize=undefined" "-Xclang-linker" "-fsanitize=undefined")
98-
elseif(LLVM_USE_SANITIZER STREQUAL "Thread")
99-
set(_Swift_SANITIZER_FLAGS "-sanitize=thread" "-Xclang-linker" "-fsanitize=thread")
100-
elseif(LLVM_USE_SANITIZER STREQUAL "DataFlow")
101-
# Not supported
102-
elseif(LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
103-
LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
104-
set(_Swift_SANITIZER_FLAGS "-sanitize=address" "-sanitize=undefined" "-Xclang-linker" "-fsanitize=address" "-Xclang-linker" "-fsanitize=undefined")
105-
elseif(LLVM_USE_SANITIZER STREQUAL "Leaks")
106-
# Not supported
107-
else()
108-
message(SEND_ERROR "unsupported value for LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
109-
endif()
110-
111-
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:${_Swift_SANITIZER_FLAGS}>)
112-
endif()
113-
endfunction()
114-
11585
function(swift_get_host_triple out_var)
11686
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
11787
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
@@ -145,8 +115,6 @@ function(_add_host_variant_c_compile_link_flags name)
145115

146116
if (CMAKE_Swift_COMPILER)
147117
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${SWIFT_HOST_TRIPLE}>)
148-
149-
_add_host_variant_swift_sanitizer_flags(${name})
150118
endif()
151119

152120
set(_sysroot

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,35 @@ function(swift_common_llvm_config target)
369369
llvm_config("${target}" ${ARGN})
370370
endif()
371371
endfunction()
372+
373+
# Set sanitizer options to all Swift compiler flags. Similar options are added to C/CXX compiler in 'HandleLLVMOptions'
374+
macro(swift_common_sanitizer_config)
375+
if(LLVM_USE_SANITIZER)
376+
if(LLVM_USE_SANITIZER STREQUAL "Address")
377+
set(_Swift_SANITIZER_FLAGS "-sanitize=address -Xclang-linker -fsanitize=address")
378+
elseif(LLVM_USE_SANITIZER STREQUAL "HWAddress")
379+
# Not supported?
380+
elseif(LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
381+
# Not supported
382+
if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
383+
# Not supported
384+
endif()
385+
elseif(LLVM_USE_SANITIZER STREQUAL "Undefined")
386+
set(_Swift_SANITIZER_FLAGS "-sanitize=undefined -Xclang-linker -fsanitize=undefined")
387+
elseif(LLVM_USE_SANITIZER STREQUAL "Thread")
388+
set(_Swift_SANITIZER_FLAGS "-sanitize=thread -Xclang-linker -fsanitize=thread")
389+
elseif(LLVM_USE_SANITIZER STREQUAL "DataFlow")
390+
# Not supported
391+
elseif(LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
392+
LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
393+
set(_Swift_SANITIZER_FLAGS "-sanitize=address -sanitize=undefined -Xclang-linker -fsanitize=address -Xclang-linker -fsanitize=undefined")
394+
elseif(LLVM_USE_SANITIZER STREQUAL "Leaks")
395+
# Not supported
396+
else()
397+
message(SEND_ERROR "unsupported value for LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
398+
endif()
399+
400+
set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS} ${_Swift_SANITIZER_FLAGS}")
401+
402+
endif()
403+
endmacro()

0 commit comments

Comments
 (0)