Skip to content
acinclude.m4 84.3 KiB
Newer Older
#if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK > 0)
Yang Tse's avatar
Yang Tse committed
      /*
      The monotonic clock will not be used unless the feature test macro is
      defined with a value greater than zero indicating _always_ supported.
      */
      struct timespec ts;
      (void)clock_gettime(CLOCK_MONOTONIC, &ts);
#else
      HAVE_CLOCK_GETTIME_MONOTONIC shall not be defined.
#endif
  ],[
    AC_MSG_RESULT([yes])
    ac_cv_func_clock_gettime="yes"
  ],[
    AC_MSG_RESULT([no])
    ac_cv_func_clock_gettime="no"
  ])
  dnl Definition of HAVE_CLOCK_GETTIME_MONOTONIC is intentionally
  dnl postponed until library linking checks for clock_gettime pass.
]) dnl AC_DEFUN

dnl CURL_CHECK_LIBS_CLOCK_GETTIME_MONOTONIC
dnl -------------------------------------------------
dnl If monotonic clock_gettime is available then,
dnl check and prepended to LIBS any needed libraries.

AC_DEFUN([CURL_CHECK_LIBS_CLOCK_GETTIME_MONOTONIC], [
  AC_REQUIRE([CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC])dnl
  if test "$ac_cv_func_clock_gettime" = "yes"; then
    #
    AC_MSG_CHECKING([for clock_gettime in libraries])
    #
    curl_cv_save_LIBS="$LIBS"
    curl_cv_gclk_LIBS="unknown"
    #
    for x_xlibs in '' '-lrt' '-lposix4' ; do
      if test "$curl_cv_gclk_LIBS" = "unknown"; then
        if test -z "$x_xlibs"; then
          LIBS="$curl_cv_save_LIBS"
        else
          LIBS="$x_xlibs $curl_cv_save_LIBS"
        fi
        AC_LINK_IFELSE([
          AC_LANG_PROGRAM([[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#ifdef TIME_WITH_SYS_TIME
#include <time.h>
#endif
#else
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#endif
          ]],[[
            struct timespec ts;
            (void)clock_gettime(CLOCK_MONOTONIC, &ts);
          ]])
        ],[
          curl_cv_gclk_LIBS="$x_xlibs"
        ])
      fi
    done
    #
    LIBS="$curl_cv_save_LIBS"
    #
    case X-"$curl_cv_gclk_LIBS" in
      X-unknown)
        AC_MSG_RESULT([cannot find clock_gettime])
        AC_MSG_WARN([HAVE_CLOCK_GETTIME_MONOTONIC will not be defined])
        ac_cv_func_clock_gettime="no"
        ;;
      X-)
        AC_MSG_RESULT([no additional lib required])
        ac_cv_func_clock_gettime="yes"
        ;;
      *)
        if test -z "$curl_cv_save_LIBS"; then
          LIBS="$curl_cv_gclk_LIBS"
        else
          LIBS="$curl_cv_gclk_LIBS $curl_cv_save_LIBS"
        fi
        AC_MSG_RESULT([$curl_cv_gclk_LIBS])
        ac_cv_func_clock_gettime="yes"
        ;;
    esac
    #
    dnl only do runtime verification when not cross-compiling
    if test "x$cross_compiling" != "xyes" &&
      test "$ac_cv_func_clock_gettime" = "yes"; then
      AC_MSG_CHECKING([if monotonic clock_gettime works])
      AC_RUN_IFELSE([
        AC_LANG_PROGRAM([[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#ifdef TIME_WITH_SYS_TIME
#include <time.h>
#endif
#else
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#endif
        ]],[[
#if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK > 0)
          struct timespec ts;
          if (0 == clock_gettime(CLOCK_MONOTONIC, &ts))
            exit(0);
#endif
          exit(1);
        ]])
      ],[
        AC_MSG_RESULT([yes])
      ],[
        AC_MSG_RESULT([no])
        AC_MSG_WARN([HAVE_CLOCK_GETTIME_MONOTONIC will not be defined])
        ac_cv_func_clock_gettime="no"
        LIBS="$curl_cv_save_LIBS"
      ])
    fi
    #
    case "$ac_cv_func_clock_gettime" in
      yes)
        AC_DEFINE_UNQUOTED(HAVE_CLOCK_GETTIME_MONOTONIC, 1,
          [Define to 1 if you have the clock_gettime function and monotonic timer.])
        ;;
    esac
    #
  fi

