• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
8cmake_minimum_required(VERSION 3.2)
9
10include(CheckCXXCompilerFlag)
11
12# String used to cache failed CXX flags.
13set(LIBWEBM_FAILED_CXX_FLAGS)
14
15# Checks C++ compiler for support of $cxx_flag. Adds $cxx_flag to
16# $CMAKE_CXX_FLAGS when the compile test passes. Caches $c_flag in
17# $LIBWEBM_FAILED_CXX_FLAGS when the test fails.
18function (add_cxx_flag_if_supported cxx_flag)
19  unset(CXX_FLAG_FOUND CACHE)
20  string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
21  unset(CXX_FLAG_FAILED CACHE)
22  string(FIND "${LIBWEBM_FAILED_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FAILED)
23
24  if (${CXX_FLAG_FOUND} EQUAL -1 AND ${CXX_FLAG_FAILED} EQUAL -1)
25    unset(CXX_FLAG_SUPPORTED CACHE)
26    message("Checking CXX compiler flag support for: " ${cxx_flag})
27    check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)
28    if (CXX_FLAG_SUPPORTED)
29      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${cxx_flag}" CACHE STRING ""
30          FORCE)
31    else ()
32      set(LIBWEBM_FAILED_CXX_FLAGS "${LIBWEBM_FAILED_CXX_FLAGS} ${cxx_flag}"
33          CACHE STRING "" FORCE)
34    endif ()
35  endif ()
36endfunction ()
37
38# Checks CXX compiler for support of $cxx_flag and terminates generation when
39# support is not present.
40function (require_cxx_flag cxx_flag)
41  unset(CXX_FLAG_FOUND CACHE)
42  string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
43
44  if (${CXX_FLAG_FOUND} EQUAL -1)
45    unset(LIBWEBM_HAVE_CXX_FLAG CACHE)
46    message("Checking CXX compiler flag support for: " ${cxx_flag})
47    check_cxx_compiler_flag("${cxx_flag}" LIBWEBM_HAVE_CXX_FLAG)
48    if (NOT LIBWEBM_HAVE_CXX_FLAG)
49      message(FATAL_ERROR
50              "${PROJECT_NAME} requires support for CXX flag: ${cxx_flag}.")
51    endif ()
52    set(CMAKE_CXX_FLAGS "${cxx_flag} ${CMAKE_CXX_FLAGS}" CACHE STRING "" FORCE)
53  endif ()
54endfunction ()
55
56# Checks only non-MSVC targets for support of $cxx_flag.
57function (require_cxx_flag_nomsvc cxx_flag)
58  if (NOT MSVC)
59    require_cxx_flag(${cxx_flag})
60  endif ()
61endfunction ()
62
63# Adds $preproc_def to CXX compiler command line (as -D$preproc_def) if not
64# already present.
65function (add_cxx_preproc_definition preproc_def)
66  unset(PREPROC_DEF_FOUND CACHE)
67  string(FIND "${CMAKE_CXX_FLAGS}" "${preproc_def}" PREPROC_DEF_FOUND)
68
69  if (${PREPROC_DEF_FOUND} EQUAL -1)
70    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D${preproc_def}" CACHE STRING ""
71        FORCE)
72  endif ()
73endfunction ()
74