1# socketlib.m4 serial 2
2dnl Copyright (C) 2008-2020 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl gl_SOCKETLIB
8dnl Determines the library to use for socket functions.
9dnl Sets and AC_SUBSTs LIBSOCKET.
10
11AC_DEFUN([gl_SOCKETLIB],
12[
13  gl_PREREQ_SYS_H_WINSOCK2 dnl for HAVE_WINSOCK2_H
14  LIBSOCKET=
15  if test $HAVE_WINSOCK2_H = 1; then
16    dnl Native Windows API (not Cygwin).
17    AC_CACHE_CHECK([if we need to call WSAStartup in winsock2.h and -lws2_32],
18                   [gl_cv_func_wsastartup], [
19      gl_save_LIBS="$LIBS"
20      LIBS="$LIBS -lws2_32"
21      AC_LINK_IFELSE([AC_LANG_PROGRAM([[
22#ifdef HAVE_WINSOCK2_H
23# include <winsock2.h>
24#endif]], [[
25          WORD wVersionRequested = MAKEWORD(1, 1);
26          WSADATA wsaData;
27          int err = WSAStartup(wVersionRequested, &wsaData);
28          WSACleanup ();]])],
29        gl_cv_func_wsastartup=yes, gl_cv_func_wsastartup=no)
30      LIBS="$gl_save_LIBS"
31    ])
32    if test "$gl_cv_func_wsastartup" = "yes"; then
33      AC_DEFINE([WINDOWS_SOCKETS], [1], [Define if WSAStartup is needed.])
34      LIBSOCKET='-lws2_32'
35    fi
36  else
37    dnl Unix API.
38    dnl Solaris has most socket functions in libsocket.
39    dnl Haiku has most socket functions in libnetwork.
40    dnl BeOS has most socket functions in libnet.
41    dnl On HP-UX, do NOT link with libxnet, because in 64-bit mode this would
42    dnl break code (e.g. in libraries) that invokes accept(), getpeername(),
43    dnl getsockname(), getsockopt(), or recvfrom() with a 32-bit addrlen. See
44    dnl "man xopen_networking" for details.
45    AC_CACHE_CHECK([for library containing setsockopt], [gl_cv_lib_socket], [
46      gl_cv_lib_socket=
47      AC_LINK_IFELSE([AC_LANG_PROGRAM([[extern
48#ifdef __cplusplus
49"C"
50#endif
51char setsockopt();]], [[setsockopt();]])],
52        [],
53        [gl_save_LIBS="$LIBS"
54         LIBS="$gl_save_LIBS -lsocket"
55         AC_LINK_IFELSE([AC_LANG_PROGRAM([[extern
56#ifdef __cplusplus
57"C"
58#endif
59char setsockopt();]], [[setsockopt();]])],
60           [gl_cv_lib_socket="-lsocket"])
61         if test -z "$gl_cv_lib_socket"; then
62           LIBS="$gl_save_LIBS -lnetwork"
63           AC_LINK_IFELSE([AC_LANG_PROGRAM([[extern
64#ifdef __cplusplus
65"C"
66#endif
67char setsockopt();]], [[setsockopt();]])],
68             [gl_cv_lib_socket="-lnetwork"])
69           if test -z "$gl_cv_lib_socket"; then
70             LIBS="$gl_save_LIBS -lnet"
71             AC_LINK_IFELSE([AC_LANG_PROGRAM([[extern
72#ifdef __cplusplus
73"C"
74#endif
75char setsockopt();]], [[setsockopt();]])],
76               [gl_cv_lib_socket="-lnet"])
77           fi
78         fi
79         LIBS="$gl_save_LIBS"
80        ])
81      if test -z "$gl_cv_lib_socket"; then
82        gl_cv_lib_socket="none needed"
83      fi
84    ])
85    if test "$gl_cv_lib_socket" != "none needed"; then
86      LIBSOCKET="$gl_cv_lib_socket"
87    fi
88  fi
89  AC_SUBST([LIBSOCKET])
90])
91