CheckStructHasMember.cmake 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. CheckStructHasMember
  5. --------------------
  6. Check if the given struct or class has the specified member variable
  7. .. command:: check_struct_has_member
  8. .. code-block:: cmake
  9. check_struct_has_member(<struct> <member> <headers> <variable>
  10. [LANGUAGE <language>])
  11. Check that the struct or class ``<struct>`` has the specified ``<member>``
  12. after including the given header(s) ``<headers>`` where the prototype should
  13. be declared. Specify the list of header files in one argument as a
  14. semicolon-separated list. The result is stored in an internal cache variable
  15. ``<variable>``.
  16. The options are:
  17. ``LANGUAGE <language>``
  18. Use the ``<language>`` compiler to perform the check.
  19. Acceptable values are ``C`` and ``CXX``.
  20. If not specified, it defaults to ``C``.
  21. The following variables may be set before calling this macro to modify
  22. the way the check is run:
  23. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  24. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  25. .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
  26. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  27. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  28. .. include:: /module/CMAKE_REQUIRED_LINK_DIRECTORIES.txt
  29. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  30. Example
  31. ^^^^^^^
  32. .. code-block:: cmake
  33. include(CheckStructHasMember)
  34. check_struct_has_member("struct timeval" tv_sec sys/select.h
  35. HAVE_TIMEVAL_TV_SEC LANGUAGE C)
  36. #]=======================================================================]
  37. include_guard(GLOBAL)
  38. include(CheckSourceCompiles)
  39. macro (CHECK_STRUCT_HAS_MEMBER _STRUCT _MEMBER _HEADER _RESULT)
  40. set(_INCLUDE_FILES)
  41. foreach (it ${_HEADER})
  42. string(APPEND _INCLUDE_FILES "#include <${it}>\n")
  43. endforeach ()
  44. if("x${ARGN}" STREQUAL "x")
  45. set(_lang C)
  46. elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
  47. set(_lang "${CMAKE_MATCH_1}")
  48. else()
  49. message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
  50. endif()
  51. set(_CHECK_STRUCT_MEMBER_SOURCE_CODE "
  52. ${_INCLUDE_FILES}
  53. int main(void)
  54. {
  55. (void)sizeof(((${_STRUCT} *)0)->${_MEMBER});
  56. return 0;
  57. }
  58. ")
  59. if("${_lang}" STREQUAL "C")
  60. CHECK_SOURCE_COMPILES(C "${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
  61. elseif("${_lang}" STREQUAL "CXX")
  62. CHECK_SOURCE_COMPILES(CXX "${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
  63. else()
  64. message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
  65. endif()
  66. endmacro ()
  67. # FIXME(#24994): The following modules are included only for compatibility
  68. # with projects that accidentally relied on them with CMake 3.26 and below.
  69. include(CheckCSourceCompiles)
  70. include(CheckCXXSourceCompiles)