WriteCompilerDetectionHeader.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. # ``cxx_thread_local`` ``<PREFIX>_THREAD_LOCAL`` ``thread_local``
  198. # ============================= ================================ =====================
  199. #
  200. # A use-case which arises with such deprecation macros is the deprecation
  201. # of an entire library. In that case, all public API in the library may
  202. # be decorated with the ``<PREFIX>_DEPRECATED`` macro. This results in
  203. # very noisy build output when building the library itself, so the macro
  204. # may be may be defined to empty in that case when building the deprecated
  205. # library:
  206. #
  207. # .. code-block:: cmake
  208. #
  209. # add_library(compat_support ${srcs})
  210. # target_compile_definitions(compat_support
  211. # PRIVATE
  212. # CompatSupport_DEPRECATED=
  213. # )
  214. #=============================================================================
  215. # Copyright 2014 Stephen Kelly <[email protected]>
  216. #
  217. # Distributed under the OSI-approved BSD License (the "License");
  218. # see accompanying file Copyright.txt for details.
  219. #
  220. # This software is distributed WITHOUT ANY WARRANTY; without even the
  221. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  222. # See the License for more information.
  223. #=============================================================================
  224. # (To distribute this file outside of CMake, substitute the full
  225. # License text for the above reference.)
  226. include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
  227. include(${CMAKE_CURRENT_LIST_DIR}/CMakeCompilerIdDetection.cmake)
  228. function(_load_compiler_variables CompilerId lang)
  229. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-${lang}-FeatureTests.cmake" OPTIONAL)
  230. set(_cmake_oldestSupported_${CompilerId} ${_cmake_oldestSupported} PARENT_SCOPE)
  231. foreach(feature ${ARGN})
  232. set(_cmake_feature_test_${CompilerId}_${feature} ${_cmake_feature_test_${feature}} PARENT_SCOPE)
  233. endforeach()
  234. include("${CMAKE_ROOT}/Modules/Compiler/${CompilerId}-DetermineCompiler.cmake" OPTIONAL)
  235. set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE)
  236. endfunction()
  237. function(write_compiler_detection_header
  238. file_keyword file_arg
  239. prefix_keyword prefix_arg
  240. )
  241. if (NOT file_keyword STREQUAL FILE)
  242. message(FATAL_ERROR "write_compiler_detection_header: FILE parameter missing.")
  243. endif()
  244. if (NOT prefix_keyword STREQUAL PREFIX)
  245. message(FATAL_ERROR "write_compiler_detection_header: PREFIX parameter missing.")
  246. endif()
  247. set(options)
  248. set(oneValueArgs VERSION EPILOG PROLOG OUTPUT_FILES_VAR OUTPUT_DIR)
  249. set(multiValueArgs COMPILERS FEATURES)
  250. cmake_parse_arguments(_WCD "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  251. if (NOT _WCD_COMPILERS)
  252. message(FATAL_ERROR "Invalid arguments. write_compiler_detection_header requires at least one compiler.")
  253. endif()
  254. if (NOT _WCD_FEATURES)
  255. message(FATAL_ERROR "Invalid arguments. write_compiler_detection_header requires at least one feature.")
  256. endif()
  257. if(_WCD_UNPARSED_ARGUMENTS)
  258. message(FATAL_ERROR "Unparsed arguments: ${_WCD_UNPARSED_ARGUMENTS}")
  259. endif()
  260. if (prefix_arg STREQUAL "")
  261. message(FATAL_ERROR "A prefix must be specified")
  262. endif()
  263. string(MAKE_C_IDENTIFIER ${prefix_arg} cleaned_prefix)
  264. if (NOT prefix_arg STREQUAL cleaned_prefix)
  265. message(FATAL_ERROR "The prefix must be a valid C identifier.")
  266. endif()
  267. if(NOT _WCD_VERSION)
  268. set(_WCD_VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION})
  269. endif()
  270. set(_min_version 3.1.0) # Version which introduced this function
  271. if (_WCD_VERSION VERSION_LESS _min_version)
  272. set(err "VERSION compatibility for write_compiler_detection_header is set to ${_WCD_VERSION}, which is too low.")
  273. set(err "${err} It must be set to at least ${_min_version}. ")
  274. set(err "${err} Either set the VERSION parameter to the write_compiler_detection_header function, or update")
  275. set(err "${err} your minimum required CMake version with the cmake_minimum_required command.")
  276. message(FATAL_ERROR "${err}")
  277. endif()
  278. if(_WCD_OUTPUT_FILES_VAR)
  279. if(NOT _WCD_OUTPUT_DIR)
  280. message(FATAL_ERROR "If OUTPUT_FILES_VAR is specified, then OUTPUT_DIR must also be specified.")
  281. endif()
  282. endif()
  283. if(_WCD_OUTPUT_DIR)
  284. if(NOT _WCD_OUTPUT_FILES_VAR)
  285. message(FATAL_ERROR "If OUTPUT_DIR is specified, then OUTPUT_FILES_VAR must also be specified.")
  286. endif()
  287. get_filename_component(main_file_dir ${file_arg} DIRECTORY)
  288. if (NOT IS_ABSOLUTE ${main_file_dir})
  289. set(main_file_dir "${CMAKE_CURRENT_BINARY_DIR}/${main_file_dir}")
  290. endif()
  291. if (NOT IS_ABSOLUTE ${_WCD_OUTPUT_DIR})
  292. set(_WCD_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${_WCD_OUTPUT_DIR}")
  293. endif()
  294. get_filename_component(out_file_dir ${_WCD_OUTPUT_DIR} ABSOLUTE)
  295. string(FIND ${out_file_dir} ${main_file_dir} idx)
  296. if (NOT idx EQUAL 0)
  297. message(FATAL_ERROR "The compiler-specific output directory must be within the same directory as the main file.")
  298. endif()
  299. if (main_file_dir STREQUAL out_file_dir)
  300. unset(_WCD_OUTPUT_DIR)
  301. else()
  302. string(REPLACE "${main_file_dir}/" "" _WCD_OUTPUT_DIR "${out_file_dir}/")
  303. endif()
  304. endif()
  305. set(compilers
  306. GNU
  307. Clang
  308. AppleClang
  309. )
  310. set(_hex_compilers ADSP Borland Embarcadero SunPro)
  311. foreach(_comp ${_WCD_COMPILERS})
  312. list(FIND compilers ${_comp} idx)
  313. if (idx EQUAL -1)
  314. message(FATAL_ERROR "Unsupported compiler ${_comp}.")
  315. endif()
  316. if (NOT _need_hex_conversion)
  317. list(FIND _hex_compilers ${_comp} idx)
  318. if (NOT idx EQUAL -1)
  319. set(_need_hex_conversion TRUE)
  320. endif()
  321. endif()
  322. endforeach()
  323. set(file_content "
  324. // This is a generated file. Do not edit!
  325. #ifndef ${prefix_arg}_COMPILER_DETECTION_H
  326. #define ${prefix_arg}_COMPILER_DETECTION_H
  327. ")
  328. if (_WCD_PROLOG)
  329. set(file_content "${file_content}\n${_WCD_PROLOG}\n")
  330. endif()
  331. if (_need_hex_conversion)
  332. set(file_content "${file_content}
  333. #define ${prefix_arg}_DEC(X) (X)
  334. #define ${prefix_arg}_HEX(X) ( \\
  335. ((X)>>28 & 0xF) * 10000000 + \\
  336. ((X)>>24 & 0xF) * 1000000 + \\
  337. ((X)>>20 & 0xF) * 100000 + \\
  338. ((X)>>16 & 0xF) * 10000 + \\
  339. ((X)>>12 & 0xF) * 1000 + \\
  340. ((X)>>8 & 0xF) * 100 + \\
  341. ((X)>>4 & 0xF) * 10 + \\
  342. ((X) & 0xF) \\
  343. )\n")
  344. endif()
  345. foreach(feature ${_WCD_FEATURES})
  346. if (feature MATCHES "^cxx_")
  347. list(APPEND _langs CXX)
  348. list(APPEND CXX_features ${feature})
  349. elseif (feature MATCHES "^c_")
  350. list(APPEND _langs C)
  351. list(APPEND C_features ${feature})
  352. else()
  353. message(FATAL_ERROR "Unsupported feature ${feature}.")
  354. endif()
  355. endforeach()
  356. list(REMOVE_DUPLICATES _langs)
  357. if(_WCD_OUTPUT_FILES_VAR)
  358. get_filename_component(main_file_name ${file_arg} NAME)
  359. set(compiler_file_content_
  360. "#ifndef ${prefix_arg}_COMPILER_DETECTION_H
  361. # error This file may only be included from ${main_file_name}
  362. #endif\n")
  363. endif()
  364. foreach(_lang ${_langs})
  365. get_property(known_features GLOBAL PROPERTY CMAKE_${_lang}_KNOWN_FEATURES)
  366. foreach(feature ${${_lang}_features})
  367. list(FIND known_features ${feature} idx)
  368. if (idx EQUAL -1)
  369. message(FATAL_ERROR "Unsupported feature ${feature}.")
  370. endif()
  371. endforeach()
  372. if(_lang STREQUAL CXX)
  373. set(file_content "${file_content}\n#ifdef __cplusplus\n")
  374. else()
  375. set(file_content "${file_content}\n#ifndef __cplusplus\n")
  376. endif()
  377. compiler_id_detection(ID_CONTENT ${_lang} PREFIX ${prefix_arg}_
  378. ID_DEFINE
  379. )
  380. set(file_content "${file_content}${ID_CONTENT}\n")
  381. set(pp_if "if")
  382. foreach(compiler ${_WCD_COMPILERS})
  383. _load_compiler_variables(${compiler} ${_lang} ${${_lang}_features})
  384. set(file_content "${file_content}\n# ${pp_if} ${prefix_arg}_COMPILER_IS_${compiler}\n")
  385. if(_WCD_OUTPUT_FILES_VAR)
  386. set(compile_file_name "${_WCD_OUTPUT_DIR}${prefix_arg}_COMPILER_INFO_${compiler}.h")
  387. set(file_content "${file_content}\n# include \"${compile_file_name}\"\n")
  388. endif()
  389. if(_WCD_OUTPUT_FILES_VAR)
  390. set(compiler_file_content compiler_file_content_${compiler})
  391. else()
  392. set(compiler_file_content file_content)
  393. endif()
  394. set(${compiler_file_content} "${${compiler_file_content}}
  395. # if !(${_cmake_oldestSupported_${compiler}})
  396. # error Unsupported compiler version
  397. # endif\n")
  398. set(PREFIX ${prefix_arg}_)
  399. if (_need_hex_conversion)
  400. set(MACRO_DEC ${prefix_arg}_DEC)
  401. set(MACRO_HEX ${prefix_arg}_HEX)
  402. else()
  403. set(MACRO_DEC)
  404. set(MACRO_HEX)
  405. endif()
  406. string(CONFIGURE "${_compiler_id_version_compute_${compiler}}" VERSION_BLOCK @ONLY)
  407. set(${compiler_file_content} "${${compiler_file_content}}${VERSION_BLOCK}\n")
  408. set(PREFIX)
  409. set(MACRO_DEC)
  410. set(MACRO_HEX)
  411. set(pp_if "elif")
  412. foreach(feature ${${_lang}_features})
  413. string(TOUPPER ${feature} feature_upper)
  414. set(feature_PP "COMPILER_${feature_upper}")
  415. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 0\n")
  416. if (_cmake_feature_test_${compiler}_${feature} STREQUAL "1")
  417. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 1\n")
  418. elseif (_cmake_feature_test_${compiler}_${feature})
  419. set(_define_item "\n# define ${prefix_arg}_${feature_PP} 0\n")
  420. set(_define_item "\n# if ${_cmake_feature_test_${compiler}_${feature}}\n# define ${prefix_arg}_${feature_PP} 1\n# else${_define_item}# endif\n")
  421. endif()
  422. set(${compiler_file_content} "${${compiler_file_content}}${_define_item}")
  423. endforeach()
  424. endforeach()
  425. if(pp_if STREQUAL "elif")
  426. set(file_content "${file_content}
  427. # else
  428. # error Unsupported compiler
  429. # endif\n")
  430. endif()
  431. foreach(feature ${${_lang}_features})
  432. string(TOUPPER ${feature} feature_upper)
  433. set(feature_PP "COMPILER_${feature_upper}")
  434. set(def_name ${prefix_arg}_${feature_PP})
  435. if (feature STREQUAL c_restrict)
  436. set(def_value "${prefix_arg}_RESTRICT")
  437. set(file_content "${file_content}
  438. # if ${def_name}
  439. # define ${def_value} restrict
  440. # else
  441. # define ${def_value}
  442. # endif
  443. \n")
  444. endif()
  445. if (feature STREQUAL cxx_constexpr)
  446. set(def_value "${prefix_arg}_CONSTEXPR")
  447. set(file_content "${file_content}
  448. # if ${def_name}
  449. # define ${def_value} constexpr
  450. # else
  451. # define ${def_value}
  452. # endif
  453. \n")
  454. endif()
  455. if (feature STREQUAL cxx_final)
  456. set(def_value "${prefix_arg}_FINAL")
  457. set(file_content "${file_content}
  458. # if ${def_name}
  459. # define ${def_value} final
  460. # else
  461. # define ${def_value}
  462. # endif
  463. \n")
  464. endif()
  465. if (feature STREQUAL cxx_override)
  466. set(def_value "${prefix_arg}_OVERRIDE")
  467. set(file_content "${file_content}
  468. # if ${def_name}
  469. # define ${def_value} override
  470. # else
  471. # define ${def_value}
  472. # endif
  473. \n")
  474. endif()
  475. if (feature STREQUAL cxx_static_assert)
  476. set(def_value "${prefix_arg}_STATIC_ASSERT(X)")
  477. set(def_value_msg "${prefix_arg}_STATIC_ASSERT_MSG(X, MSG)")
  478. set(static_assert_struct "template<bool> struct ${prefix_arg}StaticAssert;\ntemplate<> struct ${prefix_arg}StaticAssert<true>{};\n")
  479. set(def_standard "# define ${def_value} static_assert(X, #X)\n# define ${def_value_msg} static_assert(X, MSG)")
  480. set(def_alternative "${static_assert_struct}# define ${def_value} sizeof(${prefix_arg}StaticAssert<X>)\n# define ${def_value_msg} sizeof(${prefix_arg}StaticAssert<X>)")
  481. set(file_content "${file_content}# if ${def_name}\n${def_standard}\n# else\n${def_alternative}\n# endif\n\n")
  482. endif()
  483. if (feature STREQUAL cxx_alignas)
  484. set(def_value "${prefix_arg}_ALIGNAS(X)")
  485. set(file_content "${file_content}
  486. # if ${def_name}
  487. # define ${def_value} alignas(X)
  488. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  489. # define ${def_value} __attribute__ ((__aligned__(X)))
  490. # else
  491. # define ${def_value}
  492. # endif
  493. \n")
  494. endif()
  495. if (feature STREQUAL cxx_alignof)
  496. set(def_value "${prefix_arg}_ALIGNOF(X)")
  497. set(file_content "${file_content}
  498. # if ${def_name}
  499. # define ${def_value} alignof(X)
  500. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  501. # define ${def_value} __alignof__(X)
  502. # endif
  503. \n")
  504. endif()
  505. if (feature STREQUAL cxx_deleted_functions)
  506. set(def_value "${prefix_arg}_DELETED_FUNCTION")
  507. set(file_content "${file_content}
  508. # if ${def_name}
  509. # define ${def_value} = delete
  510. # else
  511. # define ${def_value}
  512. # endif
  513. \n")
  514. endif()
  515. if (feature STREQUAL cxx_extern_templates)
  516. set(def_value "${prefix_arg}_EXTERN_TEMPLATE")
  517. set(file_content "${file_content}
  518. # if ${def_name}
  519. # define ${def_value} extern
  520. # else
  521. # define ${def_value}
  522. # endif
  523. \n")
  524. endif()
  525. if (feature STREQUAL cxx_noexcept)
  526. set(def_value "${prefix_arg}_NOEXCEPT")
  527. set(file_content "${file_content}
  528. # if ${def_name}
  529. # define ${def_value} noexcept
  530. # define ${def_value}_EXPR(X) noexcept(X)
  531. # else
  532. # define ${def_value}
  533. # define ${def_value}_EXPR(X)
  534. # endif
  535. \n")
  536. endif()
  537. if (feature STREQUAL cxx_nullptr)
  538. set(def_value "${prefix_arg}_NULLPTR")
  539. set(file_content "${file_content}
  540. # if ${def_name}
  541. # define ${def_value} nullptr
  542. # else
  543. # define ${def_value} static_cast<void*>(0)
  544. # endif
  545. \n")
  546. endif()
  547. if (feature STREQUAL cxx_thread_local)
  548. set(def_value "${prefix_arg}_THREAD_LOCAL")
  549. set(file_content "${file_content}
  550. # if ${def_name}
  551. # define ${def_value} thread_local
  552. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang || ${prefix_arg}_COMPILER_IS_AppleClang
  553. # define ${def_value} __thread
  554. # elif ${prefix_arg}_COMPILER_IS_MSVC
  555. # define ${def_value} __declspec(thread)
  556. # else
  557. // ${def_value} not defined for this configuration.
  558. # endif
  559. \n")
  560. endif()
  561. if (feature STREQUAL cxx_attribute_deprecated)
  562. set(def_name ${prefix_arg}_${feature_PP})
  563. set(def_value "${prefix_arg}_DEPRECATED")
  564. set(file_content "${file_content}
  565. # ifndef ${def_value}
  566. # if ${def_name}
  567. # define ${def_value} [[deprecated]]
  568. # define ${def_value}_MSG(MSG) [[deprecated(MSG)]]
  569. # elif ${prefix_arg}_COMPILER_IS_GNU || ${prefix_arg}_COMPILER_IS_Clang
  570. # define ${def_value} __attribute__((__deprecated__))
  571. # define ${def_value}_MSG(MSG) __attribute__((__deprecated__(MSG)))
  572. # elif ${prefix_arg}_COMPILER_IS_MSVC
  573. # define ${def_value} __declspec(deprecated)
  574. # define ${def_value}_MSG(MSG) __declspec(deprecated(MSG))
  575. # else
  576. # define ${def_value}
  577. # define ${def_value}_MSG(MSG)
  578. # endif
  579. # endif
  580. \n")
  581. endif()
  582. endforeach()
  583. set(file_content "${file_content}#endif\n")
  584. endforeach()
  585. if(_WCD_OUTPUT_FILES_VAR)
  586. foreach(compiler ${_WCD_COMPILERS})
  587. set(CMAKE_CONFIGURABLE_FILE_CONTENT "${compiler_file_content_}")
  588. set(CMAKE_CONFIGURABLE_FILE_CONTENT "${CMAKE_CONFIGURABLE_FILE_CONTENT}${compiler_file_content_${compiler}}")
  589. set(compile_file_name "${_WCD_OUTPUT_DIR}${prefix_arg}_COMPILER_INFO_${compiler}.h")
  590. set(full_path "${main_file_dir}/${compile_file_name}")
  591. list(APPEND ${_WCD_OUTPUT_FILES_VAR} ${full_path})
  592. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  593. "${full_path}"
  594. @ONLY
  595. )
  596. endforeach()
  597. set(${_WCD_OUTPUT_FILES_VAR} ${${_WCD_OUTPUT_FILES_VAR}} PARENT_SCOPE)
  598. endif()
  599. if (_WCD_EPILOG)
  600. set(file_content "${file_content}\n${_WCD_EPILOG}\n")
  601. endif()
  602. set(file_content "${file_content}\n#endif")
  603. set(CMAKE_CONFIGURABLE_FILE_CONTENT ${file_content})
  604. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  605. "${file_arg}"
  606. @ONLY
  607. )
  608. endfunction()