CheckStructHasMember.cmake 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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> <header> <variable>
  10. [LANGUAGE <language>])
  11. ::
  12. <struct> - the name of the struct or class you are interested in
  13. <member> - the member which existence you want to check
  14. <header> - the header(s) where the prototype should be declared
  15. <variable> - variable to store the result
  16. <language> - the compiler to use (C or CXX)
  17. The following variables may be set before calling this macro to modify
  18. the way the check is run:
  19. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  20. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  21. .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
  22. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  23. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  24. .. include:: /module/CMAKE_REQUIRED_LINK_DIRECTORIES.txt
  25. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  26. Example:
  27. .. code-block:: cmake
  28. CHECK_STRUCT_HAS_MEMBER("struct timeval" tv_sec sys/select.h
  29. HAVE_TIMEVAL_TV_SEC LANGUAGE C)
  30. #]=======================================================================]
  31. include_guard(GLOBAL)
  32. include(CheckSourceCompiles)
  33. macro (CHECK_STRUCT_HAS_MEMBER _STRUCT _MEMBER _HEADER _RESULT)
  34. set(_INCLUDE_FILES)
  35. foreach (it ${_HEADER})
  36. string(APPEND _INCLUDE_FILES "#include <${it}>\n")
  37. endforeach ()
  38. if("x${ARGN}" STREQUAL "x")
  39. set(_lang C)
  40. elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
  41. set(_lang "${CMAKE_MATCH_1}")
  42. else()
  43. message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
  44. endif()
  45. set(_CHECK_STRUCT_MEMBER_SOURCE_CODE "
  46. ${_INCLUDE_FILES}
  47. int main(void)
  48. {
  49. (void)sizeof(((${_STRUCT} *)0)->${_MEMBER});
  50. return 0;
  51. }
  52. ")
  53. if("${_lang}" STREQUAL "C")
  54. CHECK_SOURCE_COMPILES(C "${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
  55. elseif("${_lang}" STREQUAL "CXX")
  56. CHECK_SOURCE_COMPILES(CXX "${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
  57. else()
  58. message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
  59. endif()
  60. endmacro ()
  61. # FIXME(#24994): The following modules are included only for compatibility
  62. # with projects that accidentally relied on them with CMake 3.26 and below.
  63. include(CheckCSourceCompiles)
  64. include(CheckCXXSourceCompiles)