dnl Process this file with autoconf to produce a configure script.
AC_INIT([gfarm], [2.7.11])
AC_CONFIG_SRCDIR(RELNOTES)
AC_CONFIG_AUX_DIR(makes)
AC_CONFIG_HEADER(include/gfarm/gfarm_config.h)
AC_CONFIG_MACRO_DIR([m4])

# Determine target system
# This is called "host" on autotools, because its terminology is following:
# build system - system which is used to build a tool
# host system - system which is used to run a tool
# target system - system which is used to run a program generated by a tool
AC_CANONICAL_HOST

# $host_os_nickname is used to select startup RC scripts, etc.
# currently GNU/Hurd and Debian/kFreeBSD are treated as Linux, because they
# are using Debian way, but not really tested.
case $host_os in
gnu*)		host_os_nickname=linux;;	# "gnu" (GNU/Hurd)
kfreebsd-gnu*)	host_os_nickname=linux;;	# (Debian/kFreeBSD)
linux-gnu*)	host_os_nickname=linux;;	# (Linux)
netbsdelf*)	host_os_nickname=netbsd;;	# "netbsdelf4.0"
osf*)		host_os_nickname=osf1;;		# "osf5.1b"
*)		host_os_nickname=`echo $host_os | sed -e 's/[[0-9.]]*$//'`;;
			# "netbsd6.1.4" -> "netbsd"
esac

######
###### Checks for programs.
######

AC_PROG_CC(cc gcc) # search cc before gcc
if test x"$GCC" = x"yes"; then # CFLAGS='-g -O2'
  CFLAGS="${CFLAGS} -Wall"
else # CFLAGS='-g'
  CFLAGS="-O" # don't use -g, but use -O
fi
AC_OPENMP

# Check whether gcc accepts '-rdynamic' option
# This check is performed for backtrace_symbols() function of glibc.
dynamic_ldflags=
if test x"$GCC" = x"yes"; then
  AC_MSG_CHECKING([whether gcc accepts -rdynamic])
  LDFLAGS_SAVE="$LDFLAGS"
  LDFLAGS="$LDFLAGS -rdynamic"
  AC_TRY_LINK([], [
      return 0;
    ], [
      AC_MSG_RESULT([yes])
      dynamic_ldflags="-rdynamic"
    ], [
      AC_MSG_RESULT([no])
    ])
  LDFLAGS="$LDFLAGS_SAVE"
fi
AC_SUBST(dynamic_ldflags)

AC_PROG_LEX
# the following definition should not be needed.
# but there is stupid environment which flex exists but libfl.a doesn't.
if test x"$LEX" = x"flex"; then
  AC_CHECK_LIB(fl, yywrap, [LEXLIB=-lfl], [LEXLIB=-ll])
fi

if mkdir -p . 2>/dev/null; then
  MKDIR_P='mkdir -p'
else
  test -d ./-p && rmdir ./-p
  MKDIR_P='$(top_srcdir)/makes/mkdirhier'
fi
AC_SUBST(MKDIR_P)

AC_PROG_YACC
AC_PROG_INSTALL
AC_PROG_LIBTOOL

AC_PATH_PROG([RUBY], [ruby], [/usr/bin/ruby])
AC_PATH_PROG([PERL], [perl], [/usr/bin/perl])

######
###### parsing command line options
######

###
### --with-pthread=PTHREAD_ROOT, or --without-pthread
###

AC_MSG_CHECKING([pthread])
AC_ARG_WITH(pthread,
[  --with-pthread=PTHREAD_ROOT	pthread root directory
				[[default=/usr]]
  --without-pthread             disable pthread support],
[with_pthread="${withval}"
])

ax_cv_have_pthread=no

if test x"${with_pthread}" != x"no"; then

  AC_DEFUN([AC_TRY_PTHREAD], [
    CPPFLAGS_SAVE="$CPPFLAGS"
    LIBS_SAVE="$LIBS"
    CPPFLAGS="${pthread_includes} $1 $CPPFLAGS_SAVE"
    # -R is not portable without libtool, and not necessary for AC_TRY_LINK
    LIBS="`echo $2 | sed 's/-R[[^ ]]*//g'` $LIBS_SAVE"
    AC_TRY_LINK([
#include <pthread.h>

void *foo(void *p){ return 0; }],[
	pthread_t thread_id;
	pthread_create(&thread_id, NULL, foo, NULL);
], [	ax_cv_have_pthread=yes
	pthread_includes="${pthread_includes} $1"
	pthread_libs="${pthread_libs} $2"
    ], [
	if test x"${pthread_specified}" = x"explicit"; then
	  AC_MSG_ERROR([\"$3\" is specified, but cannot be linked, aborted])
	fi
    ])
    CPPFLAGS="$CPPFLAGS_SAVE"
    LIBS="$LIBS_SAVE"
  ])

  # always define _REENTRANT
  pthread_includes="-D_REENTRANT"
  pthread_libs=

  if expr x"${with_pthread}" : x"/.*" >/dev/null; then # pathname specified?
    pthread_specified=explicit
    case ${with_pthread} in
    /usr)
      AC_TRY_PTHREAD(,-lpthread,${with_pthread})
      ;;
    *)
      AC_TRY_PTHREAD(-I${with_pthread}/include,-R${with_pthread}/lib -L${with_pthread}/lib -lpthread,${with_pthread})
      ;;
    esac
  elif test x"${with_pthread}" != x && test x"${with_pthread}" != x"yes"; then
    pthread_specified=explicit
    AC_TRY_PTHREAD(${with_pthread},${with_pthread},${with_pthread})
  elif test x"${PTHREAD_CFLAGS+1}${PTHREAD_LDFLAGS+2}${PTHREAD_LIBS+3}" != x""
  then # NetBSD pkgsrc configuration is specified?
    pthread_specified=explicit
    AC_TRY_PTHREAD(${PTHREAD_CFLAGS},${PTHREAD_LDFLAGS} ${PTHREAD_LIBS},${PTHREAD_CFLAGS} ${PTHREAD_LDFLAGS} ${PTHREAD_LIBS})
  else # otherwise try system supplied pthread library
    pthread_specified=yes
    case $host in
    *-*-aix*)
	AC_TRY_PTHREAD(-mthreads,-mthreads,);;
    *-*-freebsd[[2-4]].*) # FreeBSD 2.X ... 4.X (-lc_r) needs -D_THREAD_SAFE
	AC_TRY_PTHREAD(-D_THREAD_SAFE -pthread,-pthread,);;
    *-*-hpux*)
	AC_TRY_PTHREAD(-D_POSIX_C_SOURCE=199506L -D_HPUX_SOURCE,-lpthread,);;
    *-*-solaris*)
	# -D_POSIX_PTHREAD_SEMANTICS is for SUSv2 compliant getpwuid_r() etc.
	if test x"$GCC" = x"yes"; then
		AC_TRY_PTHREAD(-pthread -D_POSIX_PTHREAD_SEMANTICS,-pthread,)
	else
		AC_TRY_PTHREAD(-mt -D_POSIX_PTHREAD_SEMANTICS,-mt -lpthread,)
	fi;;
    *-*-sysv5uw7*) # UnixWare 7
	if test x"$GCC" = x"yes"; then
		AC_TRY_PTHREAD(-pthread,-pthread,)
	else
		AC_TRY_PTHREAD(-Kthread,-Kthread,)
	fi;;
    *)
	# Tru64 (OSF1): -pthread
	# FreeBSD 5.X: -pthread, 3 libraries are available as follows:
	#	-lpthread (M:N, default), -lc_r (1:N), and -lthr (1:1)
	# NetBSD 2.X or later: -pthread
	# Linux: -pthread
	AC_TRY_PTHREAD(-pthread,-pthread,);;
    esac

    # and if it fails too, just try default ${CFLAGS}, ${LDFLAGS} and ${LIBS}
    if test x"${ax_cv_have_pthread}" != x"yes"; then
      AC_TRY_PTHREAD(,,)
    fi
  fi
