CheckStructHasMember.cmake 2.5 KB

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