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