1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Check if the given struct or class has the specified member variable
10 .. command:: CHECK_STRUCT_HAS_MEMBER
14 CHECK_STRUCT_HAS_MEMBER(<struct> <member> <header> <variable>
15 [LANGUAGE <language>])
19 <struct> - the name of the struct or class you are interested in
20 <member> - the member which existence you want to check
21 <header> - the header(s) where the prototype should be declared
22 <variable> - variable to store the result
23 <language> - the compiler to use (C or CXX)
26 The following variables may be set before calling this macro to modify
27 the way the check is run:
29 .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
31 .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
33 .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
35 .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
37 .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
39 .. include:: /module/CMAKE_REQUIRED_QUIET.txt
46 CHECK_STRUCT_HAS_MEMBER("struct timeval" tv_sec sys/select.h
47 HAVE_TIMEVAL_TV_SEC LANGUAGE C)
48 #]=======================================================================]
51 include(CheckSourceCompiles)
53 macro (CHECK_STRUCT_HAS_MEMBER _STRUCT _MEMBER _HEADER _RESULT)
55 foreach (it ${_HEADER})
56 string(APPEND _INCLUDE_FILES "#include <${it}>\n")
59 if("x${ARGN}" STREQUAL "x")
61 elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
62 set(_lang "${CMAKE_MATCH_1}")
64 message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
67 set(_CHECK_STRUCT_MEMBER_SOURCE_CODE "
71 (void)sizeof(((${_STRUCT} *)0)->${_MEMBER});
76 if("${_lang}" STREQUAL "C")
77 CHECK_SOURCE_COMPILES(C "${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
78 elseif("${_lang}" STREQUAL "CXX")
79 CHECK_SOURCE_COMPILES(CXX "${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
81 message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
85 # FIXME(#24994): The following modules are included only for compatibility
86 # with projects that accidentally relied on them with CMake 3.26 and below.
87 include(CheckCSourceCompiles)
88 include(CheckCXXSourceCompiles)