cmake/compile_flag.cmake

84 lines
3.0 KiB
CMake
Raw Normal View History

include(CheckCXXSourceCompiles)
2018-07-04 16:46:32 +10:00
include(CMakeCheckCompilerFlagCommonPatterns)
2017-01-17 21:25:31 +11:00
# Test if a compiler flag is supported.
#
# Note: We _cannot_ use check_cxx_compiler_flag because it will not add the
# flag to the linker parameters and some compiler flags _must_ be passed to
# the linker but the test it performs fails to do so. eg, checking for
# -fsanitize=address is impossible with this macro.
#
# Instead, we roll out own. Yay cmake...
2022-08-23 13:35:26 +10:00
#
# Accepts a second parameter that names an output variable. It will be set to
# 1 if-and-only-if the flag was accepted by the compiler. Otherwise it will be
# left untouched. It is up to the user to reset this flag between invocations
# if necessary.
2017-01-17 19:20:12 +11:00
macro (append_compile_flag _flag)
string (MAKE_C_IDENTIFIER ${_flag} _name)
2018-07-04 16:46:32 +10:00
CHECK_COMPILER_FLAG_COMMON_PATTERNS(_append_compile_flag_COMMON_PATTERNS)
set(_append_compile_flag_COMMON_PATTERNS
${_append_compile_flag_COMMON_PATTERNS}
FAIL_REGEX "argument unused during compilation" #clang
)
if (NOT DEFINED compile_flag_${_name})
# If we have an argument of the form "-Wno-foo" then we can't test it
# directly as GCC will unconditioally succeed right up until the
# point any other warning or error is triggered and *then* dump a
# warning message.
#
# Instead you have to test for -Wfoo and only if that succeeds can
# you use the negation.
string (REGEX MATCH "^-Wno-(.+)$" compile_flag_inverse ${_flag})
set(_append_compile_flag_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
if ("x" STREQUAL "x${compile_flag_inverse}")
set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}")
else ()
string (REGEX REPLACE "-Wno-" "" _inverse_flag ${_flag})
set(CMAKE_REQUIRED_FLAGS "${CMAKE_CXX_FLAGS} -W${_inverse_flag}")
endif ()
2018-07-04 16:46:32 +10:00
check_cxx_source_compiles(
"int main(int,char**) { return 0; }"
compile_flag_${_name}
${_append_compile_flag_COMMON_PATTERNS}
)
set(CMAKE_REQUIRED_FLAGS "${_append_compile_flag_REQUIRED_FLAGS}")
2017-01-17 19:20:12 +11:00
endif ()
if (compile_flag_${_name})
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}")
2022-08-23 13:35:26 +10:00
if (${ARGC} GREATER_EQUAL 2)
set(${ARGV1} 1)
endif()
endif()
2017-01-17 19:20:12 +11:00
endmacro(append_compile_flag)
2022-08-23 13:35:26 +10:00
# Find the first compile flag in the list of arguments that is supported by
# the compiler and set it.
macro (append_first_compile_flag)
message(CHECK_START "First CXXFLAGS ${ARGV}")
foreach(__append_first_compile_flag__flag ${ARGV})
set(__append_first_compile_flag__test 0)
append_compile_flag(
${__append_first_compile_flag__flag}
__append_first_compile_flag__test
)
if (${__append_first_compile_flag__test})
message(CHECK_PASS "Found ${__append_first_compile_flag__flag}")
break()
endif()
endforeach()
if (NOT ${__append_first_compile_flag__test})
message(CHECK_FAIL "No valid flags")
endif ()
endmacro()