Commit 186a7ba5 by Patrick Steinhardt

cmake: error out if required C flags are not supported

We do want to notify users compiling our source code early on if they
try to use C flags which aren't supported. Add a new macro `AddCFlag`,
which results in a fatal error in case the flag is not supported, and
use it for our fuzzing flags.
parent 07cf8b38
...@@ -250,20 +250,25 @@ ELSE() ...@@ -250,20 +250,25 @@ ELSE()
ENDIF() ENDIF()
IF(NOT USE_SANITIZER STREQUAL "OFF") IF(NOT USE_SANITIZER STREQUAL "OFF")
SET(CMAKE_C_FLAGS "-fsanitize=${USE_SANITIZER} ${CMAKE_C_FLAGS}") # Workaround to force linking against -lasan
SET(CMAKE_C_FLAGS "-fno-omit-frame-pointer ${CMAKE_C_FLAGS}") SET(CMAKE_REQUIRED_FLAGS "-fsanitize=${USE_SANITIZER}")
SET(CMAKE_C_FLAGS "-fno-optimize-sibling-calls ${CMAKE_C_FLAGS}") ADD_C_FLAG(-fsanitize=${USE_SANITIZER})
UNSET(CMAKE_REQUIRED_FLAGS)
ADD_C_FLAG(-fno-omit-frame-pointer)
ADD_C_FLAG(-fno-optimize-sibling-calls)
ENDIF() ENDIF()
IF(USE_COVERAGE) IF(USE_COVERAGE)
SET(CMAKE_C_FLAGS "-fcoverage-mapping ${CMAKE_C_FLAGS}") ADD_C_FLAG(-fcoverage-mapping)
SET(CMAKE_C_FLAGS "-fprofile-instr-generate ${CMAKE_C_FLAGS}") ADD_C_FLAG(-fprofile-instr-generate)
ENDIF() ENDIF()
IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS) IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
# The actual sanitizer link target will be added when linking the fuzz # The actual sanitizer link target will be added when linking the fuzz
# targets. # targets.
SET(CMAKE_C_FLAGS "-fsanitize=fuzzer-no-link ${CMAKE_C_FLAGS}") SET(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
ADD_C_FLAG(-fsanitize=fuzzer-no-link)
UNSET(CMAKE_REQUIRED_FLAGS)
ENDIF () ENDIF ()
ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(src)
......
...@@ -5,9 +5,21 @@ ...@@ -5,9 +5,21 @@
INCLUDE(CheckCCompilerFlag) INCLUDE(CheckCCompilerFlag)
MACRO(ADD_C_FLAG _FLAG)
STRING(TOUPPER ${_FLAG} UPCASE)
STRING(REGEX REPLACE "^-" "" UPCASE_PRETTY ${UPCASE})
CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)
IF(IS_${UPCASE_PRETTY}_SUPPORTED)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
ELSE()
MESSAGE(FATAL_ERROR "Required flag ${_FLAG} is not supported")
ENDIF()
ENDMACRO()
MACRO(ADD_C_FLAG_IF_SUPPORTED _FLAG) MACRO(ADD_C_FLAG_IF_SUPPORTED _FLAG)
STRING(TOUPPER ${_FLAG} UPCASE) STRING(TOUPPER ${_FLAG} UPCASE)
STRING(REGEX REPLACE "^-" "" UPCASE_PRETTY ${UPCASE}) STRING(REGEX REPLACE "^-" "" UPCASE_PRETTY ${UPCASE})
CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED) CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)
IF(IS_${UPCASE_PRETTY}_SUPPORTED) IF(IS_${UPCASE_PRETTY}_SUPPORTED)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment