CheckPrototypeDefinition.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckPrototypeDefinition
  5. ------------------------
  6. Check if the prototype we expect is correct.
  7. .. command:: check_prototype_definition
  8. .. code-block:: cmake
  9. check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
  10. ::
  11. FUNCTION - The name of the function (used to check if prototype exists)
  12. PROTOTYPE- The prototype to check.
  13. RETURN - The return value of the function.
  14. HEADER - The header files required.
  15. VARIABLE - The variable to store the result.
  16. Will be created as an internal cache variable.
  17. Example:
  18. .. code-block:: cmake
  19. check_prototype_definition(getpwent_r
  20. "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
  21. "NULL"
  22. "unistd.h;pwd.h"
  23. SOLARIS_GETPWENT_R)
  24. The following variables may be set before calling this function to modify
  25. the way the check is run:
  26. ::
  27. CMAKE_REQUIRED_FLAGS = string of compile command line flags
  28. CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  29. CMAKE_REQUIRED_INCLUDES = list of include directories
  30. CMAKE_REQUIRED_LINK_OPTIONS = list of options to pass to link command
  31. CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  32. CMAKE_REQUIRED_QUIET = execute quietly without messages
  33. #]=======================================================================]
  34. #
  35. get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  36. include_guard(GLOBAL)
  37. function(check_prototype_definition _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
  38. if (NOT DEFINED ${_VARIABLE})
  39. set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
  40. set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
  41. if (CMAKE_REQUIRED_LINK_OPTIONS)
  42. set(CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS
  43. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  44. else()
  45. set(CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS)
  46. endif()
  47. if (CMAKE_REQUIRED_LIBRARIES)
  48. set(CHECK_PROTOTYPE_DEFINITION_LIBS
  49. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  50. else()
  51. set(CHECK_PROTOTYPE_DEFINITION_LIBS)
  52. endif()
  53. if (CMAKE_REQUIRED_INCLUDES)
  54. set(CMAKE_SYMBOL_EXISTS_INCLUDES
  55. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  56. else()
  57. set(CMAKE_SYMBOL_EXISTS_INCLUDES)
  58. endif()
  59. foreach(_FILE ${_HEADER})
  60. string(APPEND CHECK_PROTOTYPE_DEFINITION_HEADER
  61. "#include <${_FILE}>\n")
  62. endforeach()
  63. set(CHECK_PROTOTYPE_DEFINITION_SYMBOL ${_FUNCTION})
  64. set(CHECK_PROTOTYPE_DEFINITION_PROTO ${_PROTOTYPE})
  65. set(CHECK_PROTOTYPE_DEFINITION_RETURN ${_RETURN})
  66. configure_file("${__check_proto_def_dir}/CheckPrototypeDefinition.c.in"
  67. "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c" @ONLY)
  68. file(READ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c _SOURCE)
  69. try_compile(${_VARIABLE}
  70. ${CMAKE_BINARY_DIR}
  71. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckPrototypeDefinition.c
  72. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  73. ${CHECK_PROTOTYPE_DEFINITION_LINK_OPTIONS}
  74. ${CHECK_PROTOTYPE_DEFINITION_LIBS}
  75. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
  76. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  77. OUTPUT_VARIABLE OUTPUT)
  78. if (${_VARIABLE})
  79. set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
  80. if(NOT CMAKE_REQUIRED_QUIET)
  81. message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - True")
  82. endif()
  83. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  84. "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
  85. "${OUTPUT}\n\n")
  86. else ()
  87. if(NOT CMAKE_REQUIRED_QUIET)
  88. message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - False")
  89. endif()
  90. set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
  91. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  92. "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
  93. "${OUTPUT}\n\n${_SOURCE}\n\n")
  94. endif ()
  95. endif()
  96. endfunction()