dnl Process this file with autoconf 2.71 or later to produce
dnl a configure script.
AC_PREREQ([2.71])
m4_define([VERSION],m4_normalize(m4_include([version])))
AC_INIT([nasm],VERSION,[https://bugs.nasm.us/],[nasm],[https://www.nasm.us/])
AC_CONFIG_SRCDIR([config/config.h.in])
AC_CONFIG_HEADERS([config/config.h])
AC_PREFIX_PROGRAM(nasm)
AC_CONFIG_AUX_DIR(autoconf/helpers)

dnl Mark where in config.h.in macros auto-generated by the configuration
dnl start; this is used to generate config/unconfig.h.
AH_BOTTOM([
/* Begin unconfig.h */])

dnl This prevents us from running Wine and thinking we are not
dnl cross-compiling when in fact we are; running Wine here is at
dnl the best very slow and doesn't buy us a single thing at all.
PA_CROSS_COMPILE

dnl Enable any available C extensions
PA_PROG_CC([[-std=c23],[-std=c17],[-std=c11],[-std=c99]])
AC_USE_SYSTEM_EXTENSIONS
dnl Some environments abuse __STRICT_ANSI__ to disable some
dnl function declarations
PA_ADD_CFLAGS([-U__STRICT_ANSI__])

dnl Options for debugging and profiling
PA_OPTION_DEBUG
PA_OPTION_PROFILING

dnl Large files
AC_SYS_LARGEFILE

dnl Abort on panic
PA_ARG_ENABLED([panic-abort],
 [call abort() on panic to trap in the debugger],
 [AC_DEFINE(ABORT_ON_PANIC)])
AH_TEMPLATE(ABORT_ON_PANIC,
[Define to 1 to call abort() on panics (internal errors), for debugging.])

dnl Externally specified zlib
AC_ARG_WITH([zlib],
[AS_HELP_STRING([--with-zlib=path], [specify location of external zlib library])],
[AS_IF([test "x$with_zlib" = xno],
 [ac_cv_search_inflate=no],
 [AS_IF([test -d "x$with_zlib"],
  [for p in "$with_zlib"/include "$with_zlib"
   do
     AS_IF([test -d "$p"], [CPPFLAGS="$CPPFLAGS -I$p"])
   done
   for p in "$with_zlib"/lib64 "$with_zlib"/lib "$with_zlib"
   do
     AS_IF([test -d "$p"], [LDFLAGS="-L$p $LDFLAGS"])
   done])])])

AS_IF([test "x$ac_cv_search_inflate" != xno],
      [AC_SEARCH_LIBS([inflate], [z zlib])])

AS_IF([test "x$ac_cv_search_inflate" = xno],
      [AC_SUBST([ZLIB],['$(ZLIBOBJ)'])
       AC_SUBST([ZLIBINC],['-I$(top_srcdir)/zlib'])])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T

dnl LLVM doesn't error out on invalid -W options unless this option is
dnl specified first.  Enable this so this script can actually discover
dnl which -W options are possible for this compiler.
PA_ADD_CFLAGS([-Werror=unknown-warning-option])

dnl Force gcc and gcc-compatible compilers treat signed integers
dnl as 2's complement
PA_ADD_CFLAGS([-fwrapv])

dnl Force clang to behave in a predictable manner, in order to make bugs
dnl possible to track down. gcc appears to have this behavior by default.
PA_ADD_CFLAGS([-ftrivial-auto-var-init=zero])

dnl Don't put things in common if we can avoid it.  We don't want to
dnl assume all compilers support common, and this will help find those
dnl problems.  This also works around an OSX linker problem.
PA_ADD_CFLAGS([-fno-common])

dnl Check for library extension
PA_LIBEXT

dnl Look for programs...
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PROG_MKDIR_P

AC_CHECK_PROGS([ASCIIDOC], asciidoc, false)
AC_CHECK_PROGS([XMLTO],    xmlto,    false)
AC_CHECK_PROGS([XZ],       xz,       false)
AC_CHECK_PROGS([PYTHON3],  [python3 python], false)

dnl Check for progs needed for manpage generation
MANPAGES=manpages
AS_IF([test x$ASCIIDOC = xfalse],
  [AC_MSG_WARN([No asciidoc package found, cannot build man pages])
   MANPAGES='']
)
AS_IF([test x"$XMLTO" = xfalse],
  [AC_MSG_WARN([No xmlto package found, cannot build man pages])
   MANPAGES='']
)
AC_SUBST([MANPAGES])

dnl Don't create .pdf.xz if there is no xz
AS_IF([test x"$XZ" = xfalse],
  [],
  [XZFILES=xzfiles])
AC_SUBST([XZFILES])

dnl Can't create NSIS package if there is no makensis
dnl ... but it only applies to a Windows target ...
dnl Note: AC_CHECK_TOOLS is supposed to check for the "plain"
dnl version of the program name, but it doesn't seem to.
AC_ARG_WITH([nsis],
[AS_HELP_STRING([[--with-nsis[=makensis]]],
 [build an install .exe using NSIS on Windows hosts])],
 [], [with_nsis=yes])
AS_IF([test x"$MAKENSIS" = x], [],
      [AS_IF([test x"$with_nsis" = xno], []
             [with_nsis="$MAKENSIS"])])

MAKENSIS=false

AS_CASE([$host],
	[*-win* | *-mingw*],
	[AS_IF([test x"$with_nsis" = xno], [],
	 [NSIS=nsis
	  AS_IF([test x"$with_nsis" = xyes],
	  [AC_CHECK_TOOL(MAKENSIS_TOOL, makensis, false)
	   MAKENSIS="$MAKENSIS_TOOL"
	   AS_IF([test x"$MAKENSIS" = xfalse],
	       [AC_CHECK_PROGS(MAKENSIS_PLAIN, makensis, false)
	         MAKENSIS="$MAKENSIS_PLAIN"])
	   AS_IF([test x"$MAKENSIS" = xfalse],
	         [AC_MSG_WARN([no makensis found, cannot build installer])
	          NSIS=''])],
	  [MAKENSIS="$with_nsis"])])])
AC_SUBST([MAKENSIS])
AC_SUBST([NSIS])

dnl Check for host compiler tools
AC_CHECK_TOOL(AR, ar)
AC_CHECK_TOOL(RANLIB, ranlib, :)
AC_CHECK_TOOL(STRIP, strip, :)

dnl
dnl NOTE: the tests for header files and library functions use constructs
dnl that create warnings on modern compilers, due to lack of prototypes,
dnl etc. Therefore, do not add the -Werror options before this.
dnl

dnl Tests which may trigger warnings on some compilers
AC_C_CONST
AC_C_INLINE
AC_C_RESTRICT

dnl Checks for header files.
AC_CHECK_INCLUDES_DEFAULT

dnl Check for variadic macro support
PA_VARIADIC_MACROS

dnl Check if the compiler supports __func__ or some equivalent
PA_HAVE_FUNC_NAME

dnl Handle Windows embedded manifests
AS_CASE([$host],
	[*-win* | *-mingw*],
	[AC_CHECK_TOOL(WINDRES, windres, false)
	 AS_IF([test x"$WINDRES" != xfalse],
	 [AC_SUBST(MANIFEST, ['win/manifest.$(O)'])])])

PA_ADD_HEADERS(string.h)
PA_ADD_HEADERS(stdarg.h)
PA_ADD_HEADERS(inttypes.h)
AC_CHECK_HEADERS(strings.h)
AC_HEADER_STDBOOL
AC_CHECK_HEADERS(stdnoreturn.h)
AC_CHECK_HEADERS(io.h)
AC_CHECK_HEADERS(fcntl.h)
AC_CHECK_HEADERS(unistd.h)
AC_CHECK_HEADERS(sys/mman.h)
AC_CHECK_HEADERS(sys/types.h)
AC_CHECK_HEADERS(sys/stat.h)
AC_CHECK_HEADERS(sys/resource.h)

dnl Checks for library functions.
AC_CHECK_FUNCS(strcasecmp stricmp)
AC_CHECK_FUNCS(strncasecmp strnicmp)
AC_CHECK_FUNCS(strsep)
AC_CHECK_FUNCS(strnlen)
AC_CHECK_FUNCS(iscntrl)
AC_CHECK_FUNCS(isascii)
AC_CHECK_FUNCS(mempcpy)
AC_CHECK_FUNCS(mempset)

AC_CHECK_FUNCS(getuid)
AC_CHECK_FUNCS(getgid)
AC_CHECK_FUNCS(getrlimit)

AC_CHECK_FUNCS(realpath)
AC_CHECK_FUNCS(canonicalize_file_name)
AC_CHECK_FUNCS(_fullpath)
AC_CHECK_FUNCS(pathconf)

AC_FUNC_FSEEKO
AC_CHECK_FUNCS([_fseeki64])
AC_CHECK_FUNCS([ftruncate _chsize _chsize_s])
AC_CHECK_FUNCS([fileno _fileno])

AC_FUNC_MMAP
AC_CHECK_FUNCS(getpagesize)
AC_CHECK_FUNCS(sysconf)

AC_CHECK_FUNCS([access _access faccessat])

PA_HAVE_FUNC(__builtin_expect,(1,1))
PA_HAVE_FUNC(__builtin_unreachable,())

PA_FUNC_SNPRINTF
PA_FUNC_VSNPRINTF
AC_CHECK_FUNCS([strlcpy])

dnl These types are POSIX-specific, and Windows does it differently...
AC_CHECK_TYPES([struct stat], [AC_CHECK_FUNCS([stat fstat])])
AC_CHECK_TYPES([struct _stati64], [AC_CHECK_FUNCS([_stati64 _fstati64])])
PA_HAVE_FUNC([S_ISREG],,[unistd.h fcntl.h sys/stat.h io.h])

dnl Check for functions that might not be declared in the headers for
dnl various idiotic reasons (mostly because of library authors
dnl abusing the meaning of __STRICT_ANSI__)
AC_CHECK_DECLS(strcasecmp)
AC_CHECK_DECLS(stricmp)
AC_CHECK_DECLS(strncasecmp)
AC_CHECK_DECLS(strnicmp)
AC_CHECK_DECLS(strsep)
AC_CHECK_DECLS(strlcpy)
AC_CHECK_DECLS(strnlen)

dnl Check for missing types
AC_TYPE_UINTMAX_T
AC_TYPE_UINTPTR_T

dnl Documentation: should we generate an uncompressed PDF?  It is
dnl about twice as big, but it can be externally compressed (e.g. with xz)
dnl and becomes significantly smaller than the original.
PA_ARG_DISABLED([pdf-compression],
  [generate an uncompressed documentation PDF],
  [PDFOPT='-nocompress'])
AC_SUBST([PDFOPT])

dnl
dnl Look for byte-swapping support and endian detection
dnl
PA_ENDIAN

dnl
dnl ilog2() building blocks
dnl
PA_HAVE_FUNC([stdc_leading_zeros],(0U),[stdbit.h])
AS_IF([test x$pa_cv_func_stdc_leading_zeros != xyes],
[PA_FIND_FUNC([__builtin_clz,(0U)],
[_BitScanReverse,(0UL),[intrin.h]])
PA_FIND_FUNC([__builtin_clzll,(0ULL)],
[_BitScanReverse64,(0ULL),[intrin.h]])])

dnl
dnl Some rather useful gcc extensions...
dnl
PA_HAVE_FUNC(__builtin_constant_p, (0))
PA_HAVE_FUNC(__builtin_choose_expr, (0,1,2))
PA_HAVE_FUNC(__builtin_prefetch, (NULL))

dnl
dnl Check for supported gcc attributes; some compilers (e.g. Sun CC)
dnl support these, but don't define __GNUC__ as they don't support
dnl some other features of gcc.
dnl
PA_COMMON_ATTRIBUTES

dnl
dnl support function sections (if available)
dnl
PA_OPTION_GC

dnl
dnl support LTO
dnl
PA_OPTION_LTO

dnl
dnl support sanitizers (if available)
dnl
PA_OPTION_SANITIZER

dnl
dnl Don't make symbols visible, there is no point and it just
dnl makes the code slower. This mainly affects ELF.
dnl
PA_ADD_CFLAGS([-fvisibility=hidden])

dnl
dnl If we have gcc, add appropriate code cleanliness options.  Do this
dnl here at the end, because configure relies on being able to use
dnl some very, very old C constructs.
dnl
PA_ADD_CFLAGS([-Wall])
PA_ARG_DISABLED([pedantic],
[disable some extra paranoid compiler warnings],
[],
[PA_ADD_CFLAGS([-W])
PA_ADD_CFLAGS([-pedantic])
])
dnl Want to turn this on at some point...
dnl PA_ADD_CFLAGS([-Wwrite-strings])
PA_ARG_ENABLED([werror],
 [compile with -Werror to error out on any warning],
 [PA_ADD_CFLAGS([-Werror])],
 [PA_ADD_CFLAGS([-Werror=implicit])
  PA_ADD_CFLAGS([-Werror=missing-braces])
  PA_ADD_CFLAGS([-Werror=return-type])
  PA_ADD_CFLAGS([-Werror=trigraphs])
  PA_ADD_CFLAGS([-Werror=pointer-arith])
  PA_ADD_CFLAGS([-Werror=comment])
  PA_ADD_CFLAGS([-Werror=vla])]
)

dnl These warnings are apparently not included in -Wall -W -pedantic
dnl for some bizarre reason. They are, however, absolutely forbidden
dnl in this code base.
PA_ADD_CFLAGS([-Werror=strict-prototypes])
PA_ADD_CFLAGS([-Werror=missing-prototypes])
PA_ADD_CFLAGS([-Werror=missing-declarations])

dnl Variadic macros are used in this code, but only under explicit guard
PA_ADD_CFLAGS([-Wvariadic-macros],[-Wno-variadic-macros])
dnl Suppress format warning on Windows targets due to their <inttypes.h>
PA_ADD_CFLAGS([-Wpedantic-ms-format],[-Wno-pedantic-ms-format])
PA_ADD_CFLAGS([-Wlong-long],[-Wno-long-long])
dnl This is needed because we intentionally expect strncpy() to fill
dnl in a zero-padded (not zero-terminated) buffer in several backends
PA_ADD_CFLAGS([-Wstringop-truncation],[-Wno-stringop-truncation])
dnl This is needed because we assume 2's-completement signed arithmetic;
dnl on compilers with gcc-like command line syntax we pass the -fwrapv
dnl option for exactly that reason.
PA_ADD_CFLAGS([-Wshift-negative-value],[-Wno-shift-negative-value])

dnl Warnings that are probabilistic based on the compiler version, and
dnl only should be used specifically when looking for opportunities to
dnl address or optimize these cases.
PA_ARG_ENABLED([suggestions],
 [compile with compiler suggestion warnings enabled],
 [PA_ADD_CFLAGS([-Wsuggest-attribute=pure])
  PA_ADD_CFLAGS([-Wsuggest-attribute=const])
  PA_ADD_CFLAGS([-Wsuggest-attribute=noreturn])
  PA_ADD_CFLAGS([-Wsuggest-attribute=format])
  PA_ADD_CFLAGS([-Wsuggest-attribute=cold])
  PA_ADD_CFLAGS([-Wsuggest-attribute=malloc])])

dnl
dnl Test compiler features. On some compilers, this can be affected
dnl by -Werror options, so run this *after* those options are added.
dnl
PA_CHECK_BAD_STDC_INLINE
PA_C_TYPEOF

AC_CONFIG_FILES([Makefile doc/Makefile misc/Makefile test/Makefile])
AC_OUTPUT
