CheckFunctionExists.cmake 3.77 KB
Newer Older
1 2 3 4 5 6
#.rst:
# CheckFunctionExists
# -------------------
#
# Check if a C function can be linked
#
7 8 9 10
# CHECK_FUNCTION_EXISTS(<function> <variable>)
#
# Check that the <function> is provided by libraries on the system and
# store the result in a <variable>.  This does not verify that any
11 12
# system header file declares the function, only that it can be found at
# link time (consider using CheckSymbolExists).
13
# <variable> will be created as an internal cache variable.
14 15 16
#
# The following variables may be set before calling this macro to modify
# the way the check is run:
17
#
18
# ::
19
#
20 21 22 23
#   CMAKE_REQUIRED_FLAGS = string of compile command line flags
#   CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
#   CMAKE_REQUIRED_INCLUDES = list of include directories
#   CMAKE_REQUIRED_LIBRARIES = list of libraries to link
24
#   CMAKE_REQUIRED_QUIET = execute quietly without messages
25

26
#=============================================================================
27
# Copyright 2002-2011 Kitware, Inc.
28 29 30 31 32 33 34 35
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
36
# (To distribute this file outside of CMake, substitute the full
37 38
#  License text for the above reference.)

39 40


41
macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
42
  if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
43
    set(MACRO_CHECK_FUNCTION_DEFINITIONS
44
      "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
45 46 47
    if(NOT CMAKE_REQUIRED_QUIET)
      message(STATUS "Looking for ${FUNCTION}")
    endif()
48 49
    if(CMAKE_REQUIRED_LIBRARIES)
      set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
50
        LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
51
    else()
52
      set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
53
    endif()
54 55
    if(CMAKE_REQUIRED_INCLUDES)
      set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
56
        "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
57
    else()
58
      set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
59
    endif()
60 61 62 63 64 65 66 67 68 69

    if(CMAKE_C_COMPILER_LOADED)
      set(_cfe_source ${CMAKE_ROOT}/Modules/CheckFunctionExists.c)
    elseif(CMAKE_CXX_COMPILER_LOADED)
      set(_cfe_source ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckFunctionExists/CheckFunctionExists.cxx)
      configure_file(${CMAKE_ROOT}/Modules/CheckFunctionExists.c "${_cfe_source}" COPYONLY)
    else()
      message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
    endif()

70
    try_compile(${VARIABLE}
71
      ${CMAKE_BINARY_DIR}
72
      ${_cfe_source}
73
      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
74
      ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
75
      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
76
      "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
77
      OUTPUT_VARIABLE OUTPUT)
78 79
    unset(_cfe_source)

80 81
    if(${VARIABLE})
      set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
82 83 84
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Looking for ${FUNCTION} - found")
      endif()
85
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
86 87
        "Determining if the function ${FUNCTION} exists passed with the following output:\n"
        "${OUTPUT}\n\n")
88
    else()
89 90 91
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Looking for ${FUNCTION} - not found")
      endif()
92 93
      set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
94
        "Determining if the function ${FUNCTION} exists failed with the following output:\n"
95
        "${OUTPUT}\n\n")
96 97 98
    endif()
  endif()
endmacro()