Skip to content

Commit eb914c7

Browse files
committed
Improved add_sanitizers() CMake function.
Improved compatibility on Windows. Disable incremental linking and debug information format. Signed-off-by: kilo52 <phil.gaiser@raven-computing.com>
1 parent 1b339a0 commit eb914c7

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

share/c/cmake/SanitizerUtil.cmake

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2025 Raven Computing
1+
# Copyright (C) 2026 Raven Computing
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -24,7 +24,9 @@
2424
# Adds sanitizer support to a given CMake target.
2525
#
2626
# Appends the appropriate compiler and linker flags to enable sanitizers for
27-
# the specified target.
27+
# the specified target. Supported platforms are GNU/Linux and Windows.
28+
# Sanitizers can be used for release build variants but preferably are only
29+
# enabled for debug builds to get the most informative output.
2830
#
2931
# Arguments:
3032
#
@@ -42,10 +44,11 @@ function(add_sanitizers target_name)
4244
"-fno-omit-frame-pointer"
4345
)
4446
set(
45-
SAN_LINK_FLAGS
47+
SAN_LINK_FLAGS_LINUX
4648
"-fsanitize=address" "-fsanitize=leak" "-fsanitize=undefined"
4749
)
48-
set(SAN_COMPILE_FLAGS_WINDOWS "/fsanitize=address" "/Oy-")
50+
set(SAN_COMPILE_FLAGS_WINDOWS "/fsanitize=address" "/Oy-" "/Zi")
51+
set(SAN_LINK_FLAGS_WINDOWS "/INCREMENTAL:NO")
4952

5053
target_compile_options(
5154
${target_name}
@@ -56,7 +59,8 @@ function(add_sanitizers target_name)
5659
target_link_options(
5760
${target_name}
5861
PUBLIC
59-
$<$<NOT:$<PLATFORM_ID:Windows>>:${SAN_LINK_FLAGS}>
62+
$<$<PLATFORM_ID:Linux>:${SAN_LINK_FLAGS_LINUX}>
63+
$<$<PLATFORM_ID:Windows>:${SAN_LINK_FLAGS_WINDOWS}>
6064
)
6165

6266
endfunction()

0 commit comments

Comments
 (0)