CheckLibraryExists.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckLibraryExists
  5. ------------------
  6. Check once if the function exists in system or specified library.
  7. .. command:: check_library_exists
  8. .. code-block:: cmake
  9. check_library_exists(<library> <function> <location> <variable>)
  10. Check that the library ``<library>`` exists in the given location
  11. ``<location>`` and has the specified ``<function>``. The result is stored in
  12. an internal cache variable ``<variable>``. If ``<location>`` is empty string,
  13. default directories are searched.
  14. Prefer using :module:`CheckSymbolExists` or :module:`CheckSourceCompiles`
  15. instead of this module for more robust detection if a function is available in
  16. a library.
  17. The following variables may be set before calling this macro to modify
  18. the way the check is run:
  19. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  20. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  21. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  22. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  23. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  24. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  25. Examples
  26. ^^^^^^^^
  27. This module can be useful for performing so-called sanity checks to verify that
  28. the specified library provides the expected functionality and is indeed the
  29. correct one being located.
  30. For example, to check if the ``curl`` library exists in the default paths and
  31. has the ``curl_easy_perform`` function:
  32. .. code-block:: cmake
  33. include(CheckLibraryExists)
  34. check_library_exists(curl curl_easy_perform "" HAVE_LIBRARY_CURL)
  35. To check if library exists in specific non-standard location and has a specified
  36. function:
  37. .. code-block:: cmake
  38. include(CheckLibraryExists)
  39. check_library_exists(curl curl_easy_perform "/opt/curl/lib" HAVE_LIBRARY_CURL)
  40. Also :ref:`IMPORTED library <add_library imported libraries>` (for example,
  41. from the ``find_package()`` call) can be used:
  42. .. code-block:: cmake
  43. find_package(CURL)
  44. # ...
  45. if(TARGET CURL::libcurl)
  46. include(CheckLibraryExists)
  47. check_library_exists(CURL::libcurl curl_easy_perform "" HAVE_LIBRARY_CURL)
  48. endif()
  49. #]=======================================================================]
  50. include_guard(GLOBAL)
  51. macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
  52. if(NOT DEFINED "${VARIABLE}")
  53. set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  54. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  55. if(NOT CMAKE_REQUIRED_QUIET)
  56. message(CHECK_START "Looking for ${FUNCTION} in ${LIBRARY}")
  57. endif()
  58. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS)
  59. if(CMAKE_REQUIRED_LINK_OPTIONS)
  60. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS
  61. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  62. endif()
  63. set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  64. if(CMAKE_REQUIRED_LIBRARIES)
  65. set(CHECK_LIBRARY_EXISTS_LIBRARIES
  66. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
  67. endif()
  68. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  69. set(_CLE_LINK_DIRECTORIES
  70. "-DLINK_DIRECTORIES:STRING=${LOCATION};${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  71. else()
  72. set(_CLE_LINK_DIRECTORIES "-DLINK_DIRECTORIES:STRING=${LOCATION}")
  73. endif()
  74. if(CMAKE_C_COMPILER_LOADED)
  75. set(_cle_source CheckFunctionExists.c)
  76. elseif(CMAKE_CXX_COMPILER_LOADED)
  77. set(_cle_source CheckFunctionExists.cxx)
  78. else()
  79. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  80. endif()
  81. try_compile(${VARIABLE}
  82. SOURCE_FROM_FILE "${_cle_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
  83. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  84. ${CHECK_LIBRARY_EXISTS_LINK_OPTIONS}
  85. LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
  86. CMAKE_FLAGS
  87. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  88. "${_CLE_LINK_DIRECTORIES}"
  89. )
  90. unset(_cle_source)
  91. unset(_CLE_LINK_DIRECTORIES)
  92. if(${VARIABLE})
  93. if(NOT CMAKE_REQUIRED_QUIET)
  94. message(CHECK_PASS "found")
  95. endif()
  96. set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  97. else()
  98. if(NOT CMAKE_REQUIRED_QUIET)
  99. message(CHECK_FAIL "not found")
  100. endif()
  101. set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  102. endif()
  103. endif()
  104. endmacro()