# $Id: configure.ac 30153 2026-06-25 13:27:28Z yeti-dn $
dnl Process this file with autoconf to produce a configure script.

#############################################################################
# Setup and versions.
m4_define([gwy_version_major],[3])
m4_define([gwy_version_minor],[11])
m4_define([gwy_version_string],[gwy_version_major.gwy_version_minor])
AC_INIT([Gwyddion],[gwy_version_string],[klapetek@gwyddion.net])
# Use -release for unstable version libs instead of -version-info, because
# we *do* break compatibility each version
LIBRARY_RELEASE=gwy_version_major.gwy_version_minor
PACKAGE_URL="https://gwyddion.net/"
AC_PREREQ(2.71)
AC_CONFIG_SRCDIR([gwyddion/gwyddion.c])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_FILES(\
  Makefile \
  m4/Makefile \
  build-aux/Makefile \
  libgwyddion/Makefile \
  libgwyddion/libgwyddion3.pc \
  tests/Makefile \
  tests/runtest.sh \
  libgwyddion/version.h \
  libgwyui/Makefile \
  libgwyui/libgwyui3.pc \
  libgwyapp/Makefile \
  libgwyapp/libgwyapp3.pc \
  gwyddion/Makefile \
  modules/Makefile \
  modules/cmap/Makefile \
  modules/file/Makefile \
  modules/file/hdf5/Makefile \
  modules/graph/Makefile \
  modules/image/Makefile \
  modules/pygwy/Makefile \
  modules/synth/Makefile \
  modules/tools/Makefile \
  modules/volume/Makefile \
  modules/xyz/Makefile \
  pixmaps/Makefile \
  pixmaps/src/Makefile \
  po/Makefile.in \
  data/Makefile \
  data/glmaterials/Makefile \
  data/gradients/Makefile \
  utils/Makefile \
  utils/bundler \
  utils/mkosxlauncher \
  thumbnailer/Makefile \
  devel-docs/Makefile \
  devel-docs/libgwyapp/Makefile \
  devel-docs/libgwyapp/releaseinfo.xml \
  devel-docs/libgwyddion/Makefile \
  devel-docs/libgwyddion/releaseinfo.xml \
  devel-docs/libgwyui/Makefile \
  devel-docs/libgwyui/releaseinfo.xml \
  gwyddion3.spec:data/gwyddion3.spec.in \
  data/gwyddion3.nsit \
  data/mingw-gwyddion3-libs.spec
)
AC_CONFIG_HEADERS([config.h:build-aux/config.h.in])

AM_INIT_AUTOMAKE([1.16 gnu check-news silent-rules dist-xz no-dist-gzip nostdinc -Wcross -Wobsolete -Woverride -Wsyntax -Wunsupported])
AM_MAINTAINER_MODE
AM_SILENT_RULES([yes])
AC_REQUIRE_AUX_FILE([tap-driver.sh])
AC_REQUIRE_AUX_FILE([config.rpath])

GWY_VERSION_MAJOR=gwy_version_major
GWY_VERSION_MINOR=gwy_version_minor
GWY_VERSION_STRING='"gwy_version_string"'
AC_SUBST(GWY_VERSION_MAJOR)
AC_SUBST(GWY_VERSION_MINOR)
AC_SUBST(GWY_VERSION_STRING)
AC_SUBST(LIBRARY_RELEASE)

#############################################################################
# Compilers, linkers, etc.
AC_CANONICAL_HOST
AC_PROG_CC
AC_MSG_CHECKING([for C standard])
AC_MSG_RESULT([$ac_prog_cc_stdc])
case "$ac_prog_cc_stdc" in
  no|c89)
    AC_MSG_ERROR([C compiler does not support C99.])
    ;;
  *)
    : # Probably nothing to do here?
    ;;
esac
AC_PROG_CXX
AC_USE_SYSTEM_EXTENSIONS
LT_INIT([disable-static,win32-dll,dlopen])

# Check if the C++ compiler actually compiles. Autoconf sets a fallback "g++"
# when no C++ compiler is found, expecting subsequent tests will fail.
# But do not have any nautral subsequent C++ tests, so make an artificial one.
AC_LANG_PUSH([C++])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])],[have_cxx=yes],[have_cxx=no])
AC_LANG_POP([C++])
AC_MSG_CHECKING([if the C++ compiler is a real thing])
AC_MSG_RESULT([$have_cxx])
AM_CONDITIONAL([HAVE_CXX],[test "x$have_cxx" != xno])

#############################################################################
# Basic tools.
AC_PROG_INSTALL
AC_CHECK_PROGS([XSLTPROC],[xsltproc],[:])
GTK_DOC_CHECK(1.32)
GWY_CHECK_EU_NM

#############################################################################
# 32bit/64bit
# TODO: Only support 64bit
AC_CHECK_SIZEOF([void*])
if test $ac_cv_sizeof_voidp = 4; then
  arch_32bit=yes
else
  arch_32bit=no
fi

#############################################################################
# Win32.
AC_MSG_CHECKING([for native Microsoft Windows])
case "$host_os" in
  mingw*)
    os_mswin=yes
    HOST_CFLAGS="-mms-bitfields"
    HOST_LDFLAGS="-Wl,-subsystem,windows"
    case "$host_cpu" in
      *64) MSWIN_BITS=64;;
      *) MSWIN_BITS=32;;
    esac
    ;;
  *)
    os_mswin=no
    ;;
esac
AC_MSG_RESULT([$os_mswin])
AM_CONDITIONAL([OS_MSWIN],[test "$os_mswin" = "yes"])
AC_SUBST(MSWIN_BITS)

#############################################################################
# Darwin.
AC_MSG_CHECKING([for Darwin])
case "$host_os" in
  darwin*)
    os_darwin=yes
    ;;
  *)
    os_darwin=no
    ;;
esac
AC_MSG_RESULT([$os_darwin])
AM_CONDITIONAL([OS_DARWIN],[test "$os_darwin" = "yes"])

