dnl Process this file with autoconf to produce a configure script.
AC_INIT([dosemu2],m4_esyscmd(./getversion -s | tr -d '\n'),[https://github.com/dosemu2/dosemu2/issues],[dosemu2])
dnl AM_SILENT_RULES and AM_DEP_TRACK should not be used explicitly,
dnl AM_INIT_AUTOMAKE includes them
AM_SILENT_RULES
AM_DEP_TRACK
AM_MAINTAINER_MODE
AC_CONFIG_SRCDIR([src/include/emu.h])
AC_CONFIG_MACRO_DIRS([m4])
AC_PREREQ([2.64])

AC_CONFIG_HEADERS([config.hh])

dnl Checks for programs.
: ${CFLAGS=""}
AC_PROG_CC
#AC_PROG_CC_C11([AC_DEFINE(HAVE_STD_C11)])
AC_DEFINE(HAVE_STD_C11)
AC_PROG_CPP
AX_PROG_CC_FOR_BUILD
AC_USE_SYSTEM_EXTENSIONS

# termux uses PREFIX var for default prefix
AC_PREFIX_DEFAULT([${PREFIX:-/usr/local}])

AC_MSG_CHECKING([for $CC actually being clang])
if $CC -v 2>&1|grep 'clang' >/dev/null; then
  AC_MSG_RESULT([yes])
  AC_MSG_CHECKING([for clang >= 3.7])
  clang_ver=`$CC -v 2>&1 | grep version | sed 's/.*version //' | cut -d " " -f 1`
  clang_maj=`echo $clang_ver | cut -d "." -f 1`
  clang_mid=`echo $clang_ver | cut -d "." -f 2`
  if test $clang_maj -ge 3 -a $clang_mid -ge 7 -o $clang_maj -ge 4 ; then
    AC_MSG_RESULT([yes, $clang_ver])
  else
    AC_MSG_RESULT([no, $clang_ver])
    AC_MSG_ERROR([Need clang-3.7 or newer, or use gcc instead])
  fi
  DOSEMU_CFLAGS="$DOSEMU_CFLAGS -Wno-microsoft -Wno-incompatible-pointer-types \
    -Wno-address-of-packed-member"
  LDFLAGS="$LDFLAGS -Wno-unused-command-line-argument"
  use_clang="yes"

  AS="$CC"
  clang_as_as=1
  ASFLAGS="-xassembler -c -"
else
  AC_MSG_RESULT([no])
  use_clang="no"
  SO_LDFLAGS="$SO_LDFLAGS -Wl,--no-undefined"

  AC_MSG_CHECKING([for $CC actually being c++])
  if $CC -v 2>&1 | grep "g++" | grep "COLLECT_GCC" >/dev/null; then
    AC_MSG_RESULT([yes, using -fpermissive])
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fpermissive -Wno-narrowing"
  else
    AC_MSG_RESULT([no])
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fplan9-extensions -Wstrict-aliasing=3"
  fi
fi
AC_SUBST(SO_LDFLAGS)

AX_SAVE_FLAGS([ce_check])
CFLAGS="-std=gnu23"
AC_MSG_CHECKING([for constexpr])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[static constexpr const int test = 5;]])],
  [
    AC_DEFINE([CONSTEXPR], [constexpr], [constexpr support])
    AC_MSG_RESULT([yes])
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -std=gnu23"
  ], [
    AC_DEFINE([CONSTEXPR], [])
    AC_MSG_RESULT([no])
  ]
)
AX_RESTORE_FLAGS([ce_check])

CONFIG_HOST=`uname -s`
CONFIG_OS=`uname -o`
AC_SUBST(CONFIG_HOST)
machine=`$CC -dumpmachine | cut -d- -f1 | sed 's/i.86/i386/'`

if test "$machine" = "x86_64"; then
 AC_ARG_ENABLE(target-bits-32, [AS_HELP_STRING([--enable-target-bits-32],
    [build for 32bit target (multilib in case of x86_64 host)])],
  CPPFLAGS="$CPPFLAGS -m32"
  LDFLAGS="$LDFLAGS -m32"
  ASFLAGS="--32"
  with_target_bits="32"
 )
fi

if test -z "$AS"; then
  AC_PATH_PROG([AS], [as])
  if test -z "$AS"; then
    AC_PATH_PROG([AS], [clang])
    if test -z "$AS"; then
      AC_MSG_ERROR(as not found)
    fi
    AC_MSG_NOTICE(Using clang as as...)
    clang_as_as=1
    ASFLAGS="-xassembler -c -"
  fi
fi
if test -n "$CROSS_PREFIX"; then
  AC_PATH_PROG([XAS], [${CROSS_PREFIX}as])
  if test -z "$XAS"; then
    AC_MSG_ERROR([${CROSS_PREFIX}as not found, CROSS_PREFIX is wrong])
  fi
else
  AC_PATH_PROGS([XAS], [i686-linux-gnu-as i686-unknown-linux-gnu-as i586-suse-linux-as i386-elf-as])
fi
if test -z "$XAS"; then
  AC_PATH_PROG([XAS], [x86_64-linux-gnu-as])
  if test -z "$XAS"; then
    if test \( "$machine" = "i386" -o "$machine" = "x86_64" \) -a "$CONFIG_HOST" != "Darwin"; then
      XAS="$AS"
      if test "$clang_as_as" = "1"; then
        XASFLAGS="$ASFLAGS -m32"
      else
        XASFLAGS="--32"
      fi
    else
      AC_PATH_PROG([XAS], [clang])
      if test -n "$XAS"; then
        XASFLAGS="-x assembler -target i686-unknown-linux-gnu -c -"
      else
        AC_MSG_ERROR([386 assembler not found, please install clang or binutils-i686-linux-gnu])
      fi
    fi
  else
    XASFLAGS="--32"
  fi
