CheckSymbolExists.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # - Check if a symbol exists as a function, variable, or macro
  2. # CHECK_SYMBOL_EXISTS(<symbol> <files> <variable>)
  3. #
  4. # Check that the <symbol> is available after including given header
  5. # <files> and store the result in a <variable>. Specify the list
  6. # of files in one argument as a semicolon-separated list.
  7. #
  8. # If the header files define the symbol as a macro it is considered
  9. # available and assumed to work. If the header files declare the
  10. # symbol as a function or variable then the symbol must also be
  11. # available for linking. If the symbol is a type or enum value
  12. # it will not be recognized (consider using CheckTypeSize or
  13. # CheckCSourceCompiles).
  14. # If the check needs to be done in C++, consider using CHECK_CXX_SYMBOL_EXISTS(),
  15. # which does the same as CHECK_SYMBOL_EXISTS(), but in C++.
  16. #
  17. # The following variables may be set before calling this macro to
  18. # modify the way the check is run:
  19. #
  20. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  21. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  22. # CMAKE_REQUIRED_INCLUDES = list of include directories
  23. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  24. #=============================================================================
  25. # Copyright 2003-2011 Kitware, Inc.
  26. #
  27. # Distributed under the OSI-approved BSD License (the "License");
  28. # see accompanying file Copyright.txt for details.
  29. #
  30. # This software is distributed WITHOUT ANY WARRANTY; without even the
  31. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  32. # See the License for more information.
  33. #=============================================================================
  34. # (To distribute this file outside of CMake, substitute the full
  35. # License text for the above reference.)
  36. MACRO(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
  37. _CHECK_SYMBOL_EXISTS("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  38. ENDMACRO(CHECK_SYMBOL_EXISTS)
  39. MACRO(_CHECK_SYMBOL_EXISTS SOURCEFILE SYMBOL FILES VARIABLE)
  40. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  41. SET(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  42. SET(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
  43. IF(CMAKE_REQUIRED_LIBRARIES)
  44. SET(CHECK_SYMBOL_EXISTS_LIBS
  45. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  46. ELSE(CMAKE_REQUIRED_LIBRARIES)
  47. SET(CHECK_SYMBOL_EXISTS_LIBS)
  48. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  49. IF(CMAKE_REQUIRED_INCLUDES)
  50. SET(CMAKE_SYMBOL_EXISTS_INCLUDES
  51. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  52. ELSE(CMAKE_REQUIRED_INCLUDES)
  53. SET(CMAKE_SYMBOL_EXISTS_INCLUDES)
  54. ENDIF(CMAKE_REQUIRED_INCLUDES)
  55. FOREACH(FILE ${FILES})
  56. SET(CMAKE_CONFIGURABLE_FILE_CONTENT
  57. "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
  58. ENDFOREACH(FILE)
  59. SET(CMAKE_CONFIGURABLE_FILE_CONTENT
  60. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\nint main(int argc, char** argv)\n{\n (void)argv;\n#ifndef ${SYMBOL}\n return ((int*)(&${SYMBOL}))[argc];\n#else\n (void)argc;\n return 0;\n#endif\n}\n")
  61. CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  62. "${SOURCEFILE}" @ONLY IMMEDIATE)
  63. MESSAGE(STATUS "Looking for ${SYMBOL}")
  64. TRY_COMPILE(${VARIABLE}
  65. ${CMAKE_BINARY_DIR}
  66. "${SOURCEFILE}"
  67. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  68. CMAKE_FLAGS
  69. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
  70. "${CHECK_SYMBOL_EXISTS_LIBS}"
  71. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  72. OUTPUT_VARIABLE OUTPUT)
  73. IF(${VARIABLE})
  74. MESSAGE(STATUS "Looking for ${SYMBOL} - found")
  75. SET(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
  76. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  77. "Determining if the ${SYMBOL} "
  78. "exist passed with the following output:\n"
  79. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  80. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  81. ELSE(${VARIABLE})
  82. MESSAGE(STATUS "Looking for ${SYMBOL} - not found.")
  83. SET(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
  84. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  85. "Determining if the ${SYMBOL} "
  86. "exist failed with the following output:\n"
  87. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  88. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  89. ENDIF(${VARIABLE})
  90. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  91. ENDMACRO(_CHECK_SYMBOL_EXISTS)