fi

case ${ax_cv_have_pthread} in
yes)
  AC_MSG_RESULT([using CFLAGS=\"${pthread_includes}\" and LDLIBS=\"${pthread_libs}\"])
  AC_DEFINE(HAVE_PTHREAD, 1, [pthread library exists])

  pthread_srcs='${PTHREAD_SRCS}'
  pthread_objs='${PTHREAD_OBJS}'
  pthread_cflags='${PTHREAD_CFLAGS}'
  pthread_targets='${PTHREAD_TARGETS}'
  ;;
*)
  if test x"${with_pthread}" = x"no"; then
    AC_MSG_RESULT([disabled])
  else
    # in this case, must be: test x"${pthread_specified}" != x"explicit"
    AC_MSG_RESULT([no])
  fi

  pthread_includes=
  pthread_libs=
  pthread_srcs=
  pthread_objs=
  pthread_cflags=
  pthread_targets=
  ;;
esac

AC_SUBST(pthread_includes)
AC_SUBST(pthread_libs)
# for conditional compilation which depends on pthread
AC_SUBST(pthread_srcs)
AC_SUBST(pthread_objs)
AC_SUBST(pthread_cflags)
AC_SUBST(pthread_targets)

###
### --with-openssl=OpenSSL_ROOT
###

# sanity check for OpenSSL is deferred, because ${openssl_includes}
# and ${openssl_lib} may be overrided by Globus autoconfiguration.

AC_ARG_WITH(openssl,
[  --with-openssl=OpenSSL_ROOT	openssl root directory
				[[default=/usr]]],
[with_openssl="${withval}"
],
[with_openssl=/usr
])
AC_MSG_RESULT([using \"${with_openssl}\"])

openssl_libs='-lssl -lcrypto'
if test x"${with_openssl}" = x"/usr"; then
   openssl_includes=
else
   openssl_includes="-I${with_openssl}/include"
   openssl_libs="-R${with_openssl}/lib -L${with_openssl}/lib ${openssl_libs}"
fi


AC_SUBST(openssl_includes)
AC_SUBST(openssl_libs)

###
### --with-globus=GLOBUS_ROOT --with-globus-flavor=FLAVOR -with-globus-static
###

AC_ARG_WITH(globus_static,
[  --with-globus-static		link static version of globus libraries instead of shared version
				[[default=disable]]],
[globus_static="${withval}"
],
[globus_static=""
])

AC_ARG_WITH(globus_flavor,
[  --with-globus-flavor=FLAVOR	globus flavor name
				[[default=guessed]]],
[globus_flavor="${withval}"
],
[globus_flavor=
])

AC_SUBST(globus_flavor)

AC_ARG_WITH(globus_libdir,
[  --with-globus-libdir=DIR	globus library directory
				[[default=guessed]]],
[globus_libdir="${withval}"
],
[globus_libdir=
])

AC_MSG_CHECKING([globus])
AC_ARG_WITH(globus,
[  --with-globus=GLOBUS_ROOT	globus root directory
				[[default=disable]]],
[globus_location="${withval}"
 case "${globus_location}" in yes) globus_location=${GLOBUS_LOCATION};; esac

 # try pkg-config first
 try_pkgconfig=true
 if test x"$globus_libdir" != x &&
      test -f "$globus_libdir/pkgconfig/globus-gssapi-gsi.pc"
 then
    GLOBUS_PKG_CONFIG_ENV="env PKG_CONFIG_PATH=$globus_libdir/pkgconfig${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}"
 elif test x"$globus_location" = x; then
    : # nothing to do
 elif test -f "$globus_location/lib/pkgconfig/globus-gssapi-gsi.pc"; then
    GLOBUS_PKG_CONFIG_ENV="env PKG_CONFIG_PATH=$globus_location/lib/pkgconfig${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}"
 elif globus_pkg_config=`
	find $globus_location -name globus-gssapi-gsi.pc -print | head -1`
      test x"$globus_pkg_config" != x
 then
    GLOBUS_PKG_CONFIG_ENV="env PKG_CONFIG_PATH=`dirname $globus_pkg_config`${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}"
 else
    # if --with-globus=DIR is specified, but globus-gssapi-gsi.pc doesn't
    # exist in the DIR, do not try "pkg-config globus-gssapi-gsi"
    try_pkgconfig=false
 fi
 if $try_pkgconfig && $GLOBUS_PKG_CONFIG_ENV pkg-config globus-gssapi-gsi; then
	case ${globus_static} in
	yes) globus_static=--static;;
	*)   globus_static=        ;;
	esac
	globus_gssapi_libs=`$GLOBUS_PKG_CONFIG_ENV pkg-config ${globus_static} --libs globus-gssapi-gsi`
	globus_includes=`$GLOBUS_PKG_CONFIG_ENV pkg-config --cflags globus-gssapi-gsi`
	AC_MSG_RESULT([found])
 else
   # Grid Packaging Tools (GPT)
   GLBlocation_candidates=${globus_location:-/usr/grid}
   if test -n "$globus_flavor"; then
	GLBflavor_candidates="$globus_flavor"
   else
	GLBflavor_candidates=
	if test x"$GCC" = x"yes"; then
		tmp='gcc vendorcc'
	else
		tmp='vendorcc gcc'
	fi
	for GLBbits in 64 32; do
		for GLBcc in $tmp; do
			for GLBdebug in "" dbg; do
				# gfmd requires thread-safe library
				for GLBpthread in pthr; do
					GLBflavor_candidates="$GLBflavor_candidates $GLBcc$GLBbits$GLBdebug$GLBpthread"
				done
			done
		done
	done
   fi
   globus_found=
   for d in ${GLBlocation_candidates}; do
	for f in ${GLBflavor_candidates}; do
		if test -f "$d/include/$f/gssapi.h"; then
			globus_location=$d
			globus_flavor=$f
			globus_found=yes
			break
		fi
	done
	if test x"$globus_found" = x"yes"; then break; fi
   done
   if test x"$globus_found" != x"yes"; then
	AC_MSG_RESULT([not found])
	AC_MSG_RESULT([searched directories are: ${GLBlocation_candidates}])
	AC_MSG_RESULT([searched flavors are: ${GLBflavor_candidates}])
	if test -n "$globus_flavor"; then
		AC_MSG_ERROR([cannot find the globus flavor \"$globus_flavor\", missing include/$globus_flavor/gssapi.h under ${GLBlocation_candidates}.])
	else
		AC_MSG_ERROR([cannot automatically find include/\${GLOBUS_FLAVOR}/gssapi.h under ${GLBlocation_candidates}, please specify the flavor by --with-globus-flavor=GLOBUS_FLAVOR])
	fi
	exit 1
   fi
   f_pthr=`expr "$globus_flavor" : '.*\(pthr\)'`
   if test x"$f_pthr" != xpthr; then
	AC_MSG_RESULT([bad flavor \"$globus_flavor\"])
	AC_MSG_ERROR([MT-safe flavor required])
	exit 1
   fi
   AC_MSG_RESULT([found in \"${globus_location}\" with \"${globus_flavor}\" flavor])

   globus_includes="-I${globus_location}/include/${globus_flavor}"
   if test x"$globus_libdir" = x; then
	globus_libdir="${globus_location}/lib"
   fi

   cfgmhtmp=/tmp/cfgmh$$
   AC_MSG_CHECKING([globus library names])
   if	case ${globus_static} in
	yes)	env GLOBUS_LOCATION=${globus_location} ${globus_location}/bin/globus-makefile-header -static -flavor=${globus_flavor} globus_gssapi_gsi;;
	*)	env GLOBUS_LOCATION=${globus_location} ${globus_location}/bin/globus-makefile-header         -flavor=${globus_flavor} globus_gssapi_gsi;;
	esac > $cfgmhtmp
   then
	AC_MSG_RESULT([done])
   else
	rm -f $cfgmhtmp
	AC_MSG_RESULT([failed to call ${globus_location}/bin/globus-makefile-header command.])
	AC_MSG_RESULT([Please run \${GPT_LOCATION}/sbin/gpt-postinstall, if it is not invoked yet.])
	AC_MSG_ERROR([Globus installation is incomplete, aborted.])
   fi
   globus_gssapi_libs=`sed -n '/^GLOBUS_PKG_LIBS *=/s///p' $cfgmhtmp`
   rm -f $cfgmhtmp
   case ${globus_static} in
   yes)	:;;
   *)	globus_gssapi_libs="-R${globus_libdir} -L${globus_libdir} $globus_gssapi_libs"
   esac

   # replace openssl library with that included in globus
   if test -x ${globus_location}/bin/globus-version; then
     globus_version=`env GLOBUS_LOCATION=${globus_location} ${globus_location}/bin/globus-version`
     case ${globus_version} in
     4.0*|3*|2*)
       openssl_includes="${globus_includes}"
       case ${globus_static} in
       yes) openssl_libs="${globus_libdir}/libssl_${globus_flavor}.a ${globus_libdir}/libcrypto_${globus_flavor}.a"
	  AC_MSG_RESULT([using globus static library: yes]);;
       *)	  openssl_libs="-R${globus_libdir} -L${globus_libdir} -lssl_${globus_flavor} -lcrypto_${globus_flavor}"
	  AC_MSG_RESULT([using globus static library: no]);;
       esac;;
     *) # after 4.2.0, globus does not include openssl libarary.
       ;;
     esac
   fi
 fi # End of Grid Packaging Tools (GPT)

 # for conditional compilation which depends on whether globus is enabled or not
 globus_srcs='${GLOBUS_SRCS}'
 globus_objs='${GLOBUS_OBJS}'
 globus_cflags='${GLOBUS_CFLAGS}'
 globus_targets='${GLOBUS_TARGETS}'

 AC_DEFINE(HAVE_GSI, 1, [Grid Security Infrastructure exists])
],
[AC_MSG_RESULT([disabled])
 globus_location=
 globus_includes=
 globus_gssapi_libs=

 # for conditional compilation which depends on whether globus is enabled or not
 globus_srcs=
 globus_objs=
 globus_cflags=
 globus_targets=
])

AC_SUBST(globus_location)
AC_SUBST(globus_includes)
AC_SUBST(globus_gssapi_libs)

# for conditional compilation which depends on whether globus is enabled or not
AC_SUBST(globus_srcs)
AC_SUBST(globus_objs)
AC_SUBST(globus_cflags)
AC_SUBST(globus_targets)

###
### --without-mtsafe-netdb ... are getaddrinfo(3) and getnameinfo(3) MT-Safe?
###

AC_ARG_WITH(mtsafe_netdb,
[  --without-mtsafe-netdb	getaddrinfo(3) and getnameinfo(3) are MT-Safe?
				[[default=yes]]],
[mtsafe_netdb="${withval}"], [mtsafe_netdb="yes"])

case ${mtsafe_netdb} in
no) :;;
*)  AC_DEFINE(HAVE_MTSAFE_NETDB, 1, [getaddrinfo(3) and getnameinfo(3) are MT-Safe]);;
esac

###
### --with-infiniband ...
###
AC_MSG_CHECKING([InfiniBand])
AC_ARG_WITH(infiniband,
[  --with-infiniband=InfiniBand_ROOT		support InfiniBand RDMA between client and gfsd
   			      	[[default=/usr]]],
[ib_rdma="${withval}"
], [ib_rdma=no])

rdma_includes=
rdma_libs=
case ${ib_rdma} in
yes|'')
	rdma_libs=" -libverbs"
	rdma_include=
	ib_rdma=yes
	;;