fi

AS_LDFLAGS=""
AC_PATH_PROGS([LD], [ld ld.lld])
if test -z "$LD"; then
  AC_MSG_ERROR(ld not found)
fi
if test -n "$CROSS_PREFIX"; then
  AC_PATH_PROG([AS_LD], [${CROSS_PREFIX}ld])
  if test -z "$AS_LD"; then
    AC_MSG_ERROR([${CROSS_PREFIX}ld not found, CROSS_PREFIX is wrong])
  fi
else
  AC_PATH_PROGS([AS_LD], [i686-linux-gnu-ld i686-unknown-linux-gnu-ld i586-suse-linux-ld i386-elf-ld x86_64-linux-gnu-ld ld.lld ld])
  if test "$CONFIG_HOST" = "Darwin" -a `basename "$AS_LD"` = "ld"; then
    AC_MSG_ERROR([Please install lld or i386-elf-binutils])
  fi
  if test `basename "$AS_LD"` = "ld.lld"; then
    # newer lld (>=21) needs this, for older it's optional
    AS_LDFLAGS="$AS_LDFLAGS --image-base 0"
  fi
fi
if test -z "$AS_LD"; then
  AS_LD="$LD"
fi

AC_PATH_PROG([OBJCOPY], [objcopy])
if test -n "$OBJCOPY"; then
  O_VER=`$OBJCOPY -V | head -n 1 | grep "GNU objcopy"`
  if test -z "$O_VER"; then
    AC_MSG_WARN([rejecting BSD objcopy])
    OBJCOPY=""
  fi
fi
if test -z "$OBJCOPY"; then
  AC_PATH_PROG([LOBJCOPY], [llvm-objcopy])
  if test -z "$LOBJCOPY" -a "$CONFIG_HOST" = "Darwin"; then
    brew_llvm_bin=`brew --prefix llvm`/bin
    AC_PATH_PROG([LOBJCOPY], [llvm-objcopy], [], [$PATH${PATH_SEPARATOR}$brew_llvm_bin])
    if test -z "$LOBJCOPY"; then
      AC_MSG_ERROR([llvm-objcopy not found, please install llvm])
    fi
  fi
  if test -z "$LOBJCOPY"; then
    AC_MSG_ERROR(objcopy not found)
  fi
  OBJCOPY="$LOBJCOPY"
fi

# cross-objcopy is used to convert elf to binary.
# We can't use --oformat=binary on linker level, because with
# objcopy we copy only the section .text and not anything else.
if test -n "$CROSS_PREFIX"; then
  AC_PATH_PROG([XOBJCOPY], [${CROSS_PREFIX}objcopy])
  if test -z "$XOBJCOPY"; then
    AC_MSG_ERROR([${CROSS_PREFIX}objcopy not found, CROSS_PREFIX is wrong])
  fi
else
  AC_PATH_PROGS([XOBJCOPY], [i686-linux-gnu-objcopy i686-unknown-linux-gnu-objcopy i586-suse-linux-objcopy x86_64-linux-gnu-objcopy llvm-objcopy objcopy])
fi
if test -z "$XOBJCOPY"; then
  XOBJCOPY="$OBJCOPY"
fi
AC_SUBST(AS)
AC_SUBST(XAS)
AC_SUBST(AS_LD)
AC_SUBST(XOBJCOPY)
AC_PROG_INSTALL
AC_PROG_LEX([noyywrap])
if test -z "$LEX" -o "$LEX" = ":"; then
  AC_MSG_WARN(Your system doesn't seem to have lex or flex available.)
  AC_MSG_ERROR(Install lex or flex and retry.)
fi

AC_PROG_LN_S
AC_MSG_CHECKING([if $LN_S supports -T])
if test -n "`$LN_S --help 2>&1 | grep -- -T`"; then
  AC_MSG_RESULT([yes])
  LN_SFT="$LN_S -f -T"
else
  AC_MSG_RESULT([no, using -F])
  LN_SFT="$LN_S -f -F"
fi
AC_SUBST(LN_SFT)

AC_CHECK_PROGS([MAKE], [gmake make])
if test -z "$MAKE"; then
  AC_MSG_ERROR(You don't have make installed)
fi
# export MAKE to run from scripts
export MAKE="$MAKE"
#AC_PROG_AR
AC_CHECK_TOOL([AR], [ar], [:])
if test -z "$AR" -o "$AR" = ":"; then
  AC_MSG_ERROR(You don't have ar installed)
fi
AC_PROG_RANLIB
if test -z "$RANLIB" -o "$RANLIB" = ":"; then
  AC_MSG_ERROR(You don't have ranlib installed)
fi
AC_PROG_YACC
dnl The bison problem
if test -z "`echo $YACC | grep bison`" ; then
  AC_MSG_WARN( )
  AC_MSG_WARN(Your system doesn't seem to have bison available.)
  AC_MSG_ERROR(Install bison and retry.)
fi
AC_PROG_GREP
AC_PROG_EGREP
AC_PROG_SED
AC_PROG_AWK
if test "$AWK" != "gawk" ; then
  AC_MSG_ERROR(Install gawk and retry.)
fi
AC_CHECK_PROGS([PKG_CONFIG], [pkg-config])
if test -z "$PKG_CONFIG"; then
  AC_MSG_ERROR(Install pkg-config and retry.)
fi

dnl Checks for libraries.

dnl Checks for header files.
dnl need largefile check here, dont remove, it defines magic macros
AC_SYS_LARGEFILE

dnl Checks for library functions.
AC_MSG_CHECKING([for static_assert support])
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <assert.h>]], [[
  static_assert(sizeof(char) == 1, "sizeof doesn't work");
]])], [
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_STATIC_ASSERT)
], [
  AC_MSG_RESULT([no])
])

