CheckLibraryExists.cmake 3.2 KB

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