WriteCompilerDetectionHeader.cmake 23 KB

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