dnl CURL_CHECK_FUNC_SELECT
dnl -------------------------------------------------
dnl Test if the socket select() function is available,
dnl and check its return type and the types of its
dnl arguments. If the function succeeds HAVE_SELECT
dnl will be defined, defining the types of the
dnl arguments in SELECT_TYPE_ARG1, SELECT_TYPE_ARG234
dnl and SELECT_TYPE_ARG5, defining the type of the
dnl function return value in SELECT_TYPE_RETV, and
dnl also defining the type qualifier of fifth argument
dnl in SELECT_QUAL_ARG5.

AC_DEFUN([CURL_CHECK_FUNC_SELECT], [
  AC_REQUIRE([CURL_CHECK_STRUCT_TIMEVAL])dnl
  AC_CHECK_HEADERS(sys/select.h sys/socket.h)
  #
  AC_MSG_CHECKING([for select])
    AC_LANG_PROGRAM([[
#undef inline
#ifdef HAVE_WINDOWS_H
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#else
#ifdef HAVE_WINSOCK_H
#include <winsock.h>
#endif
#endif
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#ifdef TIME_WITH_SYS_TIME
#include <time.h>
#endif
#else
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#endif
#ifndef HAVE_WINDOWS_H
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#endif
      select(0, 0, 0, 0, 0);
  ],[
    AC_MSG_RESULT([yes])
    curl_cv_select="yes"
  ],[
    AC_MSG_RESULT([no])
    curl_cv_select="no"
  ])
  #
  if test "$curl_cv_select" = "yes"; then
    AC_CACHE_CHECK([types of args and return type for select],
      [curl_cv_func_select_args], [
      curl_cv_func_select_args="unknown"
      for sel_retv in 'int' 'ssize_t'; do
        for sel_arg1 in 'int' 'ssize_t' 'size_t' 'unsigned long int' 'unsigned int'; do
          for sel_arg234 in 'fd_set *' 'int *' 'void *'; do
            for sel_arg5 in 'struct timeval *' 'const struct timeval *'; do
              if test "$curl_cv_func_select_args" = "unknown"; then
                AC_COMPILE_IFELSE([
                  AC_LANG_PROGRAM([[
#undef inline
#ifdef HAVE_WINDOWS_H
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#else
#ifdef HAVE_WINSOCK_H
#include <winsock.h>
#endif
#endif
#define SELECTCALLCONV PASCAL
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#ifdef TIME_WITH_SYS_TIME
#include <time.h>
#endif
#else
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#endif
#ifndef HAVE_WINDOWS_H
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#define SELECTCALLCONV
#endif
#ifndef HAVE_STRUCT_TIMEVAL
                    struct timeval {
                      long tv_sec;
                      long tv_usec;
                    };
#endif
                    extern $sel_retv SELECTCALLCONV select($sel_arg1,
                                                           $sel_arg234,
                                                           $sel_arg234,
                                                           $sel_arg234,
                                                           $sel_arg5);
                  ]],[[
                    $sel_arg1   nfds=0;
                    $sel_arg234 rfds=0;
                    $sel_arg234 wfds=0;
                    $sel_arg234 efds=0;
                    $sel_retv res = select(nfds, rfds, wfds, efds, 0);
                  ]])
                ],[
                  curl_cv_func_select_args="$sel_arg1,$sel_arg234,$sel_arg5,$sel_retv"
                ])
              fi
            done
          done
        done
      done
    ]) # AC_CACHE_CHECK
    if test "$curl_cv_func_select_args" = "unknown"; then
      AC_MSG_WARN([Cannot find proper types to use for select args])
      AC_MSG_WARN([HAVE_SELECT will not be defined])
    else
      select_prev_IFS=$IFS; IFS=','
      set dummy `echo "$curl_cv_func_select_args" | sed 's/\*/\*/g'`
      IFS=$select_prev_IFS
      shift
      #
      sel_qual_type_arg5=$[3]
      #
      AC_DEFINE_UNQUOTED(SELECT_TYPE_ARG1, $[1],
        [Define to the type of arg 1 for select.])
      AC_DEFINE_UNQUOTED(SELECT_TYPE_ARG234, $[2],
        [Define to the type of args 2, 3 and 4 for select.])
      AC_DEFINE_UNQUOTED(SELECT_TYPE_RETV, $[4],
        [Define to the function return type for select.])
      #
      prev_sh_opts=$-
      #
      case $prev_sh_opts in
        *f*)
          ;;
        *)
          set -f
          ;;
      esac
      #
      case "$sel_qual_type_arg5" in
        const*)
          sel_qual_arg5=const
          sel_type_arg5=`echo $sel_qual_type_arg5 | sed 's/^const //'`
        ;;
        *)
          sel_qual_arg5=
          sel_type_arg5=$sel_qual_type_arg5
        ;;
      esac
      #
      AC_DEFINE_UNQUOTED(SELECT_QUAL_ARG5, $sel_qual_arg5,
        [Define to the type qualifier of arg 5 for select.])
      AC_DEFINE_UNQUOTED(SELECT_TYPE_ARG5, $sel_type_arg5,
        [Define to the type of arg 5 for select.])
      #
      case $prev_sh_opts in
        *f*)
          ;;
        *)
          set +f
          ;;
      esac
      #
      AC_DEFINE_UNQUOTED(HAVE_SELECT, 1,
        [Define to 1 if you have the select function.])
      ac_cv_func_select="yes"
    fi
  fi
]) # AC_DEFUN


