Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CheckPrototypeDefinition.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 CheckPrototypeDefinition
6 ------------------------
7
8 Check if the prototype we expect is correct.
9
10 .. command:: check_prototype_definition
11
12   .. code-block:: cmake
13
14     check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
15
16   ::
17
18     FUNCTION - The name of the function (used to check if prototype exists)
19     PROTOTYPE- The prototype to check.
20     RETURN - The return value of the function.
21     HEADER - The header files required.
22     VARIABLE - The variable to store the result.
23                Will be created as an internal cache variable.
24
25   Example:
26
27   .. code-block:: cmake
28
29     check_prototype_definition(getpwent_r
30      "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
31      "NULL"
32      "unistd.h;pwd.h"
33      SOLARIS_GETPWENT_R)
34
35 The following variables may be set before calling this function to modify
36 the way the check is run:
37
38 ``CMAKE_REQUIRED_FLAGS``
39   string of compile command line flags.
40 ``CMAKE_REQUIRED_DEFINITIONS``
41   list of macros to define (-DFOO=bar).
42 ``CMAKE_REQUIRED_INCLUDES``
43   list of include directories.
44 ``CMAKE_REQUIRED_LINK_OPTIONS``
45   .. versionadded:: 3.14
46     list of options to pass to link command.
47 ``CMAKE_REQUIRED_LIBRARIES``
48   list of libraries to link.
49 ``CMAKE_REQUIRED_QUIET``
50   .. versionadded:: 3.1
51     execute quietly without messages.
52 #]=======================================================================]
53
54 #
55
56 get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
57
58 include_guard(GLOBAL)
59
60 function(check_prototype_definition _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
61
62   if (NOT DEFINED ${_VARIABLE})
63     if(NOT CMAKE_REQUIRED_QUIET)
64       message(CHECK_START "Checking prototype ${_FUNCTION} for ${_VARIABLE}")
65     endif()
66     set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
67
68     set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
69     if (CMAKE_REQUIRED_LINK_OPTIONS)
70       set(CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS
71         LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
72     else()
73       set(CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS)
74     endif()
75     if (CMAKE_REQUIRED_LIBRARIES)
76       set(CHECK_PROTOTYPE_DEFINITION_LIBS
77         LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
78     else()
79       set(CHECK_PROTOTYPE_DEFINITION_LIBS)
80     endif()
81     if (CMAKE_REQUIRED_INCLUDES)
82       set(CMAKE_SYMBOL_EXISTS_INCLUDES
83         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
84     else()
85       set(CMAKE_SYMBOL_EXISTS_INCLUDES)
86     endif()
87
88     foreach(_FILE ${_HEADER})
89       string(APPEND CHECK_PROTOTYPE_DEFINITION_HEADER
90         "#include <${_FILE}>\n")
91     endforeach()
92
93     set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
94     set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
95     set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
96
97     file(READ ${__check_proto_def_dir}/CheckPrototypeDefinition.c.in _SOURCE)
98     string(CONFIGURE "${_SOURCE}" _SOURCE @ONLY)
99
100     try_compile(${_VARIABLE}
101       SOURCE_FROM_VAR CheckPrototypeDefinition.c _SOURCE
102       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
103       ${CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS}
104       ${CHECK_PROTOTYPE_DEFINITION_LIBS}
105       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
106       "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
107       OUTPUT_VARIABLE OUTPUT)
108
109     if (${_VARIABLE})
110       set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
111       if(NOT CMAKE_REQUIRED_QUIET)
112         message(CHECK_PASS "True")
113       endif()
114       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
115         "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
116         "${OUTPUT}\n\n")
117     else ()
118       if(NOT CMAKE_REQUIRED_QUIET)
119         message(CHECK_FAIL "False")
120       endif()
121       set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
122       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
123         "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
124         "${OUTPUT}\n\n${_SOURCE}\n\n")
125     endif ()
126   endif()
127
128 endfunction()