Skip to content

Commit

Permalink
Use toolchain for asan flag overriding (#922)
Browse files Browse the repository at this point in the history
This is more correct. We have total control over the flags used when building with ASAN
  • Loading branch information
ekilmer authored Apr 12, 2022
1 parent 3c0d79b commit d3df3a8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
32 changes: 32 additions & 0 deletions toolchain/vcpkg_unix_sanitizer_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This toolchain only sets up override compiler flags for CMake's 'Release' and
# 'Debug' build types. You _must_ set your sanitizer flag(s)
# ('-fsanitize=address') in the vcpkg triplet. These flags aren't globally
# applied because some programs like LLVM don't play nice with global
# sanitizers and have their own buildsystem options to enable compilation with
# those flags.
get_property(_CMAKE_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
if(NOT _CMAKE_IN_TRY_COMPILE)
# Our override flags for sanitization, but does not include sanitizer flags,
# that comes from vcpkg flags
set(common_flags "-O1 -g -fno-omit-frame-pointer -fno-optimize-sibling-calls")

# Copied from vcpkg's 'scripts/toolchain/linux.cmake'. Don't use the `_INIT`
# suffix because we want to _override_ any flags that CMake would use
set(CMAKE_C_FLAGS " -fPIC ${common_flags} ${VCPKG_C_FLAGS} ")
set(CMAKE_CXX_FLAGS " -fPIC ${common_flags} ${VCPKG_CXX_FLAGS} ")
set(CMAKE_C_FLAGS_DEBUG " ${VCPKG_C_FLAGS_DEBUG} ")
set(CMAKE_CXX_FLAGS_DEBUG " ${VCPKG_CXX_FLAGS_DEBUG} ")
set(CMAKE_C_FLAGS_RELEASE " -DNDEBUG ${VCPKG_C_FLAGS_RELEASE} ")
set(CMAKE_CXX_FLAGS_RELEASE " -DNDEBUG ${VCPKG_CXX_FLAGS_RELEASE} ")

set(CMAKE_SHARED_LINKER_FLAGS " ${VCPKG_LINKER_FLAGS} ")
set(CMAKE_EXE_LINKER_FLAGS " ${VCPKG_LINKER_FLAGS} ")
if(VCPKG_CRT_LINKAGE STREQUAL "static")
string(APPEND CMAKE_SHARED_LINKER_FLAGS "-static ")
string(APPEND CMAKE_EXE_LINKER_FLAGS APPEND "-static ")
endif()
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG " ${VCPKG_LINKER_FLAGS_DEBUG} ")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG " ${VCPKG_LINKER_FLAGS_DEBUG} ")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE " ${VCPKG_LINKER_FLAGS_RELEASE} ")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE " ${VCPKG_LINKER_FLAGS_RELEASE} ")
endif()
7 changes: 5 additions & 2 deletions triplets/arm64-osx-asan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ set(VCPKG_USE_SANITIZER "Address")
# If the following flags cause errors during build, you might need to manually
# ignore the PORT and check VCPKG_USE_SANITIZER
if(NOT PORT MATCHES "^((upb))$")
set(VCPKG_CXX_FLAGS "-O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_C_FLAGS "-O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_CXX_FLAGS "-fsanitize=address")
set(VCPKG_C_FLAGS "-fsanitize=address")
endif()

# Always apply sanitizer to linker flags
set(VCPKG_LINKER_FLAGS "-fsanitize=address")

# This is where we override default CMake compiler/linker flags
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/../toolchain/vcpkg_unix_sanitizer_toolchain.cmake")

set(VCPKG_CMAKE_SYSTEM_NAME Darwin)
set(VCPKG_OSX_ARCHITECTURES arm64)
7 changes: 5 additions & 2 deletions triplets/x64-linux-asan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ set(VCPKG_USE_SANITIZER "Address")
# If the following flags cause errors during build, you might need to manually
# ignore the PORT and check VCPKG_USE_SANITIZER
if(NOT PORT MATCHES "^((upb))$")
set(VCPKG_CXX_FLAGS "-O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_C_FLAGS "-O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_CXX_FLAGS "-fsanitize=address")
set(VCPKG_C_FLAGS "-fsanitize=address")
endif()