dnl ************************************************************
dnl check for "localhost", if it doesn't exist, we can't do the
dnl gethostbyname_r tests!
dnl 

AC_DEFUN([CURL_CHECK_WORKING_RESOLVER],[
AC_MSG_CHECKING([if "localhost" resolves])
AC_TRY_RUN([
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
#ifndef NULL
#define NULL (void *)0
#endif

int
main () {
struct hostent *h;
h = gethostbyname("localhost");
exit (h == NULL ? 1 : 0); }],[
      AC_MSG_RESULT(yes)],[
      AC_MSG_RESULT(no)
      AC_MSG_ERROR([can't figure out gethostbyname_r() since localhost doesn't resolve])

      ]
)
])

dnl ************************************************************
dnl check for working getaddrinfo() that works with AI_NUMERICHOST
AC_DEFUN([CURL_CHECK_WORKING_GETADDRINFO],[
  AC_CACHE_CHECK(for working getaddrinfo, ac_cv_working_getaddrinfo,[
  AC_TRY_RUN( [
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>

    struct addrinfo hints, *ai;
    int error;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    error = getaddrinfo("127.0.0.1", "8080", &hints, &ai);
    if (error) {
}
],[
  ac_cv_working_getaddrinfo="yes"
],[
  ac_cv_working_getaddrinfo="no"
],[
  ac_cv_working_getaddrinfo="yes"
])])
if test "$ac_cv_working_getaddrinfo" = "yes"; then
  AC_DEFINE(HAVE_GETADDRINFO, 1, [Define if getaddrinfo exists and works])
  AC_DEFINE(ENABLE_IPV6, 1, [Define if you want to enable IPv6 support])

  IPV6_ENABLED=1
  AC_SUBST(IPV6_ENABLED)
fi
])


  dnl check for localtime_r
  AC_CHECK_FUNCS(localtime_r,[
    AC_MSG_CHECKING(whether localtime_r is declared)
    AC_EGREP_CPP(localtime_r,[
#include <time.h>],[
      AC_MSG_RESULT(yes)],[
      AC_MSG_RESULT(no)
      AC_MSG_CHECKING(whether localtime_r with -D_REENTRANT is declared)
      AC_EGREP_CPP(localtime_r,[
#define _REENTRANT
#include <time.h>],[
	AC_DEFINE(NEED_REENTRANT)
	AC_MSG_RESULT(yes)],
	AC_MSG_RESULT(no))])])
])

