Blame m4/size_max.m4

Packit Service 85e8c2
# size_max.m4 serial 9
Packit Service 85e8c2
dnl Copyright (C) 2003, 2005-2006, 2008-2010 Free Software Foundation, Inc.
Packit Service 85e8c2
dnl This file is free software; the Free Software Foundation
Packit Service 85e8c2
dnl gives unlimited permission to copy and/or distribute it,
Packit Service 85e8c2
dnl with or without modifications, as long as this notice is preserved.
Packit Service 85e8c2
Packit Service 85e8c2
dnl From Bruno Haible.
Packit Service 85e8c2
Packit Service 85e8c2
AC_DEFUN([gl_SIZE_MAX],
Packit Service 85e8c2
[
Packit Service 85e8c2
  AC_CHECK_HEADERS([stdint.h])
Packit Service 85e8c2
  dnl First test whether the system already has SIZE_MAX.
Packit Service 85e8c2
  AC_CACHE_CHECK([for SIZE_MAX], [gl_cv_size_max], [
Packit Service 85e8c2
    gl_cv_size_max=
Packit Service 85e8c2
    AC_EGREP_CPP([Found it], [
Packit Service 85e8c2
#include <limits.h>
Packit Service 85e8c2
#if HAVE_STDINT_H
Packit Service 85e8c2
#include <stdint.h>
Packit Service 85e8c2
#endif
Packit Service 85e8c2
#ifdef SIZE_MAX
Packit Service 85e8c2
Found it
Packit Service 85e8c2
#endif
Packit Service 85e8c2
], [gl_cv_size_max=yes])
Packit Service 85e8c2
    if test -z "$gl_cv_size_max"; then
Packit Service 85e8c2
      dnl Define it ourselves. Here we assume that the type 'size_t' is not wider
Packit Service 85e8c2
      dnl than the type 'unsigned long'. Try hard to find a definition that can
Packit Service 85e8c2
      dnl be used in a preprocessor #if, i.e. doesn't contain a cast.
Packit Service 85e8c2
      AC_COMPUTE_INT([size_t_bits_minus_1], [sizeof (size_t) * CHAR_BIT - 1],
Packit Service 85e8c2
        [#include <stddef.h>
Packit Service 85e8c2
#include <limits.h>], [size_t_bits_minus_1=])
Packit Service 85e8c2
      AC_COMPUTE_INT([fits_in_uint], [sizeof (size_t) <= sizeof (unsigned int)],
Packit Service 85e8c2
        [#include <stddef.h>], [fits_in_uint=])
Packit Service 85e8c2
      if test -n "$size_t_bits_minus_1" && test -n "$fits_in_uint"; then
Packit Service 85e8c2
        if test $fits_in_uint = 1; then
Packit Service 85e8c2
          dnl Even though SIZE_MAX fits in an unsigned int, it must be of type
Packit Service 85e8c2
          dnl 'unsigned long' if the type 'size_t' is the same as 'unsigned long'.
Packit Service 85e8c2
          AC_TRY_COMPILE([#include <stddef.h>
Packit Service 85e8c2
            extern size_t foo;
Packit Service 85e8c2
            extern unsigned long foo;
Packit Service 85e8c2
            ], [], [fits_in_uint=0])
Packit Service 85e8c2
        fi
Packit Service 85e8c2
        dnl We cannot use 'expr' to simplify this expression, because 'expr'
Packit Service 85e8c2
        dnl works only with 'long' integers in the host environment, while we
Packit Service 85e8c2
        dnl might be cross-compiling from a 32-bit platform to a 64-bit platform.
Packit Service 85e8c2
        if test $fits_in_uint = 1; then
Packit Service 85e8c2
          gl_cv_size_max="(((1U << $size_t_bits_minus_1) - 1) * 2 + 1)"
Packit Service 85e8c2
        else
Packit Service 85e8c2
          gl_cv_size_max="(((1UL << $size_t_bits_minus_1) - 1) * 2 + 1)"
Packit Service 85e8c2
        fi
Packit Service 85e8c2
      else
Packit Service 85e8c2
        dnl Shouldn't happen, but who knows...
Packit Service 85e8c2
        gl_cv_size_max='((size_t)~(size_t)0)'
Packit Service 85e8c2
      fi
Packit Service 85e8c2
    fi
Packit Service 85e8c2
  ])
Packit Service 85e8c2
  if test "$gl_cv_size_max" != yes; then
Packit Service 85e8c2
    AC_DEFINE_UNQUOTED([SIZE_MAX], [$gl_cv_size_max],
Packit Service 85e8c2
      [Define as the maximum value of type 'size_t', if the system doesn't define it.])
Packit Service 85e8c2
  fi
Packit Service 85e8c2
  dnl Don't redefine SIZE_MAX in config.h if config.h is re-included after
Packit Service 85e8c2
  dnl <stdint.h>. Remember that the #undef in AH_VERBATIM gets replaced with
Packit Service 85e8c2
  dnl #define by AC_DEFINE_UNQUOTED.
Packit Service 85e8c2
  AH_VERBATIM([SIZE_MAX],
Packit Service 85e8c2
[/* Define as the maximum value of type 'size_t', if the system doesn't define
Packit Service 85e8c2
   it. */
Packit Service 85e8c2
#ifndef SIZE_MAX
Packit Service 85e8c2
# undef SIZE_MAX
Packit Service 85e8c2
#endif])
Packit Service 85e8c2
])
Packit Service 85e8c2
Packit Service 85e8c2
dnl Autoconf >= 2.61 has AC_COMPUTE_INT built-in.
Packit Service 85e8c2
dnl Remove this when we can assume autoconf >= 2.61.
Packit Service 85e8c2
m4_ifdef([AC_COMPUTE_INT], [], [
Packit Service 85e8c2
  AC_DEFUN([AC_COMPUTE_INT], [_AC_COMPUTE_INT([$2],[$1],[$3],[$4])])
Packit Service 85e8c2
])