dnl Process this file with autoconf to produce a configure script.

# Copyright (c) 2005-2011  Günther Brammer
# Copyright (c) 2005, 2008  Peter Wortmann
# Copyright (c) 2006  Julian Raschke
# Copyright (c) 2010  Armin Burgmeier
# Copyright (c) 2010  Martin Plicht
# Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de

# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

AC_PREREQ([2.67])
AC_INIT([openclonk], [m4_esyscmd([sed -n 's/SET(C4XVER1\s\+\(.\+\))/\1/ p' Version.txt | tr -d '\n' | tr -d '\r'
    ]).m4_esyscmd([sed -n 's/SET(C4XVER2\s\+\(.\+\))/\1/ p' Version.txt | tr -d '\n' | tr -d '\r'
    ]).m4_esyscmd([sed -n 's/SET(C4XVER3\s\+\(.\+\))/\1/ p' Version.txt | tr -d '\n' | tr -d '\r'])])
AC_COPYRIGHT([©2005-2011 Günther Brammer])
AC_CONFIG_SRCDIR([/src/C4Include.h])
AC_CONFIG_AUX_DIR([autotools])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_CANONICAL_HOST
dnl foreign to tell automake to shut up,
dnl and subdir-objects because it makes the Makefile smaller.
dnl no-define because PACKAGE and VERSION are not used
AM_INIT_AUTOMAKE([foreign tar-ustar nostdinc no-define 1.10])
m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])

AC_PROG_CXX
AC_LANG([C++])
AX_CHECK_COMPILE_FLAG([-std=gnu++0x], [CXX="${CXX} -std=gnu++0x"], [])

AC_MSG_CHECKING([for rvalue references])
AC_COMPILE_IFELSE([void f(struct D&&); int main() { return 0; }], [
    AC_DEFINE([HAVE_RVALUE_REF], [], [C++ Compiler has rvalue references, a C++0x feature.])
    AC_MSG_RESULT([yes])
  ], [
    AC_MSG_RESULT([no])
    AX_PROG_CXX_REFTOTEMP
  ])

AC_MSG_CHECKING([for nullptr])
AC_COMPILE_IFELSE([int main() { void *d = nullptr; }], [
    AC_DEFINE([HAVE_NULLPTR], [], [Define to 1 if you have support for nullptr.])
    AC_MSG_RESULT([yes])
  ], [AC_MSG_RESULT([no])])

AC_MSG_CHECKING([for static_assert])
AC_COMPILE_IFELSE([int main() { static_assert(true, \"\"); }], [
    AC_DEFINE([HAVE_STATIC_ASSERT], [], [Define to 1 if your compiler supports static_assert])
    AC_MSG_RESULT([yes])
  ], [AC_MSG_RESULT([no])])

AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_RANLIB
# Automake 1.12 breaks if this isn't run unconditionally
AC_PROG_OBJCXX

case $host in
  *-*-mingw32* | *-*-cygwin* | *-*-windows*)
    win32=true; osx=false;;
  *-*-darwin*)
    win32=false; osx=true;;
  *)
    win32=false; osx=false;;
esac

# Detect Wine. Unfortunately, at the moment winegcc does not meet the demands
AC_CHECK_HEADER([windows.h], [win32=true], [], [[ ]])

AM_CONDITIONAL(WIN32, [test $win32 = true])
AM_CONDITIONAL(MACOSX, [test $osx = true])

