diff options
Diffstat (limited to 'Modules/WriteCompilerDetectionHeader.cmake')
-rw-r--r-- | Modules/WriteCompilerDetectionHeader.cmake | 130 |
1 files changed, 49 insertions, 81 deletions
diff --git a/Modules/WriteCompilerDetectionHeader.cmake b/Modules/WriteCompilerDetectionHeader.cmake index 82e8fba..0b16aa4 100644 --- a/Modules/WriteCompilerDetectionHeader.cmake +++ b/Modules/WriteCompilerDetectionHeader.cmake @@ -20,6 +20,8 @@ # [VERSION <version>] # [PROLOG <prolog>] # [EPILOG <epilog>] +# [ALLOW_UNKNOWN_COMPILERS] +# [ALLOW_UNKNOWN_COMPILER_VERSIONS] # ) # # The ``write_compiler_detection_header`` function generates the @@ -76,10 +78,16 @@ # Available features in this version of CMake are listed in the # :prop_gbl:`CMAKE_C_KNOWN_FEATURES` and # :prop_gbl:`CMAKE_CXX_KNOWN_FEATURES` global properties. +# The ``{c,cxx}_std_*`` meta-features are ignored if requested. # # See the :manual:`cmake-compile-features(7)` manual for information on # compile features. # +# ``ALLOW_UNKNOWN_COMPILERS`` and ``ALLOW_UNKNOWN_COMPILER_VERSIONS`` cause +# the module to generate conditions that treat unknown compilers as simply +# lacking all features. Without these options the default behavior is to +# generate a ``#error`` for unknown compilers. +# # Feature Test Macros # =================== # @@ -215,7 +223,6 @@ # CompatSupport_DEPRECATED= # ) -include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake) include(${CMAKE_CURRENT_LIST_DIR}/CMakeCompilerIdDetection.cmake) function(_load_compiler_variables CompilerId lang) @@ -232,6 +239,19 @@ function(_load_compiler_variables CompilerId lang) set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE) endfunction() +macro(_simpledefine FEATURE_NAME FEATURE_TESTNAME FEATURE_STRING FEATURE_DEFAULT_STRING) + if (feature STREQUAL "${FEATURE_NAME}") + set(def_value "${prefix_arg}_${FEATURE_TESTNAME}") + string(APPEND file_content " +# if defined(${def_name}) && ${def_name} +# define ${def_value} ${FEATURE_STRING} +# else +# define ${def_value} ${FEATURE_DEFAULT_STRING} +# endif +\n") + endif() +endmacro() + function(write_compiler_detection_header file_keyword file_arg prefix_keyword prefix_arg @@ -242,7 +262,7 @@ function(write_compiler_detection_header if (NOT "x${prefix_keyword}" STREQUAL "xPREFIX") message(FATAL_ERROR "write_compiler_detection_header: PREFIX parameter missing.") endif() - set(options) + set(options ALLOW_UNKNOWN_COMPILERS ALLOW_UNKNOWN_COMPILER_VERSIONS) set(oneValueArgs VERSION EPILOG PROLOG OUTPUT_FILES_VAR OUTPUT_DIR) set(multiValueArgs COMPILERS FEATURES) cmake_parse_arguments(_WCD "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) @@ -358,7 +378,11 @@ function(write_compiler_detection_header endif() foreach(feature ${_WCD_FEATURES}) - if (feature MATCHES "^cxx_") + if (feature MATCHES "^c_std_") + # ignored + elseif (feature MATCHES "^cxx_std_") + # ignored + elseif (feature MATCHES "^cxx_") list(APPEND _langs CXX) list(APPEND CXX_features ${feature}) elseif (feature MATCHES "^c_") @@ -422,10 +446,12 @@ function(write_compiler_detection_header set(compiler_file_content file_content) endif() - set(${compiler_file_content} "${${compiler_file_content}} + if(NOT _WCD_ALLOW_UNKNOWN_COMPILER_VERSIONS) + set(${compiler_file_content} "${${compiler_file_content}} # if !(${_cmake_oldestSupported_${compiler}}) # error Unsupported compiler version # endif\n") + endif() set(PREFIX ${prefix_arg}_) if (_need_hex_conversion) @@ -456,67 +482,36 @@ function(write_compiler_detection_header endforeach() endforeach() if(pp_if STREQUAL "elif") - string(APPEND file_content " + if(_WCD_ALLOW_UNKNOWN_COMPILERS) + string(APPEND file_content " +# endif\n") + else() + string(APPEND file_content " # else # error Unsupported compiler # endif\n") + endif() endif() foreach(feature ${${_lang}_features}) string(TOUPPER ${feature} feature_upper) set(feature_PP "COMPILER_${feature_upper}") set(def_name ${prefix_arg}_${feature_PP}) - if (feature STREQUAL c_restrict) - set(def_value "${prefix_arg}_RESTRICT") - string(APPEND file_content " -# if ${def_name} -# define ${def_value} restrict -# else -# define ${def_value} -# endif -\n") - endif() - if (feature STREQUAL cxx_constexpr) - set(def_value "${prefix_arg}_CONSTEXPR") - string(APPEND file_content " -# if ${def_name} -# define ${def_value} constexpr -# else -# define ${def_value} -# endif -\n") - endif() - if (feature STREQUAL cxx_final) - set(def_value "${prefix_arg}_FINAL") - string(APPEND file_content " -# if ${def_name} -# define ${def_value} final -# else -# define ${def_value} -# endif -\n") - endif() - if (feature STREQUAL cxx_override) - set(def_value "${prefix_arg}_OVERRIDE") - string(APPEND file_content " -# if ${def_name} -# define ${def_value} override -# else -# define ${def_value} -# endif -\n") - endif() + _simpledefine(c_restrict RESTRICT restrict "") + _simpledefine(cxx_constexpr CONSTEXPR constexpr "") + _simpledefine(cxx_final FINAL final "") + _simpledefine(cxx_override OVERRIDE override "") if (feature STREQUAL cxx_static_assert) set(def_value "${prefix_arg}_STATIC_ASSERT(X)") set(def_value_msg "${prefix_arg}_STATIC_ASSERT_MSG(X, MSG)") set(static_assert_struct "template<bool> struct ${prefix_arg}StaticAssert;\ntemplate<> struct ${prefix_arg}StaticAssert<true>{};\n") set(def_standard "# define ${def_value} static_assert(X, #X)\n# define ${def_value_msg} static_assert(X, MSG)") set(def_alternative "${static_assert_struct}# define ${def_value} sizeof(${prefix_arg}StaticAssert<X>)\n# define ${def_value_msg} sizeof(${prefix_arg}StaticAssert<X>)") - string(APPEND file_content "# if ${def_name}\n${def_standard}\n# else\n${def_alternative}\n# endif\n\n") + string(APPEND file_content "# if defined(${def_name}) && ${def_name}\n${def_standard}\n# else\n${def_alternative}\n# endif\n\n") endif() if (feature STREQUAL cxx_alignas) set(def_value "${prefix_arg}_ALIGNAS(X)") string(APPEND file_content " -# if ${def_name} +# if defined(${def_name}) && ${def_name} # define ${def_value} alignas(X) # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang # define ${def_value} __attribute__ ((__aligned__(X))) @@ -530,7 +525,7 @@ function(write_compiler_detection_header if (feature STREQUAL cxx_alignof) set(def_value "${prefix_arg}_ALIGNOF(X)") string(APPEND file_content " -# if ${def_name} +# if defined(${def_name}) && ${def_name} # define ${def_value} alignof(X) # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang # define ${def_value} __alignof__(X) @@ -539,30 +534,12 @@ function(write_compiler_detection_header # endif \n") endif() - if (feature STREQUAL cxx_deleted_functions) - set(def_value "${prefix_arg}_DELETED_FUNCTION") - string(APPEND file_content " -# if ${def_name} -# define ${def_value} = delete -# else -# define ${def_value} -# endif -\n") - endif() - if (feature STREQUAL cxx_extern_templates) - set(def_value "${prefix_arg}_EXTERN_TEMPLATE") - string(APPEND file_content " -# if ${def_name} -# define ${def_value} extern -# else -# define ${def_value} -# endif -\n") - endif() + _simpledefine(cxx_deleted_functions DELETED_FUNCTION "= delete" "") + _simpledefine(cxx_extern_templates EXTERN_TEMPLATE extern "") if (feature STREQUAL cxx_noexcept) set(def_value "${prefix_arg}_NOEXCEPT") string(APPEND file_content " -# if ${def_name} +# if defined(${def_name}) && ${def_name} # define ${def_value} noexcept # define ${def_value}_EXPR(X) noexcept(X) # else @@ -571,20 +548,11 @@ function(write_compiler_detection_header # endif \n") endif() - if (feature STREQUAL cxx_nullptr) - set(def_value "${prefix_arg}_NULLPTR") - string(APPEND file_content " -# if ${def_name} -# define ${def_value} nullptr -# else -# define ${def_value} 0 -# endif -\n") - endif() + _simpledefine(cxx_nullptr NULLPTR nullptr 0) if (feature STREQUAL cxx_thread_local) set(def_value "${prefix_arg}_THREAD_LOCAL") string(APPEND file_content " -# if ${def_name} +# if defined(${def_name}) && ${def_name} # define ${def_value} thread_local # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang # define ${def_value} __thread @@ -600,7 +568,7 @@ function(write_compiler_detection_header set(def_value "${prefix_arg}_DEPRECATED") string(APPEND file_content " # ifndef ${def_value} -# if ${def_name} +# if defined(${def_name}) && ${def_name} # define ${def_value} [[deprecated]] # define ${def_value}_MSG(MSG) [[deprecated(MSG)]] # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang |