FindWinSock.cmake 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #.rst:
  2. # FindWinSock
  3. # --------
  4. #
  5. # Find the native realtime includes and library.
  6. #
  7. # IMPORTED Targets
  8. # ^^^^^^^^^^^^^^^^
  9. #
  10. # This module defines :prop_tgt:`IMPORTED` target ``WINSOCK::WINSOCK``, if
  11. # WINSOCK has been found.
  12. #
  13. # Result Variables
  14. # ^^^^^^^^^^^^^^^^
  15. #
  16. # This module defines the following variables:
  17. #
  18. # ::
  19. #
  20. # WINSOCK_INCLUDE_DIRS - where to find winsock.h, etc.
  21. # WINSOCK_LIBRARIES - List of libraries when using librt.
  22. # WINSOCK_FOUND - True if realtime library found.
  23. #
  24. # Hints
  25. # ^^^^^
  26. #
  27. # A user may set ``WINSOCK_ROOT`` to a realtime installation root to tell this
  28. # module where to look.
  29. macro(REMOVE_DUPLICATE_PATHS LIST_VAR)
  30. set(WINSOCK_LIST "")
  31. foreach(PATH IN LISTS ${LIST_VAR})
  32. get_filename_component(PATH "${PATH}" REALPATH)
  33. list(APPEND WINSOCK_LIST "${PATH}")
  34. endforeach(PATH)
  35. set(${LIST_VAR} ${WINSOCK_LIST})
  36. list(REMOVE_DUPLICATES ${LIST_VAR})
  37. endmacro(REMOVE_DUPLICATE_PATHS)
  38. set(WINSOCK_INCLUDE_PATHS "${WINSOCK_ROOT}/include/")
  39. if(MINGW)
  40. execute_process(
  41. COMMAND ${CMAKE_C_COMPILER} -xc -E -v -
  42. RESULT_VARIABLE RESULT
  43. INPUT_FILE nul
  44. ERROR_VARIABLE ERR
  45. OUTPUT_QUIET
  46. )
  47. if (NOT RESULT)
  48. string(FIND "${ERR}" "#include <...> search starts here:" START)
  49. string(FIND "${ERR}" "End of search list." END)
  50. if (NOT ${START} EQUAL -1 AND NOT ${END} EQUAL -1)
  51. math(EXPR START "${START} + 36")
  52. math(EXPR END "${END} - 1")
  53. math(EXPR LENGTH "${END} - ${START}")
  54. string(SUBSTRING "${ERR}" ${START} ${LENGTH} WINSOCK_INCLUDE_PATHS)
  55. string(REPLACE "\n " ";" WINSOCK_INCLUDE_PATHS "${WINSOCK_INCLUDE_PATHS}")
  56. list(REVERSE WINSOCK_INCLUDE_PATHS)
  57. endif()
  58. endif()
  59. endif()
  60. remove_duplicate_paths(WINSOCK_INCLUDE_PATHS)
  61. set(WINSOCK_LIBRARY_PATHS "${WINSOCK_ROOT}/lib/")
  62. if(MINGW)
  63. execute_process(
  64. COMMAND ${CMAKE_C_COMPILER} -print-search-dirs
  65. RESULT_VARIABLE RESULT
  66. OUTPUT_VARIABLE OUT
  67. ERROR_QUIET
  68. )
  69. if (NOT RESULT)
  70. string(REGEX MATCH "libraries: =([^\r\n]*)" OUT "${OUT}")
  71. list(APPEND WINSOCK_LIBRARY_PATHS "${CMAKE_MATCH_1}")
  72. endif()
  73. endif()
  74. if ("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "AMD64" AND "${CMAKE_SIZEOF_VOID_P}" EQUAL 4)
  75. list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/SysWOW64")
  76. endif()
  77. list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/System32")
  78. remove_duplicate_paths(WINSOCK_LIBRARY_PATHS)
  79. find_path(WINSOCK_INCLUDE_DIRS
  80. NAMES winsock2.h
  81. PATHS ${WINSOCK_INCLUDE_PATHS}
  82. )
  83. find_library(WINSOCK_LIBRARIES ws2_32
  84. PATHS ${WINSOCK_LIBRARY_PATHS}
  85. NO_DEFAULT_PATH
  86. )
  87. include(FindPackageHandleStandardArgs)
  88. find_package_handle_standard_args(WinSock DEFAULT_MSG WINSOCK_LIBRARIES WINSOCK_INCLUDE_DIRS)
  89. mark_as_advanced(WINSOCK_INCLUDE_DIRS WINSOCK_LIBRARIES)
  90. if(WINSOCK_FOUND)
  91. if(NOT TARGET WINSOCK::WINSOCK)
  92. add_library(WINSOCK::WINSOCK UNKNOWN IMPORTED)
  93. set_target_properties(WINSOCK::WINSOCK PROPERTIES
  94. IMPORTED_LOCATION "${WINSOCK_LIBRARIES}"
  95. INTERFACE_INCLUDE_DIRECTORIES "${WINSOCK_INCLUDE_DIRS}")
  96. endif()
  97. endif()