# various used headers
dnl the whitespace is there to prevent AC_INCLUDES_DEFAULT
AC_CHECK_HEADERS([stdint.h unistd.h poll.h sys/file.h sys/stat.h sys/types.h locale.h sys/socket.h signal.h langinfo.h sys/eventfd.h sys/timerfd.h sys/inotify.h], , , [[ ]])
AC_CHECK_HEADERS([io.h direct.h share.h natupnp.h dbghelp.h], [], [], [[#include <windows.h>]])
# iconv
AX_ICONV
# vasprintf is a GNU extension
AC_CHECK_FUNCS(vasprintf)
# so is execinfo.h - and some systems have the header despite the functions not being in the c library
AC_CHECK_HEADER(execinfo.h)
if test "x$ac_cv_header_execinfo_h" = xyes; then
  AC_CHECK_FUNC(backtrace, [], [
    AC_CHECK_LIB(execinfo, backtrace, [
      CLONK_LIBS="-lexecinfo $CLONK_LIBS"
    ], [
      ac_cv_header_execinfo_h=no
    ])
  ])
fi
if test "x$ac_cv_header_execinfo_h" = xyes; then
  AC_DEFINE(HAVE_EXECINFO_H, 1, [The backtrace function is declared in execinfo.h and works])
fi

AC_ARG_ENABLE([debug],
  [AS_HELP_STRING([--enable-debug],[debugging options [default=no]])],
  , [enable_debug=no])
if test $enable_debug = yes; then
  AC_DEFINE([_DEBUG], 1, [compile with debug options])
else
  AC_DEFINE([NDEBUG], 1, [compile without debug options])
fi

AC_ARG_ENABLE([debugrec],
  [AS_HELP_STRING([--enable-debugrec],[write additional debug control to records [default=no]])],
    , [enable_debugrec=no])
if test $enable_debugrec = yes; then
  AC_DEFINE([DEBUGREC], 1, [Activate DebugRecs])
fi

AC_ARG_ENABLE([console],
  [AS_HELP_STRING([--enable-console],[compile as pure console application [default=no]])],
  , [enable_console=no])

AC_ARG_ENABLE([sound],
  [AS_HELP_STRING([--enable-sound],[compile with sound support [default=yes]])],
  , [if test $enable_console = yes; then enable_sound=no; else enable_sound=yes; fi])

# GTK+
GTK2_REQUIRED="glib-2.0 >= 2.24 gtk+-2.0 >= 2.20"
GTK3_REQUIRED="glib-2.0 >= 2.32 gtk+-3.0 >= 3.4"
PKG_PROG_PKG_CONFIG
AC_MSG_CHECKING([which gtk+ version to compile against])
AC_ARG_WITH([gtk],
  [AS_HELP_STRING([--with-gtk=2.0|3.0],[which gtk+ version to compile against [default: auto]])],
  [case "$with_gtk" in
     2.0|3.0) ;;
     no) ;;
     *) AC_MSG_ERROR([invalid gtk version specified]) ;;
   esac],
  [AS_IF([test $enable_console = no],
    [PKG_CHECK_EXISTS([$GTK3_REQUIRED],
      [with_gtk=3.0],
      [PKG_CHECK_EXISTS([$GTK2_REQUIRED],
        [with_gtk=2.0],
        [with_gtk=no])])],
    [with_gtk=no])])
AC_MSG_RESULT([$with_gtk])
AS_IF([test $with_gtk != no],[
  case "$with_gtk" in
  2.0) GTK_REQUIRED="$GTK2_REQUIRED"
       MAPE_REQUIRED="$GTK2_REQUIRED gthread-2.0 gtksourceview-2.0"
       ;;
  3.0) GTK_REQUIRED="$GTK3_REQUIRED"
       MAPE_REQUIRED="$GTK3_REQUIRED gthread-2.0 gtksourceview-3.0"
       ;;
  esac
  PKG_CHECK_MODULES(GTK, [$GTK_REQUIRED])
  AC_ARG_ENABLE([mape],
    [AS_HELP_STRING([--enable-mape],[build mape landscape.txt editor [default: auto]])], [],
    [PKG_CHECK_EXISTS([$MAPE_REQUIRED], [enable_mape=yes], [enable_mape=no])])
  AS_IF([test $enable_mape != no],
    [PKG_CHECK_MODULES(MAPE, [$MAPE_REQUIRED])])
  AC_DEFINE([WITH_GLIB], 1, [Glib])
  AC_DEFINE([WITH_DEVELOPER_MODE], 1, [Developer mode])
])
AM_CONDITIONAL(GTK, [test $with_gtk != no])
AM_CONDITIONAL(MAPE, [test $enable_mape != no])

# OpenAL
AC_ARG_WITH([openal],
  [AS_HELP_STRING([--with-openal],[compile with openal support [default=no]])],
  , [with_openal=no])
