# Source of configuration for Kyoto Cabinet



#================================================================
# Generic Settings
#================================================================

# Package name
AC_INIT(kyotocabinet, 1.2.76)

# Package information
MYLIBVER=16
MYLIBREV=13
MYFORMATVER=5

# Targets
MYHEADERFILES="kccommon.h kcutil.h kcthread.h kcfile.h"
MYHEADERFILES="$MYHEADERFILES kccompress.h kccompare.h kcmap.h kcregex.h"
MYHEADERFILES="$MYHEADERFILES kcdb.h kcplantdb.h"
MYHEADERFILES="$MYHEADERFILES kchashdb.h"
MYLIBRARYFILES="libkyotocabinet.a"
MYLIBOBJFILES="kcutil.o kcthread.o kcfile.o kccompress.o kccompare.o kcmap.o kcregex.o"
MYLIBOBJFILES="$MYLIBOBJFILES kcdb.o kcplantdb.o"
MYLIBOBJFILES="$MYLIBOBJFILES kchashdb.o"
MYCOMMANDFILES="kctreemgr"
MYMAN1FILES="kctreemgr.1"
MYDOCUMENTFILES="COPYING FOSSEXCEPTION ChangeLog doc kyotocabinet.idl"
MYPCFILES="kyotocabinet.pc"

# Building flags
MYCFLAGS="-Wall -ansi -pedantic -fPIC -fsigned-char -g0 -O2"
MYCXXFLAGS="-Wall -fPIC -fsigned-char -g0 -O2"
MYCPPFLAGS="-I. -I\$(INCLUDEDIR) -I/usr/local/include"
MYCPPFLAGS="$MYCPPFLAGS -DNDEBUG -D_GNU_SOURCE=1"
MYCPPFLAGS="$MYCPPFLAGS -D_FILE_OFFSET_BITS=64 -D_REENTRANT -D__EXTENSIONS__"
MYLDFLAGS="-L. -L\$(LIBDIR) -L/usr/local/lib"
MYCMDLDFLAGS=""
MYCMDLIBS=""
MYLDLIBPATH=""
MYLDLIBPATHENV="LD_LIBRARY_PATH"
MYPOSTCMD="true"

# Building paths
PATH=".:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:$PATH"
CPATH=".:/usr/local/include:$CPATH"
LIBRARY_PATH=".:/usr/local/lib:$LIBRARY_PATH"
LD_LIBRARY_PATH=".:/usr/local/lib:$LD_LIBRARY_PATH"
PKG_CONFIG_PATH=".:/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"
export PATH CPATH LIBRARY_PATH LD_LIBRARY_PATH PKG_CONFIG_PATH



#================================================================
# Options
#================================================================

# Internal variables
enables=""
is_static=""

# Debug mode
AC_ARG_ENABLE(debug,
  AC_HELP_STRING([--enable-debug], [build for debugging]))
if test "$enable_debug" = "yes"
then
  MYCFLAGS="-Wall -ansi -pedantic -fPIC -fsigned-char -g -O0"
  MYCXXFLAGS="-Wall -fPIC -fsigned-char -g -O0"
  MYCPPFLAGS="$MYCPPFLAGS -UNDEBUG -D_KCDEBUG"
  is_static="yes"
  enables="$enables (debug)"
fi

# Developping mode
AC_ARG_ENABLE(devel,
  AC_HELP_STRING([--enable-devel], [build for development]))
if test "$enable_devel" = "yes"
then
  MYCFLAGS="-Wall -Wextra -Wno-unused-parameter"
  MYCFLAGS="$MYCFLAGS -ansi -pedantic -fPIC -fsigned-char -O2 -fno-inline -pipe"
  MYCXXFLAGS="-Wall -Wextra -Wno-unused-parameter -Wnon-virtual-dtor"
  MYCXXFLAGS="$MYCXXFLAGS -fPIC -fsigned-char -g -O2 -fno-inline -pipe"
  MYCPPFLAGS="$MYCPPFLAGS -UNDEBUG -D_KCDEBUG"
  enables="$enables (devel)"
fi

# Disable optimization
AC_ARG_ENABLE(opt,
  AC_HELP_STRING([--disable-opt], [build without optimization]))
if test "$enable_opt" = "no"
then
  MYCFLAGS="$MYCFLAGS -O0"
  MYCXXFLAGS="$MYCXXFLAGS -O0"
  enables="$enables (no-opt)"
fi

# Profiling mode
AC_ARG_ENABLE(profile,
  AC_HELP_STRING([--enable-profile], [build for profiling]))
if test "$enable_profile" = "yes"
then
  MYCXXFLAGS="-Wall -fPIC -fsigned-char -g -pg -O2 -fno-inline -pipe"
  enables="$enables (profile)"
fi

# Micro yield mode
AC_ARG_ENABLE(uyield,
  AC_HELP_STRING([--enable-uyield], [build for detecting race conditions]))
