CheckPrototypeDefinition.cmake 4.35 KB
Newer Older
1 2 3 4 5 6
#.rst:
# CheckPrototypeDefinition
# ------------------------
#
# Check if the protoype we expect is correct.
#
7
# check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
8 9 10 11 12 13 14 15
#
# ::
#
#   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.
16
#              Will be created as an internal cache variable.
17
#
18
# Example:
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#
# ::
#
#   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 macro to modify
# the way the check is run:
#
# ::
#
#   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_LIBRARIES = list of libraries to link
37
#   CMAKE_REQUIRED_QUIET = execute quietly without messages
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#=============================================================================
# Copyright 2005-2009 Kitware, Inc.
# Copyright 2010-2011 Andreas Schneider <asn@cryptomilk.org>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
#  License text for the above reference.)
#

54

55 56
get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)

57

58 59
function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)

60
  if (NOT DEFINED ${_VARIABLE})
61 62 63 64 65
    set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")

    set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
    if (CMAKE_REQUIRED_LIBRARIES)
      set(CHECK_PROTOTYPE_DEFINITION_LIBS
66
        LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
67
    else()
68
      set(CHECK_PROTOTYPE_DEFINITION_LIBS)
69
    endif()
70 71 72
    if (CMAKE_REQUIRED_INCLUDES)
      set(CMAKE_SYMBOL_EXISTS_INCLUDES
        "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
73
    else()
74
      set(CMAKE_SYMBOL_EXISTS_INCLUDES)
75
    endif()
76 77 78 79

    foreach(_FILE ${_HEADER})
      set(CHECK_PROTOTYPE_DEFINITION_HEADER
        "${CHECK_PROTOTYPE_DEFINITION_HEADER}#include <${_FILE}>\n")
80
    endforeach()
81 82 83 84 85 86 87 88 89 90 91 92 93 94

    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}
95
      ${CHECK_PROTOTYPE_DEFINITION_LIBS}
96 97 98 99 100 101
      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}")
102 103 104
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - True")
      endif()
105 106 107
      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")
108
    else ()
109 110 111
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - False")
      endif()
112 113 114 115
      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")
116 117
    endif ()
  endif()
118

119
endfunction()