if test $with_openal = yes; then
  PKG_CHECK_MODULES(OPENAL, [vorbis vorbisfile openal freealut])
  AC_DEFINE([USE_OPEN_AL], 1, [OpenAL sound])
  if test $enable_sound = no; then
    AC_MSG_ERROR([--with-openal cannot be used with --disable-sound.])
  fi
fi

# SDL
with_sdl_mixer=no
AC_ARG_WITH([sdl],
  AS_HELP_STRING([--with-sdl], [Use SDL @<:@default=yes (no for win32)@:>@]),
  , [if test $win32 = true; then with_sdl=no; else if test $enable_console = yes; then with_sdl=no; else with_sdl=yes; fi fi])
if test $with_sdl = yes; then
  # Check for SDL
  SDL_VERSION=1.2.0
  AM_PATH_SDL($SDL_VERSION,
    [AC_DEFINE(HAVE_SDL, 1, [Define to 1 if you have SDL.])],
    [AC_MSG_ERROR([libSDL not found.])])
  AC_SUBST(SDL_CFLAGS)
  AC_SUBST(SDL_LIBS)
  if test $enable_sound = yes && test $with_openal = no; then
    # Check for SDL_mixer library
    AC_CHECK_LIB(SDL_mixer, Mix_OpenAudio,
      [AC_DEFINE(HAVE_LIBSDL_MIXER,1,[Define to 1 if you have SDL_mixer.])
      SDL_LIBS="-lSDL_mixer $SDL_LIBS"
      with_sdl_mixer=yes],
      [AC_MSG_ERROR([SDL_mixer not found.])],
      [$SDL_LIBS])
  fi
  AC_ARG_ENABLE(sdlmainloop,
    [AS_HELP_STRING([--enable-sdlmainloop],[use SDL instead of X11 or Win32 [default=no]])],
    , [if test $osx = true; then enable_sdlmainloop=yes; else enable_sdlmainloop=no; fi ])
else
  enable_sdlmainloop=no
fi
if test $enable_sdlmainloop = yes; then
  with_x=no
  AC_DEFINE(USE_SDL_MAINLOOP, 1, [Define to 1 if SDL is used for the main loop])
fi
AM_CONDITIONAL(SDL_MAIN_LOOP, [test $enable_sdlmainloop = yes])

if test $enable_sound = yes; then
  AC_ARG_ENABLE([mp3],
    [AS_HELP_STRING([--enable-mp3],[enable mp3 support [default=no]])],
    , [enable_mp3=no])
  if test $enable_mp3 = yes; then
    AC_DEFINE([USE_MP3], 1, [MP3 music])
  fi
fi

if test $enable_console = no; then
  if test $win32 = true; then
    AC_SUBST(WINDOWS_CFLAGS, "-mwindows")
  fi
else
  # console mode
  AC_DEFINE([USE_CONSOLE], 1, [dedicated server mode])
  with_x=no
  enable_sdlmainloop=no
  VL_LIB_READLINE
fi
AM_CONDITIONAL([CONSOLE], [test $enable_console = yes])

# Check for boost
AX_BOOST_BASE([1.40.0], [], [
  AC_MSG_ERROR([Boost not found.])
])

# Check for libjpeg
AC_CHECK_LIB(jpeg, jpeg_read_header, [
  CLONK_LIBS="-ljpeg $CLONK_LIBS"
], [
  AC_MSG_ERROR([libjpeg not found.])
])

# Check for libpng
AC_CHECK_LIB(png, png_read_image, [
  CLONK_LIBS="-lpng $CLONK_LIBS"
], [
  AC_MSG_ERROR([libpng not found.])
], [-lz])

# Check for libz
AC_CHECK_LIB(z, deflate, [
  Z_LIBS="-lz $Z_LIBS"
], [
  AC_MSG_ERROR([libz not found.])
])

# Check for librt
AC_CHECK_LIB(rt, clock_gettime, [
  LDADD="-lrt $LDADD"
], [])

