target_enable_cxx11.cmake 2.08 KB
Newer Older
1
2
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
3

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
macro(target_enable_cxx14 RESULT_VAR _TARGET_ _SCOPE_)
  check_cxx_compiler_flag("-std=c++14" COMPILER_SUPPORTS_CXX14_FLAG)

  set(CXX14_CODE "
  auto f() { return 1; }
  int main(){
    auto f1 = [](auto const& y) { return y; };
    auto y0 = f();
    auto y1 = f1(1);
  }")

  if (COMPILER_SUPPORTS_CXX14_FLAG)
    set(CMAKE_REQUIRED_FLAGS "-std=c++14")
    check_cxx_source_compiles("${CXX14_CODE}" CXX14_COMPILES_WITH_CXX14_FLAG)
    set(CMAKE_REQUIRED_FLAGS "")
  endif ()

  if (COMPILER_SUPPORTS_CXX14_FLAG AND CXX14_COMPILES_WITH_CXX14_FLAG)
    target_compile_options(${_TARGET_} ${_SCOPE_} "-std=c++14")
    set(${RESULT_VAR} true CACHE BOOL "Enable C++14 compiler features" FORCE)
  else ()
    check_cxx_source_compiles("${CXX14_CODE}" CXX14_COMPILES)

    if (CXX14_COMPILES)
      set(${RESULT_VAR} true CACHE BOOL "Enable C++14 compiler features" FORCE)
    else ()
      set(${RESULT_VAR} false CACHE BOOL "Enable C++14 compiler features" FORCE)
    endif ()
  endif ()
endmacro(target_enable_cxx14)

35
macro(target_enable_cxx11 RESULT_VAR _TARGET_ _SCOPE_)
Praetorius, Simon's avatar
Praetorius, Simon committed
36
  check_cxx_compiler_flag("-std=c++11" COMPILER_SUPPORTS_CXX11_FLAG)
37

Praetorius, Simon's avatar
Praetorius, Simon committed
38
39
40
41
42
43
44
45
  set(CXX11_CODE "
  #include <array>
  #include <tuple>
  template <class... T> using A = std::tuple<T...>;
  int main(){
    decltype(1) vec[]={1,2,3};
    for(auto&& x:vec);
  }")
Praetorius, Simon's avatar
Praetorius, Simon committed
46
47

  if (COMPILER_SUPPORTS_CXX11_FLAG)
48
    set(CMAKE_REQUIRED_FLAGS "-std=c++11")
Praetorius, Simon's avatar
Praetorius, Simon committed
49
    check_cxx_source_compiles("${CXX11_CODE}" CXX11_COMPILES_WITH_CXX11_FLAG)
50
51
    set(CMAKE_REQUIRED_FLAGS "")
  endif ()
52

Praetorius, Simon's avatar
Praetorius, Simon committed
53
  if (COMPILER_SUPPORTS_CXX11_FLAG AND CXX11_COMPILES_WITH_CXX11_FLAG)
54
    target_compile_options(${_TARGET_} ${_SCOPE_} "-std=c++11")
Praetorius, Simon's avatar
Praetorius, Simon committed
55
    set(${RESULT_VAR} true CACHE BOOL "Enable C++11 compiler features" FORCE)
56
  else ()
Praetorius, Simon's avatar
Praetorius, Simon committed
57
    check_cxx_source_compiles("${CXX11_CODE}" CXX11_COMPILES)
Praetorius, Simon's avatar
Praetorius, Simon committed
58

Praetorius, Simon's avatar
Praetorius, Simon committed
59
60
    if (CXX11_COMPILES)
      set(${RESULT_VAR} true CACHE BOOL "Enable C++11 compiler features" FORCE)
Praetorius, Simon's avatar
Praetorius, Simon committed
61
    else ()
Praetorius, Simon's avatar
Praetorius, Simon committed
62
      set(${RESULT_VAR} false CACHE BOOL "Enable C++11 compiler features" FORCE)
63
    endif ()
64
65
  endif ()
endmacro(target_enable_cxx11)