diff --git a/compile_flag.cmake b/compile_flag.cmake index f6bb622..deff396 100644 --- a/compile_flag.cmake +++ b/compile_flag.cmake @@ -9,6 +9,11 @@ include(CMakeCheckCompilerFlagCommonPatterns) # -fsanitize=address is impossible with this macro. # # Instead, we roll out own. Yay cmake... +# +# 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. macro (append_compile_flag _flag) string (MAKE_C_IDENTIFIER ${_flag} _name) @@ -48,5 +53,32 @@ macro (append_compile_flag _flag) if (compile_flag_${_name}) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}") + if (${ARGC} GREATER_EQUAL 2) + set(${ARGV1} 1) + endif() endif() endmacro(append_compile_flag) + + +# 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() \ No newline at end of file