# Always apply sanitizer to linker flags
set(VCPKG_LINKER_FLAGS "-fsanitize=address")

# This is where we override default CMake compiler/linker flags
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/../toolchain/vcpkg_unix_sanitizer_toolchain.cmake")

set(VCPKG_CMAKE_SYSTEM_NAME Linux)
11 changes: 7 additions & 4 deletions triplets/x64-linux-rel-asan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)

# Only release builds
set(VCPKG_BUILD_TYPE release)

# ASAN
# Make sure this value matches up with https://llvm.org/docs/CMake.html "LLVM_USE_SANITIZER"
set(VCPKG_USE_SANITIZER "Address")

# If the following flags cause errors during build, you might need to manually
# ignore the PORT and check VCPKG_USE_SANITIZER
if(NOT PORT MATCHES "^((upb))$")
set(VCPKG_CXX_FLAGS "-fsanitize=address -g -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_C_FLAGS "-fsanitize=address -g -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_CXX_FLAGS "-fsanitize=address")
set(VCPKG_C_FLAGS "-fsanitize=address")
endif()

# Always apply sanitizer to linker flags
set(VCPKG_LINKER_FLAGS "-fsanitize=address")

# Only release builds
set(VCPKG_BUILD_TYPE release)
# This is where we override default CMake compiler/linker flags
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/../toolchain/vcpkg_unix_sanitizer_toolchain.cmake")

set(VCPKG_CMAKE_SYSTEM_NAME Linux)
11 changes: 8 additions & 3 deletions triplets/x64-osx-asan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ set(VCPKG_USE_SANITIZER "Address")
# If the following flags cause errors during build, you might need to manually
# ignore the PORT and check VCPKG_USE_SANITIZER
if(NOT PORT MATCHES "^((upb))$")
set(VCPKG_CXX_FLAGS "-O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_C_FLAGS "-O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_LINKER_FLAGS "-fsanitize=address")
set(VCPKG_CXX_FLAGS "-fsanitize=address")
set(VCPKG_C_FLAGS "-fsanitize=address")
endif()

# Always apply sanitizer to linker flags
set(VCPKG_LINKER_FLAGS "-fsanitize=address")

# This is where we override default CMake compiler/linker flags
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/../toolchain/vcpkg_unix_sanitizer_toolchain.cmake")

set(VCPKG_CMAKE_SYSTEM_NAME Darwin)
set(VCPKG_OSX_ARCHITECTURES x86_64)
9 changes: 7 additions & 2 deletions triplets/x64-osx-rel-asan.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)

# Only release builds
set(VCPKG_BUILD_TYPE release)

# ASAN
Expand All @@ -10,12 +12,15 @@ set(VCPKG_USE_SANITIZER "Address")
# If the following flags cause errors during build, you might need to manually
# ignore the PORT and check VCPKG_USE_SANITIZER
if(NOT PORT MATCHES "^((upb))$")
set(VCPKG_CXX_FLAGS "-fsanitize=address -g -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_C_FLAGS "-fsanitize=address -g -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(VCPKG_CXX_FLAGS "-fsanitize=address")
set(VCPKG_C_FLAGS "-fsanitize=address")
endif()

# Always apply sanitizer to linker flags
set(VCPKG_LINKER_FLAGS "-fsanitize=address")

# This is where we override default CMake compiler/linker flags
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/../toolchain/vcpkg_unix_sanitizer_toolchain.cmake")

set(VCPKG_CMAKE_SYSTEM_NAME Darwin)
set(VCPKG_OSX_ARCHITECTURES x86_64)

0 comments on commit d3df3a8

Please sign in to comment.