Skip to content

Commit

Permalink
Updated CMake toolchains by adding support to Memory Sanitizer (only …
Browse files Browse the repository at this point in the history
…on Linux and CLang, dunno for Mac) and fixing some oversights.
  • Loading branch information
cbnolok committed Oct 15, 2023
1 parent de5373b commit 162a740
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 115 deletions.
22 changes: 12 additions & 10 deletions cmake/toolchains/Linux-Clang-native.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ function (toolchain_after_project)
IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
MESSAGE (STATUS "Detected 64 bits architecture")
SET(ARCH_BITS 64 PARENT_SCOPE)
LINK_DIRECTORIES ("/usr/lib64/mysql" "/usr/lib/x86_64-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-native64" PARENT_SCOPE)
ELSE (CMAKE_SIZEOF_VOID_P EQUAL 8)
ELSE ()
MESSAGE (STATUS "Detected 32 bits architecture")
SET(ARCH_BITS 32 PARENT_SCOPE)
LINK_DIRECTORIES ("/usr/lib/mysql" "/usr/lib/x86_64-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-native32" PARENT_SCOPE)
ENDIF (CMAKE_SIZEOF_VOID_P EQUAL 8)
ENDIF ()

endfunction()

Expand All @@ -24,15 +26,15 @@ function (toolchain_exe_stuff)
SET (CXX_ARCH_OPTS "-march=native")

IF (${ARCH_BITS} EQUAL 64)
SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib64/mysql -Wl,-rpath=/usr/lib64/mysql\
-L/usr/lib/x86_64-linux-gnu/mysql -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib64/mysql\
# -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
# PARENT_SCOPE)
ELSE ()
SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib/mysql -Wl,-rpath=/usr/lib/mysql\
-L/usr/lib/i386-linux-gnu/mysql -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib/mysql\
# -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
# PARENT_SCOPE)
ENDIF ()

toolchain_exe_stuff_common()
Expand Down
9 changes: 5 additions & 4 deletions cmake/toolchains/Linux-Clang-x86.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function (toolchain_after_project)
SET(CMAKE_SYSTEM_NAME "Linux" PARENT_SCOPE)
SET(ARCH_BITS 32 PARENT_SCOPE)

LINK_DIRECTORIES ("/usr/lib/mysql" "/usr/lib/i386-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-x86" PARENT_SCOPE)
endfunction()

Expand All @@ -14,10 +15,10 @@ function (toolchain_exe_stuff)
SET (C_ARCH_OPTS "-march=i686 -m32")
SET (CXX_ARCH_OPTS "-march=i686 -m32")

SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib/mysql -Wl,-rpath=/usr/lib/mysql\
-L/usr/lib/i386-linux-gnu/mysql -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib/mysql\
# -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
# PARENT_SCOPE)

toolchain_exe_stuff_common()

Expand Down
9 changes: 5 additions & 4 deletions cmake/toolchains/Linux-Clang-x86_64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function (toolchain_after_project)
SET(CMAKE_SYSTEM_NAME "Linux" PARENT_SCOPE)
SET(ARCH_BITS 64 PARENT_SCOPE)

LINK_DIRECTORIES ("/usr/lib64/mysql" "/usr/lib/x86_64-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-x86_64" PARENT_SCOPE)
endfunction()

Expand All @@ -14,10 +15,10 @@ function (toolchain_exe_stuff)
SET (C_ARCH_OPTS "-march=x86-64 -m64")
SET (CXX_ARCH_OPTS "-march=x86-64 -m64")

SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib64/mysql -Wl,-rpath=/usr/lib64/mysql\
-L/usr/lib/x86_64-linux-gnu/mysql -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib64/mysql\
# -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
# PARENT_SCOPE)

toolchain_exe_stuff_common()

