I want to build a test program for a repository of mine with undefined behavior sanitization enabled (at least with GCC and perhaps clang). I know how to do this manually:
- Add
-fsanitize=undefinedto the compilation flags - Add
-lubsanto the linking flags - Make sure an appropriate version of
libubsanis installed.
Now, in CMake, I would expect something like the following to work:
find_package(ubsan)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
target_compile_options(my_test PRIVATE "-fsanitize=undefined")
endif()
target_link_libraries(ubsan::ubsan)
... but there is no such version (as of CMake 3.21.0-rc2). So, how should I go about it? Grab a FindUBSan.cmake from somewhere? Perhaps do something else?
PS - The question applies similarly to C
Everything needs to be compiled with the sanitizers enabled (mix and match generally isn't possible), and enabling the santizers is compiler-specific; therefore, it's a toolchain option.
In a toolchain file, include the following:
You can also set
CMAKE_<LANG>_FLAGSat the command line to include-fsanitize=undefined.Full example: