CheckSymbolExists.cmake 4.5 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()
  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()
  50. set(CHECK_SYMBOL_EXISTS_LIBS)
  51. endif()
  52. if(CMAKE_REQUIRED_INCLUDES)
  53. set(CMAKE_SYMBOL_EXISTS_INCLUDES
  54. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  55. else()
  56. set(CMAKE_SYMBOL_EXISTS_INCLUDES)
  57. endif()
  58. foreach(FILE ${FILES})
  59. set(CMAKE_CONFIGURABLE_FILE_CONTENT
  60. "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
  61. endforeach()
  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()
  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()
  93. endif()
  94. endmacro()