FindWinSock.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. file(TOUCH ${CMAKE_BINARY_DIR}/nul)
  41. execute_process(
  42. COMMAND ${CMAKE_C_COMPILER} -xc -E -v -
  43. RESULT_VARIABLE RESULT
  44. INPUT_FILE nul
  45. ERROR_VARIABLE ERR
  46. OUTPUT_QUIET
  47. )
  48. if (NOT RESULT)
  49. string(FIND "${ERR}" "#include <...> search starts here:" START)
  50. string(FIND "${ERR}" "End of search list." END)
  51. if (NOT ${START} EQUAL -1 AND NOT ${END} EQUAL -1)
  52. math(EXPR START "${START} + 36")
  53. math(EXPR END "${END} - 1")
  54. math(EXPR LENGTH "${END} - ${START}")
  55. string(SUBSTRING "${ERR}" ${START} ${LENGTH} WINSOCK_INCLUDE_PATHS)
  56. string(REPLACE "\n " ";" WINSOCK_INCLUDE_PATHS "${WINSOCK_INCLUDE_PATHS}")
  57. list(REVERSE WINSOCK_INCLUDE_PATHS)
  58. endif()
  59. endif()
  60. endif()
  61. remove_duplicate_paths(WINSOCK_INCLUDE_PATHS)
  62. set(WINSOCK_LIBRARY_PATHS "${WINSOCK_ROOT}/lib/")
  63. if(MINGW)
  64. execute_process(
  65. COMMAND ${CMAKE_C_COMPILER} -print-search-dirs
  66. RESULT_VARIABLE RESULT
  67. OUTPUT_VARIABLE OUT
  68. ERROR_QUIET
  69. )
  70. if (NOT RESULT)
  71. string(REGEX MATCH "libraries: =([^\r\n]*)" OUT "${OUT}")
  72. if(UNIX)
  73. # On Unix systems, search dirs are separated by ':'.
  74. # Thus we need to replace ':' with ';' to make a valid cmake list.
  75. string(REPLACE ":" ";" _SEARCH_PATHS "${CMAKE_MATCH_1}")
  76. list(APPEND WINSOCK_LIBRARY_PATHS "${_SEARCH_PATHS}")
  77. unset(_SEARCH_PATHS)
  78. else()
  79. list(APPEND WINSOCK_LIBRARY_PATHS "${CMAKE_MATCH_1}")
  80. endif()
  81. endif()
  82. endif()
  83. if ("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "AMD64" AND "${CMAKE_SIZEOF_VOID_P}" EQUAL 4)
  84. list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/SysWOW64")
  85. endif()
  86. list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/System32")
  87. remove_duplicate_paths(WINSOCK_LIBRARY_PATHS)
  88. find_path(WINSOCK_INCLUDE_DIRS
  89. NAMES winsock2.h
  90. PATHS ${WINSOCK_INCLUDE_PATHS}
  91. )
  92. find_library(WINSOCK_LIBRARIES ws2_32
  93. PATHS ${WINSOCK_LIBRARY_PATHS}
  94. NO_DEFAULT_PATH
  95. )
  96. include(FindPackageHandleStandardArgs)
  97. find_package_handle_standard_args(WinSock DEFAULT_MSG WINSOCK_LIBRARIES WINSOCK_INCLUDE_DIRS)
  98. mark_as_advanced(WINSOCK_INCLUDE_DIRS WINSOCK_LIBRARIES)
  99. if(WINSOCK_FOUND)
  100. if(NOT TARGET WINSOCK::WINSOCK)
  101. add_library(WINSOCK::WINSOCK UNKNOWN IMPORTED)
  102. set_target_properties(WINSOCK::WINSOCK PROPERTIES
  103. IMPORTED_LOCATION "${WINSOCK_LIBRARIES}"
  104. INTERFACE_INCLUDE_DIRECTORIES "${WINSOCK_INCLUDE_DIRS}")
  105. endif()
  106. endif()