C++11 detection on a per-feature basis via BSD-licensed CMake scripts (that will...
[dyninst.git] / cmake / CheckCXX11Features.cmake
1 # - Check which parts of the C++11 standard the compiler supports
2 #
3 # When found it will set the following variables
4 #
5 #  CXX11_COMPILER_FLAGS         - the compiler flags needed to get C++11 features
6 #
7 #  HAS_CXX11_AUTO               - auto keyword
8 #  HAS_CXX11_AUTO_RET_TYPE      - function declaration with deduced return types
9 #  HAS_CXX11_CLASS_OVERRIDE     - override and final keywords for classes and methods
10 #  HAS_CXX11_CONSTEXPR          - constexpr keyword
11 #  HAS_CXX11_CSTDINT_H          - cstdint header
12 #  HAS_CXX11_DECLTYPE           - decltype keyword
13 #  HAS_CXX11_FUNC               - __func__ preprocessor constant
14 #  HAS_CXX11_INITIALIZER_LIST   - initializer list
15 #  HAS_CXX11_LAMBDA             - lambdas
16 #  HAS_CXX11_LIB_REGEX          - regex library
17 #  HAS_CXX11_LONG_LONG          - long long signed & unsigned types
18 #  HAS_CXX11_NULLPTR            - nullptr
19 #  HAS_CXX11_RVALUE_REFERENCES  - rvalue references
20 #  HAS_CXX11_SIZEOF_MEMBER      - sizeof() non-static members
21 #  HAS_CXX11_STATIC_ASSERT      - static_assert()
22 #  HAS_CXX11_VARIADIC_TEMPLATES - variadic templates
23
24 #=============================================================================
25 # Copyright 2011,2012 Rolf Eike Beer <eike@sf-mail.de>
26 # Copyright 2012 Andreas Weis
27 #
28 # Distributed under the OSI-approved BSD License (the "License");
29 # see accompanying file Copyright.txt for details.
30 #
31 # This software is distributed WITHOUT ANY WARRANTY; without even the
32 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
33 # See the License for more information.
34 #=============================================================================
35 # (To distribute this file outside of CMake, substitute the full
36 #  License text for the above reference.)
37
38 #
39 # Each feature may have up to 3 checks, every one of them in it's own file
40 # FEATURE.cpp              - example that must build and return 0 when run
41 # FEATURE_fail.cpp         - example that must build, but may not return 0 when run
42 # FEATURE_fail_compile.cpp - example that must fail compilation
43 #
44 # The first one is mandatory, the latter 2 are optional and do not depend on
45 # each other (i.e. only one may be present).
46 #
47
48 if (NOT CMAKE_CXX_COMPILER_LOADED)
49     message(FATAL_ERROR "CheckCXX11Features modules only works if language CXX is enabled")
50 endif ()
51
52 cmake_minimum_required(VERSION 2.8.3)
53
54 #
55 ### Check for needed compiler flags
56 #
57 include(CheckCXXCompilerFlag)
58 check_cxx_compiler_flag("-std=c++11" _HAS_CXX11_FLAG)
59 if (NOT _HAS_CXX11_FLAG)
60     check_cxx_compiler_flag("-std=c++0x" _HAS_CXX0X_FLAG)
61 endif ()
62
63 if (_HAS_CXX11_FLAG)
64     set(CXX11_COMPILER_FLAGS "-std=c++11")
65 elseif (_HAS_CXX0X_FLAG)
66     set(CXX11_COMPILER_FLAGS "-std=c++0x")
67 endif ()
68
69 function(cxx11_check_feature FEATURE_NAME RESULT_VAR)
70     if (NOT DEFINED ${RESULT_VAR})
71         set(_bindir "${CMAKE_CURRENT_BINARY_DIR}/cxx11_${FEATURE_NAME}")
72
73         set(_SRCFILE_BASE ${CMAKE_CURRENT_LIST_DIR}/CheckCXX11Features/cxx11-test-${FEATURE_NAME})
74         set(_LOG_NAME "\"${FEATURE_NAME}\"")
75         message(STATUS "Checking C++11 support for ${_LOG_NAME}")
76
77         set(_SRCFILE "${_SRCFILE_BASE}.cpp")
78         set(_SRCFILE_FAIL "${_SRCFILE_BASE}_fail.cpp")
79         set(_SRCFILE_FAIL_COMPILE "${_SRCFILE_BASE}_fail_compile.cpp")
80
81         if (CROSS_COMPILING)
82             try_compile(${RESULT_VAR} "${_bindir}" "${_SRCFILE}"
83                         COMPILE_DEFINITIONS "${CXX11_COMPILER_FLAGS}")
84             if (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})
85                 try_compile(${RESULT_VAR} "${_bindir}_fail" "${_SRCFILE_FAIL}"
86                             COMPILE_DEFINITIONS "${CXX11_COMPILER_FLAGS}")
87             endif (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})
88         else (CROSS_COMPILING)
89             try_run(_RUN_RESULT_VAR _COMPILE_RESULT_VAR
90                     "${_bindir}" "${_SRCFILE}"
91                     COMPILE_DEFINITIONS "${CXX11_COMPILER_FLAGS}")
92             if (_COMPILE_RESULT_VAR AND NOT _RUN_RESULT_VAR)
93                 set(${RESULT_VAR} TRUE)
94             else (_COMPILE_RESULT_VAR AND NOT _RUN_RESULT_VAR)
95                 set(${RESULT_VAR} FALSE)
96             endif (_COMPILE_RESULT_VAR AND NOT _RUN_RESULT_VAR)
97             if (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})
98                 try_run(_RUN_RESULT_VAR _COMPILE_RESULT_VAR
99                         "${_bindir}_fail" "${_SRCFILE_FAIL}"
100                          COMPILE_DEFINITIONS "${CXX11_COMPILER_FLAGS}")
101                 if (_COMPILE_RESULT_VAR AND _RUN_RESULT_VAR)
102                     set(${RESULT_VAR} TRUE)
103                 else (_COMPILE_RESULT_VAR AND _RUN_RESULT_VAR)
104                     set(${RESULT_VAR} FALSE)
105                 endif (_COMPILE_RESULT_VAR AND _RUN_RESULT_VAR)
106             endif (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL})
107         endif (CROSS_COMPILING)
108         if (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL_COMPILE})
109             try_compile(_TMP_RESULT "${_bindir}_fail_compile" "${_SRCFILE_FAIL_COMPILE}"
110                         COMPILE_DEFINITIONS "${CXX11_COMPILER_FLAGS}")
111             if (_TMP_RESULT)
112                 set(${RESULT_VAR} FALSE)
113             else (_TMP_RESULT)
114                 set(${RESULT_VAR} TRUE)
115             endif (_TMP_RESULT)
116         endif (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL_COMPILE})
117
118         if (${RESULT_VAR})
119             message(STATUS "Checking C++11 support for ${_LOG_NAME}: works")
120         else (${RESULT_VAR})
121             message(STATUS "Checking C++11 support for ${_LOG_NAME}: not supported")
122         endif (${RESULT_VAR})
123         set(${RESULT_VAR} ${${RESULT_VAR}} CACHE INTERNAL "C++11 support for ${_LOG_NAME}")
124     endif (NOT DEFINED ${RESULT_VAR})
125 endfunction(cxx11_check_feature)
126
127 cxx11_check_feature("__func__" HAS_CXX11_FUNC)
128 cxx11_check_feature("auto" HAS_CXX11_AUTO)
129 cxx11_check_feature("auto_ret_type" HAS_CXX11_AUTO_RET_TYPE)
130 cxx11_check_feature("class_override_final" HAS_CXX11_CLASS_OVERRIDE)
131 cxx11_check_feature("constexpr" HAS_CXX11_CONSTEXPR)
132 cxx11_check_feature("cstdint" HAS_CXX11_CSTDINT_H)
133 cxx11_check_feature("decltype" HAS_CXX11_DECLTYPE)
134 cxx11_check_feature("initializer_list" HAS_CXX11_INITIALIZER_LIST)
135 cxx11_check_feature("lambda" HAS_CXX11_LAMBDA)
136 cxx11_check_feature("long_long" HAS_CXX11_LONG_LONG)
137 cxx11_check_feature("nullptr" HAS_CXX11_NULLPTR)
138 cxx11_check_feature("regex" HAS_CXX11_LIB_REGEX)
139 cxx11_check_feature("rvalue-references" HAS_CXX11_RVALUE_REFERENCES)
140 cxx11_check_feature("sizeof_member" HAS_CXX11_SIZEOF_MEMBER)
141 cxx11_check_feature("static_assert" HAS_CXX11_STATIC_ASSERT)
142 cxx11_check_feature("variadic_templates" HAS_CXX11_VARIADIC_TEMPLATES)