/*)
	rdma_libs=" -R ${ib_rdma}/lib -L ${ib_rdma}/lib  -libverbs"
	rdma_includes=" -I ${ib_rdma}/include"
	ib_rdma=yes
	;;
no)
	ib_rdma=no
	;;
*)
	AC_MSG_ERROR([invalid option \"${ib_rdma}\" options, aborted])
	;;
esac

if test ${ib_rdma} = no; then
	AC_MSG_RESULT([no])
else
	CPPFLAGS_SAVE="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS $rdma_includes"

	LIBS_SAVE="$LIBS"
	LIBS="$LIBS $rdma_libs"
AC_TRY_LINK([
#include <infiniband/verbs.h>],[
struct ibv_device **dev_list;
dev_list = ibv_get_device_list(NULL);
], [
	AC_MSG_RESULT([yes])
	AC_DEFINE(HAVE_INFINIBAND, 1, [support InfiniBand RDMA between client and gfsd])
], [
	LIBS="$LIBS_SAVE"
	AC_MSG_ERROR([InfiniBand cannot be linked with \"${rdma_includes} ${rdma_libs}\" options, aborted])
])
fi

AC_SUBST(rdma_includes)
AC_SUBST(rdma_libs)

######
###### Checks for libraries.
######

### check sanity of $CC, $CFLAGS and $CPPFLAGS

AC_CHECK_HEADERS(stdio.h)
if test x"${ac_cv_header_stdio_h}" != x"yes"; then
	AC_MSG_RESULT([preprocessor option error on: \"$ac_cpp\"])
	#exit 1
fi

### check sanity of $CC, $CFLAGS, $CPPFLAGS and $LDFLAGS setting

AC_CHECK_FUNCS(printf)
if test x"${ac_cv_func_printf}" != x"yes"; then
	AC_MSG_RESULT([linker option error on: \"$ac_link\"])
	exit 1
fi

# We need to add those libraries to $LIBS at first,
# otherwise some libraries (e.g. OpenLDAP and OpenSSL) cannot be linked.
AC_CHECK_LIB(dl, dlopen)
AC_CHECK_LIB(nsl, gethostname)	# SVR4 and derived
AC_CHECK_LIB(socket, socket)	# SVR4 and derived
AC_CHECK_LIB(crypt, crypt)	# 4.4BSD and derived
AC_CHECK_LIB(compat, cuserid)	# NetBSD
AC_CHECK_LIB(gen, basename)	# IRIX
AC_CHECK_LIB(rt, fdatasync)	# Solaris
AC_CHECK_LIB(cext, seteuid)	# HP-UX
AC_CHECK_LIB(perfstat, perfstat_cpu_total) # AIX, to implement getloadavg()

# have pthread_attr_setstacksize(3)?
case ${ax_cv_have_pthread} in
yes)
  AC_MSG_CHECKING([for pthread_attr_setstacksize])
  CPPFLAGS_SAVE="$CPPFLAGS"
  LIBS_SAVE="$LIBS"
  CPPFLAGS="${pthread_includes} $CPPFLAGS"
  LIBS="$LIBS ${pthread_libs}"

  AC_TRY_LINK([
#   include <pthread.h>

    void test(void){}],[
      pthread_attr_t at;
      if (pthread_attr_init(&at) == 0)
        pthread_attr_setstacksize(&at, 64 * 1024);
    ], [
      AC_MSG_RESULT([yes])
      AC_DEFINE(HAVE_PTHREAD_ATTR_SETSTACKSIZE, 1, [pthread_attr_setstacksize() exists])
    ], [
      AC_MSG_RESULT([no])
    ])

  # Checks for library functions with pthread enabled
  AC_CHECK_FUNCS(pthread_barrier_wait pthread_mutex_timedlock)

  CPPFLAGS="$CPPFLAGS_SAVE"
  LIBS="$LIBS_SAVE"
  ;;
esac

# Since gfarm-1.3, libgfarm calls pthread functions internally,
# thus, we need to link ${pthread_libs}, unless libc supplies weak symbols.
# e.g. using 3rd party pthread library, FreeBSD-4.x, or NetBSD-2.x
AC_MSG_CHECKING([whether libc supplies weak symbols for pthread])
AC_TRY_LINK([
#include <pthread.h>

void init(void){}],[
    pthread_once_t once;
    pthread_once(&once, init);
], [
	AC_MSG_RESULT([yes])
], [
	AC_MSG_RESULT([no. WARNING: always link pthread])
	LIBS="${LIBS} ${pthread_libs}"
])

### use 64bit off_t, if possible.

AC_MSG_CHECKING([Large File extention for 64bit off_t])
if ( getconf LFS_CFLAGS &&
     getconf LFS_LDFLAGS &&
     getconf LFS_LIBS ) > /dev/null 2>&1; then
	AC_MSG_RESULT([yes])

	largefile_cflags="`getconf LFS_CFLAGS`"
	largefile_ldflags="`getconf LFS_LDFLAGS`"
	LIBS="$LIBS `getconf LFS_LIBS`"

	if test x"$host_os" = x"linux-gnu"; then
		# glibc requires _GNU_SOURCE for pwrite64(2)
		largefile_cflags="${largefile_cflags} -D_GNU_SOURCE"
	fi

else
	AC_MSG_RESULT([no])

	largefile_cflags=
	largefile_ldflags=
fi

AC_SUBST(largefile_cflags)
AC_SUBST(largefile_ldflags)

### is _LARGEFILE64_SOURCE API supported? i.e. can be _LFS64_LARGEFILE=1?

AC_MSG_CHECKING([_LARGEFILE64_SOURCE API])
if ( getconf LFS64_CFLAGS &&
     getconf LFS64_LDFLAGS &&
     getconf LFS64_LIBS ) > /dev/null 2>&1; then
	AC_MSG_RESULT([yes])

	largefile64_srcs='${LARGEFILE64_SRCS}'
	largefile64_objs='${LARGEFILE64_OBJS}'
	largefile64_cflags='${LARGEFILE64_CFLAGS}'
	largefile64_targets='${LARGEFILE64_TARGETS}'
else
	AC_MSG_RESULT([no])

	largefile64_srcs=
	largefile64_objs=
	largefile64_cflags=
	largefile64_targets=
fi

# for conditional compilation which depends on _LARGEFILE64_SOURCE API
AC_SUBST(largefile64_srcs)
AC_SUBST(largefile64_objs)
AC_SUBST(largefile64_cflags)
AC_SUBST(largefile64_targets)

### sanity check whether OpenSSL is really usable or not
### only OpenSSL-0.9.7 or later is supported since Gfarm-2.6.15

AC_MSG_CHECKING([openssl])

CPPFLAGS_SAVE="$CPPFLAGS"
LIBS_SAVE="$LIBS"
CPPFLAGS="${openssl_includes} $CPPFLAGS_SAVE"
# -R is not portable without libtool, and not necessary for AC_TRY_LINK
LIBS="`echo ${openssl_libs} | sed 's/-R[[^ ]]*//g'` $LIBS_SAVE"

