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 CheckPrototypeDefinition
6 ------------------------
8 Check if the prototype we expect is correct.
10 .. command:: check_prototype_definition
14 check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
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.
29 check_prototype_definition(getpwent_r
30 "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
35 The following variables may be set before calling this function to modify
36 the way the check is run:
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``
51 execute quietly without messages.
52 #]=======================================================================]
56 get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
60 function(check_prototype_definition _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
62 if (NOT DEFINED ${_VARIABLE})
63 if(NOT CMAKE_REQUIRED_QUIET)
64 message(CHECK_START "Checking prototype ${_FUNCTION} for ${_VARIABLE}")
66 set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
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})
73 set(CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS)
75 if (CMAKE_REQUIRED_LIBRARIES)
76 set(CHECK_PROTOTYPE_DEFINITION_LIBS
77 LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
79 set(CHECK_PROTOTYPE_DEFINITION_LIBS)
81 if (CMAKE_REQUIRED_INCLUDES)
82 set(CMAKE_SYMBOL_EXISTS_INCLUDES
83 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
85 set(CMAKE_SYMBOL_EXISTS_INCLUDES)
88 foreach(_FILE ${_HEADER})
89 string(APPEND CHECK_PROTOTYPE_DEFINITION_HEADER
90 "#include <${_FILE}>\n")
93 set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
94 set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
95 set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
97 file(READ ${__check_proto_def_dir}/CheckPrototypeDefinition.c.in _SOURCE)
98 string(CONFIGURE "${_SOURCE}" _SOURCE @ONLY)
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)
110 set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
111 if(NOT CMAKE_REQUIRED_QUIET)
112 message(CHECK_PASS "True")
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"
118 if(NOT CMAKE_REQUIRED_QUIET)
119 message(CHECK_FAIL "False")
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")