1 # - Check if the protoype we expect is correct.
2 # check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
4 # FUNCTION - The name of the function (used to check if prototype exists)
5 # PROTOTYPE- The prototype to check.
6 # RETURN - The return value of the function.
7 # HEADER - The header files required.
8 # VARIABLE - The variable to store the result.
12 # check_prototype_definition(getpwent_r
13 # "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
18 # The following variables may be set before calling this macro to
19 # modify the way the check is run:
21 # CMAKE_REQUIRED_FLAGS = string of compile command line flags
22 # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
23 # CMAKE_REQUIRED_INCLUDES = list of include directories
24 # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
27 function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
28 if (${_VARIABLE} MATCHES "^${_VARIABLE}$")
29 set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
31 set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
32 if (CMAKE_REQUIRED_LIBRARIES)
33 set(CHECK_PROTOTYPE_DEFINITION_LIBS
34 "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
35 else(CMAKE_REQUIRED_LIBRARIES)
36 set(CHECK_PROTOTYPE_DEFINITION_LIBS)
37 endif(CMAKE_REQUIRED_LIBRARIES)
38 if (CMAKE_REQUIRED_INCLUDES)
39 set(CMAKE_SYMBOL_EXISTS_INCLUDES
40 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
41 else(CMAKE_REQUIRED_INCLUDES)
42 set(CMAKE_SYMBOL_EXISTS_INCLUDES)
43 endif(CMAKE_REQUIRED_INCLUDES)
45 foreach(_FILE ${_HEADER})
46 set(CHECK_PROTOTYPE_DEFINITION_HEADER
47 "${CHECK_PROTOTYPE_DEFINITION_HEADER}#include <${_FILE}>\n")
50 set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
51 set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
52 set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
54 configure_file("${PROJECT_SOURCE_DIR}/cmake/CheckPrototypeDefinition.c.in"
55 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c" @ONLY)
57 file(READ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c _SOURCE)
59 try_compile(${_VARIABLE}
61 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c
62 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
63 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
64 "${CHECK_PROTOTYPE_DEFINITION_LIBS}"
65 "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
66 OUTPUT_VARIABLE OUTPUT)
69 set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
70 message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - True")
71 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
72 "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
75 message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - False")
76 set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
77 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
78 "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
79 "${OUTPUT}\n\n${_SOURCE}\n\n")
82 endfunction(CHECK_PROTOTYPE_DEFINITION)