Merge topic 'cpack-innosetup-linux'
[kiteware-cmake.git] / Modules / CheckStructHasMember.cmake
blob959f8e51701e361a6e6342ceb79fe13fbf43e7b4
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:
5 CheckStructHasMember
6 --------------------
8 Check if the given struct or class has the specified member variable
10 .. command:: CHECK_STRUCT_HAS_MEMBER
12   .. code-block:: cmake
14     CHECK_STRUCT_HAS_MEMBER(<struct> <member> <header> <variable>
15                             [LANGUAGE <language>])
17   ::
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
42 Example:
44 .. code-block:: cmake
46   CHECK_STRUCT_HAS_MEMBER("struct timeval" tv_sec sys/select.h
47                           HAVE_TIMEVAL_TV_SEC LANGUAGE C)
48 #]=======================================================================]
50 include_guard(GLOBAL)
51 include(CheckSourceCompiles)
53 macro (CHECK_STRUCT_HAS_MEMBER _STRUCT _MEMBER _HEADER _RESULT)
54   set(_INCLUDE_FILES)
55   foreach (it ${_HEADER})
56     string(APPEND _INCLUDE_FILES "#include <${it}>\n")
57   endforeach ()
59   if("x${ARGN}" STREQUAL "x")
60     set(_lang C)
61   elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
62     set(_lang "${CMAKE_MATCH_1}")
63   else()
64     message(FATAL_ERROR "Unknown arguments:\n  ${ARGN}\n")
65   endif()
67   set(_CHECK_STRUCT_MEMBER_SOURCE_CODE "
68 ${_INCLUDE_FILES}
69 int main(void)
71   (void)sizeof(((${_STRUCT} *)0)->${_MEMBER});
72   return 0;
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})
80   else()
81     message(FATAL_ERROR "Unknown language:\n  ${_lang}\nSupported languages: C, CXX.\n")
82   endif()
83 endmacro ()
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)