AC_MSG_CHECKING([for assignable stderr])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
	#include <stdio.h>
	int main(void)
	{
	    stderr = NULL;
	    return 0;
	}
    ])],
    [
	AC_MSG_RESULT([yes])
	AC_DEFINE(HAVE_ASSIGNABLE_STDERR)
    ],
    [
	AC_MSG_RESULT([no])
    ]
)

AC_MSG_CHECKING([for optreset])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
	#include <unistd.h>
	int main(void)
	{
	    optreset = 1;
	    return 0;
	}
    ])],
    [
	AC_MSG_RESULT([yes])
	AC_DEFINE(HAVE_OPTRESET)
    ],
    [
	AC_MSG_RESULT([no])
    ]
)

AC_CHECK_LIB(rt, shm_open)
dnl below defines HAVE_xxx so needed even if AC_CHECK_LIB() succeeded
AC_CHECK_FUNCS([shm_open fopencookie sigtimedwait closefrom])
AC_CHECK_FUNCS([setxattr], [
    USE_XATTRS=1
    AC_SUBST(USE_XATTRS)
])
AC_CHECK_FUNCS([timerfd_create], [
    USE_EVTIMER_FD=1
    AC_SUBST(USE_EVTIMER_FD)
], [AC_CHECK_FUNCS([kqueue], [
    USE_EVTIMER_FD=1
    AC_SUBST(USE_EVTIMER_FD)
])])
AC_CHECK_FUNCS([fls])
AC_CHECK_FUNCS([statfs])
AC_CHECK_FUNCS([memrchr])
AC_CHECK_FUNCS([mempcpy])