Expand Down
37 changes: 28 additions & 9 deletions cmake/toolchains/Linux-Clang_common.inc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ function (toolchain_exe_stuff_common)
SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} -fsanitize=address -fsanitize-address-use-after-scope")
SET (ENABLED_SANITIZER true)
ENDIF ()
IF (${USE_MSAN})
SET (C_FLAGS_EXTRA "${C_FLAGS_EXTRA} -fsanitize=memory -fsanitize-memory-track-origins=2 -fPIE")
SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} -fsanitize=memory -fsanitize-memory-track-origins=2 -fPIE")
SET (ENABLED_SANITIZER true)
ENDIF ()
IF (${USE_LSAN})
SET (C_FLAGS_EXTRA "${C_FLAGS_EXTRA} -fsanitize=leak")
SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} -fsanitize=leak")
SET (ENABLED_SANITIZER true)
ENDIF ()
IF (${USE_UBSAN})
SET (UBSAN_FLAGS "-fsanitize=undefined,\
shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds-strict,\
float-divide-by-zero,float-cast-overflow,pointer-overflow \
shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds,\
float-divide-by-zero,float-cast-overflow,pointer-overflow,\
unreachable,nonnull-attribute,returns-nonnull-attribute \
-fno-sanitize=enum")
SET (C_FLAGS_EXTRA "${C_FLAGS_EXTRA} ${UBSAN_FLAGS}")
SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} ${UBSAN_FLAGS} -fsanitize=return,vptr")
Expand All @@ -38,27 +44,36 @@ float-divide-by-zero,float-cast-overflow,pointer-overflow \
#-- Setting compiler flags common to all builds.

SET (C_WARNING_OPTS
"-Wall -Wextra -Wno-nonnull-compare -Wno-unknown-pragmas -Wno-format -Wno-switch -Wno-implicit-fallthrough\
"-Wall -Wextra -Wno-unknown-pragmas -Wno-format -Wno-switch -Wno-implicit-fallthrough\
-Wno-parentheses -Wno-misleading-indentation -Wno-strict-aliasing -Wno-unused-result\
-Wno-error=unused-but-set-variable -Wno-maybe-uninitialized -Wno-implicit-function-declaration") # this line is for warnings issued by 3rd party C code
-Wno-error=unused-but-set-variable -Wno-implicit-function-declaration") # this line is for warnings issued by 3rd party C code
SET (CXX_WARNING_OPTS
"-Wall -Wextra -Wno-nonnull-compare -Wno-unknown-pragmas -Wno-format -Wno-switch -Wno-implicit-fallthrough\
"-Wall -Wextra -Wno-unknown-pragmas -Wno-format -Wno-switch -Wno-implicit-fallthrough\
-Wno-parentheses -Wno-misleading-indentation -Wno-conversion-null -Wno-unused-result")
#SET (C_ARCH_OPTS ) # set in parent toolchain
#SET (CXX_ARCH_OPTS ) # set in parent toolchain
SET (C_OPTS "-std=c11 -pthread -fexceptions -fnon-call-exceptions")
SET (CXX_OPTS "-std=c++17 -pthread -fexceptions -fnon-call-exceptions")
SET (C_SPECIAL "-pipe -fno-expensive-optimizations")
SET (C_SPECIAL "-pipe")
SET (CXX_SPECIAL "-pipe -ffast-math")

SET (CMAKE_C_FLAGS "${C_WARNING_OPTS} ${C_OPTS} ${C_SPECIAL} ${C_FLAGS_EXTRA}" PARENT_SCOPE)
SET (CMAKE_CXX_FLAGS "${CXX_WARNING_OPTS} ${CXX_OPTS} ${CXX_SPECIAL} ${CXX_FLAGS_EXTRA}" PARENT_SCOPE)
# GCC flags not supported by clang:
# Warnings: "-Wno-nonnull-compare -Wno-maybe-uninitialized"
# Other: "-fno-expensive-optimizations"


#-- Setting common linker flags

IF (${USE_MSAN})
SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} -pie" PARENT_SCOPE)
ENDIF()

# -s and -g need to be added/removed also to/from linker flags!
SET (CMAKE_EXE_LINKER_FLAGS "-pthread -dynamic ${CMAKE_EXE_LINKER_FLAGS_EXTRA}" PARENT_SCOPE)
# Use "-stdlib=libstdc++" to link against GCC c/c++ libs (this is done by default)
# To use LLVM libc++ use "-stdlib=libc++", but you need to install it separately


#-- Adding compiler flags per build.
Expand All @@ -69,16 +84,20 @@ float-divide-by-zero,float-cast-overflow,pointer-overflow \
# -fno-omit-frame-pointer disables a good optimization which may corrupt the debugger stack trace.
SET (COMPILE_OPTIONS_EXTRA)
IF (ENABLED_SANITIZER OR TARGET spheresvr_debug)
SET (COMPILE_OPTIONS_EXTRA -fno-omit-frame-pointer)
SET (COMPILE_OPTIONS_EXTRA -fno-omit-frame-pointer -fno-inline)
ENDIF ()
IF (TARGET spheresvr_release)
TARGET_COMPILE_OPTIONS ( spheresvr_release PUBLIC -s -O3 ${COMPILE_OPTIONS_EXTRA})
ENDIF ()
IF (TARGET spheresvr_nightly)
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA})
IF (ENABLED_SANITIZER)
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -ggdb3 -O2 ${COMPILE_OPTIONS_EXTRA})
ELSE ()
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA})
ENDIF ()
ENDIF ()
IF (TARGET spheresvr_debug)
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -Og -fno-inline ${COMPILE_OPTIONS_EXTRA})
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -Og ${COMPILE_OPTIONS_EXTRA})
ENDIF ()