if test "$enable_uyield" = "yes"
then
  MYCPPFLAGS="$MYCPPFLAGS -UNDEBUG -D_KCUYIELD"
  enables="$enables (uyield)"
fi

# Static mode
AC_ARG_ENABLE(static,
  AC_HELP_STRING([--enable-static], [build by static linking]))
if test "$enable_static" = "yes"
then
  is_static="yes"
  enables="$enables (static)"
fi

# Disable shared object
AC_ARG_ENABLE(shared,
  AC_HELP_STRING([--disable-shared], [avoid to build shared libraries]))
if test "$enable_shared" = "no"
then
  enables="$enables (no-shared)"
fi

# Disable atomic operations
AC_ARG_ENABLE(atomic,
  AC_HELP_STRING([--disable-atomic], [build without atomic operations]))
if test "$enable_atomic" = "no"
then
  enables="$enables (no-atomic)"
fi

# Disable ZLIB compression
AC_ARG_ENABLE(zlib,
  AC_HELP_STRING([--disable-zlib], [build without ZLIB compression]))
if test "$enable_zlib" = "no"
then
  enables="$enables (no-zlib)"
else
  MYCPPFLAGS="$MYCPPFLAGS -D_MYZLIB"
fi

# Enable LZO compression
AC_ARG_ENABLE(lzo,
  AC_HELP_STRING([--enable-lzo], [build with LZO compression]))
if test "$enable_lzo" = "yes"
then
  MYCPPFLAGS="$MYCPPFLAGS -D_MYLZO"
  enables="$enables (lzo)"
fi

# Enable LZMA compression
AC_ARG_ENABLE(lzma,
  AC_HELP_STRING([--enable-lzma], [build with LZMA compression]))
if test "$enable_lzma" = "yes"
then
  MYCPPFLAGS="$MYCPPFLAGS -D_MYLZMA"
  enables="$enables (lzma)"
fi

# Messages
printf '#================================================================\n'
printf '# Configuring Kyoto Cabinet version %s%s.\n' "$PACKAGE_VERSION" "$enables"
printf '#================================================================\n'



#================================================================
# Checking Commands and Libraries
#================================================================

# C and C++ compilers
AC_PROG_CC
AC_PROG_CXX
AC_LANG(C++)

# Reset variables
if test "$GCC" != "yes"
then
  AC_MSG_WARN([another compiler except for GCC was detected])
  MYCFLAGS=""
  MYCXXFLAGS=""
fi
test -n "$CFLAGS" && MYCFLAGS="$CFLAGS $MYCFLAGS"
test -n "$CXXFLAGS" && MYCXXFLAGS="$CXXFLAGS $MYCXXFLAGS"
test -n "$CPPFLAGS" && MYCPPFLAGS="$CPPFLAGS $MYCPPFLAGS"
test -n "$LDFLAGS" && MYLDFLAGS="$LDFLAGS $MYLDFLAGS"

# Byte order
AC_C_BIGENDIAN(MYCPPFLAGS="$MYCPPFLAGS -D_MYBIGEND")

# System-depending optimization
printf 'checking for 64-bit availability... '
if printf 'main() {}' | $CC -xc -m64 -o config.tmp - >config.tmp 2>&1
then
  MYCFLAGS="-m64 $MYCFLAGS"
  MYCXXFLAGS="-m64 $MYCXXFLAGS"
  printf 'yes\n'
else
  printf 'no\n'
fi
if test "$enable_opt" != "no"
then
  printf 'checking for CPU optimization availability... '
  if printf 'main() {}' | $CC -xc -march=native -o config.tmp - >config.tmp 2>&1
  then
    MYCFLAGS="-march=native $MYCFLAGS"
    MYCXXFLAGS="-march=native $MYCXXFLAGS"
    printf 'yes\n'
  else
    printf 'no\n'
  fi
fi
printf 'checking for useless warnings... '
if printf 'main() {}' | $CC -xc \
  -Wno-unused-but-set-variable -Wno-unused-but-set-parameter -o config.tmp - >config.tmp 2>&1
then
  MYCFLAGS="$MYCFLAGS -Wno-unused-but-set-variable -Wno-unused-but-set-parameter"
  MYCXXFLAGS="$MYCXXFLAGS -Wno-unused-but-set-variable -Wno-unused-but-set-parameter"
  printf 'yes\n'
else
  printf 'no\n'
fi

# Atomic operations
if test "$enable_atomic" != "no"
then
  printf 'checking for atomic operations... '
  AC_TRY_COMPILE([], [__sync_fetch_and_add], [MYGCCATOMIC=yes], [MYGCCATOMIC=no])
  if test "$MYGCCATOMIC" = "yes"
  then
    MYCPPFLAGS="$MYCPPFLAGS -D_MYGCCATOMIC"
    printf 'yes\n'
  else
    printf 'no\n'
  fi
fi

