1# 2# Copyright (c) 2016, Alliance for Open Media. All rights reserved. 3# 4# This source code is subject to the terms of the BSD 2 Clause License and the 5# Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License was 6# not distributed with this source code in the LICENSE file, you can obtain it 7# at www.aomedia.org/license/software. If the Alliance for Open Media Patent 8# License 1.0 was not distributed with this source code in the PATENTS file, you 9# can obtain it at www.aomedia.org/license/patent. 10# 11if(AOM_BUILD_CMAKE_COMPILER_TESTS_CMAKE_) 12 return() 13endif() # AOM_BUILD_CMAKE_COMPILER_TESTS_CMAKE_ 14set(AOM_BUILD_CMAKE_COMPILER_TESTS_CMAKE_ 1) 15 16include(CheckCSourceCompiles) 17include(CheckCXXSourceCompiles) 18 19# CMake passes command line flags like this: 20# 21# * $compiler $lang_flags $lang_flags_config ... 22# 23# To ensure the flags tested here and elsewhere are obeyed a list of active 24# build configuration types is built, and flags are applied to the flag strings 25# for each configuration currently active for C and CXX builds as determined by 26# reading $CMAKE_CONFIGURATION_TYPES and $CMAKE_BUILD_TYPE. When 27# $CMAKE_CONFIGURATION_TYPES is non-empty a multi- configuration generator is in 28# use: currently this includes MSVC and Xcode. For other generators 29# $CMAKE_BUILD_TYPE is used. For both cases AOM_<LANG>_CONFIGS is populated with 30# CMake string variable names that contain flags for the currently available 31# configuration(s). 32unset(AOM_C_CONFIGS) 33unset(AOM_CXX_CONFIGS) 34list(LENGTH CMAKE_CONFIGURATION_TYPES num_configs) 35if(${num_configs} GREATER 0) 36 foreach(config ${CMAKE_CONFIGURATION_TYPES}) 37 string(TOUPPER ${config} config) 38 list(APPEND AOM_C_CONFIGS "CMAKE_C_FLAGS_${config}") 39 list(APPEND AOM_CXX_CONFIGS "CMAKE_CXX_FLAGS_${config}") 40 list(APPEND AOM_EXE_LINKER_CONFIGS "CMAKE_EXE_LINKER_FLAGS_${config}") 41 endforeach() 42else() 43 string(TOUPPER ${CMAKE_BUILD_TYPE} config) 44 set(AOM_C_CONFIGS "CMAKE_C_FLAGS_${config}") 45 set(AOM_CXX_CONFIGS "CMAKE_CXX_FLAGS_${config}") 46 set(AOM_EXE_LINKER_CONFIGS "CMAKE_EXE_LINKER_FLAGS_${config}") 47endif() 48 49# The basic main() function used in all compile tests. 50set(AOM_C_MAIN "\nint main(void) { return 0; }") 51set(AOM_CXX_MAIN "\nint main() { return 0; }") 52 53# Strings containing the names of passed and failed tests. 54set(AOM_C_PASSED_TESTS) 55set(AOM_C_FAILED_TESTS) 56set(AOM_CXX_PASSED_TESTS) 57set(AOM_CXX_FAILED_TESTS) 58 59function(aom_push_var var new_value) 60 set(SAVED_${var} ${${var}} PARENT_SCOPE) 61 set(${var} "${${var}} ${new_value}" PARENT_SCOPE) 62endfunction() 63 64function(aom_pop_var var) 65 set(var ${SAVED_${var}} PARENT_SCOPE) 66 unset(SAVED_${var} PARENT_SCOPE) 67endfunction() 68 69# Confirms $test_source compiles and stores $test_name in one of 70# $AOM_C_PASSED_TESTS or $AOM_C_FAILED_TESTS depending on out come. When the 71# test passes $result_var is set to 1. When it fails $result_var is unset. The 72# test is not run if the test name is found in either of the passed or failed 73# test variables. 74function(aom_check_c_compiles test_name test_source result_var) 75 if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS) 76 return() 77 endif() 78 79 unset(C_TEST_PASSED CACHE) 80 unset(C_TEST_FAILED CACHE) 81 string(FIND "${AOM_C_PASSED_TESTS}" "${test_name}" C_TEST_PASSED) 82 string(FIND "${AOM_C_FAILED_TESTS}" "${test_name}" C_TEST_FAILED) 83 if(${C_TEST_PASSED} EQUAL -1 AND ${C_TEST_FAILED} EQUAL -1) 84 unset(C_TEST_COMPILED CACHE) 85 message("Running C compiler test: ${test_name}") 86 check_c_source_compiles("${test_source} ${AOM_C_MAIN}" C_TEST_COMPILED) 87 set(${result_var} ${C_TEST_COMPILED} PARENT_SCOPE) 88 89 if(C_TEST_COMPILED) 90 set(AOM_C_PASSED_TESTS 91 "${AOM_C_PASSED_TESTS} ${test_name}" 92 CACHE STRING "" FORCE) 93 else() 94 set(AOM_C_FAILED_TESTS 95 "${AOM_C_FAILED_TESTS} ${test_name}" 96 CACHE STRING "" FORCE) 97 message("C Compiler test ${test_name} failed.") 98 endif() 99 elseif(NOT ${C_TEST_PASSED} EQUAL -1) 100 set(${result_var} 1 PARENT_SCOPE) 101 else() # ${C_TEST_FAILED} NOT EQUAL -1 102 unset(${result_var} PARENT_SCOPE) 103 endif() 104endfunction() 105 106# Confirms $test_source compiles and stores $test_name in one of 107# $AOM_CXX_PASSED_TESTS or $AOM_CXX_FAILED_TESTS depending on out come. When the 108# test passes $result_var is set to 1. When it fails $result_var is unset. The 109# test is not run if the test name is found in either of the passed or failed 110# test variables. 111function(aom_check_cxx_compiles test_name test_source result_var) 112 if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS) 113 return() 114 endif() 115 116 unset(CXX_TEST_PASSED CACHE) 117 unset(CXX_TEST_FAILED CACHE) 118 string(FIND "${AOM_CXX_PASSED_TESTS}" "${test_name}" CXX_TEST_PASSED) 119 string(FIND "${AOM_CXX_FAILED_TESTS}" "${test_name}" CXX_TEST_FAILED) 120 if(${CXX_TEST_PASSED} EQUAL -1 AND ${CXX_TEST_FAILED} EQUAL -1) 121 unset(CXX_TEST_COMPILED CACHE) 122 message("Running CXX compiler test: ${test_name}") 123 check_cxx_source_compiles("${test_source} ${AOM_CXX_MAIN}" 124 CXX_TEST_COMPILED) 125 set(${result_var} ${CXX_TEST_COMPILED} PARENT_SCOPE) 126 127 if(CXX_TEST_COMPILED) 128 set(AOM_CXX_PASSED_TESTS 129 "${AOM_CXX_PASSED_TESTS} ${test_name}" 130 CACHE STRING "" FORCE) 131 else() 132 set(AOM_CXX_FAILED_TESTS 133 "${AOM_CXX_FAILED_TESTS} ${test_name}" 134 CACHE STRING "" FORCE) 135 message("CXX Compiler test ${test_name} failed.") 136 endif() 137 elseif(NOT ${CXX_TEST_PASSED} EQUAL -1) 138 set(${result_var} 1 PARENT_SCOPE) 139 else() # ${CXX_TEST_FAILED} NOT EQUAL -1 140 unset(${result_var} PARENT_SCOPE) 141 endif() 142endfunction() 143 144# Convenience function that confirms $test_source compiles as C and C++. 145# $result_var is set to 1 when both tests are successful, and 0 when one or both 146# tests fail. Note: This function is intended to be used to write to result 147# variables that are expanded via configure_file(). $result_var is set to 1 or 0 148# to allow direct usage of the value in generated source files. 149function(aom_check_source_compiles test_name test_source result_var) 150 unset(C_PASSED) 151 unset(CXX_PASSED) 152 aom_check_c_compiles(${test_name} ${test_source} C_PASSED) 153 aom_check_cxx_compiles(${test_name} ${test_source} CXX_PASSED) 154 if(C_PASSED AND CXX_PASSED) 155 set(${result_var} 1 PARENT_SCOPE) 156 else() 157 set(${result_var} 0 PARENT_SCOPE) 158 endif() 159endfunction() 160