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