CheckSymbolExists.cmake 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. message(STATUS "files: -${FILES}-")
  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. SET(CHECK_SYMBOL_EXISTS_LIBS
  46. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  47. ELSE(CMAKE_REQUIRED_LIBRARIES)
  48. SET(CHECK_SYMBOL_EXISTS_LIBS)
  49. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  50. IF(CMAKE_REQUIRED_INCLUDES)
  51. SET(CMAKE_SYMBOL_EXISTS_INCLUDES
  52. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  53. ELSE(CMAKE_REQUIRED_INCLUDES)
  54. SET(CMAKE_SYMBOL_EXISTS_INCLUDES)
  55. ENDIF(CMAKE_REQUIRED_INCLUDES)
  56. FOREACH(FILE ${FILES})
  57. SET(CMAKE_CONFIGURABLE_FILE_CONTENT
  58. "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
  59. ENDFOREACH(FILE)
  60. SET(CMAKE_CONFIGURABLE_FILE_CONTENT
  61. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\nvoid cmakeRequireSymbol(int dummy,...){(void)dummy;}\nint main()\n{\n#ifndef ${SYMBOL}\n cmakeRequireSymbol(0,&${SYMBOL});\n#endif\n return 0;\n}\n")
  62. CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  63. "${SOURCEFILE}" @ONLY)
  64. MESSAGE(STATUS "Looking for ${SYMBOL}")
  65. TRY_COMPILE(${VARIABLE}
  66. ${CMAKE_BINARY_DIR}
  67. "${SOURCEFILE}"
  68. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  69. CMAKE_FLAGS
  70. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
  71. "${CHECK_SYMBOL_EXISTS_LIBS}"
  72. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  73. OUTPUT_VARIABLE OUTPUT)
  74. IF(${VARIABLE})
  75. MESSAGE(STATUS "Looking for ${SYMBOL} - found")
  76. SET(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
  77. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  78. "Determining if the ${SYMBOL} "
  79. "exist passed with the following output:\n"
  80. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  81. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  82. ELSE(${VARIABLE})
  83. MESSAGE(STATUS "Looking for ${SYMBOL} - not found.")
  84. SET(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
  85. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  86. "Determining if the ${SYMBOL} "
  87. "exist failed with the following output:\n"
  88. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  89. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  90. ENDIF(${VARIABLE})
  91. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  92. ENDMACRO(_CHECK_SYMBOL_EXISTS)