CheckPrototypeDefinition.cmake 3.9 KB

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