CompilerTest.cmake 4.13 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1 2

set(COMPILER_TEST_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/compilerTest)
3
file(WRITE ${COMPILER_TEST_DIR}/CMakeLists.txt
Praetorius, Simon's avatar
Praetorius, Simon committed
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
    "project(compilerTest)
     cmake_minimum_required(VERSION 2.8)
     add_definitions(-std=c++11)
# VARIADIC CLASS_TEMPLATES
     add_executable(test_variadic_templ_class test_variadic_templ_class.cpp)
     target_link_libraries(test_variadic_templ_class)
# VARIADIC FUNCTION TEMPLATES
     add_executable(test_variadic_templ_fct test_variadic_templ_fct.cpp)
     target_link_libraries(test_variadic_templ_fct)
# ALIAS TEMPLATES
     add_executable(test_alias_templates test_alias_templates.cpp)
     target_link_libraries(test_alias_templates)
# DECLTYPE
     add_executable(test_decltype test_decltype.cpp)
     target_link_libraries(test_decltype)
# AUTO SPECIFIER
     add_executable(test_auto test_auto.cpp)
     target_link_libraries(test_auto)
# CONSTEXPR
     add_executable(test_constexpr test_constexpr.cpp)
     target_link_libraries(test_constexpr)
# DELEGATING CONSTRUCTORS
     add_executable(test_delegating_constructors test_delegating_constructors.cpp)
     target_link_libraries(test_delegating_constructors)
# RANGE-BASED FOR LOOPS
     add_executable(test_range_based_for test_range_based_for.cpp)
     target_link_libraries(test_range_based_for)")
31 32

file(WRITE ${COMPILER_TEST_DIR}/test_variadic_templ_class.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
33
     "template<class... Ts> struct A{}; int main(){A<int, double> a;}")
34
file(WRITE ${COMPILER_TEST_DIR}/test_variadic_templ_fct.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
35
     "template<class... Ts> void foo(Ts... ts){}; int main(){foo(1, 2.0, 3.0f);}")
36
file(WRITE ${COMPILER_TEST_DIR}/test_alias_templates.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
37
     "template<class T> struct A{}; template<class T> using B=A<T>; int main(){B<int> b;}")
38
file(WRITE ${COMPILER_TEST_DIR}/test_decltype.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
39
     "int main(){decltype(1) a = 1;}")
40
file(WRITE ${COMPILER_TEST_DIR}/test_auto.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
41
     "int main(){auto a = 1;}")
42
file(WRITE ${COMPILER_TEST_DIR}/test_constexpr.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
43
     "constexpr int foo(){return 1;}; int main(){static constexpr int f = foo();}")
44
file(WRITE ${COMPILER_TEST_DIR}/test_delegating_constructors.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
45
     "struct A{ A(){} A(int) : A(){} }; int main(){ A a(1);}")
46
file(WRITE ${COMPILER_TEST_DIR}/test_range_based_for.cpp
Praetorius, Simon's avatar
Praetorius, Simon committed
47
     "int main(){ int vec[10]; for (int& elem : vec) { elem = 1; } }")
48 49


Praetorius, Simon's avatar
Praetorius, Simon committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
set(COMPILER_CXX11_FEATURES "")

try_compile(TEST_VARIADIC_TEMPL_CLASS ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_variadic_templ_class)
try_compile(TEST_VARIADIC_TEMPL_FCT ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_variadic_templ_fct)
if (TEST_VARIADIC_TEMPL_CLASS AND TEST_VARIADIC_TEMPL_FCT)
  message("Compiler supports variadic class/ function templates")
  list(APPEND COMPILER_CXX11_FEATURES "-DHAS_VARIADIC_TEMPLATES=1")
endif()

try_compile(TEST_ALIAS_TEMPLATES ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_alias_templates)
if (TEST_ALIAS_TEMPLATES)
  message("Compiler supports alias-templates")
  list(APPEND COMPILER_CXX11_FEATURES "-DHAS_ALIAS_TEMPLATES=1")
endif()

try_compile(TEST_DECLTYPE ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_decltype)
if (TEST_DECLTYPE)
  message("Compiler supports decltype")
  list(APPEND COMPILER_CXX11_FEATURES "-DHAS_DECLTYPE=1")
endif()

try_compile(TEST_AUTO_SPECIFIER ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_auto)
if (TEST_AUTO_SPECIFIER)
  message("Compiler supports auto")
  list(APPEND COMPILER_CXX11_FEATURES "-DHAS_AUTO=1")
endif()

try_compile(TEST_CONSTEXPR ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_constexpr)
if (TEST_CONSTEXPR)
  message("Compiler supports constexpr")
  list(APPEND COMPILER_CXX11_FEATURES "-DHAS_CONSTEXPR=1")
endif()

try_compile(TEST_DELEGATING_CONSTRUCTORS ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_delegating_constructors)
if (TEST_DELEGATING_CONSTRUCTORS)
  message("Compiler supports delegating constructors")
  list(APPEND COMPILER_CXX11_FEATURES "-DHAS_DELEGATING_CONSTRUCTORS=1")
endif()

try_compile(TEST_RANGED_BASED_FOR ${COMPILER_TEST_DIR} ${COMPILER_TEST_DIR} compilerTest test_range_based_for)
if (TEST_RANGED_BASED_FOR)
  message("Compiler supports range-based for loops")
  list(APPEND COMPILER_CXX11_FEATURES "-DHAS_RANGE_BASED_FOR=1")
endif()