WriteCompilerDetectionHeader.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #.rst:
  4. # WriteCompilerDetectionHeader
  5. # ----------------------------
  6. #
  7. # This module provides the function write_compiler_detection_header().
  8. #
  9. # The ``WRITE_COMPILER_DETECTION_HEADER`` function can be used to generate
  10. # a file suitable for preprocessor inclusion which contains macros to be
  11. # used in source code::
  12. #
  13. # write_compiler_detection_header(
  14. # FILE <file>
  15. # PREFIX <prefix>
  16. # [OUTPUT_FILES_VAR <output_files_var> OUTPUT_DIR <output_dir>]
  17. # COMPILERS <compiler> [...]
  18. # FEATURES <feature> [...]
  19. # [VERSION <version>]
  20. # [PROLOG <prolog>]
  21. # [EPILOG <epilog>]
  22. # [ALLOW_UNKNOWN_COMPILERS]
  23. # [ALLOW_UNKNOWN_COMPILER_VERSIONS]
  24. # )
  25. #
  26. # The ``write_compiler_detection_header`` function generates the
  27. # file ``<file>`` with macros which all have the prefix ``<prefix>``.
  28. #
  29. # By default, all content is written directly to the ``<file>``. The
  30. # ``OUTPUT_FILES_VAR`` may be specified to cause the compiler-specific
  31. # content to be written to separate files. The separate files are then
  32. # available in the ``<output_files_var>`` and may be consumed by the caller
  33. # for installation for example. The ``OUTPUT_DIR`` specifies a relative
  34. # path from the main ``<file>`` to the compiler-specific files. For example:
  35. #
  36. # .. code-block:: cmake
  37. #
  38. # write_compiler_detection_header(
  39. # FILE climbingstats_compiler_detection.h
  40. # PREFIX ClimbingStats
  41. # OUTPUT_FILES_VAR support_files
  42. # OUTPUT_DIR compilers
  43. # COMPILERS GNU Clang MSVC Intel
  44. # FEATURES cxx_variadic_templates
  45. # )
  46. # install(FILES
  47. # ${CMAKE_CURRENT_BINARY_DIR}/climbingstats_compiler_detection.h
  48. # DESTINATION include
  49. # )
  50. # install(FILES
  51. # ${support_files}
  52. # DESTINATION include/compilers
  53. # )
  54. #
  55. #
  56. # ``VERSION`` may be used to specify the API version to be generated.
  57. # Future versions of CMake may introduce alternative APIs. A given
  58. # API is selected by any ``<version>`` value greater than or equal
  59. # to the version of CMake that introduced the given API and less
  60. # than the version of CMake that introduced its succeeding API.
  61. # The value of the :variable:`CMAKE_MINIMUM_REQUIRED_VERSION`
  62. # variable is used if no explicit version is specified.
  63. # (As of CMake version |release| there is only one API version.)
  64. #
  65. # ``PROLOG`` may be specified as text content to write at the start of the
  66. # header. ``EPILOG`` may be specified as text content to write at the end
  67. # of the header
  68. #
  69. # At least one ``<compiler>`` and one ``<feature>`` must be listed. Compilers
  70. # which are known to CMake, but not specified are detected and a preprocessor
  71. # ``#error`` is generated for them. A preprocessor macro matching
  72. # ``<PREFIX>_COMPILER_IS_<compiler>`` is generated for each compiler
  73. # known to CMake to contain the value ``0`` or ``1``.
  74. #
  75. # Possible compiler identifiers are documented with the
  76. # :variable:`CMAKE_<LANG>_COMPILER_ID` variable.
  77. # Available features in this version of CMake are listed in the
  78. # :prop_gbl:`CMAKE_C_KNOWN_FEATURES` and
  79. # :prop_gbl:`CMAKE_CXX_KNOWN_FEATURES` global properties.
  80. # The ``{c,cxx}_std_*`` meta-features are ignored if requested.
  81. #
  82. # See the :manual:`cmake-compile-features(7)` manual for information on
  83. # compile features.
  84. #
  85. # ``ALLOW_UNKNOWN_COMPILERS`` and ``ALLOW_UNKNOWN_COMPILER_VERSIONS`` cause
  86. # the module to generate conditions that treat unknown compilers as simply
  87. # lacking all features. Without these options the default behavior is to
  88. # generate a ``#error`` for unknown compilers.
  89. #
  90. # Feature Test Macros
  91. # ===================
  92. #
  93. # For each compiler, a preprocessor macro is generated matching
  94. # ``<PREFIX>_COMPILER_IS_<compiler>`` which has the content either ``0``
  95. # or ``1``, depending on the compiler in use. Preprocessor macros for
  96. # compiler version components are generated matching
  97. # ``<PREFIX>_COMPILER_VERSION_MAJOR`` ``<PREFIX>_COMPILER_VERSION_MINOR``
  98. # and ``<PREFIX>_COMPILER_VERSION_PATCH`` containing decimal values
  99. # for the corresponding compiler version components, if defined.
  100. #
  101. # A preprocessor test is generated based on the compiler version
  102. # denoting whether each feature is enabled. A preprocessor macro
  103. # matching ``<PREFIX>_COMPILER_<FEATURE>``, where ``<FEATURE>`` is the
  104. # upper-case ``<feature>`` name, is generated to contain the value
  105. # ``0`` or ``1`` depending on whether the compiler in use supports the
  106. # feature:
  107. #
  108. # .. code-block:: cmake
  109. #
  110. # write_compiler_detection_header(
  111. # FILE climbingstats_compiler_detection.h
  112. # PREFIX ClimbingStats
  113. # COMPILERS GNU Clang AppleClang MSVC Intel
  114. # FEATURES cxx_variadic_templates
  115. # )
  116. #
  117. # .. code-block:: c++
  118. #
  119. # #if ClimbingStats_COMPILER_CXX_VARIADIC_TEMPLATES
  120. # template<typename... T>
  121. # void someInterface(T t...) { /* ... */ }
  122. # #else
  123. # // Compatibility versions
  124. # template<typename T1>
  125. # void someInterface(T1 t1) { /* ... */ }
  126. # template<typename T1, typename T2>
  127. # void someInterface(T1 t1, T2 t2) { /* ... */ }
  128. # template<typename T1, typename T2, typename T3>
  129. # void someInterface(T1 t1, T2 t2, T3 t3) { /* ... */ }
  130. # #endif
  131. #
  132. # Symbol Macros
  133. # =============
  134. #
  135. # Some additional symbol-defines are created for particular features for
  136. # use as symbols which may be conditionally defined empty:
  137. #
  138. # .. code-block:: c++
  139. #
  140. # class MyClass ClimbingStats_FINAL
  141. # {
  142. # ClimbingStats_CONSTEXPR int someInterface() { return 42; }
  143. # };
  144. #
  145. # The ``ClimbingStats_FINAL`` macro will expand to ``final`` if the
  146. # compiler (and its flags) support the ``cxx_final`` feature, and the
  147. # ``ClimbingStats_CONSTEXPR`` macro will expand to ``constexpr``
  148. # if ``cxx_constexpr`` is supported.
  149. #
  150. # The following features generate corresponding symbol defines:
  151. #
  152. # ========================== =================================== =================
  153. # Feature Define Symbol
  154. # ========================== =================================== =================
  155. # ``c_restrict`` ``<PREFIX>_RESTRICT`` ``restrict``
  156. # ``cxx_constexpr`` ``<PREFIX>_CONSTEXPR`` ``constexpr``
  157. # ``cxx_deleted_functions`` ``<PREFIX>_DELETED_FUNCTION`` ``= delete``
  158. # ``cxx_extern_templates`` ``<PREFIX>_EXTERN_TEMPLATE`` ``extern``
  159. # ``cxx_final`` ``<PREFIX>_FINAL`` ``final``
  160. # ``cxx_noexcept`` ``<PREFIX>_NOEXCEPT`` ``noexcept``
  161. # ``cxx_noexcept`` ``<PREFIX>_NOEXCEPT_EXPR(X)`` ``noexcept(X)``
  162. # ``cxx_override`` ``<PREFIX>_OVERRIDE`` ``override``
  163. # ========================== =================================== =================
  164. #
  165. # Compatibility Implementation Macros
  166. # ===================================
  167. #
  168. # Some features are suitable for wrapping in a macro with a backward
  169. # compatibility implementation if the compiler does not support the feature.
  170. #
  171. # When the ``cxx_static_assert`` feature is not provided by the compiler,
  172. # a compatibility implementation is available via the
  173. # ``<PREFIX>_STATIC_ASSERT(COND)`` and
  174. # ``<PREFIX>_STATIC_ASSERT_MSG(COND, MSG)`` function-like macros. The macros
  175. # expand to ``static_assert`` where that compiler feature is available, and
  176. # to a compatibility implementation otherwise. In the first form, the
  177. # condition is stringified in the message field of ``static_assert``. In
  178. # the second form, the message ``MSG`` is passed to the message field of
  179. # ``static_assert``, or ignored if using the backward compatibility
  180. # implementation.
  181. #
  182. # The ``cxx_attribute_deprecated`` feature provides a macro definition
  183. # ``<PREFIX>_DEPRECATED``, which expands to either the standard
  184. # ``[[deprecated]]`` attribute or a compiler-specific decorator such
  185. # as ``__attribute__((__deprecated__))`` used by GNU compilers.
  186. #
  187. # The ``cxx_alignas`` feature provides a macro definition
  188. # ``<PREFIX>_ALIGNAS`` which expands to either the standard ``alignas``
  189. # decorator or a compiler-specific decorator such as
  190. # ``__attribute__ ((__aligned__))`` used by GNU compilers.
  191. #
  192. # The ``cxx_alignof`` feature provides a macro definition
  193. # ``<PREFIX>_ALIGNOF`` which expands to either the standard ``alignof``
  194. # decorator or a compiler-specific decorator such as ``__alignof__``
  195. # used by GNU compilers.
  196. #
  197. # ============================= ================================ =====================
  198. # Feature Define Symbol
  199. # ============================= ================================ =====================
  200. # ``cxx_alignas`` ``<PREFIX>_ALIGNAS`` ``alignas``
  201. # ``cxx_alignof`` ``<PREFIX>_ALIGNOF`` ``alignof``
  202. # ``cxx_nullptr`` ``<PREFIX>_NULLPTR`` ``nullptr``
  203. # ``cxx_static_assert`` ``<PREFIX>_STATIC_ASSERT`` ``static_assert``
  204. # ``cxx_static_assert`` ``<PREFIX>_STATIC_ASSERT_MSG`` ``static_assert``
  205. # ``cxx_attribute_deprecated`` ``<PREFIX>_DEPRECATED`` ``[[deprecated]]``
  206. # ``cxx_attribute_deprecated`` ``<PREFIX>_DEPRECATED_MSG`` ``[[deprecated]]``
  207. # ``cxx_thread_local`` ``<PREFIX>_THREAD_LOCAL`` ``thread_local``
  208. # ============================= ================================ =====================
  209. #
  210. # A use-case which arises with such deprecation macros is the deprecation
  211. # of an entire library. In that case, all public API in the library may
  212. # be decorated with the ``<PREFIX>_DEPRECATED`` macro. This results in
  213. # very noisy build output when building the library itself, so the macro
  214. # may be may be defined to empty in that case when building the deprecated
  215. # library:
  216. #
  217. # .. code-block:: cmake
  218. #
  219. # add_library(compat_support ${srcs})
  220. # target_compile_definitions(compat_support
  221. # PRIVATE
  222. # CompatSupport_DEPRECATED=
  223. # )
  224. include(${CMAKE_CURRENT_LIST_DIR}/CMakeCompilerIdDetection.cmake)
  225. function(_load_compiler_variables CompilerId lang)
  226. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-${lang}-FeatureTests.cmake" OPTIONAL)
  227. set(_cmake_oldestSupported_${CompilerId} ${_cmake_oldestSupported} PARENT_SCOPE)
  228. foreach(feature ${ARGN})
  229. set(_cmake_feature_test_${CompilerId}_${feature} ${_cmake_feature_test_${feature}} PARENT_SCOPE)
  230. endforeach()
  231. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-${lang}-DetermineCompiler.cmake" OPTIONAL
  232. RESULT_VARIABLE determinedCompiler)
  233. if (NOT determinedCompiler)
  234. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-DetermineCompiler.cmake" OPTIONAL)
  235. endif()
  236. set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE)
  237. endfunction()
  238. macro(_simpledefine FEATURE_NAME FEATURE_TESTNAME FEATURE_STRING FEATURE_DEFAULT_STRING)
  239. if (feature STREQUAL "${FEATURE_NAME}")
  240. set(def_value "${prefix_arg}_${FEATURE_TESTNAME}")
  241. string(APPEND file_content "
  242. # if defined(${def_name}) && ${def_name}
  243. # define ${def_value} ${FEATURE_STRING}
  244. # else
  245. # define ${def_value} ${FEATURE_DEFAULT_STRING}
  246. # endif
  247. \n")
  248. endif()
  249. endmacro()
  250. function(write_compiler_detection_header
  251. file_keyword file_arg
  252. prefix_keyword prefix_arg
  253. )
  254. if (NOT "x${file_keyword}" STREQUAL "xFILE")
  255. message(FATAL_ERROR "write_compiler_detection_header: FILE parameter missing.")
  256. endif()
  257. if (NOT "x${prefix_keyword}" STREQUAL "xPREFIX")
  258. message(FATAL_ERROR "write_compiler_detection_header: PREFIX parameter missing.")
  259. endif()
  260. set(options ALLOW_UNKNOWN_COMPILERS ALLOW_UNKNOWN_COMPILER_VERSIONS)
  261. set(oneValueArgs VERSION EPILOG PROLOG OUTPUT_FILES_VAR OUTPUT_DIR)
  262. set(multiValueArgs COMPILERS FEATURES)
  263. cmake_parse_arguments(_WCD "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  264. if (NOT _WCD_COMPILERS)
  265. message(FATAL_ERROR "Invalid arguments. write_compiler_detection_header requires at least one compiler.")
  266. endif()
  267. if (NOT _WCD_FEATURES)
  268. message(FATAL_ERROR "Invalid arguments. write_compiler_detection_header requires at least one feature.")
  269. endif()
  270. if(_WCD_UNPARSED_ARGUMENTS)
  271. message(FATAL_ERROR "Unparsed arguments: ${_WCD_UNPARSED_ARGUMENTS}")
  272. endif()
  273. if (prefix_arg STREQUAL "")
  274. message(FATAL_ERROR "A prefix must be specified")
  275. endif()
  276. string(MAKE_C_IDENTIFIER ${prefix_arg} cleaned_prefix)
  277. if (NOT prefix_arg STREQUAL cleaned_prefix)
  278. message(FATAL_ERROR "The prefix must be a valid C identifier.")
  279. endif()
  280. if(NOT _WCD_VERSION)
  281. set(_WCD_VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION})
  282. endif()
  283. set(_min_version 3.1.0) # Version which introduced this function
  284. if (_WCD_VERSION VERSION_LESS _min_version)
  285. set(err "VERSION compatibility for write_compiler_detection_header is set to ${_WCD_VERSION}, which is too low.")
  286. string(APPEND err " It must be set to at least ${_min_version}. ")
  287. string(APPEND err " Either set the VERSION parameter to the write_compiler_detection_header function, or update")
  288. string(APPEND err " your minimum required CMake version with the cmake_minimum_required command.")
  289. message(FATAL_ERROR "${err}")
  290. endif()
  291. if(_WCD_OUTPUT_FILES_VAR)
  292. if(NOT _WCD_OUTPUT_DIR)
  293. message(FATAL_ERROR "If OUTPUT_FILES_VAR is specified, then OUTPUT_DIR must also be specified.")
  294. endif()
  295. endif()
  296. if(_WCD_OUTPUT_DIR)
  297. if(NOT _WCD_OUTPUT_FILES_VAR)
  298. message(FATAL_ERROR "If OUTPUT_DIR is specified, then OUTPUT_FILES_VAR must also be specified.")
  299. endif()
  300. get_filename_component(main_file_dir ${file_arg} DIRECTORY)
  301. if (NOT IS_ABSOLUTE ${main_file_dir})
  302. set(main_file_dir "${CMAKE_CURRENT_BINARY_DIR}/${main_file_dir}")
  303. endif()
  304. if (NOT IS_ABSOLUTE ${_WCD_OUTPUT_DIR})
  305. set(_WCD_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_WCD_OUTPUT_DIR}")
  306. endif()
  307. get_filename_component(out_file_dir ${_WCD_OUTPUT_DIR} ABSOLUTE)
  308. string(FIND ${out_file_dir} ${main_file_dir} idx)
  309. if (NOT idx EQUAL 0)
  310. message(FATAL_ERROR "The compiler-specific output directory must be within the same directory as the main file.")
  311. endif()
  312. if (main_file_dir STREQUAL out_file_dir)
  313. unset(_WCD_OUTPUT_DIR)
  314. else()
  315. string(REPLACE "${main_file_dir}/" "" _WCD_OUTPUT_DIR "${out_file_dir}/")
  316. endif()
  317. endif()
  318. set(compilers
  319. GNU
  320. Clang
  321. AppleClang
  322. MSVC
  323. SunPro
  324. Intel
  325. )
  326. set(_hex_compilers ADSP Borland Embarcadero SunPro)
  327. foreach(_comp ${_WCD_COMPILERS})
  328. list(FIND compilers ${_comp} idx)
  329. if (idx EQUAL -1)
  330. message(FATAL_ERROR "Unsupported compiler ${_comp}.")
  331. endif()
  332. if (NOT _need_hex_conversion)
  333. list(FIND _hex_compilers ${_comp} idx)
  334. if (NOT idx EQUAL -1)
  335. set(_need_hex_conversion TRUE)
  336. endif()
  337. endif()
  338. endforeach()
  339. set(file_content "
  340. // This is a generated file. Do not edit!
  341. #ifndef ${prefix_arg}_COMPILER_DETECTION_H
  342. #define ${prefix_arg}_COMPILER_DETECTION_H
  343. ")
  344. if (_WCD_PROLOG)
  345. string(APPEND file_content "\n${_WCD_PROLOG}\n")
  346. endif()
  347. if (_need_hex_conversion)
  348. string(APPEND file_content "
  349. #define ${prefix_arg}_DEC(X) (X)
  350. #define ${prefix_arg}_HEX(X) ( \\
  351. ((X)>>28 & 0xF) * 10000000 + \\
  352. ((X)>>24 & 0xF) * 1000000 + \\
  353. ((X)>>20 & 0xF) * 100000 + \\
  354. ((X)>>16 & 0xF) * 10000 + \\
  355. ((X)>>12 & 0xF) * 1000 + \\
  356. ((X)>>8 & 0xF) * 100 + \\
  357. ((X)>>4 & 0xF) * 10 + \\
  358. ((X) & 0xF) \\
  359. )\n")
  360. endif()
  361. foreach(feature ${_WCD_FEATURES})
  362. if (feature MATCHES "^c_std_")
  363. # ignored
  364. elseif (feature MATCHES "^cxx_std_")
  365. # ignored
  366. elseif (feature MATCHES "^cxx_")
  367. list(APPEND _langs CXX)
  368. list(APPEND CXX_features ${feature})
  369. elseif (feature MATCHES "^c_")
  370. list(APPEND _langs C)
  371. list(APPEND C_features ${feature})
  372. else()
  373. message(FATAL_ERROR "Unsupported feature ${feature}.")
  374. endif()
  375. endforeach()
  376. list(REMOVE_DUPLICATES _langs)
  377. if(_WCD_OUTPUT_FILES_VAR)
  378. get_filename_component(main_file_name ${file_arg} NAME)
  379. set(compiler_file_content_
  380. "#ifndef ${prefix_arg}_COMPILER_DETECTION_H
  381. # error This file may only be included from ${main_file_name}
  382. #endif\n")
  383. endif()
  384. foreach(_lang ${_langs})
  385. set(target_compilers)
  386. foreach(compiler ${_WCD_COMPILERS})
  387. _load_compiler_variables(${compiler} ${_lang} ${${_lang}_features})
  388. if(_cmake_oldestSupported_${compiler})
  389. list(APPEND target_compilers ${compiler})
  390. endif()
  391. endforeach()
  392. get_property(known_features GLOBAL PROPERTY CMAKE_${_lang}_KNOWN_FEATURES)
  393. foreach(feature ${${_lang}_features})
  394. list(FIND known_features ${feature} idx)
  395. if (idx EQUAL -1)
  396. message(FATAL_ERROR "Unsupported feature ${feature}.")
  397. endif()
  398. endforeach()
  399. if(_lang STREQUAL CXX)
  400. string(APPEND file_content "\n#ifdef __cplusplus\n")
  401. else()
  402. string(APPEND file_content "\n#ifndef __cplusplus\n")
  403. endif()
  404. compiler_id_detection(ID_CONTENT ${_lang} PREFIX ${prefix_arg}_
  405. ID_DEFINE
  406. )
  407. string(APPEND file_content "${ID_CONTENT}\n")
  408. set(pp_if "if")
  409. foreach(compiler ${target_compilers})
  410. string(APPEND file_content "\n# ${pp_if} ${prefix_arg}_COMPILER_IS_${compiler}\n")
  411. if(_WCD_OUTPUT_FILES_VAR)
  412. set(compile_file_name "${_WCD_OUTPUT_DIR}${prefix_arg}_COMPILER_INFO_${compiler}_${_lang}.h")
  413. string(APPEND file_content "\n# include \"${compile_file_name}\"\n")
  414. endif()
  415. if(_WCD_OUTPUT_FILES_VAR)
  416. set(compiler_file_content compiler_file_content_${compiler}_${_lang})
  417. else()
  418. set(compiler_file_content file_content)
  419. endif()
  420. if(NOT _WCD_ALLOW_UNKNOWN_COMPILER_VERSIONS)
  421. string(APPEND ${compiler_file_content} "
  422. # if !(${_cmake_oldestSupported_${compiler}})
  423. # error Unsupported compiler version
  424. # endif\n")
  425. endif()
  426. set(PREFIX ${prefix_arg}_)
  427. if (_need_hex_conversion)
  428. set(MACRO_DEC ${prefix_arg}_DEC)
  429. set(MACRO_HEX ${prefix_arg}_HEX)
  430. else()
  431. set(MACRO_DEC)
  432. set(MACRO_HEX)
  433. endif()
  434. string(CONFIGURE "${_compiler_id_version_compute_${compiler}}" VERSION_BLOCK @ONLY)
  435. string(APPEND ${compiler_file_content} "${VERSION_BLOCK}\n")
  436. set(PREFIX)
  437. set(MACRO_DEC)
  438. set(MACRO_HEX)
  439. set(pp_if "elif")
  440. foreach(feature ${${_lang}_features})
  441. string(TOUPPER ${feature} feature_upper)
  442. set(feature_PP "COMPILER_${feature_upper}")
  443. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 0\n")
  444. if (_cmake_feature_test_${compiler}_${feature} STREQUAL "1")
  445. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 1\n")
  446. elseif (_cmake_feature_test_${compiler}_${feature})
  447. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 0\n")
  448. set(_define_item "\n# if ${_cmake_feature_test_${compiler}_${feature}}\n# define ${prefix_arg}_${feature_PP} 1\n# else${_define_item}# endif\n")
  449. endif()
  450. string(APPEND ${compiler_file_content} "${_define_item}")
  451. endforeach()
  452. endforeach()
  453. if(pp_if STREQUAL "elif")
  454. if(_WCD_ALLOW_UNKNOWN_COMPILERS)
  455. string(APPEND file_content "
  456. # endif\n")
  457. else()
  458. string(APPEND file_content "
  459. # else
  460. # error Unsupported compiler
  461. # endif\n")
  462. endif()
  463. endif()
  464. foreach(feature ${${_lang}_features})
  465. string(TOUPPER ${feature} feature_upper)
  466. set(feature_PP "COMPILER_${feature_upper}")
  467. set(def_name ${prefix_arg}_${feature_PP})
  468. _simpledefine(c_restrict RESTRICT restrict "")
  469. _simpledefine(cxx_constexpr CONSTEXPR constexpr "")
  470. _simpledefine(cxx_final FINAL final "")
  471. _simpledefine(cxx_override OVERRIDE override "")
  472. if (feature STREQUAL cxx_static_assert)
  473. set(def_value "${prefix_arg}_STATIC_ASSERT(X)")
  474. set(def_value_msg "${prefix_arg}_STATIC_ASSERT_MSG(X, MSG)")
  475. set(def_fallback "enum { ${prefix_arg}_STATIC_ASSERT_JOIN(${prefix_arg}StaticAssertEnum, __LINE__) = sizeof(${prefix_arg}StaticAssert<X>) }")
  476. string(APPEND file_content "# if defined(${def_name}) && ${def_name}
  477. # define ${def_value} static_assert(X, #X)
  478. # define ${def_value_msg} static_assert(X, MSG)
  479. # else
  480. # define ${prefix_arg}_STATIC_ASSERT_JOIN(X, Y) ${prefix_arg}_STATIC_ASSERT_JOIN_IMPL(X, Y)
  481. # define ${prefix_arg}_STATIC_ASSERT_JOIN_IMPL(X, Y) X##Y
  482. template<bool> struct ${prefix_arg}StaticAssert;
  483. template<> struct ${prefix_arg}StaticAssert<true>{};
  484. # define ${def_value} ${def_fallback}
  485. # define ${def_value_msg} ${def_fallback}
  486. # endif
  487. \n")
  488. endif()
  489. if (feature STREQUAL cxx_alignas)
  490. set(def_value "${prefix_arg}_ALIGNAS(X)")
  491. string(APPEND file_content "
  492. # if defined(${def_name}) && ${def_name}
  493. # define ${def_value} alignas(X)
  494. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  495. # define ${def_value} __attribute__ ((__aligned__(X)))
  496. # elif ${prefix_arg}_COMPILER_IS_MSVC
  497. # define ${def_value} __declspec(align(X))
  498. # else
  499. # define ${def_value}
  500. # endif
  501. \n")
  502. endif()
  503. if (feature STREQUAL cxx_alignof)
  504. set(def_value "${prefix_arg}_ALIGNOF(X)")
  505. string(APPEND file_content "
  506. # if defined(${def_name}) && ${def_name}
  507. # define ${def_value} alignof(X)
  508. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  509. # define ${def_value} __alignof__(X)
  510. # elif ${prefix_arg}_COMPILER_IS_MSVC
  511. # define ${def_value} __alignof(X)
  512. # endif
  513. \n")
  514. endif()
  515. _simpledefine(cxx_deleted_functions DELETED_FUNCTION "= delete" "")
  516. _simpledefine(cxx_extern_templates EXTERN_TEMPLATE extern "")
  517. if (feature STREQUAL cxx_noexcept)
  518. set(def_value "${prefix_arg}_NOEXCEPT")
  519. string(APPEND file_content "
  520. # if defined(${def_name}) && ${def_name}
  521. # define ${def_value} noexcept
  522. # define ${def_value}_EXPR(X) noexcept(X)
  523. # else
  524. # define ${def_value}
  525. # define ${def_value}_EXPR(X)
  526. # endif
  527. \n")
  528. endif()
  529. _simpledefine(cxx_nullptr NULLPTR nullptr 0)
  530. if (feature STREQUAL cxx_thread_local)
  531. set(def_value "${prefix_arg}_THREAD_LOCAL")
  532. string(APPEND file_content "
  533. # if defined(${def_name}) && ${def_name}
  534. # define ${def_value} thread_local
  535. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  536. # define ${def_value} __thread
  537. # elif ${prefix_arg}_COMPILER_IS_MSVC
  538. # define ${def_value} __declspec(thread)
  539. # else
  540. // ${def_value} not defined for this configuration.
  541. # endif
  542. \n")
  543. endif()
  544. if (feature STREQUAL cxx_attribute_deprecated)
  545. set(def_name ${prefix_arg}_${feature_PP})
  546. set(def_value "${prefix_arg}_DEPRECATED")
  547. string(APPEND file_content "
  548. # ifndef ${def_value}
  549. # if defined(${def_name}) && ${def_name}
  550. # define ${def_value} [[deprecated]]
  551. # define ${def_value}_MSG(MSG) [[deprecated(MSG)]]
  552. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang
  553. # define ${def_value} __attribute__((__deprecated__))
  554. # define ${def_value}_MSG(MSG) __attribute__((__deprecated__(MSG)))
  555. # elif ${prefix_arg}_COMPILER_IS_MSVC
  556. # define ${def_value} __declspec(deprecated)
  557. # define ${def_value}_MSG(MSG) __declspec(deprecated(MSG))
  558. # else
  559. # define ${def_value}
  560. # define ${def_value}_MSG(MSG)
  561. # endif
  562. # endif
  563. \n")
  564. endif()
  565. endforeach()
  566. string(APPEND file_content "#endif\n")
  567. endforeach()
  568. if(_WCD_OUTPUT_FILES_VAR)
  569. foreach(compiler ${_WCD_COMPILERS})
  570. foreach(_lang ${_langs})
  571. if(compiler_file_content_${compiler}_${_lang})
  572. set(CMAKE_CONFIGURABLE_FILE_CONTENT "${compiler_file_content_}")
  573. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT "${compiler_file_content_${compiler}_${_lang}}")
  574. set(compile_file_name "${_WCD_OUTPUT_DIR}${prefix_arg}_COMPILER_INFO_${compiler}_${_lang}.h")
  575. set(full_path "${main_file_dir}/${compile_file_name}")
  576. list(APPEND ${_WCD_OUTPUT_FILES_VAR} ${full_path})
  577. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  578. "${full_path}"
  579. @ONLY
  580. )
  581. endif()
  582. endforeach()
  583. endforeach()
  584. set(${_WCD_OUTPUT_FILES_VAR} ${${_WCD_OUTPUT_FILES_VAR}} PARENT_SCOPE)
  585. endif()
  586. if (_WCD_EPILOG)
  587. string(APPEND file_content "\n${_WCD_EPILOG}\n")
  588. endif()
  589. string(APPEND file_content "\n#endif")
  590. set(CMAKE_CONFIGURABLE_FILE_CONTENT ${file_content})
  591. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  592. "${file_arg}"
  593. @ONLY
  594. )
  595. endfunction()