#############################################################################
# Home installation.
# If a user installs Gwyddion into a subdirectory of home, various XDG stuff
# will not end up where it is useful.  Try to put it there.
AC_ARG_ENABLE(home-installation,
              AS_HELP_STRING([--enable-home-installation],
                             [Install desktop integration files into your home]),
                             [],
                             [enable_home_installation=no])
AM_CONDITIONAL([HOME_INSTALLATION],[test "x$enable_home_installation" = xyes])
if test "x$enable_home_installation" = xyes; then
  xdg_target=home
else
  xdg_target=prefix
fi

# Do not set the actual XDG variable here!  It influences things.  Set *_DIR.
if test "x$XDG_DATA_HOME" = x; then
  XDG_DATA_HOME_DIR="$HOME/.local/share"
else
  XDG_DATA_HOME_DIR="$XDG_DATA_HOME"
fi
AC_SUBST(XDG_DATA_HOME_DIR)

#############################################################################
# Module symbol resolution and bundling.
# We try not to link modules with libraries and let symbols resolve through
# app, because it means faster app startup and much faster compilation.  But
# on some systems that isn't possible.
AC_ARG_ENABLE(library-bloat,
              AS_HELP_STRING([--enable-library-bloat],
                             [Make linking very slow]),
                             [],
                             [enable_library_bloat=no])
if test "x$enable_library_bloat" != xyes \
   && test "x$allow_undefined_flag" = xunsupported; then
  enable_library_bloat=yes
fi
AM_CONDITIONAL([MODULE_DEPENDENCIES],[test "x$enable_library_bloat" = xyes])

GWY_ENABLE([module-bundling],
           [module_bundling],
           [Bundling modules to one big shared library],
           [yes])
AM_CONDITIONAL([MODULE_BUNDLING],[test "x$enable_module_bundling" = xyes])

#############################################################################
# GLib tools
AC_CHECK_PROGS([GLIB_MKENUMS],[glib-mkenums])
AC_CHECK_PROGS([GLIB_GENMARSHAL],[glib-genmarshal])

dnl The introspection macros use this as a shell variable and autoconf somehow
dnl has this pattern forbidden.
m4_pattern_allow([PKG_CONFIG_SYSROOT_DIR])
m4_define([gwy_required_gir],[1.74.0])
AC_SUBST([GWY_REQUIRED_GIR],[gwy_required_gir])
GOBJECT_INTROSPECTION_CHECK([gwy_required_gir])
AC_SUBST([GWY_GIR_VERSION],[${GWY_VERSION_MAJOR}_${GWY_VERSION_MINOR}])

#############################################################################
# XDG stuff
AC_CHECK_PROGS([UPDATE_DESKTOP_DATABASE],[update-desktop-database],:)
AC_CHECK_PROGS([UPDATE_MIME_DATABASE],[update-mime-database],:)
GWY_ENABLE([desktop-file-update],
           [desktop_file_update],
           [Update Freedesktop databases])
AM_CONDITIONAL([DESKTOP_FILE_UPDATE],[test "$enable_desktop_file_update" != no])
# This is always supported
THUMBNAILERS="manual"

#############################################################################
# Valgrind
GWY_WITH_PACKAGE(VALGRIND,[valgrind],[valgrind],,,[compile with Valgrind support for testing])
AC_CHECK_PROGS([VALGRIND],[valgrind],:)

#############################################################################
# Python
# Must override the default interpreter list because otherwise it simply tries
# to use the highest version, which can be python 3.x. In fact, do not look for
# unversioned python at all because it is most likely to be 3.x nowadays.
GWY_WITH([python],,[Install Python modules and plug-ins])
AM_PATH_PYTHON([3.0])
AC_CHECK_SIZEOF([pid_t])
AM_CONDITIONAL([HAVE_PYTHON2],[false])

# Change backward slashes in paths to forwards slashes to avoid mixed-slash
# paths in MS Windows (forwards works just fine in MinGW).
dnl pyexecdir=$(echo "$pyexecdir" | sed 's:\\:/:g')
dnl am_cv_python_pyexecdir=$(echo "$am_cv_python_pyexecdir" | sed 's:\\:/:g')
dnl pythondir=$(echo "$pythondir" | sed 's:\\:/:g')
dnl am_cv_python_pythondir=$(echo "$am_cv_python_pythondir" | sed 's:\\:/:g')
dnl
dnl AC_ARG_ENABLE(pygwy, AS_HELP_STRING([--enable-pygwy],
dnl                                     [Build Python wrapper to Gwyddion]),[],
dnl                                     [enable_pygwy=yes])

dnl if test "x$os_mswin" != xyes; then
dnl   # This is hopefully enough on Unix.
dnl   if test "x$enable_pygwy" != xno; then
dnl     GWY_PYTHON2_DEVEL([2.7],,
dnl                      [enable_pygwy=no
dnl                       pygwy_warn=" (no usable libpython2.7)"])
dnl   fi
dnl else
dnl   # There is no python-config in MS Windows.  Rely on the user to provide
dnl   # PYTHON2_INCLUDES and PYTHON2_LDFLAGS.  Normally "-I/some/dir/include" and
dnl   # "-L/some/dir/lib -lpython27" suffice.
dnl   if test "x$enable_pygwy" != xno; then
dnl     if test "x$PYTHON2_INCLUDES" = x; then
dnl       AC_MSG_NOTICE([please set PYTHON2_INCLUDES; it is not going to work otherwise])
dnl     fi
dnl     if test "x$PYTHON2_LDFLAGS" = x; then
dnl       AC_MSG_NOTICE([please set PYTHON2_LDFLAGS; it is not going to work otherwise])
dnl     fi
dnl     GWY_PYTHON2_TRY_LINK([],
dnl                          [enable_pygwy=no
dnl                           pygwy_warn=" (cannot link with libpython2.7)"])
dnl   fi
dnl fi
dnl 
dnl if test "x$enable_pygwy" != xno; then
dnl   PKG_CHECK_MODULES(PYGTK,[pygtk-2.0 >= 2.10.0],[],
dnl                     [embed_pygtk=yes; pygwy_warn=" (embedded pygtk2)"])
dnl   if test "x$embed_pygtk" = xno; then
dnl     if test "x$PYGTK_CODEGENDIR" = 'x'; then
dnl       PYGTK_CODEGENDIR=`$PKG_CONFIG --variable=codegendir pygtk-2.0`
dnl     fi
dnl   else
dnl     # Some silly OSes want to remove pygtk2.  We can build pygwy without
dnl     # requiring the pygtk/pygobject codegen stuff to be installed.  We still
dnl     # require the runtime packages.  These will be more difficult to embed.
dnl 
dnl     # The local sub directory with codegen.
dnl     PYGTK_CODEGENDIR=pygtk-embed/codegen
dnl     # The pygtk flags are reall just the GTK+ flags, except the -I for
dnl     # pygobject.h.  But we know where we have it when embedding.
dnl     PYGTK_CFLAGS="$GTK_CFLAGS -Ipygtk-embed/include/pygtk-2.0"
dnl     PYGTK_LIBS="$GTK_LIBS"
dnl   fi
dnl   AC_SUBST(PYGTK_CFLAGS)
dnl   AC_SUBST(PYGTK_LIBS)
dnl   AC_SUBST(PYGTK_CODEGENDIR)
dnl fi
AM_CONDITIONAL([ENABLE_PYGWY], [false])

#############################################################################
# With GCC, be paranoid.  It should compile really cleanly.
GWY_OPENMP([201107],[3.1])
GWY_ENABLE([developing],[developing],[enable extra diagnostic compiler options and produce lots of bogus warnings],[no])
if test "$GCC" = yes; then
  # Switching to C23 would be nice, but not realistic yet.
  # Otherwise the standards between C99 and C23 are not that interesting so C99
  # and anything above should be OK.
  GWY_CHECK_PROG_CC_OPTION([-std=gnu99])

  GWY_PROG_CC_WARNINGS="-Wall -W"
  GWY_PROG_CC_OPTIONS([ERRORS],[-Werror=return-type])
  GWY_PROG_CC_OPTIONS([CERRORS],[-Werror=implicit-function-declaration])
  GWY_PROG_CC_OPTIONS([WARNINGS],[-Wredundant-decls -Wno-system-headers -Wshadow -Wundef])
  GWY_PROG_CC_OPTIONS([WARNINGS],[-Wstring-compare -Wcalloc-transposed-args -Walloca])
  GWY_PROG_CC_OPTIONS([CWARNINGS],[-Wstrict-prototypes -Wpointer-arith])
  GWY_PROG_CC_OPTIONS([CWARNINGS],[-Wno-pointer-sign -Wno-sign-compare -Wabsolute-value])
  # Fallthrough is usually *the* reason for choosing case statements over ifs.
  GWY_PROG_CC_OPTIONS([WARNINGS],[-Wno-implicit-fallthrough])
  # Typecasts to functions with fewer arguments are normal for callbacks.
  GWY_PROG_CC_OPTIONS([WARNINGS],[-Wno-cast-function-type])
  # The potential truncation gcc warns about is exactly the wanted result when
  # dealing with fixed-size fields in binary data formats.
  GWY_PROG_CC_OPTIONS([WARNINGS],[-Wno-stringop-truncation],,)
  # We want to do all kinds of arithmetic with enumerated values.
  GWY_PROG_CC_OPTIONS([WARNINGS],[-Wno-enum-conversion],,)
  # Single-bit bools have technically value -1 when TRUE. It is OK because if(x==TRUE) is stupid and we avoid such
  # comparison to booleans but LLVM complains.
  GWY_PROG_CC_OPTIONS([WARNINGS],[-Wno-single-bit-bitfield-constant-conversion],,)

  if test "x$enable_developing" = xyes; then
    GWY_PROG_CC_OPTIONS([WARNINGS],[-fanalyzer])
    GWY_PROG_CC_OPTIONS([WARNINGS],[-Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=malloc -Wsuggest-attribute=format])
    GWY_PROG_CC_OPTIONS([WARNINGS],[-Wduplicated-branches -Wduplicated-cond -Wunused-macros -Wlogical-op])
    GWY_PROG_CC_OPTIONS([WARNINGS],[-Wwrite-strings -Wclobbered -Winline])
    GWY_PROG_CC_OPTIONS([WARNINGS],[-Wopenmp])
    GWY_PROG_CC_OPTIONS([CWARNINGS],[-Wold-style-declaration -Wold-style-definition -Wmissing-parameter-type -Wmissing-prototypes])
  fi

  # -Wformat-security requires -Wformat
  GWY_PROG_CC_OPTION([WFORMAT_SECURITY],
                     [-Wformat -Wformat-security],
                     [GWY_PROG_CC_WARNINGS="$GWY_PROG_CC_WARNINGS -Wformat-security"],)
  # Priting a warning with no body with warning("")-style call is perfectly fine.
  GWY_PROG_CC_OPTION([WNO_FORMAT_ZERO_LENGTH],
                     [-Wformat -Wno-format-zero-length],
                     [GWY_PROG_CC_WARNINGS="$GWY_PROG_CC_WARNINGS -Wno-format-zero-length"],)

  # Optimise! We do not rely on infs and nans working and they are stripped from inputs.
  # Strict aliasing is a nice idea in principle. But half of our code is typecasts. It is also dangerous. Newer,
  # smarter compilers make more inferences about pointers (i.e. propagate "this pointer cannot be the same" to new
  # unexpected places) and break previously working code.
  GWY_PROG_CC_OPTIONS([PREMISES],[-ffast-math -fno-strict-aliasing],,)
  # -O3 enables vectorisation.
  GWY_PROG_CC_OPTIONS([OPTLEVEL],[-O3],,)

  # For OpenEXR until they somehow get rid of dynamic exception specification.
  GWY_PROG_CC_OPTION([OPENEXR],[-Wno-deprecated -Wno-shadow],,)
  # Silence warnings in gtk-doc, python and other weird stuff.
  GWY_PROG_CC_OPTIONS([EXTERNAL],[-Wno-unused-parameter -Wno-missing-field-initializers],,)

  # Flags we can't use:
  # -Wwrite-strings because it makes all struct initializations very noisy
  #PYTHON2_EXTRA_CFLAGS="$GWY_PROG_CC_EXTERNAL"
  GTKDOC_EXTRA_CFLAGS="$GWY_PROG_CC_EXTERNAL"
  OPENEXR_EXTRA_CFLAGS="$GWY_PROG_CC_OPENEXR"