# Underlying libraries
AC_CHECK_LIB(c, main)
AC_CHECK_LIB(m, main)
AC_CHECK_LIB(pthread, main)
AC_CHECK_LIB(rt, main)
AC_CHECK_LIB(stdc++, main)
AC_CHECK_LIB(regex, main)
if test "$enable_zlib" != "no"
then
  AC_CHECK_LIB(z, main)
fi
if test "$enable_lzo" = "yes"
then
  AC_CHECK_LIB(lzo2, main)
fi
if test "$enable_lzma" = "yes"
then
  AC_CHECK_LIB(lzma, main)
fi
AC_CHECK_LIB(kyotocabinet, main, AC_MSG_WARN([old version of Kyoto Cabinet was detected]))
MYLDLIBPATH="$LD_LIBRARY_PATH"

# Necessary headers
AC_CHECK_HEADER(stdlib.h, true, AC_MSG_ERROR([stdlib.h is required]))
AC_CHECK_HEADER(stdint.h, true, AC_MSG_ERROR([stdint.h is required]))
AC_CHECK_HEADER(unistd.h, true, AC_MSG_ERROR([unistd.h is required]))
AC_CHECK_HEADER(fcntl.h, true, AC_MSG_ERROR([fcntl.h is required]))
AC_CHECK_HEADER(dirent.h, true, AC_MSG_ERROR([dirent.h is required]))
AC_CHECK_HEADER(pthread.h, true, AC_MSG_ERROR([pthread.h is required]))
AC_CHECK_HEADER(regex.h, true, AC_MSG_ERROR([regex.h is required]))
if test "$enable_zlib" != "no"
then
  AC_CHECK_HEADER(zlib.h, true, AC_MSG_ERROR([zlib.h is required]))
fi
if test "$enable_lzo" = "yes"
then
  AC_CHECK_HEADER(lzo/lzo1x.h, true, AC_MSG_ERROR([lzo/lzo1x.h is required]))
fi
if test "$enable_lzma" = "yes"
then
  AC_CHECK_HEADER(lzma.h, true, AC_MSG_ERROR([lzma.h is required]))
fi

# Static linking
if test "$is_static" = "yes"
then
  MYCMDLDFLAGS="$MYCMDLDFLAGS -static"
  MYCMDLIBS="$MYCMDLIBS $LIBS"
fi

# As-needed linking
if uname | grep Linux >config.tmp
then
  MYLDFLAGS="$MYLDFLAGS -Wl,-rpath-link,.:/usr/local/lib:$MYLDLIBPATH"
  MYLDFLAGS="$MYLDFLAGS -Wl,--as-needed"
else
  MYCMDLIBS="$MYCMDLIBS $LIBS"
fi

# Shared libraries
if test "$enable_shared" != "no" && test "$enable_profile" != "yes"
then
  if uname | grep Darwin >config.tmp
  then
    MYLIBRARYFILES="$MYLIBRARYFILES libkyotocabinet.$MYLIBVER.$MYLIBREV.0.dylib"
    MYLIBRARYFILES="$MYLIBRARYFILES libkyotocabinet.$MYLIBVER.dylib"
    MYLIBRARYFILES="$MYLIBRARYFILES libkyotocabinet.dylib"
    MYLDLIBPATHENV="DYLD_LIBRARY_PATH"
  else
    MYLIBRARYFILES="$MYLIBRARYFILES libkyotocabinet.so.$MYLIBVER.$MYLIBREV.0"
    MYLIBRARYFILES="$MYLIBRARYFILES libkyotocabinet.so.$MYLIBVER"
    MYLIBRARYFILES="$MYLIBRARYFILES libkyotocabinet.so"
  fi
fi

# Work around of bugs of some environments
if uname | grep Darwin >config.tmp
then
  MYCFLAGS="$MYCFLAGS -Os"
  MYCXXFLAGS="$MYCXXFLAGS -Os"
fi



#================================================================
# Generic Settings
#================================================================

# Export variables
AC_SUBST(MYLIBVER)
AC_SUBST(MYLIBREV)
AC_SUBST(MYFORMATVER)
AC_SUBST(MYHEADERFILES)
AC_SUBST(MYLIBRARYFILES)
AC_SUBST(MYLIBOBJFILES)
AC_SUBST(MYCOMMANDFILES)
AC_SUBST(MYMAN1FILES)
AC_SUBST(MYDOCUMENTFILES)
AC_SUBST(MYPCFILES)
AC_SUBST(MYCFLAGS)
AC_SUBST(MYCXXFLAGS)
AC_SUBST(MYCPPFLAGS)
AC_SUBST(MYLDFLAGS)
AC_SUBST(MYCMDLDFLAGS)
AC_SUBST(MYCMDLIBS)
AC_SUBST(MYLDLIBPATH)
AC_SUBST(MYLDLIBPATHENV)
AC_SUBST(MYPOSTCMD)

# Targets
AC_OUTPUT(Makefile kyotocabinet.pc)

# Messages
printf '#================================================================\n'
printf '# Ready to make.\n'
printf '#================================================================\n'



# END OF FILE
