WriteCompilerDetectionHeader.cmake 24 KB

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