CheckPrototypeDefinition.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # - Check if the protoype we expect is correct.
  2. # check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
  3. # FUNCTION - The name of the function (used to check if prototype exists)
  4. # PROTOTYPE- The prototype to check.
  5. # RETURN - The return value of the function.
  6. # HEADER - The header files required.
  7. # VARIABLE - The variable to store the result.
  8. # Example:
  9. # check_prototype_definition(getpwent_r
  10. # "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
  11. # "NULL"
  12. # "unistd.h;pwd.h"
  13. # SOLARIS_GETPWENT_R)
  14. # The following variables may be set before calling this macro to
  15. # modify the way the check is run:
  16. #
  17. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  18. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  19. # CMAKE_REQUIRED_INCLUDES = list of include directories
  20. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  21. #=============================================================================
  22. # Copyright 2005-2009 Kitware, Inc.
  23. # Copyright 2010-2011 Andreas Schneider <[email protected]>
  24. #
  25. # Distributed under the OSI-approved BSD License (the "License");
  26. # see accompanying file Copyright.txt for details.
  27. #
  28. # This software is distributed WITHOUT ANY WARRANTY; without even the
  29. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30. # See the License for more information.
  31. #=============================================================================
  32. # (To distribute this file outside of CMake, substitute the full
  33. # License text for the above reference.)
  34. #
  35. include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  36. get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  37. function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
  38. if ("${_VARIABLE}" MATCHES "^${_VARIABLE}$")
  39. set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
  40. set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
  41. if (CMAKE_REQUIRED_LIBRARIES)
  42. # this one translates potentially used imported library targets to their files on disk
  43. cmake_expand_imported_targets(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  44. set(CHECK_PROTOTYPE_DEFINITION_LIBS
  45. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
  46. else(CMAKE_REQUIRED_LIBRARIES)
  47. set(CHECK_PROTOTYPE_DEFINITION_LIBS)
  48. endif(CMAKE_REQUIRED_LIBRARIES)
  49. if (CMAKE_REQUIRED_INCLUDES)
  50. set(CMAKE_SYMBOL_EXISTS_INCLUDES
  51. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  52. else(CMAKE_REQUIRED_INCLUDES)
  53. set(CMAKE_SYMBOL_EXISTS_INCLUDES)
  54. endif(CMAKE_REQUIRED_INCLUDES)
  55. foreach(_FILE ${_HEADER})
  56. set(CHECK_PROTOTYPE_DEFINITION_HEADER
  57. "${CHECK_PROTOTYPE_DEFINITION_HEADER}#include <${_FILE}>\n")
  58. endforeach(_FILE)
  59. set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
  60. set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
  61. set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
  62. configure_file("${__check_proto_def_dir}/CheckPrototypeDefinition.c.in"
  63. "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c" @ONLY)
  64. file(READ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c _SOURCE)
  65. try_compile(${_VARIABLE}
  66. ${CMAKE_BINARY_DIR}
  67. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c
  68. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  69. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
  70. "${CHECK_PROTOTYPE_DEFINITION_LIBS}"
  71. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  72. OUTPUT_VARIABLE OUTPUT)
  73. if (${_VARIABLE})
  74. set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
  75. message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - True")
  76. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  77. "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
  78. "${OUTPUT}\n\n")
  79. else (${_VARIABLE})
  80. message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - False")
  81. set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
  82. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  83. "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
  84. "${OUTPUT}\n\n${_SOURCE}\n\n")
  85. endif (${_VARIABLE})
  86. endif("${_VARIABLE}" MATCHES "^${_VARIABLE}$")
  87. endfunction(CHECK_PROTOTYPE_DEFINITION)