CheckSymbolExists.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  37. MACRO(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
  38. _CHECK_SYMBOL_EXISTS("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  39. ENDMACRO(CHECK_SYMBOL_EXISTS)
  40. MACRO(_CHECK_SYMBOL_EXISTS SOURCEFILE SYMBOL FILES VARIABLE)
  41. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  42. SET(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  43. SET(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
  44. IF(CMAKE_REQUIRED_LIBRARIES)
  45. # this one translates potentially used imported library targets to their files on disk
  46. CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  47. SET(CHECK_SYMBOL_EXISTS_LIBS
  48. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
  49. ELSE(CMAKE_REQUIRED_LIBRARIES)
  50. SET(CHECK_SYMBOL_EXISTS_LIBS)
  51. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  52. IF(CMAKE_REQUIRED_INCLUDES)
  53. SET(CMAKE_SYMBOL_EXISTS_INCLUDES
  54. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  55. ELSE(CMAKE_REQUIRED_INCLUDES)
  56. SET(CMAKE_SYMBOL_EXISTS_INCLUDES)
  57. ENDIF(CMAKE_REQUIRED_INCLUDES)
  58. FOREACH(FILE ${FILES})
  59. SET(CMAKE_CONFIGURABLE_FILE_CONTENT
  60. "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
  61. ENDFOREACH(FILE)
  62. SET(CMAKE_CONFIGURABLE_FILE_CONTENT
  63. "${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")
  64. CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  65. "${SOURCEFILE}" @ONLY IMMEDIATE)
  66. MESSAGE(STATUS "Looking for ${SYMBOL}")
  67. TRY_COMPILE(${VARIABLE}
  68. ${CMAKE_BINARY_DIR}
  69. "${SOURCEFILE}"
  70. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  71. CMAKE_FLAGS
  72. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
  73. "${CHECK_SYMBOL_EXISTS_LIBS}"
  74. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  75. OUTPUT_VARIABLE OUTPUT)
  76. IF(${VARIABLE})
  77. MESSAGE(STATUS "Looking for ${SYMBOL} - found")
  78. SET(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
  79. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  80. "Determining if the ${SYMBOL} "
  81. "exist passed with the following output:\n"
  82. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  83. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  84. ELSE(${VARIABLE})
  85. MESSAGE(STATUS "Looking for ${SYMBOL} - not found.")
  86. SET(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
  87. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  88. "Determining if the ${SYMBOL} "
  89. "exist failed with the following output:\n"
  90. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  91. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  92. ENDIF(${VARIABLE})
  93. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  94. ENDMACRO(_CHECK_SYMBOL_EXISTS)