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