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
30macro(REMOVE_DUPLICATE_PATHS LIST_VAR)
31  set(WINSOCK_LIST "")
32  foreach(PATH IN LISTS ${LIST_VAR})
33    get_filename_component(PATH "${PATH}" REALPATH)
34    list(APPEND WINSOCK_LIST "${PATH}")
35  endforeach(PATH)
36  set(${LIST_VAR} ${WINSOCK_LIST})
37  list(REMOVE_DUPLICATES ${LIST_VAR})
38endmacro(REMOVE_DUPLICATE_PATHS)
39
40set(WINSOCK_INCLUDE_PATHS "${WINSOCK_ROOT}/include/")
41if(MINGW)
42  execute_process(
43    COMMAND ${CMAKE_C_COMPILER} -xc -E -v -
44    RESULT_VARIABLE RESULT
45    INPUT_FILE nul
46    ERROR_VARIABLE ERR
47    OUTPUT_QUIET
48  )
49  if (NOT RESULT)
50    string(FIND "${ERR}" "#include <...> search starts here:" START)
51    string(FIND "${ERR}" "End of search list." END)
52    if (NOT ${START} EQUAL -1 AND NOT ${END} EQUAL -1)
53      math(EXPR START "${START} + 36")
54      math(EXPR END "${END} - 1")
55      math(EXPR LENGTH "${END} - ${START}")
56      string(SUBSTRING "${ERR}" ${START} ${LENGTH} WINSOCK_INCLUDE_PATHS)
57      string(REPLACE "\n " ";" WINSOCK_INCLUDE_PATHS "${WINSOCK_INCLUDE_PATHS}")
58      list(REVERSE WINSOCK_INCLUDE_PATHS)
59    endif()
60  endif()
61endif()
62remove_duplicate_paths(WINSOCK_INCLUDE_PATHS)
63
64set(WINSOCK_LIBRARY_PATHS "${WINSOCK_ROOT}/lib/")
65if(MINGW)
66  execute_process(
67    COMMAND ${CMAKE_C_COMPILER} -print-search-dirs
68    RESULT_VARIABLE RESULT
69    OUTPUT_VARIABLE OUT
70    ERROR_QUIET
71  )
72  if (NOT RESULT)
73    string(REGEX MATCH "libraries: =([^\r\n]*)" OUT "${OUT}")
74    list(APPEND WINSOCK_LIBRARY_PATHS "${CMAKE_MATCH_1}")
75  endif()
76endif()
77if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "AMD64" AND ${CMAKE_SIZEOF_VOID_P} EQUAL 4)
78  list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/SysWOW64")
79endif()
80list(APPEND WINSOCK_LIBRARY_PATHS "C:/Windows/System32")
81remove_duplicate_paths(WINSOCK_LIBRARY_PATHS)
82
83find_path(WINSOCK_INCLUDE_DIRS
84  NAMES winsock2.h
85  PATHS ${WINSOCK_INCLUDE_PATHS}
86)
87find_library(WINSOCK_LIBRARIES ws2_32
88  PATHS ${WINSOCK_LIBRARY_PATHS}
89  NO_DEFAULT_PATH
90)
91include(FindPackageHandleStandardArgs)
92find_package_handle_standard_args(WinSock DEFAULT_MSG WINSOCK_LIBRARIES WINSOCK_INCLUDE_DIRS)
93mark_as_advanced(WINSOCK_INCLUDE_DIRS WINSOCK_LIBRARIES)
94
95if(WINSOCK_FOUND)
96    if(NOT TARGET WINSOCK::WINSOCK)
97      add_library(WINSOCK::WINSOCK UNKNOWN IMPORTED)
98      set_target_properties(WINSOCK::WINSOCK PROPERTIES
99        IMPORTED_LOCATION "${WINSOCK_LIBRARIES}"
100        INTERFACE_INCLUDE_DIRECTORIES "${WINSOCK_INCLUDE_DIRS}")
101    endif()
102endif()
103