dnl
dnl This function checks for strerror_r(). If it isn't found at first, it
dnl retries with _THREAD_SAFE defined, as that is what AIX seems to require
dnl in order to find this function.
dnl
dnl If the function is found, it will then proceed to check how the function
dnl actually works: glibc-style or POSIX-style.
dnl
dnl glibc:
dnl      char *strerror_r(int errnum, char *buf, size_t n);
dnl  
dnl  What this one does is to return the error string (no surprises there),
dnl  but it doesn't usually copy anything into buf!  The 'buf' and 'n'
dnl  parameters are only meant as an optional working area, in case strerror_r
dnl  needs it.  A quick test on a few systems shows that it's generally not
dnl  touched at all.
dnl
dnl POSIX:
dnl      int strerror_r(int errnum, char *buf, size_t n);
dnl
AC_DEFUN([CURL_CHECK_STRERROR_R],
[
  AC_CHECK_FUNCS(strerror_r)

  if test "x$ac_cv_func_strerror_r" = "xyes"; then

    AC_MSG_CHECKING(whether strerror_r is declared)
    AC_EGREP_CPP(strerror_r,[
#include <string.h>],[
      AC_MSG_RESULT(yes)],[
      AC_MSG_RESULT(no)
      AC_MSG_CHECKING(whether strerror_r with -D_REENTRANT is declared)
      AC_EGREP_CPP(strerror_r,[
#include <string.h>],[
	AC_MSG_RESULT(yes)],
	AC_MSG_RESULT(no)
        AC_DEFINE(HAVE_NO_STRERROR_R_DECL, 1, [we have no strerror_r() proto])
       ) dnl with _THREAD_SAFE
    ]) dnl plain cpp for it
    dnl determine if this strerror_r() is glibc or POSIX
    AC_MSG_CHECKING([for a glibc strerror_r API])
    AC_TRY_RUN([
#include <string.h>
#include <errno.h>
int
main () {
  char buffer[1024]; /* big enough to play with */
  char *string =
    strerror_r(EACCES, buffer, sizeof(buffer));
    /* this should've returned a string */
    if(!string || !string[0])
      return 99;
    return 0;
}
],
    AC_DEFINE(HAVE_GLIBC_STRERROR_R, 1, [we have a glibc-style strerror_r()])
    AC_MSG_RESULT([yes]),

    dnl Use an inferior method of strerror_r detection while cross-compiling
    AC_EGREP_CPP(yes, [
#include <features.h>
#ifdef __GLIBC__
yes
#endif
], 
      dnl looks like glibc, so assume a glibc-style strerror_r()
      GLIBC_STRERROR_R="1"
      AC_DEFINE(HAVE_GLIBC_STRERROR_R, 1, [we have a glibc-style strerror_r()])
      AC_MSG_RESULT([yes]),
      AC_MSG_NOTICE([cannot determine strerror_r() style: edit lib/config.h manually!])
    ) dnl while cross-compiling
    if test -z "$GLIBC_STRERROR_R"; then

      AC_MSG_CHECKING([for a POSIX strerror_r API])
      AC_TRY_RUN([
#include <string.h>
#include <errno.h>
int
main () {
  char buffer[1024]; /* big enough to play with */
  int error =
    strerror_r(EACCES, buffer, sizeof(buffer));
    /* This should've returned zero, and written an error string in the
       buffer.*/
    if(!buffer[0] || error)
      return 99;
    return 0;
}
],
      AC_DEFINE(HAVE_POSIX_STRERROR_R, 1, [we have a POSIX-style strerror_r()])
      AC_MSG_RESULT([yes]),
      AC_MSG_RESULT([no]) ,
      dnl cross-compiling!
      AC_MSG_NOTICE([cannot determine strerror_r() style: edit lib/config.h manually!])
    )
    fi dnl if not using glibc API

  fi dnl we have a strerror_r
[
  dnl determine if function definition for inet_ntoa_r exists.
  AC_CHECK_FUNCS(inet_ntoa_r,[
    AC_MSG_CHECKING(whether inet_ntoa_r is declared)
    AC_EGREP_CPP(inet_ntoa_r,[
#include <arpa/inet.h>],[
      AC_DEFINE(HAVE_INET_NTOA_R_DECL, 1, [inet_ntoa_r() is declared])
      AC_MSG_RESULT(yes)],[
      AC_MSG_RESULT(no)
      AC_MSG_CHECKING(whether inet_ntoa_r with -D_REENTRANT is declared)
      AC_EGREP_CPP(inet_ntoa_r,[
#define _REENTRANT
#include <arpa/inet.h>],[
	AC_DEFINE(HAVE_INET_NTOA_R_DECL, 1, [inet_ntoa_r() is declared])
	AC_DEFINE(NEED_REENTRANT, 1, [need REENTRANT defined])
	AC_MSG_RESULT(yes)],
	AC_MSG_RESULT(no))])])
])


dnl CURL_CHECK_GETHOSTBYADDR_R
dnl -------------------------------------------------
dnl check number of arguments for gethostbyaddr_r, it
dnl might take either 5, 7, or 8 arguments.

AC_DEFUN([CURL_CHECK_GETHOSTBYADDR_R], [
  #
  AC_MSG_CHECKING([for gethostbyaddr_r])
  AC_LINK_IFELSE([
    AC_LANG_FUNC_LINK_TRY([gethostbyaddr_r])
  ],[
    AC_MSG_RESULT([yes])
    tmp_cv_gethostbyaddr_r="yes"
  ],[
    AC_MSG_RESULT([no])
    tmp_cv_gethostbyaddr_r="no"
  ])
  #
  if test "$tmp_cv_gethostbyaddr_r" != "yes"; then
    AC_MSG_CHECKING([deeper for gethostbyaddr_r])
    AC_LINK_IFELSE([
      AC_LANG_PROGRAM([[
      ]],[[
        gethostbyaddr_r();
      ]])
    ],[
      AC_MSG_RESULT([yes])
      tmp_cv_gethostbyaddr_r="yes"
    ],[
      AC_MSG_RESULT([but still no])
      tmp_cv_gethostbyaddr_r="no"
    ])
  fi
  #
  if test "$tmp_cv_gethostbyaddr_r" = "yes"; then

    ac_cv_gethostbyaddr_r_args="unknown"

    AC_MSG_CHECKING([if gethostbyaddr_r takes 5 arguments])
    AC_COMPILE_IFELSE([
      AC_LANG_PROGRAM([[
#include <sys/types.h>
#include <netdb.h>
      ]],[[
        char * address;
        int length;
        int type;
        struct hostent h;
        struct hostent_data hdata;
        int rc;
        rc = gethostbyaddr_r(address, length, type, &h, &hdata);
      ]])
    ],[
      AC_MSG_RESULT([yes])
      AC_DEFINE(HAVE_GETHOSTBYADDR_R_5, 1, [gethostbyaddr_r() takes 5 args])
      ac_cv_gethostbyaddr_r_args="5"
    ],[
      AC_MSG_RESULT([no])
    ])

    if test "$ac_cv_gethostbyaddr_r_args" = "unknown"; then
      AC_MSG_CHECKING([if gethostbyaddr_r with -D_REENTRANT takes 5 arguments])
      AC_COMPILE_IFELSE([
        AC_LANG_PROGRAM([[
#define _REENTRANT
#include <sys/types.h>
#include <netdb.h>
        ]],[[
          char * address;
          int length;
          int type;
          struct hostent h;
          struct hostent_data hdata;
          int rc;
          rc = gethostbyaddr_r(address, length, type, &h, &hdata);
        ]])
      ],[
        AC_MSG_RESULT([yes])
        AC_DEFINE(HAVE_GETHOSTBYADDR_R_5, 1, [gethostbyaddr_r() takes 5 args])
	AC_DEFINE(NEED_REENTRANT, 1, [need REENTRANT])
        ac_cv_gethostbyaddr_r_args="5"
      ],[
        AC_MSG_RESULT([no])
      ])
    fi

    if test "$ac_cv_gethostbyaddr_r_args" = "unknown"; then
      AC_MSG_CHECKING([if gethostbyaddr_r takes 7 arguments])
      AC_COMPILE_IFELSE([
        AC_LANG_PROGRAM([[
#include <sys/types.h>
#include <netdb.h>
        ]],[[
          char * address;
          int length;
          int type;
          struct hostent h;
          char buffer[8192];
          int h_errnop;
          struct hostent * hp;
          hp = gethostbyaddr_r(address, length, type, &h,
                               buffer, 8192, &h_errnop);
        ]])
      ],[
        AC_MSG_RESULT([yes])
        AC_DEFINE(HAVE_GETHOSTBYADDR_R_7, 1, [gethostbyaddr_r() takes 7 args])
        ac_cv_gethostbyaddr_r_args="7"
      ],[
        AC_MSG_RESULT([no])
      ])
    fi

    if test "$ac_cv_gethostbyaddr_r_args" = "unknown"; then
      AC_MSG_CHECKING([if gethostbyaddr_r takes 8 arguments])
      AC_COMPILE_IFELSE([
        AC_LANG_PROGRAM([[
#include <sys/types.h>
#include <netdb.h>
        ]],[[
          char * address;
          int length;
          int type;
          struct hostent h;
          char buffer[8192];
          int h_errnop;
          struct hostent * hp;
          int rc;
          rc = gethostbyaddr_r(address, length, type, &h,
                               buffer, 8192, &hp, &h_errnop);
        ]])
      ],[
        AC_MSG_RESULT([yes])
        AC_DEFINE(HAVE_GETHOSTBYADDR_R_8, 1, [gethostbyaddr_r() takes 8 args])
        ac_cv_gethostbyaddr_r_args="8"
      ],[
        AC_MSG_RESULT([no])
      ])
    fi

    if test "$ac_cv_gethostbyaddr_r_args" = "unknown"; then
      AC_MSG_WARN([Cannot find out how to use gethostbyaddr_r])
      AC_MSG_WARN([HAVE_GETHOSTBYADDR_R will not be defined])
      ac_cv_func_gethostbyaddr_r="no"
    else
      AC_DEFINE_UNQUOTED(HAVE_GETHOSTBYADDR_R, 1,
        [Define to 1 if you have the gethostbyaddr_r function.])
      ac_cv_func_gethostbyaddr_r="yes"
    fi

  else
    ac_cv_func_gethostbyaddr_r="no"

dnl CURL_CHECK_GETHOSTBYNAME_R
dnl -------------------------------------------------
dnl check number of arguments for gethostbyname_r, it
dnl might take either 3, 5, or 6 arguments.

AC_DEFUN([CURL_CHECK_GETHOSTBYNAME_R], [
  #
  AC_MSG_CHECKING([for gethostbyname_r])
  AC_LINK_IFELSE([
    AC_LANG_FUNC_LINK_TRY([gethostbyname_r])
  ],[
    AC_MSG_RESULT([yes])
    tmp_cv_gethostbyname_r="yes"
  ],[
    AC_MSG_RESULT([no])
    tmp_cv_gethostbyname_r="no"
  ])
  #
  if test "$tmp_cv_gethostbyname_r" != "yes"; then
    AC_MSG_CHECKING([deeper for gethostbyname_r])
    AC_LINK_IFELSE([
      AC_LANG_PROGRAM([[
      ]],[[
        gethostbyname_r();
      ]])
    ],[
      AC_MSG_RESULT([yes])
      tmp_cv_gethostbyname_r="yes"
    ],[
      AC_MSG_RESULT([but still no])
      tmp_cv_gethostbyname_r="no"
    ])
  fi
  #
  if test "$tmp_cv_gethostbyname_r" = "yes"; then

    ac_cv_gethostbyname_r_args="unknown"

    AC_MSG_CHECKING([if gethostbyname_r takes 3 arguments])
    AC_COMPILE_IFELSE([
      AC_LANG_PROGRAM([[
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
        int
        gethostbyname_r(const char *, struct hostent *,
                        struct hostent_data *);
      ]],[[
        struct hostent_data data;
        gethostbyname_r(NULL, NULL, NULL);
      ]])
    ],[
      AC_MSG_RESULT([yes])
      AC_DEFINE(HAVE_GETHOSTBYNAME_R_3, 1, [gethostbyname_r() takes 3 args])
      ac_cv_gethostbyname_r_args="3"
    ],[
      AC_MSG_RESULT([no])
    ])

    if test "$ac_cv_gethostbyname_r_args" = "unknown"; then
      AC_MSG_CHECKING([if gethostbyname_r with -D_REENTRANT takes 3 arguments])
      AC_COMPILE_IFELSE([
        AC_LANG_PROGRAM([[
#define _REENTRANT
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
          int
          gethostbyname_r(const char *, struct hostent *,
                          struct hostent_data *);
        ]],[[
          struct hostent_data data;
          gethostbyname_r(NULL, NULL, NULL);
        ]])
      ],[
        AC_MSG_RESULT([yes])
        AC_DEFINE(HAVE_GETHOSTBYNAME_R_3, 1, [gethostbyname_r() takes 3 args])
	AC_DEFINE(NEED_REENTRANT, 1, [need REENTRANT])
        ac_cv_gethostbyname_r_args="3"
      ],[
        AC_MSG_RESULT([no])
      ])
    fi
    if test "$ac_cv_gethostbyname_r_args" = "unknown"; then
      AC_MSG_CHECKING([if gethostbyname_r takes 5 arguments])
      AC_COMPILE_IFELSE([
        AC_LANG_PROGRAM([[
#include <sys/types.h>
#include <netdb.h>
          struct hostent *
          gethostbyname_r(const char *, struct hostent *,
                          char *, int, int *);
        ]],[[
          gethostbyname_r(NULL, NULL, NULL, 0, NULL);
        ]])
      ],[
        AC_MSG_RESULT([yes])
        AC_DEFINE(HAVE_GETHOSTBYNAME_R_5, 1, [gethostbyname_r() takes 5 args])
        ac_cv_gethostbyname_r_args="5"
      ],[
        AC_MSG_RESULT([no])
      ])
    fi
    if test "$ac_cv_gethostbyname_r_args" = "unknown"; then
      AC_MSG_CHECKING([if gethostbyname_r takes 6 arguments])
      AC_COMPILE_IFELSE([
        AC_LANG_PROGRAM([[
#include <sys/types.h>
#include <netdb.h>
          int
          gethostbyname_r(const char *, struct hostent *,
                          char *, size_t, struct hostent **, int *);
        ]],[[
          gethostbyname_r(NULL, NULL, NULL, 0, NULL, NULL);
        ]])
      ],[
        AC_MSG_RESULT([yes])
        AC_DEFINE(HAVE_GETHOSTBYNAME_R_6, 1, [gethostbyname_r() takes 6 args])
        ac_cv_gethostbyname_r_args="6"
      ],[
        AC_MSG_RESULT([no])
      ])
    fi

    if test "$ac_cv_gethostbyname_r_args" = "unknown"; then
      AC_MSG_WARN([Cannot find out how to use gethostbyname_r])
      AC_MSG_WARN([HAVE_GETHOSTBYNAME_R will not be defined])
      ac_cv_func_gethostbyname_r="no"
    else
      AC_DEFINE_UNQUOTED(HAVE_GETHOSTBYNAME_R, 1,
        [Define to 1 if you have the gethostbyname_r function.])
      ac_cv_func_gethostbyname_r="yes"
    fi
  else
    ac_cv_func_gethostbyname_r="no"
dnl **********************************************************************
dnl CURL_DETECT_ICC ([ACTION-IF-YES])
dnl
dnl check if this is the Intel ICC compiler, and if so run the ACTION-IF-YES
dnl sets the $ICC variable to "yes" or "no"
dnl **********************************************************************
AC_DEFUN([CURL_DETECT_ICC],
[
    ICC="no"
    AC_MSG_CHECKING([for icc in use])
    if test "$GCC" = "yes"; then
       dnl check if this is icc acting as gcc in disguise
       AC_EGREP_CPP([^__INTEL_COMPILER], [__INTEL_COMPILER],
         dnl action if the text is found, this it has not been replaced by the
         dnl cpp
         ICC="no",
         dnl the text was not found, it was replaced by the cpp
         ICC="yes"
         AC_MSG_RESULT([yes])
         [$1]
       )
    fi
    if test "$ICC" = "no"; then
        # this is not ICC
        AC_MSG_RESULT([no])
    fi
])

dnl We create a function for detecting which compiler we use and then set as
dnl pendantic compiler options as possible for that particular compiler. The
dnl options are only used for debug-builds.

AC_DEFUN([CURL_CC_DEBUG_OPTS],
[
    if test "$GCC" = "yes"; then

       dnl figure out gcc version!
       AC_MSG_CHECKING([gcc version])
       gccver=`$CC -dumpversion`
       num1=`echo $gccver | cut -d . -f1`
       num2=`echo $gccver | cut -d . -f2`
       gccnum=`(expr $num1 "*" 100 + $num2) 2>/dev/null`
       AC_MSG_RESULT($gccver)

       if test "$ICC" = "yes"; then
         dnl this is icc, not gcc.
         dnl ICC warnings we ignore:
         dnl * 269 warns on our "%Od" printf formatters for curl_off_t output:
         dnl   "invalid format string conversion"
         dnl * 279 warns on static conditions in while expressions
         dnl * 981 warns on "operands are evaluated in unspecified order"
         dnl * 1418 "external definition with no prior declaration"
         dnl * 1419 warns on "external declaration in primary source file"
         dnl   which we know and do on purpose.
Daniel Stenberg's avatar
Daniel Stenberg committed
         WARN="-wd279,269,981,1418,1419"

         if test "$gccnum" -gt "600"; then
            dnl icc 6.0 and older doesn't have the -Wall flag
         dnl this is a set of options we believe *ALL* gcc versions support:
         WARN="-W -Wall -Wwrite-strings -pedantic -Wpointer-arith -Wnested-externs -Winline -Wmissing-prototypes"
         dnl -Wcast-align is a bit too annoying on all gcc versions ;-)

           dnl gcc 2.7 or later
           WARN="$WARN -Wmissing-declarations"
         if test "$gccnum" -gt "295"; then
           dnl only if the compiler is newer than 2.95 since we got lots of
           dnl "`_POSIX_C_SOURCE' is not defined" in system headers with
           dnl gcc 2.95.4 on FreeBSD 4.9!
           WARN="$WARN -Wundef -Wno-long-long -Wsign-compare -Wshadow -Wno-multichar"

         if test "$gccnum" -ge "296"; then
           dnl gcc 2.96 or later
           WARN="$WARN -Wfloat-equal"
         fi

         if test "$gccnum" -gt "296"; then
           dnl this option does not exist in 2.96
           WARN="$WARN -Wno-format-nonliteral"
         fi
         dnl -Wunreachable-code seems totally unreliable on my gcc 3.3.2 on
         dnl on i686-Linux as it gives us heaps with false positives.
         dnl Also, on gcc 4.0.X it is totally unbearable and complains all
         dnl over making it unusable for generic purposes. Let's not use it.

         if test "$gccnum" -ge "303"; then
           dnl gcc 3.3 and later
           WARN="$WARN -Wendif-labels -Wstrict-prototypes"
           WARN="$WARN -Wdeclaration-after-statement"
              dnl Include path, provide a -isystem option for the same dir
              dnl to prevent warnings in those dirs. The -isystem was not very
              dnl reliable on earlier gcc versions.
              add=`echo $flag | sed 's/^-I/-isystem /g'`
      AC_MSG_NOTICE([Added this set of compiler options: $WARN])