xref: /aosp_15_r20/external/libwebm/build/cxx_flags.cmake (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1#  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2#
3#  Use of this source code is governed by a BSD-style license
4#  that can be found in the LICENSE file in the root of the source
5#  tree. An additional intellectual property rights grant can be found
6#  in the file PATENTS.  All contributing project authors may
7#  be found in the AUTHORS file in the root of the source tree.
8
9include(CheckCXXCompilerFlag)
10
11# String used to cache failed CXX flags.
12set(LIBWEBM_FAILED_CXX_FLAGS)
13
14# Checks C++ compiler for support of $cxx_flag. Adds $cxx_flag to
15# $CMAKE_CXX_FLAGS when the compile test passes. Caches $c_flag in
16# $LIBWEBM_FAILED_CXX_FLAGS when the test fails.
17function(add_cxx_flag_if_supported cxx_flag)
18  unset(CXX_FLAG_FOUND CACHE)
19  string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
20  unset(CXX_FLAG_FAILED CACHE)
21  string(FIND "${LIBWEBM_FAILED_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FAILED)
22
23  if(${CXX_FLAG_FOUND} EQUAL -1 AND ${CXX_FLAG_FAILED} EQUAL -1)
24    unset(CXX_FLAG_SUPPORTED CACHE)
25    message("Checking CXX compiler flag support for: " ${cxx_flag})
26    check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)
27    if(CXX_FLAG_SUPPORTED)
28      set(CMAKE_CXX_FLAGS
29          "${CMAKE_CXX_FLAGS} ${cxx_flag}"
30          CACHE STRING "" FORCE)
31    else()
32      set(LIBWEBM_FAILED_CXX_FLAGS
33          "${LIBWEBM_FAILED_CXX_FLAGS} ${cxx_flag}"
34          CACHE STRING "" FORCE)
35    endif()
36  endif()
37endfunction()
38
39# Checks CXX compiler for support of $cxx_flag and terminates generation when
40# support is not present.
41function(require_cxx_flag cxx_flag)
42  unset(CXX_FLAG_FOUND CACHE)
43  string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
44
45  if(${CXX_FLAG_FOUND} EQUAL -1)
46    unset(LIBWEBM_HAVE_CXX_FLAG CACHE)
47    message("Checking CXX compiler flag support for: " ${cxx_flag})
48    check_cxx_compiler_flag("${cxx_flag}" LIBWEBM_HAVE_CXX_FLAG)
49    if(NOT LIBWEBM_HAVE_CXX_FLAG)
50      message(
51        FATAL_ERROR
52          "${PROJECT_NAME} requires support for CXX flag: ${cxx_flag}.")
53    endif()
54    set(CMAKE_CXX_FLAGS
55        "${cxx_flag} ${CMAKE_CXX_FLAGS}"
56        CACHE STRING "" FORCE)
57  endif()
58endfunction()
59
60# Checks only non-MSVC targets for support of $cxx_flag.
61function(require_cxx_flag_nomsvc cxx_flag)
62  if(NOT MSVC)
63    require_cxx_flag(${cxx_flag})
64  endif()
65endfunction()
66
67# Adds $preproc_def to CXX compiler command line (as -D$preproc_def) if not
68# already present.
69function(add_cxx_preproc_definition preproc_def)
70  unset(PREPROC_DEF_FOUND CACHE)
71  string(FIND "${CMAKE_CXX_FLAGS}" "${preproc_def}" PREPROC_DEF_FOUND)
72
73  if(${PREPROC_DEF_FOUND} EQUAL -1)
74    set(CMAKE_CXX_FLAGS
75        "${CMAKE_CXX_FLAGS} -D${preproc_def}"
76        CACHE STRING "" FORCE)
77  endif()
78endfunction()
79