# Check for libfreetype
if test $enable_console = no; then
  PKG_CHECK_MODULES(FREETYPE, freetype2, ,[
    if test "$cross_compiling" = no; then
      AC_CHECK_PROG(FREETYPE_CONFIG, freetype-config, freetype-config)
    fi
    if test $FREETYPE_CONFIG; then
      AC_SUBST(FREETYPE_CFLAGS, [`$FREETYPE_CONFIG --cflags`])
      AC_SUBST(FREETYPE_LIBS, [`$FREETYPE_CONFIG --libs`])
    else
      AC_MSG_ERROR([Freetype not found.])
    fi
  ])
  AC_DEFINE(HAVE_FREETYPE, 1, [Define to 1 if you have FreeType2.])
fi

# X11, Xrandr
AC_PATH_XTRA
if test $have_x = yes; then
  AC_DEFINE(USE_X11, 1, [Define to 1 if the X Window System is used])
  CLONK_LIBS="$X_LIBS $CLONK_LIBS"
  AC_CHECK_HEADERS([X11/keysym.h X11/extensions/Xrandr.h], ,
    [AC_MSG_ERROR([A required X11 header was not found.])], [[#include <X11/Xlib.h>]])
  AC_CHECK_LIB(X11, XOpenDisplay, [CLONK_LIBS="-lX11 $CLONK_LIBS"],
    [AC_MSG_ERROR([libX11 not found.])], [$X_LIBS])
  AC_CHECK_LIB(Xrandr, XRRQueryExtension, [CLONK_LIBS="-lXrandr $CLONK_LIBS"],
    [AC_MSG_ERROR([libXrandr not found.])], [$X_LIBS])
fi

# OpenGL
AC_ARG_WITH([gl],
  [AS_HELP_STRING([--with-gl],[compile with opengl support [default=yes]])],
  , [if test $enable_console = yes; then with_gl=no; else with_gl=yes; fi])
if test $with_gl = yes; then
  if test $osx = true; then
    CLONK_LIBS="-framework OpenGL $CLONK_LIBS"
    PKG_CHECK_MODULES(GLEW, [glew], [CLONK_LIBS="$GLEW_LIBS $CLONK_LIBS"], [AC_MSG_ERROR([glew not found.])])
  elif test $win32 = true; then
    AC_CHECK_LIB(opengl32, main, [CLONK_LIBS="-lopengl32 $CLONK_LIBS"],
      [AC_MSG_ERROR([opengl32 not found.])])
    AC_CHECK_LIB(glu32, main, [CLONK_LIBS="-lglu32 $CLONK_LIBS"],
      [AC_MSG_ERROR([glu32 not found.])])
    AC_CHECK_LIB(glew32, main, [CLONK_LIBS="-lglew32 $CLONK_LIBS"],
      [AC_MSG_ERROR([glew not found.])])
    AC_SUBST([GLEW_CFLAGS],["-DGLEW_STATIC"])
  else
    AC_CHECK_LIB(GL, glBegin, [CLONK_LIBS="-lGL $CLONK_LIBS"],
      [AC_MSG_ERROR([libGL not found.])])
    AC_CHECK_LIB(GLU, gluOrtho2D, [CLONK_LIBS="-lGLU $CLONK_LIBS"],
      [AC_MSG_ERROR([libGLU not found.])])
    AC_CHECK_LIB(GLEW, glewInit, [CLONK_LIBS="-lGLEW $CLONK_LIBS"],
      [AC_MSG_ERROR([glew not found.])])
  fi
  AC_DEFINE([USE_GL], 1, [OpenGL graphics])
fi

# DirectX
AC_ARG_WITH([directx],
  [AS_HELP_STRING([--with-directx],[compile with directx support [default=no]])],
  , [with_directx="no"])
if test "$with_directx" = yes; then
  AC_CHECK_LIB(d3d9, main, [CLONK_LIBS="-ld3d9 $CLONK_LIBS"],
    [AC_MSG_ERROR([d3d9 not found.])])
  AC_DEFINE([USE_DIRECTX], 1, [DirectX graphics])
fi

# UPnP
AC_ARG_WITH([upnp],
  [AS_HELP_STRING([--with-upnp],[compile with upnp support [default=no]])],
  , [with_upnp=no])
AS_IF([test $with_upnp = yes], [
  AM_CONDITIONAL(NATUPNP, [test "$ac_cv_header_natupnp_h" = yes])
  PKG_CHECK_MODULES(LIBUPNP, [libupnp], [have_libupnp=yes], [have_libupnp=no])
], [
  AM_CONDITIONAL(NATUPNP, [false])
  have_libupnp=no
])
AM_CONDITIONAL(LIBUPNP, [test "$have_libupnp" = yes])

# Automatic Update
AC_ARG_ENABLE([autoupdate],
  [AS_HELP_STRING([--disable-autoupdate],[support for automatic engine updates [default=yes]])],
  , [enable_autoupdate="yes"])
AS_IF([test "$enable_autoupdate" = yes], [
  AC_DEFINE([WITH_AUTOMATIC_UPDATE], 1, [Enable automatic update system])
])
AM_CONDITIONAL(AUTOUPDATE, [test "$enable_autoupdate" = yes])

if test $win32 = false; then
  # pthread
  AX_PTHREAD( , [AC_MSG_ERROR([No pthread support.])])
  AC_SUBST(PTHREAD_LIBS)
  AC_SUBST(PTHREAD_CFLAGS)
else
  # Windows
  for WIN32LIB in gdi32 comdlg32 ws2_32
  do
    AC_CHECK_LIB([$WIN32LIB], main, [CLONK_LIBS="-l$WIN32LIB $CLONK_LIBS"], [AC_MSG_ERROR([$WIN32LIB not found.])])
  done

  AC_CHECK_LIB([winmm], main, [LDADD="-lwinmm $LDADD"], [AC_MSG_ERROR([winmm not found.])])

  AC_CHECK_LIB([vfw32], main, [
    CLONK_LIBS="-lvfw32 $CLONK_LIBS"
    AC_DEFINE([HAVE_VFW32], 1, [ ])
    ], [])

  AC_CHECK_LIB([dbghelp], main, [AS_IF([test "x$ac_cv_header_dbghelp_h" = xyes], [
    CLONK_LIBS="-ldbghelp $CLONK_LIBS"
    AC_DEFINE([HAVE_DBGHELP], 1, [Generate minidumps on crash])
    ])], [])

  # windres
  AC_CHECK_TOOL(WINDRES, windres, no)
  if test $WINDRES = no; then
    AC_MSG_ERROR([windres not found.])
  fi

  # ASLR/DEP
  LDADD="-Wl,--nxcompat -Wl,--dynamicbase $LDADD"
fi

AC_SUBST(LDADD)
AC_SUBST(CLONK_LIBS)
AC_SUBST(Z_LIBS)

# precompiled headers and -Wextra
AC_MSG_CHECKING([whether we are using a GNU C++ compiler version 4.1 or newer])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [[#if defined(__GNUC__) && (((__GNUC__ >= 4) && (__GNUC_MINOR__ >= 1)) || (__GNUC__ > 4))
// all ok
#else
       choke me
#endif
]])], RECENT_GCC="yes", RECENT_GCC="no")
AC_MSG_RESULT([$RECENT_GCC])
AM_CONDITIONAL(RECENT_GCC, [test $RECENT_GCC = yes])

AC_OUTPUT

echo "Configuration:
  CFLAGS:	$CFLAGS
  CXXFLAGS:	$CXXFLAGS
  Debug:	$enable_debug
  Debugrec:	$enable_debugrec
  Pure console:	$enable_console
  Autoupdate:	$enable_autoupdate
  DirectX:	$with_directx
  OpenGL:	$with_gl
  Nat/LibUPnP:	$ac_cv_header_natupnp_h/$have_libupnp
  Sound:	$enable_sound
  SDL_Mixer:	$with_sdl_mixer
  OpenAL:	$with_openal
  GTK+: 	$with_gtk
  X11:  	$have_x"

if test "$enable_sdlmainloop" = yes; then echo "  SDL:  	mainloop";
else echo "  SDL:  	$with_sdl"; fi