AC_TRY_LINK([
#include <stdio.h>
#include <openssl/evp.h>],[
EVP_MD_CTX *md_ctx = EVP_MD_CTX_create();
EVP_MD *md_type = EVP_get_digestbyname("sha1");

EVP_DigestInit_ex(md_ctx, md_type, NULL);
], [], [
  AC_MSG_ERROR([OpenSSL cannot be linked with \"${openssl_includes} ${openssl_libs}\" options, aborted])
])

AC_MSG_RESULT([using \"${openssl_includes} ${openssl_libs}\"])

# these functions disappeared since OpenSSL-1.1.0
AC_CHECK_FUNCS(CRYPTO_set_locking_callback CRYPTO_set_id_callback)

CPPFLAGS="$CPPFLAGS_SAVE"
LIBS="$LIBS_SAVE"

###
### --with-openldap=OpenLDAP_ROOT
###

# This command line option must be handled here,
# because this needs AC_CHECK_LIB(nsl, ...)

AC_MSG_CHECKING([openldap])
AC_ARG_WITH(openldap,
[  --with-openldap=OpenLDAP_ROOT	openldap root directory
				[[default=guessed]]
  --without-openldap			disable openldap],
[with_openldap="${withval}"
],
[with_openldap=
])

ldap_specified=
case ${with_openldap} in
/*)
  if test -f "${with_openldap}"/include/ldap.h; then
	ldap_specified=explicit
  else
	AC_MSG_ERROR([not found in \"${with_openldap}\", aborted])
  fi;;
yes|'')
  for d in /usr/local/openldap /usr/local /usr/pkg /usr
  do
    if test -f $d/include/ldap.h; then
	with_openldap=$d
	ldap_specified=yes
	break
    fi
  done;;
no)
  ;;
*)
  ldap_specified=explicit;;
esac

ldap_usable=

if test x"${ldap_specified}" = x""; then
  AC_MSG_RESULT([no])
else
  AC_DEFUN([AC_TRY_LDAP], [
    CPPFLAGS_SAVE="$CPPFLAGS"
    LIBS_SAVE="$LIBS"
    CPPFLAGS="$1 $CPPFLAGS_SAVE"
    # -R is not portable without libtool, and not necessary for AC_TRY_LINK
    LIBS="`echo $2 | sed 's/-R[[^ ]]*//g'` $3 $4 ${openssl_libs} $LIBS_SAVE"
	# OpenLDAP may need OpenSSL due to its SSL/TLS support.
    AC_TRY_LINK([
#include <lber.h>
#include <ldap.h>],[
	LDAP *ldap_server = ldap_open("example.com", 389);
], [
	if test x"$4" != x""; then
		AC_MSG_RESULT([using \"$1 $2 $3\" with \"$4\" for LDAP])
		ldap_libs="$2 $3 $4"
	else
		AC_MSG_RESULT([using \"$1 $2 $3\" for LDAP])
		ldap_libs="$2 $3"
	fi
	ldap_usable=yes
	# this needs to be checked here due to settings of $CPPFLAGS and $LIBS
	AC_CHECK_FUNCS(ldap_memfree ldap_set_option ldap_start_tls_s ldap_perror)
    ], [])
    CPPFLAGS="$CPPFLAGS_SAVE"
    LIBS="$LIBS_SAVE"
  ])

  case ${with_openldap} in
  /*)	case ${with_openldap} in
	/usr)	ldap_includes=
		ldap_libs=;;
	*)	ldap_includes="-I${with_openldap}/include"
		ldap_libs="-R${with_openldap}/lib -L${with_openldap}/lib";;
	esac
	# OpenLDAP since 2.x calls res_query() and dn_expand(), if dnssrv is
	# configured, and DNS resolver library doesn't belongs to libc on some
	# systems.
	# But we cannot blindly link libresolv, because it may change behaviour
	# of gethostbyname() and friends.
	for libs in "-lldap -llber" "-lldap"; do
	  for extra_lib in "" "-lresolv" "-lbind"; do
	    AC_TRY_LDAP(${ldap_includes},${ldap_libs},${libs},${extra_lib})
	    if test x"${ldap_usable}" = x"yes"; then
	      break 2
	    fi
	  done
	done
	;;
  *)	ldap_includes=${with_openldap}
	ldap_libs=${with_openldap}
	AC_TRY_LDAP(${ldap_includes},${ldap_libs},,)
	;;
  esac

  if test x"${ldap_usable}" != x"yes"; then
    case $ldap_specified in
    explicit)
	  AC_MSG_ERROR([\"${with_openldap}\" is specified, but cannot be linked, aborted]);;
    yes)
	  AC_MSG_RESULT([found in \"${with_openldap}\", but cannot be linked, ignored]);;
    esac
  fi
fi

case $ldap_usable in
yes)	AC_DEFINE(HAVE_LDAP, 1, [LDAP library exists])
	ldap_srcs='${LDAP_SRCS}'
	ldap_objs='${LDAP_OBJS}'
	ldap_cflags='${LDAP_CFLAGS}'
	ldap_targets='${LDAP_TARGETS}'
	;;
*)	ldap_includes=
	ldap_libs=
	ldap_srcs=
	ldap_objs=
	ldap_cflags=
	ldap_targets=
	;;
esac

AC_SUBST(ldap_includes)
AC_SUBST(ldap_libs)

# for conditional compilation which depends on LDAP
AC_SUBST(ldap_srcs)
AC_SUBST(ldap_objs)
AC_SUBST(ldap_cflags)
AC_SUBST(ldap_targets)

###
### --with-postgresql=PostgreSQL_ROOT
###

AC_MSG_CHECKING([PostgreSQL])
AC_ARG_WITH(postgresql,
[  --with-postgresql=PostgreSQL_ROOT	PostgreSQL root directory
				[[default=guessed]]
  --without-postgresql			disable PostgreSQL],
[with_postgresql="${withval}"
],
[with_postgresql=
])

postgresql_specified=
case ${with_postgresql} in
/*)
  if test -f "${with_postgresql}"/bin/pg_config; then
	postgresql_specified=explicit
  else
	AC_MSG_ERROR([not found in \"${with_postgresql}\", aborted])
  fi;;
yes|'')
  for d in /usr/local/pgsql /usr/local /usr/pkg /usr
  do
    if test -f $d/bin/pg_config; then
	with_postgresql=$d
	postgresql_specified=yes
	break
    fi
  done;;
no)
  ;;
*)
  postgresql_specified=explicit;;
esac

postgresql_usable=

if test x"${postgresql_specified}" = x""; then
  AC_MSG_RESULT([no])
else
  if test -x ${with_postgresql}/bin/pg_config; then
    if ${with_postgresql}/bin/pg_config --version > /dev/null 2>&1; then
	:
    else
	AC_MSG_RESULT([failed to call ${with_postgresql}/bin/pg_config command.])
	AC_MSG_ERROR([PostgreSQL installation is incomplete, aborted.])
    fi
    postgresql_include_dir=`${with_postgresql}/bin/pg_config --includedir`
    postgresql_lib_dir=`${with_postgresql}/bin/pg_config --libdir`
    case ${postgresql_include_dir} in
    /usr/include) postgresql_includes= ;;
    *) postgresql_includes="-I${postgresql_include_dir}" ;;
    esac
    case ${postgresql_lib_dir} in
    /usr/lib) postgresql_libs="-lpq" ;;
    *) postgresql_libs="-R${postgresql_lib_dir} -L${postgresql_lib_dir} -lpq" ;;
    esac
  else
    postgresql_includes="${with_postgresql}"
    postgresql_libs="${with_postgresql}"
  fi

  CPPFLAGS_SAVE="$CPPFLAGS"
  LIBS_SAVE="$LIBS"

  CPPFLAGS="${postgresql_includes} $CPPFLAGS_SAVE"
  # -R is not portable without libtool, and not necessary for AC_TRY_LINK
  LIBS="`echo $postgresql_libs | sed 's/-R[[^ ]]*//g'` $LIBS_SAVE"

  AC_TRY_LINK([
#include <stdio.h>
#include <libpq-fe.h>],[
	PGconn *conn = PQconnectdb("");
	PGresult *res = PQexecParams(conn, "", 0, NULL, NULL, NULL, NULL, 0);
  ], [
	postgresql_usable=yes
	AC_MSG_RESULT([using \"${with_postgresql}\"])
  ], [
	case $postgresql_specified in
	explicit)
		AC_MSG_ERROR([\"${with_postgresql}\" is specified, but cannot be linked, aborted]);;
	yes)
		AC_MSG_RESULT([found in \"${with_postgresql}\", but cannot be linked, ignored]);;
	esac
  ])

  CPPFLAGS="$CPPFLAGS_SAVE"
  LIBS="$LIBS_SAVE"
fi

case $postgresql_usable in
yes)	AC_DEFINE(HAVE_POSTGRESQL, 1, [PostgreSQL library exists])
	postgresql_srcs='${POSTGRESQL_SRCS}'
	postgresql_objs='${POSTGRESQL_OBJS}'
	postgresql_cflags='${POSTGRESQL_CFLAGS}'
	postgresql_targets='${POSTGRESQL_TARGETS}'
	;;
*)	postgresql_includes=
	postgresql_libs=
	postgresql_srcs=
	postgresql_objs=
	postgresql_cflags=
	postgresql_targets=
	;;
esac

AC_SUBST(postgresql_includes)
AC_SUBST(postgresql_libs)

# for conditional compilation which depends on PostgreSQL
AC_SUBST(postgresql_srcs)
AC_SUBST(postgresql_objs)
AC_SUBST(postgresql_cflags)
AC_SUBST(postgresql_targets)

###
### sanity check. We need either PostgreSQL or LDAP.
###

if test x"${ldap_usable}" != x"yes" && test x"${postgresql_usable}" != x"yes"
then
	AC_MSG_RESULT([WARNING: neither LDAP or PostgreSQL is found])
fi

######
###### Checks for header files.
######

AC_CHECK_HEADERS(inttypes.h shadow.h crypt.h machine/endian.h sys/loadavg.h byteswap.h execinfo.h sys/xattr.h)

######
###### Checks for types.
######

AC_C_BIGENDIAN

AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(long long)

# Solaris, Linux
AC_CHECK_MEMBERS([struct stat.st_atim.tv_nsec, struct stat.st_mtim.tv_nsec])
# *BSD, including MacOS X
AC_CHECK_MEMBERS([struct stat.st_atimespec.tv_nsec, struct stat.st_mtimespec.tv_nsec])

### socklen_t is available?

AC_MSG_CHECKING([socklen_t])

case $host_os_nickname in
hpux)
  # There are two ways to deal with socklen_t on HP-UX 11i.
  # 1. Use system supplied socklen_t.
  #   This requires CPPFLAGS=-D_XOPEN_SOURCE_EXTENDED and LIBS=-lxnet.
  # 2. Use int.
  #   This is incompatible with both -D_XOPEN_SOURCE_EXTENDED and -lxnet.
  # socklen_t had been also usable for the case 2 on HP-UX before LP64 support.
  # But this is no longer true on LP64 HP-UX, because socklen_t is now always
  # size_t on HP-UX, and size_t is uint64_t on LP64 systems. The problem is
  # that 1 and 2 are incompatible, and cannot be linked each other.
  # We choose 2 here, because that seems to be the way most third party
  # libraries are using now.
  ax_cv_have_socklen_t=no # don't try linking, it's linkable, but doesn't work.
  ;;

*)
  AC_TRY_LINK([
# include <sys/types.h>
# include <sys/socket.h>],[
  int rv, s = socket(PF_INET, SOCK_STREAM, 0);
  struct sockaddr addr;
  socklen_t len;

  rv = getpeername(s, &addr, &len);
  ], [ax_cv_have_socklen_t=yes], [ax_cv_have_socklen_t=no])
  ;;
esac

if test "$ax_cv_have_socklen_t" = yes; then
  AC_MSG_RESULT([yes])
else

  # determine size of socklen_t

  AC_TRY_RUN([
# include <sys/types.h>
# include <sys/socket.h>

  main() {
	struct msghdr h;

	/* h.msg_namelen should be socklen_t */
	h.msg_namelen = -1;
	if (sizeof(h.msg_namelen) == sizeof(int) && h.msg_namelen < 0)
		return (0); /* int */
	else
		return (1); /* size_t */
  }
  ], [ax_cv_socklen_t=int], [ax_cv_socklen_t=size_t])

  AC_MSG_RESULT([$ax_cv_socklen_t])
  AC_DEFINE_UNQUOTED(socklen_t, $ax_cv_socklen_t,
   [3rd argument type of bind(2)/connect(2), if <sys/socket.h> doesn't define])
fi

######
###### Checks for structures.
######

AC_MSG_CHECKING([struct dirent::d_off])
AC_TRY_COMPILE([
#include <sys/types.h>
#include <dirent.h>],[
struct dirent d;
long off;

off = d.d_off;
], [ax_cv_have_d_off=yes], [ax_cv_have_d_off=no])
AC_MSG_RESULT([$ax_cv_have_d_off])
if test "$ax_cv_have_d_off" = yes; then
  AC_DEFINE(HAVE_D_OFF, 1, [struct dirent has d_off member])
fi

AC_MSG_CHECKING([struct dirent::d_namlen])
AC_TRY_COMPILE([
#include <sys/types.h>
#include <dirent.h>],[
struct dirent d;
int len;

len = d.d_namlen;
], [ax_cv_have_d_namlen=yes], [ax_cv_have_d_namlen=no])
AC_MSG_RESULT([$ax_cv_have_d_namlen])
if test "$ax_cv_have_d_namlen" = yes; then
  AC_DEFINE(HAVE_D_NAMLEN, 1, [struct dirent has d_namlen member])
fi

AC_MSG_CHECKING([struct dirent::d_type])
AC_TRY_COMPILE([
#include <sys/types.h>
#include <dirent.h>],[
struct dirent d;
int len;

len = d.d_type;
], [ax_cv_have_d_type=yes], [ax_cv_have_d_type=no])
AC_MSG_RESULT([$ax_cv_have_d_type])
if test "$ax_cv_have_d_type" = yes; then
  AC_DEFINE(HAVE_D_TYPE, 1, [struct dirent has d_type member])
fi

###### Checks for compiler characteristics.
# empty for now

######
###### Checks for library functions.
######

AC_CHECK_FUNCS(clock_gettime getdents fdatasync fdopendir poll pread pwrite snprintf getpassphrase mkdtemp setlogin sigtimedwait strtoll strtoq setrlimit daemon getloadavg statvfs statfs random getifaddrs getopt_long backtrace_symbols futimes futimesat utimensat)

### Check epoll_create really implemented

AC_MSG_CHECKING([for epoll])
AC_RUN_IFELSE([AC_LANG_SOURCE([
#include <sys/epoll.h>
int main() {
	return (epoll_create(5) == -1);
}])], [ax_cv_func_epoll=yes], [ax_cv_func_epoll=no], [ax_cv_func_epoll=no])
AC_MSG_RESULT([$ax_cv_func_epoll])
if test "$ax_cv_func_epoll" = yes; then
  AC_DEFINE(HAVE_EPOLL, 1, [Define to 1 if epoll functions are implemented.])
fi

### SUSv2 getpwnam_r() has 5 args, but Solaris's only has 4.

AC_MSG_CHECKING([SUSv2 compliant getpwnam_r])
AC_TRY_LINK([
#include <sys/types.h>
#include <pwd.h>],[
int rv;
struct passwd *pwd;
char buffer[1024];

rv = getpwnam_r("root", pwd, buffer, sizeof buffer, &pwd);
], [ax_cv_have_getpwnam_r=yes], [ax_cv_have_getpwnam_r=no])
AC_MSG_RESULT([$ax_cv_have_getpwnam_r])
if test "$ax_cv_have_getpwnam_r" = yes; then
  AC_DEFINE(HAVE_GETPWNAM_R, 1,
	[5 arguments getpwnam_r() which is compatible with SUSv2 exists])
fi

### SUSv2 getspnam_r() has 5 args, but Solaris's only has 4.

AC_MSG_CHECKING([SUSv2 compliant getspnam_r])
AC_TRY_LINK([
#include <shadow.h>],[
int rv;
struct spwd *sp;
char buffer[1024];

rv = getspnam_r("root", sp, buffer, sizeof buffer, &sp);
], [ax_cv_have_getspnam_r=yes], [ax_cv_have_getspnam_r=no])
AC_MSG_RESULT([$ax_cv_have_getspnam_r])
if test "$ax_cv_have_getspnam_r" = yes; then
  AC_DEFINE(HAVE_GETSPNAM_R, 1, [5 arguments getspnam_r() exists])
fi

### Check whether PTHREAD_PRIO_INHERIT is usable or not

AC_MSG_CHECKING([PTHREAD_PRIO_INHERIT])
AC_TRY_COMPILE([
#define _GNU_SOURCE /* CentOS 6 needs this, although CentOS 7 doesn't */
#include <pthread.h>],[
pthread_mutexattr_t attr;

pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
], [ax_cv_have_pthread_prio_inherit=yes], [ax_cv_have_pthread_prio_inherit=no])
AC_MSG_RESULT([$ax_cv_have_pthread_prio_inherit])
if test "$ax_cv_have_pthread_prio_inherit" = yes; then
  AC_DEFINE(HAVE_PTHREAD_PRIO_INHERIT, 1, [PTHREAD_PRIO_INHERIT is usable])
fi


###### Checks for system services.
# empty for now

######
###### miscellaneous
######

###
### config.h template
###

AH_TOP([
#ifndef GFARM_CONFIG_H
#define GFARM_CONFIG_H
])

AH_BOTTOM([
/* Grid Security Infrastructure exists */
#undef HAVE_GSI

/* 5 arguments getpwnam_r() which is compatible with SUSv2 exists */
#undef HAVE_GETPWNAM_R

/* 5 arguments getspnam_r() exists */
#undef HAVE_GETSPNAM_R

/* 3rd argument type of bind(2)/connect(2), if <sys/socket.h> doesn't define */
#undef socklen_t

#if SIZEOF_LONG == 8
	typedef long gfarm_int64_t;
	typedef unsigned long gfarm_uint64_t;
#	define GFARM_INT64_MAX		0x7fffffffffffffffL
#	define GFARM_UINT64_MAX		0xffffffffffffffffL
#	define GFARM_PRId64 "ld"
#	define gfarm_strtoi64(s, endptr)	strtol(s, endptr, 0)
#elif SIZEOF_LONG_LONG == 8
	typedef long long gfarm_int64_t;
	typedef unsigned long long gfarm_uint64_t;
#	define GFARM_INT64_MAX		0x7fffffffffffffffLL
#	define GFARM_UINT64_MAX		0xffffffffffffffffLL
#	define GFARM_PRId64 "lld"
#	define gfarm_strtoi64(s, endptr)	strtoll(s, endptr, 0)
#else
	error! cannot find 64bit integer type
#endif

#endif /* GFARM_CONFIG_H */
])

###
### gftool/config-gfarm
###

config_gfarm_sysdep=gftool/config-gfarm/sysdep/${host_os_nickname}
if test -f ${srcdir}/${config_gfarm_sysdep}/Makefile; then
	AC_MSG_RESULT([using ${config_gfarm_sysdep} directory for system dependent part of config-gfarm.])
	config_gfarm_sysdep_subdir=${host_os_nickname}
else
	config_gfarm_sysdep_subdir=default
fi

AC_SUBST(config_gfarm_sysdep_subdir)

###
### --enable-voms
###

AC_ARG_ENABLE(voms,
AS_HELP_STRING([--enable-voms],[enable VOMS synchronization feature [default=disable]]),
[if test "${enable_voms}" = "yes"; then
  AC_CHECK_PROG(PYTHON_SPECIFIED, python, yes, no)
  gfvoms_sync_targets=''
  AC_MSG_CHECKING(VOMS synchronization available)
  voms_available="no"
  if test "${PYTHON_SPECIFIED}" = "yes" ;then
    py_ver=`python -V 2>&1 | sed -e 's/Python \([[0-9\.]]*\).*/\1/'`
    case $py_ver in
      2.[[3456]].*)
        imp_path=`python -c "import sys; print sys.path" | sed -e 's/^\[[\(.*\)\]]$/\1/' | sed -e "s/[[,']]//g"`
        found=0
        for d in $imp_path
        do
          if test -d $d/ZSI ; then
            found=1
            break 1
          fi
        done
        if test $found -eq 0 ; then
          break 1
        fi
        found=0
        for d in $imp_path
        do
          if test -d $d/_xmlplus ; then
            found=1
            break 1
          fi
        done
        if test $found -eq 0 ; then
          break 1
        fi
        imp_test=`python -c "import ZSI" 2>&1`
        if test ${#imp_test} == 0; then
          gfvoms_sync_targets='${GFVOMS_SYNC_TARGETS}'
          voms_available="yes"
        fi
        ;;
    esac
  fi
  AC_MSG_RESULT(${voms_available})
else
  gfvoms_sync_targets=''
fi
],
[gfvoms_sync_targets=''
])
AC_SUBST(gfvoms_sync_targets)

###
###  --enable-xmlattr		support XML extended attributes and XPath search
###

AC_ARG_ENABLE(xmlattr,
AS_HELP_STRING([--enable-xmlattr],[support XML extended attributes and XPath search [default=disable]]),
[if test "${enable_xmlattr}" = "yes"; then
	AC_DEFINE(ENABLE_XMLATTR, 1, [support XML extended attributes and XPath search])
fi],[])

###
###  --enable-linuxkernel		support Linux kernel module utils
###

may_ib_symvers=''
may_ib_include=''

AC_MSG_CHECKING([linuxkernel])
AC_ARG_ENABLE(linuxkernel,
AS_HELP_STRING([--enable-linuxkernel],[support Linux kernel module utils.[[default=no]]]),
[if test "${enable_linuxkernel}" = "yes"; then
	linuxkernel_targets='linux'
	linuxkernel_makefile='linux/kernel/Makefile'
fi],[linuxkernel_targets=''])
AC_SUBST(linuxkernel_targets)

if test "${linuxkernel_targets}" = "linux"; then
	if test x"$host_os" != x"linux-gnu"; then
		AC_MSG_RESULT([host ${host_os} is not linux.])
		exit 1
	fi
	krelease=`uname -r`
	case $krelease in
	2.6.32*)
		AC_MSG_RESULT([yes])
		;;
	*)
		AC_MSG_RESULT([release ${krelease} is not supported.])
		exit 1
		;;
	esac
	tmp=`mktemp`
	linux/config/findIBmodules >$tmp
	val=$(grep -e --with-ib-symvers $tmp)
	if test "x${val}" != "x" ; then
		may_ib_symvers=$(echo $val|sed 's/.*=//')
	fi
	val=$(grep -e --with-ib-include $tmp)
	if test "x${val}" != "x" ; then
		may_ib_include=$(echo $val|sed 's/.*=//')
	fi
	rm -f $tmp
	###
	###  --extra-infiniband		InfiniBand synvers and include
	###
	ib_symvers=''
	AC_MSG_CHECKING([use of extra InfiniBand Module.symvers])
	AC_ARG_WITH(ib_symvers,
	[  --with-ib-symvers=PATH	extra InfiniBand Module.symvers,
			with --enable-linuxkernel.
			if you are not sure, run linux/config/findIBmodules
	],
	[ib_symvers="${withval}"
	],
	[ib_symvers=""
	])

	if test "x${ib_symvers}" = "x"; then
	  if test "x${may_ib_symvers}" = "x"; then
	    AC_MSG_RESULT([not use.])
	  else
	    AC_MSG_RESULT([using ${may_ib_symvers}])
	    ib_symvers=${may_ib_symvers}
	  fi
	else
	  if test -f "${ib_symvers}"; then
	    AC_MSG_RESULT([use ${ib_symvers}])
	  else
	    AC_MSG_RESULT([${ib_symvers} is not a file.])
	    exit 1
	  fi
	fi
	AC_SUBST(ib_symvers)

	ib_include=''
	AC_MSG_CHECKING([use of extra InfiniBand headers])
	AC_ARG_WITH(ib_include,
	[  --with-ib-include=DIR		extra InfiniBand headers,
			with --enable-linuxkernel.
			if you are not sure, run linux/config/findIBmodules
	],
	[ib_include="${withval}"
	],
	[ib_include=""
	])

	if test "x${ib_include}" = "x"; then
	  if test "x${may_ib_include}" = "x"; then
	    AC_MSG_RESULT([not use.])
	  else
	    AC_MSG_RESULT([using ${may_ib_include}])
	    ib_include=${may_ib_include}
	  fi
	else
	  if test -f "${ib_include}/rdma/ib_verbs.h"; then
	    AC_MSG_RESULT([use ${ib_include}])
	  else
	    AC_MSG_RESULT(no ib_verbs.h in [${ib_include}.])
	    exit 1
	  fi
	fi
	AC_SUBST(ib_include)

	if test "x${ib_include}" = "x"; then
		ib_verbs="<rdma/ib_verbs.h>"
	else
		ib_verbs="\"$ib_include/rdma/ib_verbs.h\""
	fi

	CFLAGS_SAVE="$CFLAGS"
	CFLAGS=""
	CPPFLAGS_SAVE="$CPPFLAGS"
	kbuild="/lib/modules/$krelease/build"
	CPPFLAGS="-I$kbuild/include -I$kbuild/arch/x86/include -D__KERNEL__"
	CPPFLAGS="$CPPFLAGS -include $kbuild/include/linux/kconfig.h"
	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
	[
	#define _DYNAMIC_DEBUG_H
	#include $ib_verbs
	]],
	[(void)ib_query_gid(NULL, 0, 0, NULL, NULL);]
	)], [ib_query_gid=5], [ib_query_gid=4])
	AC_MSG_RESULT(ib_query_gid has [${ib_query_gid} args.])
	AC_DEFINE_UNQUOTED(IB_QUERY_GID_ARG, ${ib_query_gid},
		[ib_query_gid has ${ib_query_gid} args.])
	CPPFLAGS="$CPPFLAGS_SAVE"
	CFLAGS="$CFLAGS_SAVE"
else
	AC_MSG_RESULT([no])
fi
###
### --with-private-srcdir
###

private_srcdir=''
AC_MSG_CHECKING([use of private source directory])
AC_ARG_WITH(private_srcdir,
[  --with-private-srcdir=DIR	private source directory
],
[private_srcdir="${withval}"
],
[private_srcdir=""
])

top_private_dir=''
if test "x${private_srcdir}" = "x"; then
  AC_MSG_RESULT([not use.])
else
  if test -d "${private_srcdir}"; then
    top_private_dir="`(cd ${private_srcdir}; /bin/pwd)`"
    AC_MSG_RESULT([use ${top_private_dir}])
  else
    AC_MSG_RESULT([${private_srcdir} is not a directory.])
    exit 1
  fi
fi

AC_SUBST(top_private_dir)

if test "x${top_private_dir}" = "x"; then
    # use public source directory.
    optional_config=/dev/null
    optional_rule='${PUBLIC_RULE}'
    optional_srcs='${PUBLIC_SRCS}'
    optional_objs='${PUBLIC_OBJS}'
    optional_cflags='${PUBLIC_CFLAGS}'
    optional_targets='${PUBLIC_TARGETS}'
else
    # use private source directory.
    optional_config='${top_private_dir}/private_config.mk'
    optional_rule='${PRIVATE_RULE}'
    optional_srcs='${PRIVATE_SRCS}'
    optional_objs='${PRIVATE_OBJS}'
    optional_cflags='${PRIVATE_CFLAGS}'
    optional_targets='${PRIVATE_TARGETS}'
fi

AC_SUBST(optional_config)
AC_SUBST(optional_rule)
AC_SUBST(optional_srcs)
AC_SUBST(optional_objs)
AC_SUBST(optional_cflags)
AC_SUBST(optional_targets)

##
## private sources macros end here.
##

# The following is for --srcdir option.
# AC_OUTPUT() assumes that parent directory of the Makefile exists
test -d include || mkdir include
test -d package || mkdir package
test -d util || mkdir util

AC_CONFIG_FILES(Makefile makes/config.mk makes/install-doc makes/build-script
	${ns_makefiles} ${linuxkernel_makefile}
	package/solaris/gfmd package/solaris/gfsd
)
AC_OUTPUT
