• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# - Check if a C function can be linked
2# CHECK_FUNCTION_EXISTS(<function> <variable>)
3#
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).
8#
9# The following variables may be set before calling this macro to
10# modify the way the check is run:
11#
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
16
17#=============================================================================
18# Copyright 2002-2011 Kitware, Inc.
19#
20# Distributed under the OSI-approved BSD License (the "License");
21# see accompanying file Copyright.txt for details.
22#
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.)
29
30MACRO(CHECK_FUNCTION_EXISTS_EX FUNCTION VARIABLE)
31  IF(${VARIABLE} MATCHES "^${VARIABLE}$")
32    SET(MACRO_CHECK_FUNCTION_DEFINITIONS
33      "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
34    MESSAGE(STATUS "Looking for ${FUNCTION}")
35    IF(CMAKE_REQUIRED_LIBRARIES)
36      SET(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
37        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
38    ELSE(CMAKE_REQUIRED_LIBRARIES)
39      SET(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
40    ENDIF(CMAKE_REQUIRED_LIBRARIES)
41    IF(CMAKE_REQUIRED_INCLUDES)
42      SET(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
43        "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
44    ELSE(CMAKE_REQUIRED_INCLUDES)
45      SET(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
46    ENDIF(CMAKE_REQUIRED_INCLUDES)
47    TRY_COMPILE(${VARIABLE}
48      ${CMAKE_BINARY_DIR}
49	  ${PROJECT_SOURCE_DIR}/cmake/CheckFunctionExistsEx.c
50      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
51      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
52      "${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}"
53      "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
54      OUTPUT_VARIABLE OUTPUT)
55    IF(${VARIABLE})
56      SET(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
57      MESSAGE(STATUS "Looking for ${FUNCTION} - found")
58      FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
59        "Determining if the function ${FUNCTION} exists passed with the following output:\n"
60        "${OUTPUT}\n\n")
61    ELSE(${VARIABLE})
62      MESSAGE(STATUS "Looking for ${FUNCTION} - not found")
63      SET(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
64      FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
65        "Determining if the function ${FUNCTION} exists failed with the following output:\n"
66        "${OUTPUT}\n\n")
67    ENDIF(${VARIABLE})
68  ENDIF()
69ENDMACRO(CHECK_FUNCTION_EXISTS_EX)
70