fi
#AC_SUBST(PYTHON2_EXTRA_CFLAGS)
AC_SUBST(GTKDOC_EXTRA_CFLAGS)
AC_SUBST(OPENEXR_EXTRA_CFLAGS)

#############################################################################
# Base libraries (non-GUI).
PKG_CHECK_MODULES(GOBJECT, [gobject-2.0 >= 2.68.0 gthread-2.0 gio-2.0])
PKG_CHECK_MODULES(GMODULE, [gmodule-2.0])
PKG_CHECK_MODULES(FFTW3, [fftw3 >= 3.3])

#############################################################################
# Base libraries: X, Gtk+.
AC_PATH_XTRA
if test "x$x_includes" != x && test "x$x_includes" != xNONE; then
  CPPFLAGS="$CPPFLAGS -I$x_includes"
fi
if test "x$x_libraries" != x && test "x$x_libraries" != xNONE; then
  LDFLAGS="$LDFLAGS -L$x_libraries"
fi

PKG_CHECK_MODULES(GTK, [gtk+-3.0 >= 3.24.0 cairo >= 1.12 pango >= 1.10 gdk-pixbuf-2.0 >= 2.4 pangocairo >= 1.10])
PKG_CHECK_MODULES(PNG, [libpng])
PKG_CHECK_MODULES(ZLIB, [zlib])
GWY_CHECK_LIBM

#############################################################################
# Gtk+ targets (X11, Quartz)
# XXX: This is probably horribly broken. We should use GApplication for remote nonsense
# and detect Mac OS by different means.
AC_MSG_CHECKING([for Gdk targets])
# TODO GTK3: use GTK_CHECK_BACKEND; there is an example in the migration to GTK+3 guide
# TODO GTK3: GTK_CHECK_BACKEND itself it obsolete, you should use PKG_CHECK_MODULES for gtk+-x11-3.0 or something.
# Probably just keep it like this and then switch to GApplication.
gdk_targets=`$PKG_CONFIG --variable=targets gtk+-3.0`
AC_MSG_RESULT([$gdk_targets])
for target in $gdk_targets; do
  case $target in
    quartz)
      PKG_CHECK_MODULES(GTK_MAC, [gtk-mac-integration],
        [AC_DEFINE(HAVE_GTK_MAC_INTEGRATION,1,[Define if we have the gtk-mac-integration library.])
         have_gtk_mac_integration=yes],
        [have_gtk_mac_integration=no])
      break
      ;;
    *)
      ;;
  esac
done
AM_CONDITIONAL([HAVE_GTK_MAC_INTEGRATION], [test "x$have_gtk_mac_integration" != xno])

#############################################################################
# FFTW + OpenMP
# Optional.
# Pkg-config cannot tell us if FFTW libs are available with OpenMP support.
# We have to try linking with them.
fftw_openmp_libs=`echo "$FFTW3_LIBS " | sed 's/-lfftw3 /-lfftw3_omp -lfftw3 /'`
if test x$enable_openmp = xyes; then
AC_CACHE_CHECK([for FFTW3 library with OpenMP support],
  [ac_cv_gwy_fftw_openmp],
  [ye_PROG_CC_OPTION_cflags="$CFLAGS"
   ye_PROG_CC_OPTION_ldflags="$LDFLAGS"
  CFLAGS="$CFLAGS $FFTW3_CFLAGS $OPENMP_CFLAGS"
  LDFLAGS="$LDFLAGS $fftw_openmp_libs $OPENMP_CFLAGS"
  AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <fftw3.h>],[[fftw_init_threads(); fftw_cleanup_threads();]])],
     [ac_cv_gwy_fftw_openmp=yes],
     [ac_cv_gwy_fftw_openmp=no])
  CFLAGS="$ye_PROG_CC_OPTION_cflags"
  LDFLAGS="$ye_PROG_CC_OPTION_ldflags"
])
fi

if test x$ac_cv_gwy_fftw_openmp = xyes; then
  AC_DEFINE(HAVE_FFTW_WITH_OPENMP,1,[Define if we have FFTW3 with OpenMP support.])
  FFTW3_LIBS="$fftw_openmp_libs"
  pkg_cv_FFTW3_LIBS="$fftw_openmp_libs"
fi

#############################################################################
dnl # GtkSourceView
dnl # Optional.
dnl if test "x$enable_pygwy" != xno; then
dnl   GWY_WITH([gtksourceview],,[Better Pygwy console user interface])
dnl   if test "x$enable_gtksourceview" != "xno"; then
dnl     PKG_CHECK_MODULES(GTKSOURCEVIEW,[gtksourceview-2.0],
dnl                       [AC_DEFINE(HAVE_GTKSOURCEVIEW,1,
dnl                                  [Define if we have the GtkSourceView package.])],
dnl                       [enable_gtksourceview=no])
dnl   fi
dnl   if test "x$enable_gtksourceview" != "xno"; then
dnl     GTKSOURCEVIEW_DEPENDENCY=gtksourceview-2.0
dnl   else
dnl     GTKSOURCEVIEW_DEPENDENCY=
dnl     GTKSOURCEVIEW_CFLAGS="$GTK_CFLAGS"
dnl     GTKSOURCEVIEW_LIBS="$GTK_LIBS"
dnl   fi
dnl   AC_SUBST([GTKSOURCEVIEW_DEPENDENCY])
dnl fi

