cmake/Modules/CheckPrototypeDefinition.cmake

132 lines
4.5 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2019-11-11 23:01:05 +01:00
#[=======================================================================[.rst:
CheckPrototypeDefinition
------------------------
Check if the prototype we expect is correct.
.. command:: check_prototype_definition
.. code-block:: cmake
check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
::
FUNCTION - The name of the function (used to check if prototype exists)
PROTOTYPE- The prototype to check.
RETURN - The return value of the function.
HEADER - The header files required.
VARIABLE - The variable to store the result.
Will be created as an internal cache variable.
Example:
.. code-block:: cmake
check_prototype_definition(getpwent_r
"struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
"NULL"
"unistd.h;pwd.h"
SOLARIS_GETPWENT_R)
The following variables may be set before calling this function to modify
the way the check is run:
2021-09-14 00:13:48 +02:00
``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_LINK_OPTIONS``
.. versionadded:: 3.14
list of options to pass to link command.
``CMAKE_REQUIRED_LIBRARIES``
list of libraries to link.
``CMAKE_REQUIRED_QUIET``
.. versionadded:: 3.1
execute quietly without messages.
2019-11-11 23:01:05 +01:00
#]=======================================================================]
2011-06-19 15:41:06 +03:00
#
get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
2018-04-23 21:13:27 +02:00
include_guard(GLOBAL)
2012-04-19 19:04:21 +03:00
2019-11-11 23:01:05 +01:00
function(check_prototype_definition _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
2011-06-19 15:41:06 +03:00
2015-04-27 22:25:09 +02:00
if (NOT DEFINED ${_VARIABLE})
2020-08-30 11:54:41 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
message(CHECK_START "Checking prototype ${_FUNCTION} for ${_VARIABLE}")
endif()
2011-06-19 15:41:06 +03:00
set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
2019-11-11 23:01:05 +01:00
if (CMAKE_REQUIRED_LINK_OPTIONS)
set(CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS
LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
else()
set(CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS)
endif()
2011-06-19 15:41:06 +03:00
if (CMAKE_REQUIRED_LIBRARIES)
set(CHECK_PROTOTYPE_DEFINITION_LIBS
2013-03-16 19:13:01 +02:00
LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
else()
2011-06-19 15:41:06 +03:00
set(CHECK_PROTOTYPE_DEFINITION_LIBS)
2013-03-16 19:13:01 +02:00
endif()
2011-06-19 15:41:06 +03:00
if (CMAKE_REQUIRED_INCLUDES)
set(CMAKE_SYMBOL_EXISTS_INCLUDES
"-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
2013-03-16 19:13:01 +02:00
else()
2011-06-19 15:41:06 +03:00
set(CMAKE_SYMBOL_EXISTS_INCLUDES)
2013-03-16 19:13:01 +02:00
endif()
2011-06-19 15:41:06 +03:00
foreach(_FILE ${_HEADER})
2016-10-30 18:24:19 +01:00
string(APPEND CHECK_PROTOTYPE_DEFINITION_HEADER
"#include <${_FILE}>\n")
2013-03-16 19:13:01 +02:00
endforeach()
2011-06-19 15:41:06 +03:00
set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
configure_file("${__check_proto_def_dir}/CheckPrototypeDefinition.c.in"
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c" @ONLY)
file(READ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c _SOURCE)
try_compile(${_VARIABLE}
${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
2019-11-11 23:01:05 +01:00
${CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS}
2013-03-16 19:13:01 +02:00
${CHECK_PROTOTYPE_DEFINITION_LIBS}
2011-06-19 15:41:06 +03:00
CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
"${CMAKE_SYMBOL_EXISTS_INCLUDES}"
OUTPUT_VARIABLE OUTPUT)
if (${_VARIABLE})
set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "True")
2015-04-27 22:25:09 +02:00
endif()
2011-06-19 15:41:06 +03:00
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
"${OUTPUT}\n\n")
2013-03-16 19:13:01 +02:00
else ()
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
2020-08-30 11:54:41 +02:00
message(CHECK_FAIL "False")
2015-04-27 22:25:09 +02:00
endif()
2011-06-19 15:41:06 +03:00
set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
"${OUTPUT}\n\n${_SOURCE}\n\n")
2013-03-16 19:13:01 +02:00
endif ()
endif()
2011-06-19 15:41:06 +03:00
2013-03-16 19:13:01 +02:00
endfunction()