Merge topic 'cxx-checks-tolerate-unused-arguments'
[kiteware-cmake.git] / Modules / CheckFunctionExists.cmake
blobe7c47a4e7a62f2ca943f604be209a0c501a0c5fa
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 CheckFunctionExists
6 -------------------
8 Check if a C function can be linked
10 .. command:: check_function_exists
12   .. code-block:: cmake
14     check_function_exists(<function> <variable>)
16   Checks that the ``<function>`` is provided by libraries on the system and store
17   the result in a ``<variable>``, which will be created as an internal
18   cache variable.
20 The following variables may be set before calling this macro to modify the
21 way the check is run:
23 .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
25 .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
27 .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
29 .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
31 .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
33 .. include:: /module/CMAKE_REQUIRED_QUIET.txt
35 .. note::
37   Prefer using :Module:`CheckSymbolExists` instead of this module,
38   for the following reasons:
40   * ``check_function_exists()`` can't detect functions that are inlined
41     in headers or specified as a macro.
43   * ``check_function_exists()`` can't detect anything in the 32-bit
44     versions of the Win32 API, because of a mismatch in calling conventions.
46   * ``check_function_exists()`` only verifies linking, it does not verify
47     that the function is declared in system headers.
48 #]=======================================================================]
50 include_guard(GLOBAL)
52 macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
53   if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
54     set(MACRO_CHECK_FUNCTION_DEFINITIONS
55       "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
56     if(NOT CMAKE_REQUIRED_QUIET)
57       message(CHECK_START "Looking for ${FUNCTION}")
58     endif()
59     if(CMAKE_REQUIRED_LINK_OPTIONS)
60       set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
61         LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
62     else()
63       set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
64     endif()
65     if(CMAKE_REQUIRED_LIBRARIES)
66       set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
67         LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
68     else()
69       set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
70     endif()
71     if(CMAKE_REQUIRED_INCLUDES)
72       set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
73         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
74     else()
75       set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
76     endif()
78     if(CMAKE_C_COMPILER_LOADED)
79       set(_cfe_source CheckFunctionExists.c)
80     elseif(CMAKE_CXX_COMPILER_LOADED)
81       set(_cfe_source CheckFunctionExists.cxx)
82     else()
83       message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
84     endif()
86     try_compile(${VARIABLE}
87       SOURCE_FROM_FILE "${_cfe_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
88       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
89       ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
90       ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
91       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
92       "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
93       )
94     unset(_cfe_source)
96     if(${VARIABLE})
97       set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
98       if(NOT CMAKE_REQUIRED_QUIET)
99         message(CHECK_PASS "found")
100       endif()
101     else()
102       if(NOT CMAKE_REQUIRED_QUIET)
103         message(CHECK_FAIL "not found")
104       endif()
105       set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
106     endif()
107   endif()
108 endmacro()