CheckSymbolExists.cmake 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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()
  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. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  46. else()
  47. set(CHECK_SYMBOL_EXISTS_LIBS)
  48. endif()
  49. if(CMAKE_REQUIRED_INCLUDES)
  50. set(CMAKE_SYMBOL_EXISTS_INCLUDES
  51. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  52. else()
  53. set(CMAKE_SYMBOL_EXISTS_INCLUDES)
  54. endif()
  55. foreach(FILE ${FILES})
  56. set(CMAKE_CONFIGURABLE_FILE_CONTENT
  57. "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
  58. endforeach()
  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. ${CHECK_SYMBOL_EXISTS_LIBS}
  69. CMAKE_FLAGS
  70. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
  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()
  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()
  90. endif()
  91. endmacro()