CheckLibraryExists.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. ``CMAKE_REQUIRED_FLAGS``
  19. string of compile command line flags.
  20. ``CMAKE_REQUIRED_DEFINITIONS``
  21. list of macros to define (-DFOO=bar).
  22. ``CMAKE_REQUIRED_LINK_OPTIONS``
  23. .. versionadded:: 3.14
  24. list of options to pass to link command.
  25. ``CMAKE_REQUIRED_LIBRARIES``
  26. list of libraries to link.
  27. ``CMAKE_REQUIRED_QUIET``
  28. .. versionadded:: 3.1
  29. execute quietly without messages.
  30. #]=======================================================================]
  31. include_guard(GLOBAL)
  32. macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
  33. if(NOT DEFINED "${VARIABLE}")
  34. set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  35. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  36. if(NOT CMAKE_REQUIRED_QUIET)
  37. message(CHECK_START "Looking for ${FUNCTION} in ${LIBRARY}")
  38. endif()
  39. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS)
  40. if(CMAKE_REQUIRED_LINK_OPTIONS)
  41. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS
  42. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  43. endif()
  44. set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  45. if(CMAKE_REQUIRED_LIBRARIES)
  46. set(CHECK_LIBRARY_EXISTS_LIBRARIES
  47. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
  48. endif()
  49. if(CMAKE_C_COMPILER_LOADED)
  50. set(_cle_source CheckFunctionExists.c)
  51. elseif(CMAKE_CXX_COMPILER_LOADED)
  52. set(_cle_source CheckFunctionExists.cxx)
  53. else()
  54. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  55. endif()
  56. try_compile(${VARIABLE}
  57. SOURCE_FROM_FILE "${_cle_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
  58. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  59. ${CHECK_LIBRARY_EXISTS_LINK_OPTIONS}
  60. LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
  61. CMAKE_FLAGS
  62. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  63. -DLINK_DIRECTORIES:STRING=${LOCATION}
  64. )
  65. unset(_cle_source)
  66. if(${VARIABLE})
  67. if(NOT CMAKE_REQUIRED_QUIET)
  68. message(CHECK_PASS "found")
  69. endif()
  70. set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  71. else()
  72. if(NOT CMAKE_REQUIRED_QUIET)
  73. message(CHECK_FAIL "not found")
  74. endif()
  75. set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  76. endif()
  77. endif()
  78. endmacro()