Expand Down
22 changes: 12 additions & 10 deletions cmake/toolchains/Linux-GNU-native.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ function (toolchain_after_project)
IF (CMAKE_SIZEOF_VOID_P EQUAL 8)
MESSAGE (STATUS "Detected 64 bits architecture")
SET(ARCH_BITS 64 PARENT_SCOPE)
LINK_DIRECTORIES ("/usr/lib64/mysql" "/usr/lib/x86_64-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-native64" PARENT_SCOPE)
ELSE (CMAKE_SIZEOF_VOID_P EQUAL 8)
ELSE ()
MESSAGE (STATUS "Detected 32 bits architecture")
SET(ARCH_BITS 32 PARENT_SCOPE)
LINK_DIRECTORIES ("/usr/lib/mysql" "/usr/lib/i386-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-native32" PARENT_SCOPE)
ENDIF (CMAKE_SIZEOF_VOID_P EQUAL 8)
ENDIF ()

endfunction()

Expand All @@ -24,15 +26,15 @@ function (toolchain_exe_stuff)
SET (CXX_ARCH_OPTS "-march=native")

IF (${ARCH_BITS} EQUAL 64)
SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib64/mysql -Wl,-rpath=/usr/lib64/mysql\
-L/usr/lib/x86_64-linux-gnu/mysql -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib64/mysql\
# -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
# PARENT_SCOPE)
ELSE ()
SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib/mysql -Wl,-rpath=/usr/lib/mysql\
-L/usr/lib/i386-linux-gnu/mysql -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib/mysql\
# -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
# PARENT_SCOPE)
ENDIF ()

toolchain_exe_stuff_common()
Expand Down
9 changes: 5 additions & 4 deletions cmake/toolchains/Linux-GNU-x86.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function (toolchain_after_project)
SET(CMAKE_SYSTEM_NAME "Linux" PARENT_SCOPE)
SET(ARCH_BITS 32 PARENT_SCOPE)

LINK_DIRECTORIES ("/usr/lib/mysql" "/usr/lib/i386-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-x86" PARENT_SCOPE)
endfunction()

Expand All @@ -14,10 +15,10 @@ function (toolchain_exe_stuff)
SET (C_ARCH_OPTS "-march=i686 -m32")
SET (CXX_ARCH_OPTS "-march=i686 -m32")

SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib/mysql -Wl,-rpath=/usr/lib/mysql\
-L/usr/lib/i386-linux-gnu/mysql -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib/mysql\
# -Wl,-rpath=/usr/lib/i386-linux-gnu/mysql"
# PARENT_SCOPE)

toolchain_exe_stuff_common()

Expand Down
9 changes: 5 additions & 4 deletions cmake/toolchains/Linux-GNU-x86_64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function (toolchain_after_project)
SET(CMAKE_SYSTEM_NAME "Linux" PARENT_SCOPE)
SET(ARCH_BITS 64 PARENT_SCOPE)

LINK_DIRECTORIES ("/usr/lib64/mysql" "/usr/lib/x86_64-linux-gnu/mysql")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin-x86_64" PARENT_SCOPE)
endfunction()

Expand All @@ -14,10 +15,10 @@ function (toolchain_exe_stuff)
SET (C_ARCH_OPTS "-march=x86-64 -m64")
SET (CXX_ARCH_OPTS "-march=x86-64 -m64")

