CheckStructHasMember.cmake 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #.rst:
  2. # CheckStructHasMember
  3. # --------------------
  4. #
  5. # Check if the given struct or class has the specified member variable
  6. #
  7. # ::
  8. #
  9. # CHECK_STRUCT_HAS_MEMBER(<struct> <member> <header> <variable>
  10. # [LANGUAGE <language>])
  11. #
  12. # ::
  13. #
  14. # <struct> - the name of the struct or class you are interested in
  15. # <member> - the member which existence you want to check
  16. # <header> - the header(s) where the prototype should be declared
  17. # <variable> - variable to store the result
  18. # <language> - the compiler to use (C or CXX)
  19. #
  20. #
  21. #
  22. # The following variables may be set before calling this macro to modify
  23. # the way the check is run:
  24. #
  25. # ::
  26. #
  27. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  28. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  29. # CMAKE_REQUIRED_INCLUDES = list of include directories
  30. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  31. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  32. #
  33. #
  34. #
  35. # Example: CHECK_STRUCT_HAS_MEMBER("struct timeval" tv_sec sys/select.h
  36. # HAVE_TIMEVAL_TV_SEC LANGUAGE C)
  37. #=============================================================================
  38. # Copyright 2007-2009 Kitware, Inc.
  39. #
  40. # Distributed under the OSI-approved BSD License (the "License");
  41. # see accompanying file Copyright.txt for details.
  42. #
  43. # This software is distributed WITHOUT ANY WARRANTY; without even the
  44. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  45. # See the License for more information.
  46. #=============================================================================
  47. # (To distribute this file outside of CMake, substitute the full
  48. # License text for the above reference.)
  49. include(CheckCSourceCompiles)
  50. include(CheckCXXSourceCompiles)
  51. macro (CHECK_STRUCT_HAS_MEMBER _STRUCT _MEMBER _HEADER _RESULT)
  52. set(_INCLUDE_FILES)
  53. foreach (it ${_HEADER})
  54. set(_INCLUDE_FILES "${_INCLUDE_FILES}#include <${it}>\n")
  55. endforeach ()
  56. if("x${ARGN}" STREQUAL "x")
  57. set(_lang C)
  58. elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
  59. set(_lang "${CMAKE_MATCH_1}")
  60. else()
  61. message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
  62. endif()
  63. set(_CHECK_STRUCT_MEMBER_SOURCE_CODE "
  64. ${_INCLUDE_FILES}
  65. int main()
  66. {
  67. (void)sizeof(((${_STRUCT} *)0)->${_MEMBER});
  68. return 0;
  69. }
  70. ")
  71. if("${_lang}" STREQUAL "C")
  72. CHECK_C_SOURCE_COMPILES("${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
  73. elseif("${_lang}" STREQUAL "CXX")
  74. CHECK_CXX_SOURCE_COMPILES("${_CHECK_STRUCT_MEMBER_SOURCE_CODE}" ${_RESULT})
  75. else()
  76. message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
  77. endif()
  78. endmacro ()