CheckPrototypeDefinition.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #
  7. # Check if the protoype we expect is correct.
  8. #
  9. # check_prototype_definition(FUNCTION PROTOTYPE RETURN HEADER VARIABLE)
  10. #
  11. # ::
  12. #
  13. # FUNCTION - The name of the function (used to check if prototype exists)
  14. # PROTOTYPE- The prototype to check.
  15. # RETURN - The return value of the function.
  16. # HEADER - The header files required.
  17. # VARIABLE - The variable to store the result.
  18. # Will be created as an internal cache variable.
  19. #
  20. # Example:
  21. #
  22. # ::
  23. #
  24. # check_prototype_definition(getpwent_r
  25. # "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
  26. # "NULL"
  27. # "unistd.h;pwd.h"
  28. # SOLARIS_GETPWENT_R)
  29. #
  30. # The following variables may be set before calling this macro to modify
  31. # the way the check is run:
  32. #
  33. # ::
  34. #
  35. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  36. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  37. # CMAKE_REQUIRED_INCLUDES = list of include directories
  38. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  39. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  40. #
  41. get_filename_component(__check_proto_def_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  42. include_guard(GLOBAL)
  43. function(CHECK_PROTOTYPE_DEFINITION _FUNCTION _PROTOTYPE _RETURN _HEADER _VARIABLE)
  44. if (NOT DEFINED ${_VARIABLE})
  45. set(CHECK_PROTOTYPE_DEFINITION_CONTENT "/* */\n")
  46. set(CHECK_PROTOTYPE_DEFINITION_FLAGS ${CMAKE_REQUIRED_FLAGS})
  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_LIBS}
  74. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CHECK_PROTOTYPE_DEFINITION_FLAGS}
  75. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  76. OUTPUT_VARIABLE OUTPUT)
  77. if (${_VARIABLE})
  78. set(${_VARIABLE} 1 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
  79. if(NOT CMAKE_REQUIRED_QUIET)
  80. message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - True")
  81. endif()
  82. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  83. "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} passed with the following output:\n"
  84. "${OUTPUT}\n\n")
  85. else ()
  86. if(NOT CMAKE_REQUIRED_QUIET)
  87. message(STATUS "Checking prototype ${_FUNCTION} for ${_VARIABLE} - False")
  88. endif()
  89. set(${_VARIABLE} 0 CACHE INTERNAL "Have correct prototype for ${_FUNCTION}")
  90. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  91. "Determining if the prototype ${_FUNCTION} exists for ${_VARIABLE} failed with the following output:\n"
  92. "${OUTPUT}\n\n${_SOURCE}\n\n")
  93. endif ()
  94. endif()
  95. endfunction()