AC_CHECK_FUNCS([memfd_create], [
  # memfd_create is linkable on android, but may not be declared, so re-check
  AC_CHECK_DECLS([memfd_create],,, [[#include <sys/mman.h>]])
])

AC_CHECK_DECLS([F_OFD_SETLK], [
    USE_OFD_LOCKS=1
    AC_SUBST(USE_OFD_LOCKS)
  ],, [[#include <fcntl.h>]]
)
AC_CHECK_DECLS([MADV_POPULATE_WRITE, MADV_COLD],,,
  [[#include <sys/mman.h>]])
AC_CHECK_DECLS([MAP_32BIT],,, [[#include <sys/mman.h>]])
AC_CHECK_DECLS([TIOCOUTQ],,, [[#include <sys/ioctl.h>]])

AC_CHECK_TYPES([__float80],, [
    if test "$machine" != "i386" -a "$machine" != "x86_64"; then
      USE_SOFTFLOAT=1
      AC_SUBST(USE_SOFTFLOAT)
    fi
  ]
)
AC_CHECK_TYPES([_Float128])
AC_TYPE_LONG_DOUBLE_WIDER
AC_CHECK_TYPES([greg_t],,, [[#include <ucontext.h>]])
AC_CHECK_MEMBERS(struct ethhdr.h_dest,,, [[#include <netinet/if_ether.h>]])

AC_CHECK_FUNCS([strlcpy strlcat], [NEED_LIBBSD=0], [NEED_LIBBSD=1])
AC_SUBST(NEED_LIBBSD)

dnl for Haiku
AC_SEARCH_LIBS([accept], [network], [], [
  AC_MSG_ERROR([Unable to find BSD sockets support])
])

dnl Here is where we do our stuff

AC_ARG_WITH(confdir, [AS_HELP_STRING([--with-confdir=dir],
  [directory suffix under sysconfdir. default: dosemu])],
  confdir="$withval", confdir="dosemu")
AC_ARG_WITH(x11fontdir, [AS_HELP_STRING([--with-x11fontdir=dir],
  [directory to install the VGA X11 font. default: ${datadir}/dosemu/Xfonts])],
  x11fontdir="$withval", x11fontdir="${datadir}/dosemu/Xfonts")
AC_ARG_WITH(ttffontdir, [AS_HELP_STRING([--with-ttffontdir=dir],
  [directory to install the ttf font. default: ${datadir}/fonts/oldschool])],
  ttffontdir="$withval", ttffontdir="${datadir}/fonts/oldschool")
AC_ARG_WITH(fdtarball, [AS_HELP_STRING([--with-fdtarball=file],
  [path to FreeDOS tarball])],
  fdtarball="$withval")
AC_ARG_WITH(username, [AS_HELP_STRING([--with-username=username],
  [user for dosemu2 to create in a system. default: dosemu2])],
  username="$withval", username="dosemu2")

# Hard-code this. It is evaluated at run-time as <binary_dir>../lib/dosemu,
# so we do not use libdir here (which can be lib64 or something unrelated)
plugindir="\${prefix}/lib/dosemu"

AC_SUBST(confdir)
AC_SUBST(plugindir)
AC_SUBST(x11fontdir)
AC_SUBST(ttffontdir)
AC_SUBST(syshdimagedir)
AC_SUBST(fdtarball)
AC_SUBST(username)

AC_DEFINE_DIR([SYSCONFDIR], [sysconfdir], [ System config dir ])
AC_DEFINE_DIR([CONFSUBDIR], [confdir], [ dosemu-specific config subdir ])
# use PREFIXDIR instead of PREFIX to not corrupt PREFIX var used on Termux
AC_DEFINE_DIR([PREFIXDIR], [prefix], [ prefix ])
AC_DEFINE_DIR([LIBEXECDIR], [libexecdir], [ Where extra executables are placed to ])
AC_DEFINE_DIR([DATADIR], [datadir], [ Where program data placed to ])
AC_DEFINE_DIR([SYSTEM_XFONTS_PATH], [x11fontdir], [ Directory for x11 fonts ])

DOSEMU_CPPFLAGS="$DOSEMU_CPPFLAGS -imacros emu_config.h"
DOSEMU_CFLAGS="$DOSEMU_CFLAGS -Wall -Wstrict-prototypes -Wmissing-declarations \
-fms-extensions -pthread -Wcast-qual -Wwrite-strings -Wundef"
AX_CHECK_COMPILE_FLAG([-Waddress-of-packed-member],
  [DOSEMU_CFLAGS="$DOSEMU_CFLAGS -Wno-address-of-packed-member"],, [-Werror])
AX_CHECK_COMPILE_FLAG([-Wstring-plus-int],
  [DOSEMU_CFLAGS="$DOSEMU_CFLAGS -Wno-string-plus-int"],, [-Werror])
DOSEMU_LDFLAGS="-pthread"
if test "x$prefix" != xNONE; then
  DOSEMU_LDFLAGS="$DOSEMU_LDFLAGS -Wl,-rpath,$libdir"
fi

AC_CHECK_LIB(m, pow)
AC_CHECK_DECLS([exp10f],,, [[#include <math.h>]])

AC_ARG_ENABLE(dlplugins,
              AS_HELP_STRING([--disable-dlplugins], [do NOT use dynamically loaded plugins]))
if test "$enable_dlplugins" != "no"; then
              AC_MSG_NOTICE(Using dynamically loaded plugins...)
              AC_DEFINE(USE_DL_PLUGINS)
              USE_DL_PLUGINS=1
              DOSBIN_LDFLAGS="-rdynamic"
              AC_CHECK_LIB(dl, dlopen)
else
              USE_DL_PLUGINS=0
              AC_MSG_NOTICE(Not using dynamically loaded plugins...)
fi
AC_SUBST(USE_DL_PLUGINS)

SIG_PROTO_PFX="__attribute__((no_instrument_function))"
dnl check for stack protector disabling functionaity
AC_MSG_CHECKING([for __attribute__((optimize("no-stack-protector")))])
AX_SAVE_FLAGS([attr_check])
CFLAGS="-Wall -Werror -fstack-protector-all"
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
	void foo(void);
	__attribute__((optimize("no-stack-protector")))
	void foo(void)
	{
	}
    ])],
    [
	AC_MSG_RESULT([supported])
	SIG_PROTO_PFX="$SIG_PROTO_PFX __attribute__((optimize(\"no-stack-protector\")))"
    ],
    [
	AC_MSG_RESULT([unsupported])
	DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fno-stack-protector"
    ])
AX_RESTORE_FLAGS([attr_check])
AC_DEFINE_UNQUOTED(SIG_PROTO_PFX, $SIG_PROTO_PFX)

AC_CHECK_FUNC(globfree,, [
  AC_CHECK_LIB(android-glob, globfree,, [
    AC_MSG_ERROR(globfree function missing.)
  ])
])

dnl Check whether we have pthreads and whether to use it
AC_CHECK_LIB(pthread, pthread_create,, [
  dnl see if the func is provided nevertheless (not needing -lpthread)
  AC_CHECK_FUNC(pthread_create,, [
    AC_MSG_ERROR([No libpthread found, please install glibc-devel package])
  ]
)])

AC_CHECK_FUNCS([pthread_getname_np pthread_setname_np])
AC_CHECK_FUNCS([pthread_attr_setsigmask_np pthread_setattr_default_np])

# sem_open is also in pthread lib
AC_CHECK_FUNC(sem_open,, [
  AC_MSG_ERROR(sem_open function missing.)
])
AC_MSG_CHECKING([for working sem_open])
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <semaphore.h>]], [[
  sem_open("", 0);
]])], [
  AC_MSG_RESULT([yes])
], [
  AC_MSG_RESULT([no])
  AC_CHECK_LIB(android-posix-semaphore, libandroid_sem_open)
])

AC_CHECK_HEADERS([scsi/sg.h linux/cdrom.h])
# sys/io.h is present on emscripten, but is dummy
if test "$machine" != "wasm32"; then
  AC_CHECK_HEADERS([sys/io.h], [
    AC_CHECK_DECLS([outb],,, [[#include <sys/io.h>]])
    AC_CHECK_DECLS([iopl],,, [[#include <sys/io.h>]])
    AC_CHECK_DECLS([ioperm],,, [[#include <sys/io.h>]])
  ])
  AC_CHECK_HEADERS([ucontext.h])
  AC_CHECK_HEADERS([asm/ucontext.h],,, [
    #include <ucontext.h>
    #include <signal.h>
  ])

  AC_CHECK_FUNCS([mremap])
fi
# CHECK_DECLS cannot be done conditionally (like, not for wasm32)
# because they define macro to 0 or 1, and undefined state does not suit.
AC_CHECK_DECLS([MREMAP_MAYMOVE],,, [[#include <sys/mman.h>]])

AC_CHECK_HEADERS([netipx/ipx.h linux/ipx.h netpacket/packet.h])
AC_CHECK_HEADERS([netinet/if_ether.h net/ethernet.h])
AC_CHECK_HEADERS([linux/signal.h],,, [#include <signal.h>])
AC_CHECK_HEADERS([sys/soundcard.h], [
  USE_OSS=1
  AC_SUBST(USE_OSS)
])
AC_CHECK_HEADERS([sanitizer/asan_interface.h])
AC_CHECK_HEADERS([linux/landlock.h], [
  AC_ARG_ENABLE(landlock, [AS_HELP_STRING([--disable-landlock],
        [disable landlock security restrictions])])
  if test "$enable_landlock" != "no"; then
    AC_DEFINE(USE_LANDLOCK)
    USE_LANDLOCK=1
    AC_SUBST(USE_LANDLOCK)
  fi
])

DATE_FMT="%F %T %z"
test -n "$SOURCE_DATE_EPOCH" || SOURCE_DATE_EPOCH=`date +%s`
CONFIG_TIME=`date -u -d "@$SOURCE_DATE_EPOCH" "+$DATE_FMT" 2>/dev/null || date -u -r "$SOURCE_DATE_EPOCH" "+$DATE_FMT" 2>/dev/null || date -u "+$DATE_FMT"`
AC_DEFINE_UNQUOTED(CONFIG_HOST, "$CONFIG_HOST")

dnl enable EXPERIMENTAL stuff
AC_ARG_ENABLE(experimental,
              AS_HELP_STRING([--enable-experimental], [enable configuration of EXPERIMENTAL stuff]))
if test "$enable_experimental" = "yes"; then
  AC_MSG_NOTICE(Allowing EXPERIMENTAL stuff to be configured...);
  AC_DEFINE([EXPERIMENTAL], 1, [ Define this to enable experimental stuff ])
else
  AC_MSG_NOTICE(EXPERIMENTAL stuff disabled...);
fi

dnl try to hook in available plug-ins
AC_ARG_ENABLE(plugins,
              AS_HELP_STRING([--enable-plugins=list],
              [comma-separated list of plugins, use + to append]))
def_plugins=`cat $srcdir/plugin_list | sed 's/$/,/g' | tr -d '\n' | sed 's/,$//'`
if test -z "$enable_plugins"; then
    enable_plugins="$def_plugins"
fi
if test "${enable_plugins#\+}" != "$enable_plugins"; then
    enable_plugins="$def_plugins,${enable_plugins#\+}"
fi
if test "$enable_plugins" = "no"; then
    enable_plugins=""
fi

AC_ARG_WITH(plugin-options,
    [AS_HELP_STRING([--with-plugin-options=PLUGIN,OPTS],
        [Pass options OPTS to plugin PLUGIN])],
    [
        plu=`echo $withval |cut -d "," -f 1`
        val=`echo $withval |cut -d "," -f 2`
        export ${plu}_OPTS="$val"
    ]
)

SOLIB=1
AC_ARG_ENABLE(solib,
    [AS_HELP_STRING([--disable-solib], [Disable dosemu2 solib])])
if test "$enable_solib" = "no"; then
  AC_MSG_NOTICE([Disabling build of a solib...])
  SOLIB=0
fi
AC_SUBST(SOLIB)

DIRS_ALLOW=
AC_ARG_WITH([dj64-lib-dir], AS_HELP_STRING([--with-dj64-lib-dir=DIR],
        [use alternate dj64dev dir]),
    [ DIRS_ALLOW="$DIRS_ALLOW $withval" ])
AC_ARG_WITH([fdpp-lib-dir], AS_HELP_STRING([--with-fdpp-lib-dir=DIR],
        [use alternate fdpp dir]),
    [ DIRS_ALLOW="$DIRS_ALLOW $withval" ])
if test -n "$DIRS_ALLOW"; then
  AC_DEFINE_UNQUOTED(DIRS_ALLOW,["$DIRS_ALLOW"],
    [ Directories to allow an access to ])
fi

if ! test -f Makefile.conf.in; then
  AC_MSG_NOTICE([Populating makefiles...])
  wd=`pwd`
  abssrcdir=`cd $srcdir && pwd`
  if test "${wd#$abssrcdir}" != "$wd" ; then
    builddir=".${wd#$abssrcdir}"
  else
    builddir="$wd"
  fi
  mkdir -p `(cd $abssrcdir; find doc man etc src test -type d -print)`
  for i in `(cd $abssrcdir; find . -path $builddir -prune \
      -o -name 'test-*' -prune \
      -o -name '.flatpak-builder/*' -prune \
      -o -name 'bin' -prune -o -name 'commands' -prune \
      -o -name Makefile -print -o -name '*.mak' -print \
      -o -name '*.bat' -print -o -name '*.sys' -print \
      -o -name '*.cpi' -print \
    )`; do
    echo  $LN_S -f $abssrcdir/${i#./} $i
    $LN_S -f $abssrcdir/${i#./} $i
  done
  for i in `(cd $abssrcdir; find man -name '*.1')`; do
    cp $abssrcdir/$i $i
  done
fi
AC_MSG_NOTICE([Creating plug-in hooks...])
PLUGINSUBDIRS=
ST_PLUGINSUBDIRS=
CONFFILES=
$srcdir/scripts/mkpluginhooks clean
for i in $(echo $enable_plugins | tr ',' ' '); do
    if test -z "$i"; then continue; fi
    if $srcdir/scripts/plugctl.sh $i yes --disable-option-checking $ac_configure_args; then
        PLUGINSUBDIRS="$PLUGINSUBDIRS plugin/$i"
        if ! $EGREP 'NO_LIB' $srcdir/src/plugin/$i/Makefile >/dev/null; then
          if ! $EGREP 'USE_DL_PLUGINS' $srcdir/src/plugin/$i/Makefile >/dev/null || test "$enable_dlplugins" = "no"; then
            ST_PLUGINSUBDIRS="$ST_PLUGINSUBDIRS plugin/$i"
          fi
          if test -f $srcdir/src/plugin/$i/Makefile.conf.in; then
            CONFFILES="$CONFFILES plugin/$i/Makefile.conf"
          fi
        fi
    fi
done
# remove duplicate entries
PLUGINSUBDIRS=`echo $PLUGINSUBDIRS | tr ' ' '\n' | sort | uniq | paste -sd " " -`
PLUGINS=`echo $PLUGINSUBDIRS | $SED 's/plugin\//\n\t/g'`

X_PLU=`echo $PLUGINS | $SED 's/ /\n/g' | $EGREP -x 'X'`
if test -z "$X_PLU"; then USE_X=0; else USE_X=1; fi
AC_SUBST(USE_X)

FDPP_PLU=`echo $PLUGINS | $SED 's/ /\n/g' | $EGREP -x 'fdpp'`
AC_ARG_ENABLE(fdpp,
  [AS_HELP_STRING([--disable-fdpp], [compile without fdpp support])],,
  enable_fdpp="yes"
)

DJ64_PLU=`echo $PLUGINS | $SED 's/ /\n/g' | $EGREP -x 'dj64'`
AC_ARG_ENABLE(dj64,
  [AS_HELP_STRING([--disable-dj64], [compile without dj64 support])],,
  enable_dj64="yes"
)

SR_PLU=`echo $PLUGINS | $SED 's/ /\n/g' | $EGREP -x 'searpc'`
AC_ARG_ENABLE(searpc,
  [AS_HELP_STRING([--disable-searpc], [compile without searpc support])],,
  enable_searpc="yes"
)

AC_ARG_ENABLE(system_wa,
  AS_HELP_STRING([--disable-system-wa], [compile without system-specific
  work-arounds (mostly for linux kernel) that are not needed on this system]))
AS_IF([test "x$enable_system_wa" = "xno"], [
  AC_MSG_NOTICE(Disabling system-specific work-arounds)
  AC_DEFINE(DISABLE_SYSTEM_WA)
  AS_IF([test -z "$WARN_OUTDATED_WA" -a -n "$WARN_UNDISABLED_WA"], [
    AC_DEFINE(WARN_UNDISABLED_WA)])
])
AS_IF([test -n "$WARN_OUTDATED_WA"], [
  AC_MSG_NOTICE(Enabling compile-time warnings for outdated work-arounds)
  AC_DEFINE(WARN_OUTDATED_WA)
])

dnl Do compilation for GDB
HAVE_LIBBFD=0
AC_ARG_ENABLE(debug,
               AS_HELP_STRING([--enable-debug], [compile with debug info]))
if test "$enable_debug" != "no"; then
  AC_MSG_NOTICE(Compiling with debug info...)
  DOSEMU_CFLAGS="$DOSEMU_CFLAGS -ggdb3"
  if test "$machine" = "wasm32"; then
    DOSEMU_LDFLAGS="$DOSEMU_LDFLAGS -g3 -gsource-map"
  fi
  if test "$use_clang" = "yes" ; then
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fdebug-macro"
  fi
  AC_ARG_ENABLE(xbacktrace, AS_HELP_STRING([--enable-xbacktrace],
    [enable extended backtrace functionality]))
  if test "$enable_xbacktrace" = "yes"; then
    AC_CHECK_HEADER(bfd.h, [
      AC_CHECK_LIB(z, inflate)
      dnl check iberty before bfd - order matters
      AC_CHECK_LIB(iberty, lrealpath)
      AC_CHECK_LIB(bfd, bfd_init)
      if test "$ac_cv_lib_bfd_bfd_init" = "yes"; then
        HAVE_LIBBFD=1
        AC_DEFINE(HAVE_LIBBFD)
      fi
    ])
    if test "$HAVE_LIBBFD" = "1"; then
      AC_MSG_NOTICE(Enabling extended backtrace functionality)
    else
      AC_MSG_WARN(libbfd not available, no extended backtrace functionality)
    fi
  fi
  AC_CHECK_HEADER([execinfo.h], [
    AC_DEFINE(HAVE_EXECINFO)
    if test "$HAVE_LIBBFD" != "1"; then
      DOSEMU_LDFLAGS="$DOSEMU_LDFLAGS -rdynamic"
    fi
    AC_CHECK_FUNCS([backtrace backtrace_symbols])
  ])
else
  AC_MSG_NOTICE(Compiling without debug info...)
fi

AC_ARG_ENABLE(profile,
               AS_HELP_STRING([--enable-profile], [compile with profile info]))

AC_ARG_ENABLE(asan, [AS_HELP_STRING(--enable-asan, [enable address sanitizer])])
AC_ARG_ENABLE(lsan, [AS_HELP_STRING(--enable-lsan, [enable leak sanitizer])])
AC_ARG_ENABLE(tsan, [AS_HELP_STRING(--enable-tsan, [enable thread sanitizer])])
AC_ARG_ENABLE(ubsan, [AS_HELP_STRING(--enable-ubsan, [enable UB sanitizer])])

AC_SUBST(OPTIONALSUBDIRS)
AC_SUBST(REQUIRED)
AC_SUBST(PLUGINSUBDIRS)
AC_SUBST(ST_PLUGINSUBDIRS)
AC_SUBST(CONFFILES)
RELEASE_DATE=`cd $srcdir && ./getversion -d`
AC_SUBST(RELEASE_DATE)

dnl CPU emulator
AC_ARG_ENABLE(cpuemu, [AS_HELP_STRING([--disable-cpuemu],
        [do NOT compile with optional x86 emulation code])])
if test "$enable_cpuemu" != "no"; then
  AC_MSG_NOTICE(Compiling with x86 emulator)
  AC_DEFINE(X86_EMULATOR)
  X86_EMULATOR=1
  AC_SUBST(X86_EMULATOR)

  if test "$machine" = "i386" -o "$machine" = "x86_64"; then
   if test "$machine" = "i386" -o "$ac_cv_have_decl_MAP_32BIT" = "yes"; then
    AC_ARG_ENABLE(cpuemu_jit, [AS_HELP_STRING([--disable-cpuemu-jit],
        [do NOT compile with optional x86 JIT])])
    if test "$enable_cpuemu_jit" != "no"; then
      AC_MSG_NOTICE(Compiling with x86 JIT)
      AC_DEFINE(X86_JIT)
      X86_JIT=1
      AC_SUBST(X86_JIT)
    fi
   fi
  fi
fi

AC_ARG_WITH(target_cpu, [AS_HELP_STRING([--with-target-cpu=CPU],
  [use the specified target CPU. default=auto])])
if test "$with_target_cpu" = ""; then
  AC_MSG_NOTICE(Compiling with default target CPU...)
  target_cpu=""
else
  AC_MSG_NOTICE(Compiling with specified target CPU...)
  target_cpu=$with_target_cpu
fi

if test "$machine" = "i386" -o "$with_target_bits" = "32" -o \
	"$enable_profile" = "yes"; then
    AC_MSG_NOTICE(Compiling as non-PIE...)
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fno-pie"
    AX_CHECK_COMPILE_FLAG([-no-pie],
      [DOSEMU_LDFLAGS="$DOSEMU_LDFLAGS -no-pie"])
else
    AC_MSG_NOTICE(Compiling as PIE for $machine...)
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fpie"
    DOSBIN_LDFLAGS="$DOSEMU_LDFLAGS -pie"
fi
AC_SUBST(DOSBIN_LDFLAGS)

DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fpic"

if test "$CONFIG_HOST" = "Linux"; then
  if test "$machine" = "i386" -o "$machine" = "x86_64"; then
    AC_CHECK_HEADER([linux/kvm.h], [
      AC_DEFINE(USE_KVM)
      KVM=1
      AC_SUBST(KVM)
    ])
  fi
fi

PLUGIN_LDFLAGS="-shared"
if test "$CONFIG_HOST" = "Darwin"; then
   PLUGIN_LDFLAGS="$PLUGIN_LDFLAGS -Wl,-undefined,dynamic_lookup"
fi
AC_SUBST(PLUGIN_LDFLAGS)

if test "$machine" != "wasm32" -a "$machine" != "armv7hl" \
    -a "$machine" != "armv7l" -a "$machine" != "riscv64"; then
  AC_ARG_ENABLE(mcontext,
               AS_HELP_STRING([--disable-mcontext], [disable fast context switch]))
  if test "$enable_mcontext" != "no"; then
    AC_MSG_NOTICE([Enabling fast context switching...])
    AC_DEFINE(MCONTEXT)
    MCONTEXT=1
    AC_SUBST(MCONTEXT)
  fi
fi

if test "$CONFIG_OS" != "Android"; then
 if test "$enable_asan" = "yes"; then
  AX_CHECK_LINK_FLAG([-fsanitize=address], [
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fsanitize=address"
    # working around this bug:
    # https://github.com/google/sanitizers/issues/934
    # Linking to libstdc++ seems to be the cure.
    LDFLAGS="$LDFLAGS -fsanitize=address -lstdc++"
    AC_MSG_NOTICE(enabling address sanitizer)
  ])
 fi
 if test "$enable_lsan" = "yes"; then
  AX_CHECK_LINK_FLAG([-fsanitize=leak], [
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fsanitize=leak"
    LDFLAGS="$LDFLAGS -fsanitize=leak"
    AC_MSG_NOTICE(enabling leak sanitizer)
  ])
 fi
 if test "$enable_tsan" = "yes"; then
  AX_CHECK_LINK_FLAG([-fsanitize=thread], [
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fsanitize=thread"
    LDFLAGS="$LDFLAGS -fsanitize=thread"
    AC_MSG_NOTICE(enabling thread sanitizer)
  ])
 fi
 if test "$enable_ubsan" = "yes"; then
  AX_CHECK_LINK_FLAG([-fsanitize=undefined], [
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -fsanitize=undefined -fno-sanitize=alignment"
    LDFLAGS="$LDFLAGS -fsanitize=undefined"
    AC_MSG_NOTICE(enabling UB sanitizer)
  ])
 fi
fi
if test "$enable_profile" = "yes"; then
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -pg -no-pie"
    LDFLAGS="$LDFLAGS -pg -no-pie"
    AC_MSG_NOTICE(enabling profiling)
fi

AC_ARG_ENABLE(optimization,
               AS_HELP_STRING([--disable-optimization], [disable optimization]))
if test "$enable_optimization" = "no" ; then
  if test "$enable_debug" = "yes"; then
    if test "$machine" != "wasm32"; then
      DOSEMU_CFLAGS="$DOSEMU_CFLAGS -Og"
      AC_MSG_NOTICE(optimization set to -Og)
    else
      # emscripten doesn't support -Og
      DOSEMU_CFLAGS="$DOSEMU_CFLAGS -O0"
      AC_MSG_NOTICE(optimization set to -O0)
    fi
  else
    # simx86 is too slow on gcc without -O
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -O0"
    AC_MSG_NOTICE(optimization set to -O0)
  fi
else
  DOSEMU_CFLAGS="$DOSEMU_CFLAGS -O2"
  AC_MSG_NOTICE(optimization set to -O2)
  AC_ARG_ENABLE(lto, AS_HELP_STRING([--disable-lto], [disable LTO]))
  if test "$enable_lto" != "no" ; then
    DOSEMU_CFLAGS="$DOSEMU_CFLAGS -flto=auto"
    # use LDFLAGS as this opt is needed for plugins too
    LDFLAGS="$LDFLAGS -flto=auto -O2 -fpic"
  fi
fi

DOSEMU_CFLAGS="${DOSEMU_CFLAGS} ${OPT} ${PIPE}"
DOSEMU_CPPFLAGS="${DOSEMU_CPPFLAGS} -MD -DCFLAGS_STR=\"$DOSEMU_CFLAGS $CFLAGS\""
DOSEMU_VERSION=`cd $srcdir && ./getversion -b`
AS_LDFLAGS="$AS_LDFLAGS -melf_i386"

AC_SUBST(XASFLAGS)
AC_SUBST(ASFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(AS_LDFLAGS)
AC_SUBST(DOSEMU_CFLAGS)
AC_SUBST(DOSEMU_CPPFLAGS)
AC_SUBST(DOSEMU_LDFLAGS)
AC_SUBST(LIBS)
AC_SUBST(HAVE_LIBBFD)
AC_SUBST(HAVE_LIBBSD)
AC_SUBST(DOSEMU_VERSION)

cmds_rev=3
cmds_ver=0.$cmds_rev
cmdsuff=$PACKAGE_TARNAME-cmds-$cmds_ver
AC_DEFINE_UNQUOTED(CMDS_SUFF, "$cmdsuff")
AC_DEFINE_UNQUOTED(CMDS_REV, $cmds_rev)
AC_SUBST(cmdsuff)

dnl Print some warnings (if neccessary)
if test "$enable_fdpp" != "no" -a -z "$FDPP_PLU"; then
  AC_MSG_ERROR([fdpp not available!])
fi
if test "$enable_dj64" != "no" -a -z "$DJ64_PLU"; then
  AC_MSG_ERROR([dj64dev not available!])
fi
if test "$enable_searpc" != "no" -a -z "$SR_PLU"; then
  AC_MSG_ERROR([searpc not available!])
fi

dnl Create output files. If you add new ones, please do it in order.
man_files=`cd $srcdir && find man -name '*.in' | sed 's/\.in$//'`
AC_DEFUN([AC_DATAROOTDIR_CHECKED])
adl_RECURSIVE_EVAL([$bindir], [e_bindir])
adl_RECURSIVE_EVAL([$datadir], [e_datadir])
AC_SUBST([e_bindir])
AC_SUBST([e_datadir])
AC_CONFIG_FILES([Makefile.conf $man_files etc/dosemu.desktop])

AC_OUTPUT

AC_MSG_RESULT()
AC_MSG_RESULT(Enabling plugins: $PLUGINS)
VID_PLU=`echo $PLUGINS | $SED 's/ /\n/g' | $EGREP -x 'sdl|X|term|sdl1|sdl3'`
if test -z "$VID_PLU"; then
  AC_MSG_RESULT()
  AC_MSG_WARN([No video plugins available!])
fi
AUD_PLU=`echo $PLUGINS | $SED 's/ /\n/g' | $EGREP -x 'sdl|libao|sdl1|sdl3'`
if test -z "$AUD_PLU"; then
  AC_MSG_RESULT()
  AC_MSG_WARN([No audio plugins available!])
fi
if test -z "$FDPP_PLU"; then
  AC_MSG_RESULT()
  AC_MSG_WARN([fdpp not available!])
fi
if test -z "$DJ64_PLU"; then
  AC_MSG_RESULT()
  AC_MSG_WARN([dj64dev not available!])
fi
if test -z "$SR_PLU"; then
  AC_MSG_RESULT()
  AC_MSG_WARN([searpc not available!])
fi

AH_TEMPLATE([HAVE_ASSIGNABLE_STDERR],
[ Define if stderr is not const ])

AH_TEMPLATE([HAVE_OPTRESET],
[ Define if optreset is available ])

AH_TEMPLATE([CONFIG_HOST],
[ Define the host for which Dosemu is configured ])

AH_TEMPLATE([SIG_PROTO_PFX],
[ Define the signal handling function prototype prefix ])

AH_TEMPLATE([HAVE_STD_C11],
[Define this if your compiler supports c11])

AH_TEMPLATE([HAVE_STATIC_ASSERT],
[Define this if your glibc defines static_assert])

AH_TEMPLATE(HAVE_LIBBFD,
[Define this if you have binutils-devel installed])

AH_TEMPLATE(HAVE_LIBBSD,
[Define this if you have bsd-devel installed])

AH_TEMPLATE(HAVE_EXECINFO,
[Define this if you have execinfo.h in libc])

AH_TEMPLATE([USE_DL_PLUGINS],
[ DEFINE this, if you want dynamically loaded plugins ])

AH_TEMPLATE([USE_LANDLOCK],
[ DEFINE this, if you want to use landlock security restrictions ])

AH_TEMPLATE([X86_EMULATOR],
[ Define this to use the X86 CPU emulator ])

AH_TEMPLATE([X86_JIT],
[ Define this to use the X86 JIT ])

AH_TEMPLATE([USE_KVM],
[ Define this to enable kvm support ])

AH_TEMPLATE([MCONTEXT],
[ Define this to enable libmcontext use ])

AH_TEMPLATE([DISABLE_SYSTEM_WA],
[ Define this to disable system-specific work-arounds that are
unneeded on your system ])

AH_TEMPLATE([WARN_UNDISABLED_WA],
[ Define this to enable compile-time warnings for system-specific
work-arounds that were failed to disable ])

AH_TEMPLATE([WARN_OUTDATED_WA],
[ Define this to enable compile-time warnings for outdated
system-specific work-arounds ])

AH_TEMPLATE([CMDS_SUFF],
[ Commands directory suffix ])

AH_TEMPLATE([CMDS_REV],
[ Commands revision num ])
