m4_define([mednafen_version], ["1.32.1.2"])
m4_define([mednafen_version_numeric], [0x00103201])

AC_INIT([mednafen],[mednafen_version])

AC_CANONICAL_HOST
AC_CONFIG_HEADERS([config.h])
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE

AC_PROG_CC
AC_PROG_CXX

AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(long long)
AC_CHECK_SIZEOF(__int64)
AC_CHECK_SIZEOF(void *)
AC_CHECK_SIZEOF(size_t)
AC_CHECK_SIZEOF(ptrdiff_t)
AC_CHECK_SIZEOF(off_t)

AC_DEFINE([_LARGEFILE64_SOURCE], [1],
	[Define for largefile support through extra functions.])

AC_DEFINE([_LARGEFILE_SOURCE], [1],
	[Define for fseeko and ftello on some hosts.])

AC_DEFINE([HAVE_MKDIR], [], [Define for src/NativeVFS.cpp.])

AC_DEFINE([PACKAGE], ["AC_PACKAGE_NAME"], [mednafen package name.])

AC_DEFINE_UNQUOTED([MEDNAFEN_VERSION],
	[mednafen_version],
	[Mednafen version definition.])

AC_DEFINE_UNQUOTED([MEDNAFEN_VERSION_NUMERIC],
	[mednafen_version_numeric],
	[Mednafen version numeric definition.])

AC_ARG_ENABLE([avx],
	AS_HELP_STRING([--enable-avx], [Enable GNU-style AVX inline assembly]))

AS_IF([test "x$enable_avx" = "xyes"], [
	AC_MSG_CHECKING([for GNU-style AVX inline assembly])
	AC_LINK_IFELSE(
	[
		AC_LANG_SOURCE(
		[
			int main(void) {
				asm volatile("vcvtss2si %xmm6,%ecx\n\tvhaddps %ymm7,%ymm7,%ymm7\n\tvzeroupper\n\t");
				return 0;
			}
		])
	],
	[
		AC_MSG_RESULT([yes])
		AC_DEFINE([HAVE_INLINEASM_AVX], [1],
			[Define if GNU-style AVX inline assembly is supported.])
	],
	[
		AC_MSG_ERROR([GNU-style AVX inline assembly not supported])
	])
])

case "$host_cpu" in
	x86_64|amd64)
		AC_DEFINE([ARCH_X86], [1],
			[Define for 32-bit or 64-bit x86 architectures.])
		AC_DEFINE([ARCH_X86_64], [1],
			[Define for 64-bit x86 architectures.])
	;;
	i*86)
		AC_DEFINE([ARCH_X86], [1],
			[Define for 32-bit or 64-bit x86 architectures.])
		AC_DEFINE([ARCH_X86_32], [1],
			[Define for 32-bit x86 architectures.])
	;;
	powerpc*)
		AC_DEFINE([ARCH_POWERPC], [1],
			[Define for PPC architectures.])
	;;
esac

if ! expr x"$host" : 'x.*-mingw*' > /dev/null &&
		! expr x"$host" : 'x.*djgpp' > /dev/null; then
	AC_SEARCH_LIBS(pthread_create, [pthread])
	AC_CHECK_FUNCS(pthread_create sem_init, [],
		AC_MSG_ERROR([*** pthreads not found!]))
	AC_CHECK_FUNCS(sem_timedwait sem_timedwait_monotonic sem_clockwait \
		sem_clockwait_np pthread_condattr_setclock pthread_setname_np \
		pthread_set_name_np pthread_getaffinity_np \
		pthread_setaffinity_np pthread_cond_timedwait_relative_np)
	AC_CHECK_HEADERS(pthread.h sched.h)
	AC_CHECK_HEADERS(pthread_np.h, [], [], [
		#include <pthread.h>
		])
	AC_MSG_CHECKING([type for pthread affinity setting])
	AC_COMPILE_IFELSE(
	[
		AC_LANG_SOURCE(
		[
			#include <pthread.h>

			#if defined HAVE_SCHED_H
			#include <sched.h>
			#endif

			#if defined(HAVE_PTHREAD_NP_H)
			#include <pthread_np.h>
			#endif

			int main(void) {
				cpu_set_t c;
				int result;

				CPU_ZERO(&c);

				result = pthread_setaffinity_np(pthread_self(), sizeof(c), &c);
				result = pthread_getaffinity_np(pthread_self(), sizeof(c), &c);

				return result;
			}
		])
	],
		[
			AC_MSG_RESULT([cpu_set_t])
			AC_DEFINE([PTHREAD_AFFINITY_NP], [cpu_set_t],
				[Define to CPU set type if we are compiling with pthreads affinity setting support.])
		],
			AC_COMPILE_IFELSE(
			[
				AC_LANG_SOURCE(
				[
					#include <pthread.h>

					#if defined HAVE_SCHED_H
					#include <sched.h>
					#endif

					#if defined(HAVE_PTHREAD_NP_H)
					#include <pthread_np.h>
					#endif

					int main(void) {
						cpuset_t c;
						int result;

						CPU_ZERO(&c);

						result = pthread_setaffinity_np(pthread_self(), sizeof(c), &c);
						result = pthread_getaffinity_np(pthread_self(), sizeof(c), &c);

						return result;
					}
				])
			],
				[
					AC_MSG_RESULT([cpuset_t])
					AC_DEFINE([PTHREAD_AFFINITY_NP], [cpuset_t],
						[Define to CPU set type if we are compiling with pthreads affinity setting support.])
				],
				[
					AC_MSG_RESULT([unknown])
				]
			)
		)
fi

AC_OUTPUT
