1 # - Check if a C function can be linked
2 # CHECK_FUNCTION_EXISTS(<function> <variable>)
4 # Check that the <function> is provided by libraries on the system and
5 # store the result in a <variable>. This does not verify that any
6 # system header file declares the function, only that it can be found
7 # at link time (considure using CheckSymbolExists).
9 # The following variables may be set before calling this macro to
10 # modify the way the check is run:
12 # CMAKE_REQUIRED_FLAGS = string of compile command line flags
13 # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
14 # CMAKE_REQUIRED_INCLUDES = list of include directories
15 # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
17 #=============================================================================
18 # Copyright 2002-2011 Kitware, Inc.
20 # Distributed under the OSI-approved BSD License (the "License");
21 # see accompanying file Copyright.txt for details.
23 # This software is distributed WITHOUT ANY WARRANTY; without even the
24 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25 # See the License for more information.
26 #=============================================================================
27 # (To distribute this file outside of CMake, substitute the full
28 # License text for the above reference.)
30 include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
33 macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
34 if("${VARIABLE}" MATCHES "^${VARIABLE}$")
35 set(MACRO_CHECK_FUNCTION_DEFINITIONS
36 "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
37 message(STATUS "Looking for ${FUNCTION}")
38 if(CMAKE_REQUIRED_LIBRARIES)
39 # this one translates potentially used imported library targets to their files on disk
40 CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
41 set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
42 "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
44 set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
46 if(CMAKE_REQUIRED_INCLUDES)
47 set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
48 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
50 set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
52 try_compile(${VARIABLE}
54 ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
55 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
56 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
57 "${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}"
58 "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
59 OUTPUT_VARIABLE OUTPUT)
61 set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
62 message(STATUS "Looking for ${FUNCTION} - found")
63 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
64 "Determining if the function ${FUNCTION} exists passed with the following output:\n"
67 message(STATUS "Looking for ${FUNCTION} - not found")
68 set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
69 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
70 "Determining if the function ${FUNCTION} exists failed with the following output:\n"