enable_cxx11.cmake 1013 Bytes
Newer Older
1
2
3
4
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CheckIncludeFileCXX)

5
if (ENABLE_CXX11)
Praetorius, Simon's avatar
Praetorius, Simon committed
6
  check_cxx_compiler_flag("-std=c++11" COMPILER_SUPPORTS_CXX11_FLAG)
7

Praetorius, Simon's avatar
Praetorius, Simon committed
8
9
10
11
12
13
14
15
  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
16
17

  if (COMPILER_SUPPORTS_CXX11_FLAG)
18
    set(CMAKE_REQUIRED_FLAGS "-std=c++11")
Praetorius, Simon's avatar
Praetorius, Simon committed
19
    check_cxx_source_compiles("${CXX11_CODE}" CXX11_COMPILES_WITH_CXX11_FLAG)
20
21
22
    set(CMAKE_REQUIRED_FLAGS "")
  endif ()

Praetorius, Simon's avatar
Praetorius, Simon committed
23
  if (COMPILER_SUPPORTS_CXX11_FLAG AND CXX11_COMPILES_WITH_CXX11_FLAG)
24
25
    add_definitions("-std=c++11")
  else ()
Praetorius, Simon's avatar
Praetorius, Simon committed
26
    check_cxx_source_compiles("${CXX11_CODE}" CXX11_COMPILES)
27

Praetorius, Simon's avatar
Praetorius, Simon committed
28
29
    if (NOT CXX11_COMPILES)
      message (FATAL_ERROR "Compiler does not support C++11.")
30
31
    endif ()
  endif ()
32

33
  list(APPEND COMPILEFLAGS "-DHAS_CXX11=1")
34
else ()
35
  list(APPEND COMPILEFLAGS "-DHAS_CXX11=0")
36
endif (ENABLE_CXX11)