enable_gl=no
if false; then
  AC_DEFINE([HAVE_GTKGLEXT],[1],[Define if we have the GtkGLExt package.])
fi

#############################################################################
# libbz2
# Optional.  Used sometimes for data compression, e.g. in NRRD.
GWY_WITH([bzip2],,[build with bzip2])
if test "x$enable_bzip2" != xno && test -z "$BZIP2"; then
  AC_CHECK_LIB(bz2, BZ2_bzopen,
    [AC_CHECK_HEADER(bzlib.h, BZIP2='-lbz2', [enable_bzip2=no])],
    [enable_bzip2=no],
    [])
fi
if test "x$enable_bzip2" != xno && test -n "$BZIP2"; then
  AC_DEFINE(HAVE_BZIP2,1,[Define if we have the BZIP2 library.])
else
  BZIP2_WARN=" (needs bzip2)"
fi
AM_CONDITIONAL([HAVE_BZIP2],[test "x$enable_bzip2" != xno && test -n "$BZIP2"])
AC_SUBST(BZIP2)

#############################################################################
# zstd
# Optional.  Used for dumb Keyence compressed files inside compressed files.
enable_zstd=yes
PKG_CHECK_MODULES(ZSTD, [libzstd >= 1.3.0],
                  [AC_DEFINE(HAVE_ZSTD,1,
                            [Define if we have the zstd package.])],
                  [enable_zstd=no])
AM_CONDITIONAL([HAVE_ZSTD],[test "x$enable_zstd" != xno])

#############################################################################
# ZIP support libraries.
# Optional.  Used to load the crazy zip-compressed-bunch-of-XML formats as
# well as some other stuff.
#
# The choices are:
#
# minizip 1.x
# It used to be preferred over libzip and is well tested, but is now obsolete.
#
# libzip
# Alternative to minizip. Perhaps a better one.
#
# minizip 2.x or 3.x or whatever
# The new branch that broke everything and seems to be under heavy development.
# Not sure since which version is actually usable. They happily break API
# often; but 3.x seems compatible with 2.x.
#
# minizip-ng
# Fork of minizip. Version 4.x broke everything again. These people are
# apparently incapable of maintaining a stable API if their life depended on
# it.
#
# Zzip
# A simple fallback library, not good for sequential scanning of archives (see
# notes in gwyzip.h).
enable_zip=auto
AC_ARG_WITH([zip],
  AS_HELP_STRING([--with-zip=ZIPLIB],
                 [ZIP support library (minizip1, minizip2, minizip-ng, libzip, zzip or auto)]),
  [case "$withval" in
       minizip1|minizip2|minizipng|libzip|zzip|auto|no) enable_zip="$withval";;
       minizip-ng) enable_zip=minizipng;;
       yes) enable_zip=auto;;
       *) AC_MSG_ERROR([bad balue "$withval" for --with-zip]) ;;
   esac]
)
found_zip=no
zip_library=
GWY_ZIP([minizipng],[minizip-ng >= 4.0],[minizip-ng])
GWY_ZIP([libzip],[libzip >= 0.11])
GWY_ZIP([minizip1],[minizip < 2.0],[minizip 1.x])
GWY_ZIP([minizip2],[minizip >= 2.8],[minizip 2.x or 3.x])
GWY_ZIP([zziplib],[zziplib],[Zzip])
# Sometimes we get the minizip subdirectory in the include path, sometimes we do not.  Check both.
# FIXME: Do we need to do this dance also for minizip-ng?
if test "x$zip_library" = xminizip2; then
  ZIP_ORIG_CPPFLAGS="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS $ZIP_CFLAGS"
  AC_CHECK_HEADERS([minizip/mz.h mz.h],[],[],[])
  CPPFLAGS="$ZIP_ORIG_CPPFLAGS"
fi
# Print a conclusion of the above tests.
AC_MSG_CHECKING([for a ZIP library])
AC_MSG_RESULT([$zip_library])
# And define common-named variables to simplify things.
AC_SUBST([ZIP_CFLAGS])
AC_SUBST([ZIP_LIBS])
AM_CONDITIONAL([HAVE_ZIP],[test "x$found_zip" != xno])

#############################################################################
# WebP
# Optional.  Pixmap image format support.
GWY_WITH_PACKAGE(WEBP,[libwebp],[webp])

#############################################################################
# JPEG XL
# Optional.  Pixmap image format support.
# Disabled.  The library is constantly changing APIs and breaks builds; cannot
# keep compatibility.
dnl enable_jxl=yes
dnl PKG_CHECK_MODULES(JXL, [libjxl],
dnl                   [AC_DEFINE(HAVE_JXL,1,
dnl                             [Define if we have the libjxl package.])],
dnl                   [enable_jxl=no])
dnl AM_CONDITIONAL([HAVE_JXL],[test "x$enable_jxl" != xno])

#############################################################################
# OpenEXR 2.x+
# Optional.  Used for HDR greyscale OpenEXR pixmap import/export. Only try to
# enable if we have a C++ compiler because it is a C++ library.
enable_exr=$have_cxx
if test "x$enable_exr" != xno; then
  PKG_CHECK_MODULES(EXR, [OpenEXR >= 2.0],
                    [AC_DEFINE(HAVE_EXR,1,
                              [Define if we have the OpenEXR 2.x+ package.])],
                    [enable_exr=no])
fi
AM_CONDITIONAL([HAVE_EXR],[test "x$enable_exr" != xno])

#############################################################################
# HDF5
# Optional.  Used for import of HDF5-based formats.
GWY_LIB_HDF5([1.12.0])
# We like calling all the variables `enable'.  Also, we want a Makefile
# conditional, not just a C preprocessor macro.
enable_hdf5=$with_hdf5
AM_CONDITIONAL([HAVE_HDF5],[test "x$enable_hdf5" != xno])

