1 # - Check which features of the C++ standard the compiler supports
3 # When found it will set the following variables
5 # CXX11_COMPILER_FLAGS - the compiler flags needed to get C++11 features
7 # CXXFeatures_alignof_FOUND - alignof keyword
8 # CXXFeatures_auto_FOUND - auto keyword
9 # CXXFeatures_class_override_final_FOUND - override and final keywords for classes and methods
10 # CXXFeatures_constexpr_FOUND - constexpr keyword
11 # CXXFeatures_cstdint_header_FOUND - cstdint header
12 # CXXFeatures_decltype_FOUND - decltype keyword
13 # CXXFeatures_defaulted_functions_FOUND - default keyword for functions
14 # CXXFeatures_deleted_functions_FOUND - delete keyword for functions
15 # CXXFeatures_func_identifier_FOUND - __func__ preprocessor constant
16 # CXXFeatures_initializer_list_FOUND - initializer list
17 # CXXFeatures_lambda_FOUND - lambdas
18 # CXXFeatures_long_long_FOUND - long long signed & unsigned types
19 # CXXFeatures_nullptr_FOUND - nullptr
20 # CXXFeatures_rvalue_references_FOUND - rvalue references
21 # CXXFeatures_sizeof_member_FOUND - sizeof() non-static members
22 # CXXFeatures_static_assert_FOUND - static_assert()
23 # CXXFeatures_variadic_templates_FOUND - variadic templates
25 #=============================================================================
26 # Copyright 2011,2012,2013 Rolf Eike Beer <eike@sf-mail.de>
27 # Copyright 2012 Andreas Weis
28 # Copyright 2013 Jan Kundrát
30 # Distributed under the OSI-approved BSD License (the "License");
31 # see accompanying file Copyright.txt for details.
33 # This software is distributed WITHOUT ANY WARRANTY; without even the
34 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 # See the License for more information.
36 #=============================================================================
37 # (To distribute this file outside of CMake, substitute the full
38 # License text for the above reference.)
40 if (NOT CMAKE_CXX_COMPILER_LOADED)
41 message(FATAL_ERROR "CXXFeatures modules only works if language CXX is enabled")
44 cmake_minimum_required(VERSION 2.8.3)
47 ### Check for needed compiler flags
49 include(CheckCXXCompilerFlag)
50 check_cxx_compiler_flag("-std=c++11" _HAS_CXX11_FLAG)
52 set(CXX11_COMPILER_FLAGS "-std=c++11")
54 check_cxx_compiler_flag("-std=c++0x" _HAS_CXX0X_FLAG)
56 set(CXX11_COMPILER_FLAGS "-std=c++0x")
60 function(cxx_check_feature FEATURE_NAME)
61 set(RESULT_VAR "CXXFeatures_${FEATURE_NAME}_FOUND")
62 if (DEFINED ${RESULT_VAR})
66 set(_bindir "${CMAKE_CURRENT_BINARY_DIR}/cxx_${FEATURE_NAME}")
68 set(_SRCFILE_BASE ${CMAKE_CURRENT_LIST_DIR}/FindCXXFeatures/cxx11-test-${FEATURE_NAME})
69 set(_LOG_NAME "\"${FEATURE_NAME}\"")
70 message(STATUS "Checking C++ support for ${_LOG_NAME}")
72 set(_SRCFILE "${_SRCFILE_BASE}.cxx")
73 set(_SRCFILE_FAIL_COMPILE "${_SRCFILE_BASE}_fail_compile.cxx")
75 try_compile(${RESULT_VAR} "${_bindir}" "${_SRCFILE}"
76 COMPILE_DEFINITIONS "${CXX11_COMPILER_FLAGS}")
78 if (${RESULT_VAR} AND EXISTS ${_SRCFILE_FAIL_COMPILE})
79 try_compile(_TMP_RESULT "${_bindir}_fail_compile" "${_SRCFILE_FAIL_COMPILE}"
80 COMPILE_DEFINITIONS "${CXX11_COMPILER_FLAGS}")
82 set(${RESULT_VAR} FALSE)
84 set(${RESULT_VAR} TRUE)
89 message(STATUS "Checking C++ support for ${_LOG_NAME}: works")
91 message(STATUS "Checking C++ support for ${_LOG_NAME}: not supported")
93 set(${RESULT_VAR} "${${RESULT_VAR}}" CACHE INTERNAL "C++ support for ${_LOG_NAME}")
94 endfunction(cxx_check_feature)
116 if (CXXFeatures_FIND_COMPONENTS)
117 foreach (_cxx_feature IN LISTS CXXFeatures_FIND_COMPONENTS)
118 list(FIND _CXX_ALL_FEATURES "${_cxx_feature}" _feature_index)
119 if (_feature_index EQUAL -1)
120 message(FATAL_ERROR "Unknown component: '${_cxx_feature}'")
123 unset(_feature_index)
125 set(CXXFEATURES_FIND_COMPONENTS ${_CXX_ALL_FEATURES})
128 foreach (_cxx_feature IN LISTS CXXFEATURES_FIND_COMPONENTS)
129 cxx_check_feature(${_cxx_feature} ${FEATURE_NAME})
130 endforeach (_cxx_feature)
132 include(FindPackageHandleStandardArgs)
134 find_package_handle_standard_args(CXXFeatures REQUIRED_VARS DUMMY_VAR HANDLE_COMPONENTS)
136 unset(_CXX_ALL_FEATURES)