Merge topic 'cpack-innosetup-linux'
[kiteware-cmake.git] / Modules / CheckLibraryExists.cmake
blob8340500803b52f5836c601dd9cfae61d362c6811
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 CheckLibraryExists
6 ------------------
8 Check if the function exists.
10 .. command:: CHECK_LIBRARY_EXISTS
12   .. code-block:: cmake
14     CHECK_LIBRARY_EXISTS(LIBRARY FUNCTION LOCATION VARIABLE)
16   ::
18     LIBRARY  - the name of the library you are looking for
19     FUNCTION - the name of the function
20     LOCATION - location where the library should be found
21     VARIABLE - variable to store the result
22                Will be created as an internal cache variable.
26 The following variables may be set before calling this macro to modify
27 the way the check is run:
29 .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
31 .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
33 .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
35 .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
37 .. include:: /module/CMAKE_REQUIRED_QUIET.txt
39 #]=======================================================================]
41 include_guard(GLOBAL)
43 macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
44   if(NOT DEFINED "${VARIABLE}")
45     set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
46       "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
47     if(NOT CMAKE_REQUIRED_QUIET)
48       message(CHECK_START "Looking for ${FUNCTION} in ${LIBRARY}")
49     endif()
50     set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS)
51     if(CMAKE_REQUIRED_LINK_OPTIONS)
52       set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS
53         LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
54     endif()
55     set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
56     if(CMAKE_REQUIRED_LIBRARIES)
57       set(CHECK_LIBRARY_EXISTS_LIBRARIES
58         ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
59     endif()
61     if(CMAKE_C_COMPILER_LOADED)
62       set(_cle_source CheckFunctionExists.c)
63     elseif(CMAKE_CXX_COMPILER_LOADED)
64       set(_cle_source CheckFunctionExists.cxx)
65     else()
66       message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
67     endif()
69     try_compile(${VARIABLE}
70       SOURCE_FROM_FILE "${_cle_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
71       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
72       ${CHECK_LIBRARY_EXISTS_LINK_OPTIONS}
73       LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
74       CMAKE_FLAGS
75       -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
76       -DLINK_DIRECTORIES:STRING=${LOCATION}
77       )
78     unset(_cle_source)
80     if(${VARIABLE})
81       if(NOT CMAKE_REQUIRED_QUIET)
82         message(CHECK_PASS "found")
83       endif()
84       set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
85     else()
86       if(NOT CMAKE_REQUIRED_QUIET)
87         message(CHECK_FAIL "not found")
88       endif()
89       set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
90     endif()
91   endif()
92 endmacro()