CheckLibraryExists.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckLibraryExists
  5. ------------------
  6. Check if the function exists.
  7. .. command:: CHECK_LIBRARY_EXISTS
  8. .. code-block:: cmake
  9. CHECK_LIBRARY_EXISTS(LIBRARY FUNCTION LOCATION VARIABLE)
  10. ::
  11. LIBRARY - the name of the library you are looking for
  12. FUNCTION - the name of the function
  13. LOCATION - location where the library should be found
  14. VARIABLE - variable to store the result
  15. Will be created as an internal cache variable.
  16. The following variables may be set before calling this macro to modify
  17. the way the check is run:
  18. ::
  19. CMAKE_REQUIRED_FLAGS = string of compile command line flags
  20. CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  21. CMAKE_REQUIRED_LINK_OPTIONS = list of options to pass to link command
  22. CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  23. CMAKE_REQUIRED_QUIET = execute quietly without messages
  24. #]=======================================================================]
  25. include_guard(GLOBAL)
  26. macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
  27. if(NOT DEFINED "${VARIABLE}")
  28. set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  29. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  30. if(NOT CMAKE_REQUIRED_QUIET)
  31. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
  32. endif()
  33. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS)
  34. if(CMAKE_REQUIRED_LINK_OPTIONS)
  35. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS
  36. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  37. endif()
  38. set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  39. if(CMAKE_REQUIRED_LIBRARIES)
  40. set(CHECK_LIBRARY_EXISTS_LIBRARIES
  41. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
  42. endif()
  43. if(CMAKE_C_COMPILER_LOADED)
  44. set(_cle_source ${CMAKE_ROOT}/Modules/CheckFunctionExists.c)
  45. elseif(CMAKE_CXX_COMPILER_LOADED)
  46. set(_cle_source ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckLibraryExists/CheckFunctionExists.cxx)
  47. configure_file(${CMAKE_ROOT}/Modules/CheckFunctionExists.c "${_cle_source}" COPYONLY)
  48. else()
  49. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  50. endif()
  51. try_compile(${VARIABLE}
  52. ${CMAKE_BINARY_DIR}
  53. ${_cle_source}
  54. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  55. ${CHECK_LIBRARY_EXISTS_LINK_OPTIONS}
  56. LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
  57. CMAKE_FLAGS
  58. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  59. -DLINK_DIRECTORIES:STRING=${LOCATION}
  60. OUTPUT_VARIABLE OUTPUT)
  61. unset(_cle_source)
  62. if(${VARIABLE})
  63. if(NOT CMAKE_REQUIRED_QUIET)
  64. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
  65. endif()
  66. set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  67. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  68. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  69. "passed with the following output:\n"
  70. "${OUTPUT}\n\n")
  71. else()
  72. if(NOT CMAKE_REQUIRED_QUIET)
  73. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
  74. endif()
  75. set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  76. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  77. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  78. "failed with the following output:\n"
  79. "${OUTPUT}\n\n")
  80. endif()
  81. endif()
  82. endmacro()