Make CMake flag testing and propagation multi-config generator aware.
BUG=aomedia:659,aomedia:695,aomedia:705,aomedia:706
Change-Id: Iba60166370362599f3f4ef7959a6304bf079857d
diff --git a/build/cmake/compiler_flags.cmake b/build/cmake/compiler_flags.cmake
index 58ce8e7..8559f37 100644
--- a/build/cmake/compiler_flags.cmake
+++ b/build/cmake/compiler_flags.cmake
@@ -15,60 +15,91 @@
include(CheckCXXCompilerFlag)
include("${AOM_ROOT}/build/cmake/compiler_tests.cmake")
-# Strings used to cache failed C/CXX flags.
+# Strings used to cache flags.
+set(AOM_C_FLAGS)
+set(AOM_CXX_FLAGS)
+set(AOM_EXE_LINKER_FLAGS)
set(AOM_FAILED_C_FLAGS)
set(AOM_FAILED_CXX_FLAGS)
-# Checks C compiler for support of $c_flag. Adds $c_flag to $CMAKE_C_FLAGS when
-# the compile test passes. Caches $c_flag in $AOM_FAILED_C_FLAGS when the test
-# fails.
+# Sets variable named by $out_is_present to YES in the caller's scope when $flag
+# is found in the string variable named by $flag_cache. Sets the var to NO
+# otherwise.
+function(is_flag_present flag_cache flag out_is_present)
+ string(FIND "${${flag_cache}}" "${flag}" flag_pos)
+ if (${flag_pos} EQUAL -1)
+ set(${out_is_present} NO PARENT_SCOPE)
+ else ()
+ set(${out_is_present} YES PARENT_SCOPE)
+ endif ()
+endfunction ()
+
+# Appends $flag to $flags. Ignores scope via use of FORCE with set() call.
+function (append_flag flags flag)
+ string(FIND "${${flags}}" "${flag}" found)
+ if (${found} EQUAL -1)
+ set(${flags} "${${flags}} ${flag}" CACHE STRING "" FORCE)
+ endif ()
+endfunction ()
+
+# Checks C compiler for support of $c_flag. Adds $c_flag to all
+# $CMAKE_C_FLAGS_<CONFIG>s stored in AOM_C_CONFIGS when the compile test passes.
+# Caches $c_flag in $AOM_C_FLAGS or $AOM_FAILED_C_FLAGS depending on test
+# outcome.
function (add_c_flag_if_supported c_flag)
if (DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
return()
endif ()
- unset(C_FLAG_FOUND CACHE)
- string(FIND "${CMAKE_C_FLAGS}" "${c_flag}" C_FLAG_FOUND)
- unset(C_FLAG_FAILED CACHE)
- string(FIND "${AOM_FAILED_C_FLAGS}" "${c_flag}" C_FLAG_FAILED)
+ is_flag_present(AOM_C_FLAGS "${c_flag}" flag_ok)
+ is_flag_present(AOM_FAILED_C_FLAGS "${c_flag}" flag_failed)
+ if (${flag_ok} OR ${flag_failed})
+ return ()
+ endif ()
- if (${C_FLAG_FOUND} EQUAL -1 AND ${C_FLAG_FAILED} EQUAL -1)
- unset(C_FLAG_SUPPORTED CACHE)
- message("Checking C compiler flag support for: " ${c_flag})
- check_c_compiler_flag("${c_flag}" C_FLAG_SUPPORTED)
- if (C_FLAG_SUPPORTED)
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${c_flag}" CACHE STRING "" FORCE)
- else ()
- set(AOM_FAILED_C_FLAGS "${AOM_FAILED_C_FLAGS} ${c_flag}" CACHE STRING ""
- FORCE)
- endif ()
+ unset(C_FLAG_SUPPORTED CACHE)
+ message("Checking C compiler flag support for: " ${c_flag})
+ check_c_compiler_flag("${c_flag}" C_FLAG_SUPPORTED)
+
+ if (${C_FLAG_SUPPORTED})
+ append_flag(AOM_C_FLAGS "${c_flag}")
+ foreach (config ${AOM_C_CONFIGS})
+ unset(C_FLAG_FOUND)
+ append_flag("${config}" "${c_flag}")
+ endforeach ()
+ else ()
+ append_flag(AOM_FAILED_C_FLAGS "${c_flag}")
endif ()
endfunction ()
-# Checks C++ compiler for support of $cxx_flag. Adds $cxx_flag to
-# $CMAKE_CXX_FLAGS when the compile test passes. Caches $c_flag in
-# $AOM_FAILED_CXX_FLAGS when the test fails.
+# Checks C++ compiler for support of $cxx_flag. Adds $cxx_flag to all
+# $CMAKE_CXX_FLAGS_<CONFIG>s stored in AOM_CXX_CONFIGS when the compile test
+# passes.
+# Caches $cxx_flag in $AOM_CXX_FLAGS or $AOM_FAILED_CXX_FLAGS depending on test
+# outcome.
function (add_cxx_flag_if_supported cxx_flag)
if (DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
return()
endif ()
- unset(CXX_FLAG_FOUND CACHE)
- string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
- unset(CXX_FLAG_FAILED CACHE)
- string(FIND "${AOM_FAILED_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FAILED)
+ is_flag_present(AOM_CXX_FLAGS "${cxx_flag}" flag_ok)
+ is_flag_present(AOM_FAILED_CXX_FLAGS "${cxx_flag}" flag_failed)
+ if (${flag_ok} OR ${flag_failed})
+ return ()
+ endif ()
- if (${CXX_FLAG_FOUND} EQUAL -1 AND ${CXX_FLAG_FAILED} EQUAL -1)
- unset(CXX_FLAG_SUPPORTED CACHE)
- message("Checking CXX compiler flag support for: " ${cxx_flag})
- check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)
- if (CXX_FLAG_SUPPORTED)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${cxx_flag}" CACHE STRING ""
- FORCE)
- else()
- set(AOM_FAILED_CXX_FLAGS "${AOM_FAILED_CXX_FLAGS} ${cxx_flag}" CACHE
- STRING "" FORCE)
- endif ()
+ unset(CXX_FLAG_SUPPORTED CACHE)
+ message("Checking C++ compiler flag support for: " ${cxx_flag})
+ check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)
+
+ if (${CXX_FLAG_SUPPORTED})
+ append_flag(AOM_CXX_FLAGS "${cxx_flag}")
+ foreach (config ${AOM_CXX_CONFIGS})
+ unset(CXX_FLAG_FOUND)
+ append_flag("${config}" "${cxx_flag}")
+ endforeach ()
+ else ()
+ append_flag(AOM_FAILED_CXX_FLAGS "${cxx_flag}")
endif ()
endfunction ()
@@ -86,20 +117,24 @@
return()
endif ()
- unset(C_FLAG_FOUND CACHE)
- string(FIND "${CMAKE_C_FLAGS}" "${c_flag}" C_FLAG_FOUND)
+ is_flag_present(AOM_C_FLAGS "${c_flag}" flag_ok)
+ if (${flag_ok})
+ return ()
+ endif ()
- if (${C_FLAG_FOUND} EQUAL -1)
- unset(HAVE_C_FLAG CACHE)
- message("Checking C compiler flag support for: " ${c_flag})
- check_c_compiler_flag("${c_flag}" HAVE_C_FLAG)
- if (NOT HAVE_C_FLAG)
- message(FATAL_ERROR
- "${PROJECT_NAME} requires support for C flag: ${c_flag}.")
- endif ()
- if (update_c_flags)
- set(CMAKE_C_FLAGS "${c_flag} ${CMAKE_C_FLAGS}" CACHE STRING "" FORCE)
- endif ()
+ unset(HAVE_C_FLAG CACHE)
+ message("Checking C compiler flag support for: " ${c_flag})
+ check_c_compiler_flag("${c_flag}" HAVE_C_FLAG)
+ if (NOT HAVE_C_FLAG)
+ message(FATAL_ERROR
+ "${PROJECT_NAME} requires support for C flag: ${c_flag}.")
+ endif ()
+
+ append_flag(AOM_C_FLAGS "${c_flag}")
+ if (update_c_flags)
+ foreach (config ${AOM_C_CONFIGS})
+ set(${config} "${${config}} ${c_flag}" CACHE STRING "" FORCE)
+ endforeach ()
endif ()
endfunction ()
@@ -110,21 +145,24 @@
return()
endif ()
- unset(CXX_FLAG_FOUND CACHE)
- string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
+ is_flag_present(AOM_CXX_FLAGS "${cxx_flag}" flag_ok)
+ if (${flag_ok})
+ return ()
+ endif ()
- if (${CXX_FLAG_FOUND} EQUAL -1)
- unset(HAVE_CXX_FLAG CACHE)
- message("Checking CXX compiler flag support for: " ${cxx_flag})
- check_cxx_compiler_flag("${cxx_flag}" HAVE_CXX_FLAG)
- if (NOT HAVE_CXX_FLAG)
- message(FATAL_ERROR
- "${PROJECT_NAME} requires support for CXX flag: ${cxx_flag}.")
- endif ()
- if (update_cxx_flags)
- set(CMAKE_CXX_FLAGS "${cxx_flag} ${CMAKE_CXX_FLAGS}" CACHE STRING ""
- FORCE)
- endif ()
+ unset(HAVE_CXX_FLAG CACHE)
+ message("Checking C compiler flag support for: " ${cxx_flag})
+ check_cxx_compiler_flag("${cxx_flag}" HAVE_CXX_FLAG)
+ if (NOT HAVE_CXX_FLAG)
+ message(FATAL_ERROR
+ "${PROJECT_NAME} requires support for C flag: ${cxx_flag}.")
+ endif ()
+
+ append_flag(AOM_CXX_FLAGS "${cxx_flag}")
+ if (update_cxx_flags)
+ foreach (config ${AOM_CXX_CONFIGS})
+ set(${config} "${${config}} ${cxx_flag}" CACHE STRING "" FORCE)
+ endforeach ()
endif ()
endfunction ()
@@ -162,25 +200,29 @@
# Adds $preproc_def to C compiler command line (as -D$preproc_def) if not
# already present.
function (add_c_preproc_definition preproc_def)
- unset(PREPROC_DEF_FOUND CACHE)
- string(FIND "${CMAKE_C_FLAGS}" "${preproc_def}" PREPROC_DEF_FOUND)
-
- if (${PREPROC_DEF_FOUND} EQUAL -1)
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D${preproc_def}" CACHE STRING ""
- FORCE)
+ set(preproc_def "-D${preproc_def}")
+ is_flag_present(AOM_C_FLAGS "${preproc_def}" flag_cached)
+ if (${flag_cached})
+ return ()
endif ()
+
+ foreach (config ${AOM_C_CONFIGS})
+ set(${config} "${${config}} ${preproc_def}" CACHE STRING "" FORCE)
+ endforeach ()
endfunction ()
# Adds $preproc_def to CXX compiler command line (as -D$preproc_def) if not
# already present.
function (add_cxx_preproc_definition preproc_def)
- unset(PREPROC_DEF_FOUND CACHE)
- string(FIND "${CMAKE_CXX_FLAGS}" "${preproc_def}" PREPROC_DEF_FOUND)
-
- if (${PREPROC_DEF_FOUND} EQUAL -1)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D${preproc_def}" CACHE STRING ""
- FORCE)
+ set(preproc_def "-D${preproc_def}")
+ is_flag_present(AOM_CXX_FLAGS "${preproc_def}" flag_cached)
+ if (${flag_cached})
+ return ()
endif ()
+
+ foreach (config ${AOM_CXX_CONFIGS})
+ set(${config} "${${config}} ${preproc_def}" CACHE STRING "" FORCE)
+ endforeach ()
endfunction ()
# Adds $preproc_def to C and CXX compiler command line (as -D$preproc_def) if
@@ -192,32 +234,35 @@
# Adds $flag to assembler command line.
function (append_as_flag flag)
- unset(AS_FLAG_FOUND CACHE)
- string(FIND "${AOM_AS_FLAGS}" "${flag}" AS_FLAG_FOUND)
-
- if (${AS_FLAG_FOUND} EQUAL -1)
- set(AOM_AS_FLAGS "${AOM_AS_FLAGS} ${flag}" CACHE STRING "" FORCE)
+ is_flag_present(AOM_AS_FLAGS "${flag}" flag_cached)
+ if (${flag_cached})
+ return ()
endif ()
+ append_flag(AOM_AS_FLAGS "${flag}")
endfunction ()
# Adds $flag to the C compiler command line.
function (append_c_flag flag)
- unset(C_FLAG_FOUND CACHE)
- string(FIND "${CMAKE_C_FLAGS}" "${flag}" C_FLAG_FOUND)
-
- if (${C_FLAG_FOUND} EQUAL -1)
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" CACHE STRING "" FORCE)
+ is_flag_present(AOM_C_FLAGS "${flag}" flag_cached)
+ if (${flag_cached})
+ return ()
endif ()
+
+ foreach (config ${AOM_C_CONFIGS})
+ append_flag(${config} "${flag}")
+ endforeach ()
endfunction ()
# Adds $flag to the CXX compiler command line.
function (append_cxx_flag flag)
- unset(CXX_FLAG_FOUND CACHE)
- string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" CXX_FLAG_FOUND)
-
- if (${CXX_FLAG_FOUND} EQUAL -1)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" CACHE STRING "" FORCE)
+ is_flag_present(AOM_CXX_FLAGS "${flag}" flag_cached)
+ if (${flag_cached})
+ return ()
endif ()
+
+ foreach (config ${AOM_CXX_CONFIGS})
+ append_flag(${config} "${flag}")
+ endforeach ()
endfunction ()
# Adds $flag to the C and CXX compiler command lines.
@@ -226,30 +271,29 @@
append_cxx_flag(${flag})
endfunction ()
-# Adds $flag to the executable linker command line.
+# Adds $flag to the executable linker command line when not present.
function (append_exe_linker_flag flag)
- unset(LINKER_FLAG_FOUND CACHE)
- string(FIND "${CMAKE_EXE_LINKER_FLAGS}" "${flag}" LINKER_FLAG_FOUND)
-
- if (${LINKER_FLAG_FOUND} EQUAL -1)
- set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}" CACHE STRING
- "" FORCE)
+ is_flag_present(AOM_EXE_LINKER_FLAGS "${flag}" flag_cached)
+ if (${flag_cached})
+ return()
endif ()
+
+ append_flag(AOM_EXE_LINKER_FLAGS "${flag}")
+ foreach (config ${AOM_EXE_LINKER_CONFIGS})
+ append_flag(${config} "${flag}")
+ endforeach ()
endfunction ()
# Adds $flag to the link flags for $target.
-function (append_link_flag_to_target target flags)
+function (append_link_flag_to_target target flag)
unset(target_link_flags)
get_target_property(target_link_flags ${target} LINK_FLAGS)
if (target_link_flags)
- unset(link_flag_found)
- string(FIND "${target_link_flags}" "${flags}" link_flag_found)
-
- if (NOT ${link_flag_found} EQUAL -1)
+ is_flag_present(target_link_flags "${flag}" flag_found)
+ if (${flag_found})
return()
endif ()
-
set(target_link_flags "${target_link_flags} ${flags}")
else ()
set(target_link_flags "${flags}")
diff --git a/build/cmake/compiler_tests.cmake b/build/cmake/compiler_tests.cmake
index 05bebeb..c759234 100644
--- a/build/cmake/compiler_tests.cmake
+++ b/build/cmake/compiler_tests.cmake
@@ -14,6 +14,34 @@
include(CheckCSourceCompiles)
include(CheckCXXSourceCompiles)
+# CMake passes command line flags like this:
+# $compiler $lang_flags $lang_flags_config ...
+# To ensure the flags tested here and elsewhere are obeyed a list of active
+# build configuration types is built, and flags are applied to the flag strings
+# for each configuration currently active for C and CXX builds as determined by
+# reading $CMAKE_CONFIGURATION_TYPES and $CMAKE_BUILD_TYPE. When
+# $CMAKE_CONFIGURATION_TYPES is non-empty a multi-configuration generator is in
+# use: currently this includes MSVC and Xcode. For other generators
+# $CMAKE_BUILD_TYPE is used. For both cases AOM_<LANG>_CONFIGS is populated with
+# CMake string variable names that contain flags for the currently available
+# configuration(s).
+unset(AOM_C_CONFIGS)
+unset(AOM_CXX_CONFIGS)
+list(LENGTH CMAKE_CONFIGURATION_TYPES num_configs)
+if (${num_configs} GREATER 0)
+ foreach (config ${CMAKE_CONFIGURATION_TYPES})
+ string(TOUPPER ${config} config)
+ list(APPEND AOM_C_CONFIGS "CMAKE_C_FLAGS_${config}")
+ list(APPEND AOM_CXX_CONFIGS "CMAKE_CXX_FLAGS_${config}")
+ list(APPEND AOM_EXE_LINKER_CONFIGS "CMAKE_EXE_LINKER_FLAGS_${config}")
+ endforeach ()
+else ()
+ string(TOUPPER ${CMAKE_BUILD_TYPE} config)
+ set(AOM_C_CONFIGS "CMAKE_C_FLAGS_${config}")
+ set(AOM_CXX_CONFIGS "CMAKE_CXX_FLAGS_${config}")
+ set(AOM_EXE_LINKER_CONFIGS "CMAKE_EXE_LINKER_FLAGS_${config}")
+endif ()
+
# The basic main() function used in all compile tests.
set(AOM_C_MAIN "\nint main(void) { return 0; }")
set(AOM_CXX_MAIN "\nint main() { return 0; }")
@@ -108,9 +136,9 @@
# Convenience function that confirms $test_source compiles as C and C++.
# $result_var is set to 1 when both tests are successful, and 0 when one or both
# tests fail.
-# Note: This function is intended to be used to write to result variables that are
-# expanded via configure_file(). $result_var is set to 1 or 0 to allow direct
-# usage of the value in generated source files.
+# Note: This function is intended to be used to write to result variables that
+# are expanded via configure_file(). $result_var is set to 1 or 0 to allow
+# direct usage of the value in generated source files.
function(aom_check_source_compiles test_name test_source result_var)
unset(C_PASSED)
unset(CXX_PASSED)