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:
8 Check if the function exists.
10 .. command:: CHECK_LIBRARY_EXISTS
14 CHECK_LIBRARY_EXISTS(LIBRARY FUNCTION LOCATION VARIABLE)
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 ``CMAKE_REQUIRED_FLAGS``
30 string of compile command line flags.
31 ``CMAKE_REQUIRED_DEFINITIONS``
32 list of macros to define (-DFOO=bar).
33 ``CMAKE_REQUIRED_LINK_OPTIONS``
34 .. versionadded:: 3.14
35 list of options to pass to link command.
36 ``CMAKE_REQUIRED_LIBRARIES``
37 list of libraries to link.
38 ``CMAKE_REQUIRED_QUIET``
40 execute quietly without messages.
41 #]=======================================================================]
45 macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
46 if(NOT DEFINED "${VARIABLE}")
47 set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
48 "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
49 if(NOT CMAKE_REQUIRED_QUIET)
50 message(CHECK_START "Looking for ${FUNCTION} in ${LIBRARY}")
52 set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS)
53 if(CMAKE_REQUIRED_LINK_OPTIONS)
54 set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS
55 LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
57 set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
58 if(CMAKE_REQUIRED_LIBRARIES)
59 set(CHECK_LIBRARY_EXISTS_LIBRARIES
60 ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
63 if(CMAKE_C_COMPILER_LOADED)
64 set(_cle_source CheckFunctionExists.c)
65 elseif(CMAKE_CXX_COMPILER_LOADED)
66 set(_cle_source CheckFunctionExists.cxx)
68 message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
71 try_compile(${VARIABLE}
72 SOURCE_FROM_FILE "${_cle_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
73 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
74 ${CHECK_LIBRARY_EXISTS_LINK_OPTIONS}
75 LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
77 -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
78 -DLINK_DIRECTORIES:STRING=${LOCATION}
79 OUTPUT_VARIABLE OUTPUT)
83 if(NOT CMAKE_REQUIRED_QUIET)
84 message(CHECK_PASS "found")
86 set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
87 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
88 "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
89 "passed with the following output:\n"
92 if(NOT CMAKE_REQUIRED_QUIET)
93 message(CHECK_FAIL "not found")
95 set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
96 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
97 "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
98 "failed with the following output:\n"