GenerateExportHeader.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. GenerateExportHeader
  5. --------------------
  6. This module provides commands for generating a header file containing
  7. preprocessor macro definitions to control C/C++ symbol visibility.
  8. Load this module in CMake project with:
  9. .. code-block:: cmake
  10. include(GenerateExportHeader)
  11. .. versionadded:: 3.12
  12. Support for C projects. Previous versions supported C++ projects only.
  13. When developing C or C++ projects, especially for cross-platform use, symbol
  14. visibility determines which functions, classes, global variables, templates,
  15. and other symbols are made visible to users of the library.
  16. For example, on Windows, symbols must be explicitly marked with
  17. ``__declspec(dllexport)`` when building a shared library, and
  18. ``__declspec(dllimport)`` when using it. Other platforms may use attributes
  19. like ``__attribute__((visibility("default")))``.
  20. This module simplifies the creation and usage of preprocessor macros to
  21. manage these requirements, avoiding repetitive and error-prone ``#ifdef``
  22. blocks in source code.
  23. Some symbol visibility can also be controlled with compiler options. In
  24. CMake, target properties such as :prop_tgt:`<LANG>_VISIBILITY_PRESET` and
  25. :prop_tgt:`VISIBILITY_INLINES_HIDDEN` enable compiler visibility flags,
  26. where appropriate. See also related convenience variables
  27. :variable:`CMAKE_<LANG>_VISIBILITY_PRESET` and
  28. :variable:`CMAKE_VISIBILITY_INLINES_HIDDEN` to enable it for all targets in
  29. current scope. These are commonly used in combination with this module to
  30. further simplify C/C++ code, removing the need for some of the preprocessor
  31. macros in the source code.
  32. Commands
  33. ^^^^^^^^
  34. This module provides the following commands:
  35. Generating Export Header
  36. """"""""""""""""""""""""
  37. .. command:: generate_export_header
  38. Generates a header file suitable for inclusion in source code, containing
  39. preprocessor *export* macros for controlling the visibility of symbols:
  40. .. code-block:: cmake
  41. generate_export_header(
  42. <target>
  43. [BASE_NAME <base-name>]
  44. [EXPORT_FILE_NAME <export-file-name>]
  45. [EXPORT_MACRO_NAME <export-macro-name>]
  46. [NO_EXPORT_MACRO_NAME <no-export-macro-name>]
  47. [DEPRECATED_MACRO_NAME <deprecated-macro-name>]
  48. [DEFINE_NO_DEPRECATED]
  49. [NO_DEPRECATED_MACRO_NAME <no-deprecated-macro-name>]
  50. [STATIC_DEFINE <static-define>]
  51. [PREFIX_NAME <prefix>]
  52. [CUSTOM_CONTENT_FROM_VARIABLE <variable>]
  53. [INCLUDE_GUARD_NAME <include-guard-name>]
  54. )
  55. By default, this command generates a header file named
  56. ``<target-name-lowercase>_export.h`` in the current binary directory
  57. (:variable:`CMAKE_CURRENT_BINARY_DIR`). This header defines a set of
  58. preprocessor macros used to mark API symbols as exported, hidden, or
  59. deprecated across different platforms and build types (e.g., static or
  60. shared builds), and is intended to be installed along with the library's
  61. public headers, because it affects public API declarations:
  62. * ``<MACRO>_EXPORT``: Marks symbols for export or import, making them
  63. visible as part of the public API when building or consuming a shared
  64. library.
  65. * ``<MACRO>_NO_EXPORT``: Marks symbols that should not be exported.
  66. If the :prop_tgt:`<LANG>_VISIBILITY_PRESET` target property is set to
  67. ``hidden``, using this macro in source code is typically redundant.
  68. * ``<MACRO>_DEPRECATED``: Marks symbols as deprecated. When such symbols
  69. are used, the compiler emits a warning at compile-time.
  70. * ``<MACRO>_DEPRECATED_EXPORT``: Combines export/import and deprecation
  71. markers for a symbol that is both part of the public API and deprecated.
  72. * ``<MACRO>_DEPRECATED_NO_EXPORT``: Marks a deprecated symbol that should
  73. not be exported (internal and deprecated).
  74. * ``<MACRO>_NO_DEPRECATED``: A macro that can be used in source code to
  75. conditionally exclude deprecated code parts from the build via
  76. preprocessor logic.
  77. The ``<MACRO>`` part is derived by default from the uppercase name of the
  78. target or the explicitly provided ``<base-name>``. All macro names can be
  79. customized using the optional arguments.
  80. .. rubric:: The arguments are:
  81. ``<target>``
  82. Name of a target for which the export header will be generated.
  83. Supported target types:
  84. * ``STATIC`` library (in this case, export-related macros are defined
  85. without values)
  86. * ``SHARED`` library
  87. * ``MODULE`` library
  88. * .. versionadded:: 3.1
  89. ``OBJECT`` library
  90. ``BASE_NAME <base-name>``
  91. If specified, it overrides the default file name and macro names.
  92. ``EXPORT_FILE_NAME <export-file-name>``
  93. If specified, it overrides the full path and the name of the generated
  94. export header file (``<base-name-lowercase>_export.h``) to
  95. ``<export-file-name>``. If given as a relative path, it will be
  96. interpreted relative to the current binary directory
  97. (:variable:`CMAKE_CURRENT_BINARY_DIR`).
  98. ``EXPORT_MACRO_NAME <export-macro-name>``
  99. If specified, it overrides the default macro name for the export
  100. directive.
  101. ``NO_EXPORT_MACRO_NAME <no-export-macro-name>``
  102. If specified, the ``<no-export-macro-name>`` will be used for the macro
  103. name that designates the attribute for items that shouldn't be exported.
  104. ``DEPRECATED_MACRO_NAME <deprecated-macro-name>``
  105. If specified, the following names will be used:
  106. * ``<deprecated-macro-name>`` (macro for marking deprecated symbols)
  107. * ``<deprecated-macro-name>_EXPORT`` (macro for deprecated symbols with
  108. export markers)
  109. * ``<deprecated-macro-name>_NO_EXPORT`` (macro for deprecated symbols
  110. with no-export markers)
  111. instead of the default names in format of
  112. ``<MACRO>_DEPRECATED{,_EXPORT,_NO_EXPORT}``.
  113. ``DEFINE_NO_DEPRECATED``
  114. If specified, this will define a macro named ``<MACRO>_NO_DEPRECATED``.
  115. ``NO_DEPRECATED_MACRO_NAME <no-deprecated-macro-name>``
  116. Used in combination with ``DEFINE_NO_DEPRECATED`` option. If specified,
  117. then a macro named ``<no-deprecated-macro-name>`` is used instead of the
  118. default ``<MACRO>_NO_DEPRECATED``.
  119. ``STATIC_DEFINE <static-define>``
  120. If specified, the ``<static-define>`` macro name will be used instead
  121. of the default ``<MACRO>_STATIC_DEFINE``. This macro controls the
  122. symbol export behavior in the generated header for static libraries.
  123. It is typically used when building both shared and static variants of a
  124. library from the same sources using a single generated export header.
  125. When this macro is defined for static library, the export-related macros
  126. will expand to nothing. This is important also on Windows, where symbol
  127. decoration is required only for shared libraries, not for static ones.
  128. ``PREFIX_NAME <prefix>``
  129. If specified, the additional ``<prefix>`` is prepended to all generated
  130. macro names.
  131. ``CUSTOM_CONTENT_FROM_VARIABLE <variable>``
  132. .. versionadded:: 3.7
  133. If specified, the content from the ``<variable>`` value is appended to
  134. the generated header file content after the preprocessor macros
  135. definitions.
  136. ``INCLUDE_GUARD_NAME <include-guard-name>``
  137. .. versionadded:: 3.11
  138. If specified, the ``<include-guard-name>`` is used as the preprocessor
  139. macro name to guard multiple inclusions of the generated header instead
  140. of the default name ``<export-macro-name>_H``.
  141. .. code-block:: c++
  142. :caption: ``<base-name-lowercase>_export.h``
  143. #ifndef <include-guard-name>
  144. #define <include-guard-name>
  145. // ...
  146. #endif /* <include-guard-name> */
  147. Deprecated Command
  148. """"""""""""""""""
  149. .. command:: add_compiler_export_flags
  150. .. deprecated:: 3.0
  151. Set the target properties
  152. :prop_tgt:`CXX_VISIBILITY_PRESET <<LANG>_VISIBILITY_PRESET>` and
  153. :prop_tgt:`VISIBILITY_INLINES_HIDDEN` instead.
  154. Adds C++ compiler options ``-fvisibility=hidden`` (and
  155. ``-fvisibility-inlines-hidden``, if supported) to hide all symbols by
  156. default to either :variable:`CMAKE_CXX_FLAGS <CMAKE_<LANG>_FLAGS>`
  157. variable or to a specified variable:
  158. .. code-block:: cmake
  159. add_compiler_export_flags([<output_variable>])
  160. This command is a no-op on Windows which does not need extra compiler flags
  161. for exporting support.
  162. ``<output-variable>``
  163. Optional variable name that will be populated with a string of
  164. space-separated C++ compile options required to enable visibility
  165. support for the compiler/architecture in use. If this argument is
  166. specified, the :variable:`CMAKE_CXX_FLAGS <CMAKE_<LANG>_FLAGS>` variable
  167. will not be modified.
  168. Examples
  169. ^^^^^^^^
  170. Example: Generating Export Header
  171. """""""""""""""""""""""""""""""""
  172. The following example demonstrates how to use this module to generate an
  173. export header in the current binary directory (``example_export.h``) and use
  174. it in a C++ library named ``example`` to control symbols visibility. The
  175. generated header defines the preprocessor macros ``EXAMPLE_EXPORT``,
  176. ``EXAMPLE_NO_EXPORT``, ``EXAMPLE_DEPRECATED``, ``EXAMPLE_DEPRECATED_EXPORT``,
  177. and ``EXAMPLE_DEPRECATED_NO_EXPORT``, and is installed along with the
  178. library's other public headers:
  179. .. code-block:: cmake
  180. :caption: ``CMakeLists.txt``
  181. :emphasize-lines: 10,11
  182. cmake_minimum_required(VERSION 3.24)
  183. project(GenerateExportHeaderExample)
  184. # Set default visibility of all symbols to hidden
  185. set(CMAKE_CXX_VISIBILITY_PRESET "hidden")
  186. set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)
  187. add_library(example)
  188. include(GenerateExportHeader)
  189. generate_export_header(example)
  190. target_sources(
  191. example
  192. PRIVATE example.cxx
  193. PUBLIC
  194. FILE_SET HEADERS
  195. FILES example.h
  196. FILE_SET generated_headers
  197. TYPE HEADERS
  198. BASE_DIRS $<TARGET_PROPERTY:example,BINARY_DIR>
  199. FILES ${CMAKE_CURRENT_BINARY_DIR}/example_export.h
  200. )
  201. target_include_directories(example PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
  202. install(
  203. TARGETS example
  204. FILE_SET HEADERS
  205. FILE_SET generated_headers
  206. )
  207. And in the ABI header files:
  208. .. code-block:: c++
  209. :caption: ``example.h``
  210. #include "example_export.h"
  211. // This class is part of the public API and is exported
  212. class EXAMPLE_EXPORT SomeClass
  213. {
  214. public:
  215. SomeClass();
  216. void doSomething();
  217. // This method is deprecated
  218. EXAMPLE_DEPRECATED void legacyMethod();
  219. };
  220. // This function is exported and deprecated
  221. EXAMPLE_DEPRECATED_EXPORT void legacyPublicFunction();
  222. // This function is deprecated but not exported
  223. EXAMPLE_DEPRECATED void legacyInternalFunction();
  224. .. code-block:: c++
  225. :caption: ``example.cxx``
  226. #include <iostream>
  227. #include "example.h"
  228. SomeClass::SomeClass() = default;
  229. void SomeClass::doSomething()
  230. {
  231. std::cout << "SomeClass::doSomething() called" << std::endl;
  232. }
  233. void SomeClass::legacyMethod()
  234. {
  235. std::cout << "SomeClass::legacyMethod() is deprecated" << std::endl;
  236. }
  237. void legacyPublicFunction()
  238. {
  239. std::cout << "legacyPublicFunction() is deprecated" << std::endl;
  240. }
  241. void internalLegacyFunction()
  242. {
  243. std::cout << "legacyInternalFunction() is deprecated" << std::endl;
  244. }
  245. Examples: Customizing Generated Header
  246. """"""""""""""""""""""""""""""""""""""
  247. The ``BASE_NAME`` argument can be used to override the generated file name
  248. and the names used for the macros. The following will generate a file
  249. named ``other_name_export.h`` containing export-related macros such as
  250. ``OTHER_NAME_EXPORT``, ``OTHER_NAME_NO_EXPORT``, ``OTHER_NAME_DEPRECATED``,
  251. etc.
  252. .. code-block:: cmake
  253. add_library(example example.cxx)
  254. include(GenerateExportHeader)
  255. generate_export_header(example BASE_NAME "other_name")
  256. The ``BASE_NAME`` may be overridden by specifying other command options.
  257. For example, the following creates a macro ``OTHER_NAME_EXPORT`` instead of
  258. ``EXAMPLE_EXPORT``, but other macros and the generated header file name are
  259. set to their default values:
  260. .. code-block:: cmake
  261. add_library(example example.cxx)
  262. include(GenerateExportHeader)
  263. generate_export_header(example EXPORT_MACRO_NAME "OTHER_NAME_EXPORT")
  264. The following example creates ``KDE_DEPRECATED`` macro instead of
  265. default ``EXAMPLE_DEPRECATED``:
  266. .. code-block:: cmake
  267. add_library(example example.cxx)
  268. include(GenerateExportHeader)
  269. generate_export_header(example DEPRECATED_MACRO_NAME "KDE_DEPRECATED")
  270. The ``DEFINE_NO_DEPRECATED`` option can be used to define a macro which can
  271. be used to remove deprecated code from preprocessor output:
  272. .. code-block:: cmake
  273. option(EXCLUDE_DEPRECATED "Exclude deprecated parts of the library")
  274. if(EXCLUDE_DEPRECATED)
  275. set(NO_BUILD_DEPRECATED DEFINE_NO_DEPRECATED)
  276. endif()
  277. include(GenerateExportHeader)
  278. generate_export_header(example ${NO_BUILD_DEPRECATED})
  279. .. code-block:: c++
  280. :caption: ``example.h``
  281. class EXAMPLE_EXPORT SomeClass
  282. {
  283. public:
  284. #ifndef EXAMPLE_NO_DEPRECATED
  285. EXAMPLE_DEPRECATED void legacyMethod();
  286. #endif
  287. };
  288. .. code-block:: c++
  289. :caption: ``example.cxx``
  290. #ifndef EXAMPLE_NO_DEPRECATED
  291. void SomeClass::legacyMethod() { }
  292. #endif
  293. The ``PREFIX_NAME`` argument can be used to prepend all generated macro names
  294. with some prefix. For example, the following will generate macros such as
  295. ``VTK_SOMELIB_EXPORT``, etc.
  296. .. code-block:: cmake
  297. include(GenerateExportHeader)
  298. generate_export_header(somelib PREFIX_NAME "VTK_")
  299. Appending additional content to generated header can be done with the
  300. ``CUSTOM_CONTENT_FROM_VARIABLE`` argument:
  301. .. code-block:: cmake
  302. include(GenerateExportHeader)
  303. set(content [[#include "project_api.h"]])
  304. generate_export_header(example CUSTOM_CONTENT_FROM_VARIABLE content)
  305. Example: Building Shared and Static Library
  306. """""""""""""""""""""""""""""""""""""""""""
  307. In the following example both a shared and a static library are built from
  308. the same sources, and the ``<MACRO>_STATIC_DEFINE`` macro compile definition
  309. is defined to ensure the same generated export header works for both:
  310. .. code-block:: cmake
  311. add_library(example_shared SHARED example.cxx)
  312. add_library(example_static STATIC example.cxx)
  313. include(GenerateExportHeader)
  314. generate_export_header(example_shared BASE_NAME "example")
  315. # Define macro to disable export attributes for static build
  316. target_compile_definitions(example_static PRIVATE EXAMPLE_STATIC_DEFINE)
  317. Example: Upgrading Deprecated Command
  318. """""""""""""""""""""""""""""""""""""
  319. In earlier versions of CMake, ``add_compiler_export_flags()`` command was
  320. used to add symbol visibility compile options:
  321. .. code-block:: cmake
  322. :caption: ``CMakeLists.txt``
  323. add_library(example example.cxx)
  324. include(GenerateExportHeader)
  325. add_compiler_export_flags(flags)
  326. string(REPLACE " " ";" flags "${flags}")
  327. set_property(TARGET example APPEND PROPERTY COMPILE_OPTIONS "${flags}")
  328. generate_export_header(example)
  329. In new code, the following target properties are used to achieve the same
  330. functionality:
  331. .. code-block:: cmake
  332. :caption: ``CMakeLists.txt``
  333. add_library(example example.cxx)
  334. include(GenerateExportHeader)
  335. set_target_properties(
  336. example
  337. PROPERTIES
  338. CXX_VISIBILITY_PRESET hidden
  339. VISIBILITY_INLINES_HIDDEN TRUE
  340. )
  341. generate_export_header(example)
  342. See Also
  343. ^^^^^^^^
  344. * The :prop_tgt:`DEFINE_SYMBOL` target property to customize the preprocessor
  345. macro name used by the generated header. This macro determines whether
  346. the library header is being included during the library's own compilation
  347. or when it is used by another project (e.g., after installation).
  348. * The :prop_tgt:`ENABLE_EXPORTS` target property.
  349. * The :prop_tgt:`WINDOWS_EXPORT_ALL_SYMBOLS` target property.
  350. #]=======================================================================]
  351. include(CheckCompilerFlag)
  352. include(CheckSourceCompiles)
  353. # TODO: Install this macro separately?
  354. macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
  355. check_source_compiles(CXX "${_ATTRIBUTE} int somefunc() { return 0; }
  356. int main() { return somefunc();}" ${_RESULT}
  357. )
  358. endmacro()
  359. # TODO: Install this macro separately?
  360. macro(_check_c_compiler_attribute _ATTRIBUTE _RESULT)
  361. check_source_compiles(C "${_ATTRIBUTE} int somefunc(void) { return 0; }
  362. int main(void) { return somefunc();}" ${_RESULT}
  363. )
  364. endmacro()
  365. macro(_test_compiler_hidden_visibility)
  366. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
  367. AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.2")
  368. set(GCC_TOO_OLD TRUE)
  369. elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU"
  370. AND CMAKE_C_COMPILER_VERSION VERSION_LESS "4.2")
  371. set(GCC_TOO_OLD TRUE)
  372. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
  373. set(_INTEL_TOO_OLD TRUE)
  374. endif()
  375. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  376. # the check_compiler_flag passes
  377. if(NOT GCC_TOO_OLD
  378. AND NOT _INTEL_TOO_OLD
  379. AND NOT WIN32
  380. AND NOT CYGWIN
  381. AND NOT CMAKE_CXX_COMPILER_ID MATCHES "^(IBMClang|XLClang|XL)$"
  382. AND NOT CMAKE_CXX_COMPILER_ID MATCHES "^(PGI|NVHPC)$"
  383. AND NOT CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  384. if (CMAKE_CXX_COMPILER_LOADED)
  385. check_compiler_flag(CXX -fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  386. check_compiler_flag(CXX -fvisibility-inlines-hidden
  387. COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  388. else()
  389. check_compiler_flag(C -fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  390. check_compiler_flag(C -fvisibility-inlines-hidden
  391. COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  392. endif()
  393. endif()
  394. endmacro()
  395. macro(_test_compiler_has_deprecated)
  396. # NOTE: Some Embarcadero compilers silently compile __declspec(deprecated)
  397. # without error, but this is not a documented feature and the attribute does
  398. # not actually generate any warnings.
  399. if(CMAKE_CXX_COMPILER_ID MATCHES Borland
  400. OR CMAKE_CXX_COMPILER_ID MATCHES Embarcadero
  401. OR CMAKE_CXX_COMPILER_ID MATCHES HP
  402. OR GCC_TOO_OLD
  403. OR CMAKE_CXX_COMPILER_ID MATCHES "^(PGI|NVHPC)$"
  404. OR CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  405. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
  406. "Compiler support for a deprecated attribute")
  407. else()
  408. if (CMAKE_CXX_COMPILER_LOADED)
  409. _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
  410. COMPILER_HAS_DEPRECATED_ATTR)
  411. if(COMPILER_HAS_DEPRECATED_ATTR)
  412. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  413. CACHE INTERNAL "Compiler support for a deprecated attribute")
  414. else()
  415. _check_cxx_compiler_attribute("__declspec(deprecated)"
  416. COMPILER_HAS_DEPRECATED)
  417. endif()
  418. else()
  419. _check_c_compiler_attribute("__attribute__((__deprecated__))"
  420. COMPILER_HAS_DEPRECATED_ATTR)
  421. if(COMPILER_HAS_DEPRECATED_ATTR)
  422. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  423. CACHE INTERNAL "Compiler support for a deprecated attribute")
  424. else()
  425. _check_c_compiler_attribute("__declspec(deprecated)"
  426. COMPILER_HAS_DEPRECATED)
  427. endif()
  428. endif()
  429. endif()
  430. endmacro()
  431. get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
  432. "${CMAKE_CURRENT_LIST_FILE}" PATH)
  433. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  434. set(DEFINE_DEPRECATED)
  435. set(DEFINE_EXPORT)
  436. set(DEFINE_IMPORT)
  437. set(DEFINE_NO_EXPORT)
  438. if (COMPILER_HAS_DEPRECATED_ATTR AND NOT WIN32)
  439. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  440. elseif(COMPILER_HAS_DEPRECATED)
  441. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  442. endif()
  443. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  444. if(NOT ${type} STREQUAL "STATIC_LIBRARY")
  445. if(WIN32 OR CYGWIN)
  446. set(DEFINE_EXPORT "__declspec(dllexport)")
  447. set(DEFINE_IMPORT "__declspec(dllimport)")
  448. elseif(COMPILER_HAS_HIDDEN_VISIBILITY)
  449. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  450. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  451. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  452. endif()
  453. endif()
  454. endmacro()
  455. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  456. # Option overrides
  457. set(options DEFINE_NO_DEPRECATED)
  458. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
  459. DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
  460. NO_DEPRECATED_MACRO_NAME CUSTOM_CONTENT_FROM_VARIABLE INCLUDE_GUARD_NAME)
  461. set(multiValueArgs)
  462. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}"
  463. ${ARGN})
  464. set(BASE_NAME "${TARGET_LIBRARY}")
  465. if(_GEH_BASE_NAME)
  466. set(BASE_NAME ${_GEH_BASE_NAME})
  467. endif()
  468. string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  469. string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
  470. # Default options
  471. set(EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_EXPORT")
  472. set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
  473. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  474. set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
  475. set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
  476. set(NO_DEPRECATED_MACRO_NAME
  477. "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
  478. if(_GEH_UNPARSED_ARGUMENTS)
  479. message(FATAL_ERROR "Unknown keywords given to generate_export_header(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  480. endif()
  481. if(_GEH_EXPORT_MACRO_NAME)
  482. set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
  483. endif()
  484. string(MAKE_C_IDENTIFIER ${EXPORT_MACRO_NAME} EXPORT_MACRO_NAME)
  485. if(_GEH_EXPORT_FILE_NAME)
  486. if(IS_ABSOLUTE ${_GEH_EXPORT_FILE_NAME})
  487. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  488. else()
  489. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  490. endif()
  491. endif()
  492. if(_GEH_DEPRECATED_MACRO_NAME)
  493. set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
  494. endif()
  495. string(MAKE_C_IDENTIFIER ${DEPRECATED_MACRO_NAME} DEPRECATED_MACRO_NAME)
  496. if(_GEH_NO_EXPORT_MACRO_NAME)
  497. set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
  498. endif()
  499. string(MAKE_C_IDENTIFIER ${NO_EXPORT_MACRO_NAME} NO_EXPORT_MACRO_NAME)
  500. if(_GEH_STATIC_DEFINE)
  501. set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
  502. endif()
  503. string(MAKE_C_IDENTIFIER ${STATIC_DEFINE} STATIC_DEFINE)
  504. if(_GEH_DEFINE_NO_DEPRECATED)
  505. set(DEFINE_NO_DEPRECATED 1)
  506. else()
  507. set(DEFINE_NO_DEPRECATED 0)
  508. endif()
  509. if(_GEH_NO_DEPRECATED_MACRO_NAME)
  510. set(NO_DEPRECATED_MACRO_NAME
  511. ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
  512. endif()
  513. string(MAKE_C_IDENTIFIER ${NO_DEPRECATED_MACRO_NAME} NO_DEPRECATED_MACRO_NAME)
  514. if(_GEH_INCLUDE_GUARD_NAME)
  515. set(INCLUDE_GUARD_NAME ${_GEH_INCLUDE_GUARD_NAME})
  516. else()
  517. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  518. endif()
  519. get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
  520. if(NOT EXPORT_IMPORT_CONDITION)
  521. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  522. endif()
  523. string(MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
  524. if(_GEH_CUSTOM_CONTENT_FROM_VARIABLE)
  525. if(DEFINED "${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}")
  526. set(CUSTOM_CONTENT "${${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}}")
  527. else()
  528. set(CUSTOM_CONTENT "")
  529. endif()
  530. endif()
  531. configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
  532. "${EXPORT_FILE_NAME}" @ONLY)
  533. endmacro()
  534. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  535. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  536. if(NOT ${type} STREQUAL "STATIC_LIBRARY"
  537. AND NOT ${type} STREQUAL "SHARED_LIBRARY"
  538. AND NOT ${type} STREQUAL "OBJECT_LIBRARY"
  539. AND NOT ${type} STREQUAL "MODULE_LIBRARY")
  540. message(WARNING "This macro can only be used with libraries")
  541. return()
  542. endif()
  543. _test_compiler_hidden_visibility()
  544. _test_compiler_has_deprecated()
  545. _do_set_macro_values(${TARGET_LIBRARY})
  546. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  547. endfunction()
  548. function(add_compiler_export_flags)
  549. if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.12)
  550. message(DEPRECATION "The add_compiler_export_flags function is obsolete. Use the CXX_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN target properties instead.")
  551. endif()
  552. _test_compiler_hidden_visibility()
  553. _test_compiler_has_deprecated()
  554. option(USE_COMPILER_HIDDEN_VISIBILITY
  555. "Use HIDDEN visibility support if available." ON)
  556. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  557. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  558. # Just return if there are no flags to add.
  559. return()
  560. endif()
  561. set (EXTRA_FLAGS "-fvisibility=hidden")
  562. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  563. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  564. endif()
  565. # Either return the extra flags needed in the supplied argument, or to the
  566. # CMAKE_CXX_FLAGS if no argument is supplied.
  567. if(ARGC GREATER 0)
  568. set(${ARGV0} "${EXTRA_FLAGS}" PARENT_SCOPE)
  569. else()
  570. string(APPEND CMAKE_CXX_FLAGS " ${EXTRA_FLAGS}")
  571. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
  572. endif()
  573. endfunction()
  574. # FIXME(#24994): The following module(s) are included only for compatibility
  575. # with projects that accidentally relied on them with CMake 3.26 and below.
  576. include(CheckCCompilerFlag)
  577. include(CheckCXXCompilerFlag)