SET (CMAKE_EXE_LINKER_FLAGS_EXTRA
"-L/usr/lib64/mysql -Wl,-rpath=/usr/lib64/mysql\
-L/usr/lib/x86_64-linux-gnu/mysql -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
PARENT_SCOPE)
#SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} \
# -Wl,-rpath=/usr/lib64/mysql\
# -Wl,-rpath=/usr/lib/x86_64-linux-gnu/mysql"
# PARENT_SCOPE)

toolchain_exe_stuff_common()

Expand Down
24 changes: 20 additions & 4 deletions cmake/toolchains/Linux-GNU_common.inc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ function (toolchain_exe_stuff_common)
SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} -fsanitize=address -fsanitize-address-use-after-scope")
SET (ENABLED_SANITIZER true)
ENDIF ()
IF (${USE_MSAN})
MESSAGE (FATAL_ERROR "Linux GCC doesn't yet support MSAN")
SET (USE_MSAN false)
#SET (C_FLAGS_EXTRA "${C_FLAGS_EXTRA} -fsanitize=memory -fsanitize-memory-track-origins=2 -fPIE")
#SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} -fsanitize=memory -fsanitize-memory-track-origins=2 -fPIE")
#SET (ENABLED_SANITIZER true)
ENDIF ()
IF (${USE_LSAN})
SET (C_FLAGS_EXTRA "${C_FLAGS_EXTRA} -fsanitize=leak")
SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} -fsanitize=leak")
Expand All @@ -24,7 +31,8 @@ function (toolchain_exe_stuff_common)
IF (${USE_UBSAN})
SET (UBSAN_FLAGS "-fsanitize=undefined,\
shift,integer-divide-by-zero,vla-bound,null,signed-integer-overflow,bounds-strict,\
float-divide-by-zero,float-cast-overflow,pointer-overflow \
float-divide-by-zero,float-cast-overflow,pointer-overflow,\
unreachable,nonnull-attribute,returns-nonnull-attribute \
-fno-sanitize=enum")
SET (C_FLAGS_EXTRA "${C_FLAGS_EXTRA} ${UBSAN_FLAGS}")
SET (CXX_FLAGS_EXTRA "${CXX_FLAGS_EXTRA} ${UBSAN_FLAGS} -fsanitize=return,vptr")
Expand Down Expand Up @@ -57,6 +65,10 @@ float-divide-by-zero,float-cast-overflow,pointer-overflow \

#-- Setting common linker flags

IF (${USE_MSAN})
SET (CMAKE_EXE_LINKER_FLAGS_EXTRA "${CMAKE_EXE_LINKER_FLAGS_EXTRA} -pie" PARENT_SCOPE)
ENDIF()

# -s and -g need to be added/removed also to/from linker flags!
SET (CMAKE_EXE_LINKER_FLAGS "-pthread -dynamic ${CMAKE_EXE_LINKER_FLAGS_EXTRA}" PARENT_SCOPE)

Expand All @@ -69,16 +81,20 @@ float-divide-by-zero,float-cast-overflow,pointer-overflow \
# -fno-omit-frame-pointer disables a good optimization which may corrupt the debugger stack trace.
SET (COMPILE_OPTIONS_EXTRA)
IF (ENABLED_SANITIZER OR TARGET spheresvr_debug)
SET (COMPILE_OPTIONS_EXTRA -fno-omit-frame-pointer)
SET (COMPILE_OPTIONS_EXTRA -fno-omit-frame-pointer -fno-inline)
ENDIF ()
IF (TARGET spheresvr_release)
TARGET_COMPILE_OPTIONS ( spheresvr_release PUBLIC -s -O3 ${COMPILE_OPTIONS_EXTRA})
ENDIF ()
IF (TARGET spheresvr_nightly)
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA})
IF (ENABLED_SANITIZER)
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -ggdb3 -O2 ${COMPILE_OPTIONS_EXTRA})
ELSE ()
TARGET_COMPILE_OPTIONS ( spheresvr_nightly PUBLIC -O3 ${COMPILE_OPTIONS_EXTRA})
ENDIF ()
ENDIF ()
IF (TARGET spheresvr_debug)
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -Og -fno-inline ${COMPILE_OPTIONS_EXTRA})
TARGET_COMPILE_OPTIONS ( spheresvr_debug PUBLIC -ggdb3 -Og ${COMPILE_OPTIONS_EXTRA})
ENDIF ()


Expand Down
Loading

0 comments on commit 162a740

Please sign in to comment.