#############################################################################
# CFITSIO
# Optional.  Used for FITS image import.
GWY_WITH_PACKAGE(CFITSIO,[cfitsio],[cfitsio])

#############################################################################
# LibXML2.
# Optional, for SPML, APE DAX, Anasys XML and Zyvex importers.
GWY_WITH_PACKAGE(XML2,[libxml-2.0 >= 2.6],[libxml2])

#############################################################################
# JSON-GLIB
# Optional, for PS-PPT and FusionScope importers and NHF metadata.
GWY_WITH_PACKAGE(JSON_GLIB,[json-glib-1.0 >= 1.6],[json-glib],[json_glib])

dnl #############################################################################
dnl # KDE4 and Qt4 (for KDE thumbnailer plug-in)
dnl # Very optional.
dnl #
dnl # Note we need headers of both, but actually link only with Qt.
dnl #
dnl # KDE4 uses cmake as the build system.  So either we are going to break things
dnl # by mixing automake & cmake (because the thumbnailer needs to be linked with
dnl # gwyddion libs), or we are going to break things by doing everything manually.
dnl # In the latter case the poor user should have a better chance to fix it by
dnl # setting CXXFLAGS, LIBS, etc. herself.
dnl GWY_WITH([kde4-thumbnailer],
dnl          [kde4_thumbnailer],
dnl          [KDE4 thumbnail creator module],
dnl          [no])
dnl 
dnl if test "x$enable_kde4_thumbnailer" != xno; then
dnl   # Find kde4-config.
dnl   AC_ARG_VAR([KDE4_CONFIG],[path to kde4-config utility])
dnl   if test "x$ac_cv_env_KDE4_CONFIG_set" != xset; then
dnl     AC_PATH_TOOL([KDE4_CONFIG],[kde4-config])
dnl   fi
dnl   if test "x$KDE4_CONFIG" = x; then
dnl     enable_kde4_thumbnailer=no
dnl   else
dnl     # XXX: This leaves lots to desire.  What if someone installs Gwyddion to
dnl     # home?  Let her at least overrride KDE4_MODULE_DIR
dnl     KDE4_MODULE_DIR=`$KDE4_CONFIG --install module`
dnl     KDE4_PREFIX=`$KDE4_CONFIG --prefix`
dnl     KDE4_LIBSUFFIX=`$KDE4_CONFIG --libsuffix`
dnl     QT4_PREFIX=`$KDE4_CONFIG --qt-prefix`
dnl     QT4_LIBDIR=`$KDE4_CONFIG --qt-libraries`
dnl   fi
dnl 
dnl   AC_LANG_PUSH([C++])
dnl   KDE4_ORIG_LIBS="$LIBS"
dnl   KDE4_ORIG_LDFLAGS="$LDFLAGS"
dnl   KDE4_ORIG_CPPFLAGS="$CPPFLAGS"
dnl   KDE4_CPPFLAGS="-I$KDE4_PREFIX/include/kde4 -I$QT4_PREFIX/include"
dnl   KDE4_LDFLAGS="-L$KDE4_PREFIX/lib$KDE4_LIBSUFFIX -L$QT4_LIBDIR"
dnl   KDE4_LIBS="-lQtCore -lQtGui"
dnl   CPPFLAGS="$CPPFLAGS $KDE4_CPPFLAGS"
dnl   LDFLAGS="$LDFLAGS $KDE4_LDFLAGS"
dnl   LIBS="$LIBS $KDE4_LIBS"
dnl 
dnl   # Try to link with QtCore and QtGui.
dnl   if test "x$enable_kde4_thumbnailer" != xno; then
dnl     AC_MSG_CHECKING([for qVersion in $KDE4_LIBS])
dnl     LIBS="$LIBS $KDE4_LIBS"
dnl     AC_LINK_IFELSE([AC_LANG_SOURCE([char qVersion(void); int main(void){ return qVersion(); }])],
dnl                    [:],[enable_kde4_thumbnailer=no])
dnl     AC_MSG_RESULT([$enable_kde4_thumbnailer])
dnl   fi
dnl 
dnl   # Try to compile with the corresponding headers.
dnl   if test "x$enable_kde4_thumbnailer" != xno; then
dnl     AC_CHECK_HEADER([QtCore/qstring.h],[:],[enable_kde4_thumbnailer=no])
dnl   fi
dnl   if test "x$enable_kde4_thumbnailer" != xno; then
dnl     AC_CHECK_HEADER([QtGui/qimage.h],[:],[enable_kde4_thumbnailer=no])
dnl   fi
dnl   if test "x$enable_kde4_thumbnailer" != xno; then
dnl     AC_CHECK_HEADER([kio/thumbcreator.h],[:],[enable_kde4_thumbnailer=no])
dnl   fi
dnl 
dnl   CPPFLAGS="$KDE4_ORIG_CPPFLAGS"
dnl   LDFLAGS="$KDE4_ORIG_LDFLAGS"
dnl   LIBS="$KDE4_ORIG_LIBS"
dnl   AC_LANG_POP([C++])
dnl fi
dnl 
dnl AM_CONDITIONAL([KDE4_THUMBNAILER],[test "x$enable_kde4_thumbnailer" != xno])
dnl if test "x$enable_kde4_thumbnailer" != xno; then
dnl   THUMBNAILERS="$THUMBNAILERS KDE4"
dnl fi
dnl 
AM_CONDITIONAL([KDE4_THUMBNAILER],[false])
AC_SUBST(KDE4_MODULE_DIR)
AC_SUBST(KDE4_CPPFLAGS)
AC_SUBST(KDE4_LDFLAGS)
AC_SUBST(KDE4_LIBS)

#############################################################################
# Check for header files.
AC_MSG_CHECKING([for anyone actually reading this nonsense])
AC_MSG_RESULT([no])
AC_CHECK_HEADERS([GL/glext.h], [], [], [[#include <GL/gl.h>]])

#############################################################################
# Check for maths library functions.
# These we simply require.
AC_CHECK_FUNCS([log1p log2 expm1 exp2],,
               [AC_MSG_ERROR([Required maths function $ac_func not found.])])
# For these we have public gwy_foo() which is either the system function or ours,
# but always have some implementation. Therefore, the public gwyconfig.h must
# say whether the system provides them (config.h could too, but it would be
# redundant).
GWY_CHECK_MATH_FUNCS([exp10 sincos])
# These are used internally and/or optionally. Gwyddion library users will not
# get. So config.h must tell whether the system provides them (but not the public
# gwyconfig.h).
AC_CHECK_FUNCS([j0 j1 y0 y1 memrchr memmem])

#############################################################################
# I18n
GETTEXT_PACKAGE=$PACKAGE_TARNAME
AM_GNU_GETTEXT([external])
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE",[Gettext package name])
AC_SUBST(GETTEXT_PACKAGE)
if test "x$USE_NLS" != xyes; then
  AC_MSG_ERROR([NLS support is required. Since it is required by GLib, you should not ever see this error.])
fi

#############################################################################
# Libs
BASIC_LIBS="$GOBJECT_LIBS $INTLLIBS $LIBM"
LIBS="$ORIG_LIBS"
AC_SUBST(BASIC_LIBS)
COMMON_LDFLAGS="$HOST_LDFLAGS"
AC_SUBST(COMMON_LDFLAGS)

common_c_and_cxx_flags="$HOST_CFLAGS $GOBJECT_CFLAGS -DGLIB_DISABLE_DEPRECATION_WARNINGS $GWY_PROG_CC_ERRORS $GWY_PROG_CC_WARNINGS $GWY_PROG_CC_PREMISES $GWY_PROG_CC_OPTLEVEL"
COMMON_CFLAGS="$common_c_and_cxx_flags $GWY_PROG_CC_CERRORS $GWY_PROG_CC_CWARNINGS"
COMMON_CXXFLAGS="$common_c_and_cxx_flags"
AC_SUBST(COMMON_CFLAGS)
AC_SUBST(COMMON_CXXFLAGS)

#############################################################################
# RPM
AC_DEFINE_UNQUOTED(PACKAGE_URL,"$PACKAGE_URL",
                   [Define to package home page, as a string.])
AC_SUBST(PACKAGE_URL)

#############################################################################
# Generate gwyconfig.h
AC_CONFIG_COMMANDS([gwyconfig3.h],
[
  outfile=gwyconfig3.h-tmp
  cat > $outfile <<\_______EOF
/*
 * gwyconfig3.h
 *
 * This is a generated file.  Please modify 'configure.ac'.
 *
 * It contains system-dependent information which can influence public Gwyddion headers.
 */

#ifndef __GWY_CONFIG3_H__
#define __GWY_CONFIG3_H__

_______EOF

  if test "x$enable_gl" = xno; then
    echo '#undef GWYDDION_HAS_OPENGL' >>$outfile
  else
    echo '#define GWYDDION_HAS_OPENGL 1' >>$outfile
  fi
  echo >>$outfile

  echo "#define GWY_SHARED_LIBRARY_EXTENSION \"$acl_shlibext\"" >>$outfile
  echo >>$outfile

GWY_OUTPUT_MATH_FUNCS([exp10 sincos], [$outfile])
  cat >>$outfile <<_______EOF

#endif
_______EOF


  if cmp -s $outfile gwyconfig3.h; then
    AC_MSG_NOTICE([gwyconfig3.h is unchanged])
    rm -f $outfile
  else
    mv $outfile gwyconfig3.h
  fi
],
[
  # Initialise the variables. This goes to config.status with values expanded.
  enable_gl=$enable_gl
  acl_shlibext=$acl_shlibext
  gwy_math_exp10=$gwy_math_exp10
  gwy_math_sincos=$gwy_math_sincos
])

#############################################################################
# Make msgmerge work on Darwin.  See
# http://subcommanderblog.wordpress.com/2009/05/16/msgmerge-on-macosx-and-no-such-file-or-directory/
if test "$os_darwin" = yes; then
  AC_MSG_NOTICE([fixing msgmerge to work on Darwin])
  AC_CONFIG_COMMANDS([osx-chmod],
  [
    chmod +a "" po/*.po
    chmod a+x utils/bundler utils/mkosxlauncher
  ])
fi

#############################################################################
# Create lists of included and exclued optional file formats.
# This table needs to be kept in sync with modules/file/Makefile.am.
cat >conftest.out <<EOF
found_zip? NanoObserver
found_zip? NanoScanTech
found_zip? OpenGPS
found_zip? PLUx
found_zip? SPMxFormat
found_zip? JPK force
found_zip? Olympus POIR
found_zip? Keyence VK6/VK7
found_zip? ZON
found_zip? SCNS
found_zip? numpy NPZ
found_zip? enable_zstd? ZON nested zstd
enable_libxml2? found_zip? APE DAX
enable_libxml2? SPML
enable_libxml2? Anasys
enable_libxml2? Zyvex
enable_exr? have_cxx? OpenEXR
enable_cfitsio? FITS
enable_bzip2? NRRD/bzip2
have_cxx? PNG/16bit
have_cxx? BigTIFF
have_cxx? PGM/16bit
enable_webp? WebP export
enable_hdf5? generic HDF5
enable_hdf5? Ergo
enable_hdf5? Shilps
enable_hdf5? DATX
enable_hdf5? NSID
enable_hdf5? EPFL
enable_hdf5? Matlab 7
enable_hdf5? enable_json_glib? FusionScope
enable_hdf5? enable_json_glib? Velox EMD
enable_json_glib? PS-PPT
enable_json_glib? NHF JSON meta
EOF

for dep in enable_bzip2 enable_cfitsio enable_exr enable_json_glib enable_libxml2 enable_webp enable_hdf5 found_zip have_cxx; do
  AS_VAR_COPY([x],[$dep])
  if test "x$x" != xno; then
    sed "s/$dep? //" conftest.out >conftest.tmp
  else
    sed "s/$dep? /#/" conftest.out >conftest.tmp
  fi
  cat conftest.tmp >conftest.out
done
rm -f conftest.tmp

fmtwidth=44
if test "x$COLUMNS" != x && test "$COLUMNS" -gt 70; then
  fmtwidth=$(( $COLUMNS - 36 ))
fi

enabled_formats=`grep -v '^#' conftest.out | sort -i | sed 's/$/, /' | tr -d '\n' | sed 's/, *$//'`
enabled_formats=`echo "$enabled_formats" | fmt -w $fmtwidth | sed 's/^/                                   /;1s/^ *//'`
disabled_formats=`grep '^#' conftest.out | sort -i | sed 's/$/, /' | tr -d '#\n' | sed 's/, *$//'`
disabled_formats=`echo "$disabled_formats" | fmt -w $fmtwidth | sed 's/^/                                   /;1s/^ *//'`
rm -f conftest.out

#############################################################################
# Generate gwyfeatures.h
# (Do not call it just features; it is highly prone to be included in place of
# the system features.h header).
AC_CONFIG_COMMANDS([gwyfeatures.h],
[
  outfile=gwyfeatures.h-tmp
  cat > $outfile <<\_______EOF
/*
 * gwyfeatures.h
 *
 * This is a generated file.  Please modify 'configure.ac'.
 *
 * It contains build information.
 */

#ifndef __GWYFEATURES_H__
#define __GWYFEATURES_H__

_______EOF

for feature in have_cxx found_zip enable_exr enable_libxml2 enable_cfitsio enable_bzip2 enable_zstd enable_webp enable_hdf5 enable_json_glib enable_introspection enable_library_bloat enable_module_bundling; do
  AS_VAR_COPY([x],[$feature])
  if test "x$x" != xno; then
    echo "#define GWY_APP_FEATURE_$feature TRUE"
  else
    echo "#define GWY_APP_FEATURE_$feature FALSE"
  fi
done >>$outfile

echo "#define GWY_APP_FEATURE_zip_library \"$zip_library\"" >>$outfile

  cat >> $outfile <<\_______EOF

#endif
_______EOF

  if cmp -s $outfile gwyfeatures.h; then
    AC_MSG_NOTICE([gwyfeatures.h is unchanged])
    rm -f $outfile
  else
    mv $outfile gwyfeatures.h
  fi
],
[
  # Initialise the variables. This goes to config.status with values expanded.
  have_cxx=$have_cxx
  found_zip=$found_zip
  zip_library=$zip_library
  enable_exr=$enable_exr
  enable_libxml2=$enable_libxml2
  enable_cfitsio=$enable_cfitsio
  enable_bzip2=$enable_bzip2
  enable_zstd=$enable_zstd
  enable_webp=$enable_webp
  enable_hdf5=$enable_hdf5
  enable_json_glib=$enable_json_glib
  enable_introspection=$enable_introspection
  enable_library_bloat=$enable_library_bloat
  enable_module_bundling=$enable_module_bundling
])


#############################################################################
# Actually run the output commands.
AC_OUTPUT

dnl Solve chicken and egg problem by generating a Makefile fragment here.
$PYTHON $srcdir/build-aux/expand-test-list.py MK <$srcdir/tests/test-list.txt >tests/test-list.mk

#############################################################################
# Print the summary.
dnl pygwy_hl=
dnl if test "x$enable_pygwy" = xyes; then
dnl   if test "x$enable_gtksourceview" = xyes; then
dnl     pygwy_hl=' (with syntax highlighting)'
dnl   else
dnl     pygwy_hl=' (no syntax highlighting)'
dnl   fi
dnl fi

echo "================================================================="
echo "Configuration Summary:"
echo "  OpenGL 3D widgets:               $enable_gl$GL_WARN"
echo "  OpenMP parallelization:          $enable_openmp"
echo "  Optional file formats included:  $enabled_formats"
echo "  Optional file formats excluded:  $disabled_formats"
echo "  Module linking:                  bundling=$enable_module_bundling, bloat=$enable_library_bloat"
echo "  Thumbnailers to build:           $THUMBNAILERS"
#echo "  Python interface (pygwy):        $enable_pygwy$pygwy_hl$pygwy_warn"
echo "  Desktop integration files go to: $xdg_target"
echo "================================================================="

#############################################################################
# Maintainer mode
if test "x$enable_maintainer_mode" = xyes; then
  missing_mmode_stuff=
  if test "x$PYTHON" = x || test "x$PYTHON" = x:; then
    missing_mmode_stuff="$missing_mmode_stuff"
  fi
  if test "x$GLIB_MKENUMS" = x || test "x$GLIB_MKENUMS" = x:; then
    missing_mmode_stuff="$missing_mmode_stuff, glib-mkenums"
  fi
  if test "x$GLIB_GENMARSHAL" = x || test "x$GLIB_GENMARSHAL" = x:; then
    missing_mmode_stuff="$missing_mmode_stuff, glib-genmarshal"
  fi
  if test "x$XSLTPROC" = x || test "x$XSLTPROC" = x:; then
    missing_mmode_stuff="$missing_mmode_stuff, xsltproc"
  fi
  if test "x$enable_gtk_doc" != xyes; then
    missing_mmode_stuff="$missing_mmode_stuff, gtk-doc"
  fi
  missing_mmode_stuff=`echo "$missing_mmode_stuff" | sed 's/^, //'`
  if test "x$missing_mmode_stuff" != x; then
    AC_MSG_WARN([Missing maintainer mode tools.
*******************************************************************************
  Maintainer mode is enabled, but some tools used to generate various files
  are missing:

     $missing_mmode_stuff

  Depending on what you are doing and why you enabled the maintainer mode,
  this may be OK or things may break horribly.
*******************************************************************************])
  fi
fi

# vim: set columns=120 ts=2 sw=2 et :
