################################################################################
#
# autotools.cygclass - functions for building autotool-based packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2020 Cygport authors
# Provided by the Cygwin project <https://cygwin.com/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <https://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/autotools.cygclass
#  DESCRIPTION
#  GNU Autotools (comprising of Gettext, Autoconf, Automake, and Libtool)
#  comprise the build system of many source packages of all types.
#
#  This Cygclass provides the functions necessary to configure and build
#  autotool-based packages.  Since these are so widespread, this is the
#  assumed build system if no other is specified, and provides the default
#  src_compile accordingly.
#  EXAMPLES
#  A basic autotool-based program package can be as simple as:
#
#    NAME="grep"
#    VERSION=2.14
#    RELEASE=1
#    CATEGORY="Base"
#    SUMMARY="Searches files for text patterns"
#    DESCRIPTION="The grep command searches one or more input files for lines
#    containing a match to a specified pattern. By default, grep prints the
#    matching lines."
#    HOMEPAGE="https://www.gnu.org/software/grep/"
#    LICENSE="GPL-2.0-or-later"
#    SRC_URI="mirror://gnu/grep/grep-${VERSION}.tar.xz"
#
#  If the only customization needed is to add configure flags, use CYGCONF_ARGS:
#
#    NAME="nano"
#    VERSION=2.2.6
#    RELEASE=1
#    CATEGORY="Editors"
#    SUMMARY="Enhanced clone of Pico editor"
#    DESCRIPTION="GNU nano is a small and friendly console-mode text editor, based
#    on and mostly compatible with UW Pico. Besides basic text editing, nano offers
#    many extra features like an interactive search and replace, go to line and
#    column number, auto-indentation, feature toggles, internationalization support,
#    and filename tab completion."
#    HOMEPAGE="https://www.nano-editor.org/"
#    LICENSE="GPL-3.0-or-later"
#    SRC_URI="https://www.nano-editor.org/dist/v${VERSION%.*}/nano-${VERSION}.tar.gz"
#    PATCH_URI="2.2.4-wchar.patch"
#    
#    DIFF_EXCLUDES="nano.spec"
#    
#    CYGCONF_ARGS="--without-slang"
#
#  Packages providing libraries need a bit more to handle split lib/devel
#  packaging:
#
#    NAME="libdatrie"
#    VERSION=0.2.6
#    RELEASE=1
#    CATEGORY="Libs"
#    SUMMARY="Double-array digital search tree library"
#    DESCRIPTION="This is an implementation of double-array structure for
#    representing trie, as proposed by Junichi Aoe."
#    HOMEPAGE="https://linux.thai.net/~thep/datrie/datrie.html"
#    LICENSE="LGPL"
#    
#    PKG_NAMES="${NAME}1 ${NAME}-devel ${NAME}-doc"
#    # see PKG_CONTENTS
#    libdatrie1_CONTENTS="usr/bin/cygdatrie-1.dll"
#    # trietool is a dev-util
#    libdatrie_devel_CONTENTS="usr/bin/trietool* usr/include/ usr/lib/ usr/share/man/"
#    libdatrie_doc_CONTENTS="usr/share/doc/"
#
#  Sometimes a package uses its own aclocal macros but does not ship them,
#  or uses an older autoconf which is incompatible with the current version,
#  making cygautoreconf impossible.  In that case, as long as the package does
#  not use libtool, you may be able to skip cygautoreconf:
#
#    src_compile() {
#      # cannot autoreconf: undefined macro: AC_ACVERSION
#      cd ${B}
#      cygconf --enable-iconv
#      cygmake
#    }
#
#  For more complicated packages, additional compile and/or install steps
#  may be necessary, in which case you should override the default src_compile
#  and/or src_install:
#
#    src_compile() {
#      # the configure script is not distributed, and VPATH builds aren't supported
#      cd ${S}
#      cygmake configure
#      lndirs
#      cd ${B}
#      cygconf --htmldir=/usr/share/doc/${NAME}/html --mandir=/usr/share/man
#      cygmake all
#      # these can't be built in parallel
#      cygmake html
#      cygmake man
#      cygmake info
#      cygmake pdf
#    }
#    
#    src_install() {
#      cd ${B}
#      cyginstall install-html install-man install-info install-pdf pdfdir=/usr/share/doc/${NAME}
#    
#      # ship bash completion
#      insinto /etc/bash_completion.d
#      doins ${S}/contrib/completion/*.bash
#    }
#
#  As noted below, cygconf is only intended for autoconf-compatible configure
#  scripts.  Some packages use a custom configure script which is not compatible,
#  in which case you need to call configure yourself:
#
#    src_compile() {
#      # package is not VPATH aware
#      lndirs
#      cd ${B}
#      # custom configure incompatible with autoconf
#      ./configure --prefix=/usr || error "configure failed"
#      cygmake
#   }
#
#  NOTES
#  These functions were previously part of cygport itself, but were moved
#  out for easier maintainability.  For backwards compatibility, this
#  cygclass is automatically inherit()ed, so these functions are always
#  available as before.
#
#  Normally, a cygclass can only be inherited once.  Even though this
#  is auto-inherit()ed, it can still be manually inherit()ed once more if desired.
#  In most cases this is unnecessary, but it can be useful if you inherit
#  other cygclasses but still want the default src_compile().  For example:
#    inherit perl autotools
#  provides all the PERL_* definitions and perl_*() functions, but will build
#  with cygconf/cygmake by default instead of with perl's ExtUtils::MakeMaker
#  or Module::Build.
#  INHERITED BY
#  No Cygclass actually inherit()s autotools.cygclass for the reasons stated above,
#  but the functions defined herein are used by a number of Cygclasses:
#
#  gnome2.cygclass, gst-plugins.cygclass, gtkmm.cygclass, httpd.cygclass,
#  kde3.cygclass, mate.cygclass, php.cygclass, pygtk.cygclass, toolchain.cygclass,
#  xfce4.cygclass, xorg.cygclass
#****

# utility function used in cygautoreconf, gnome2_autoreconf, mate_autoreconf
__parse_aclocal_amflags() {
	local arg;

	for arg in $@
	do
		case $1 in
		-I) echo $2 ;;
		esac
		shift
	done
}


#****C* autotools.cygclass/gnuconfigize
#  SYNOPSIS
#  gnuconfigize DIRECTORY [DIRECTORY ...]
#  DESCRIPTION
#  Updates the config.guess and config.sub files in the given directory(ies).
#  This may be necessary when building packages natively for x86_64-cygwin,
#  which is not recognized by older versions of this file.
#  NOTE
#  This function is called automatically during cygautoreconf; it need only
#  be called directly for packages which install their copies of this file
#  (e.g. the autotools themselves) or in the unusual case where cygautoreconf
#  absolutely cannot be used.
#****

gnuconfigize() {
	local d f ts_old ts_new

	(( $# >= 1 )) || error "gnuconfigize: no destinations specified"

	for d in $@
	do
		for f in config.{guess,sub}
		do
			[ -f ${d}/${f} ] || error "gnuconfigize: $d: not a gnuconfig directory"
			ts_old=$(sed -n "s/^timestamp='\([-0-9]*\)'.*$/\1/p" ${d}/${f})
			ts_new=$(sed -n "s/^timestamp='\([-0-9]*\)'.*$/\1/p" ${_privgnuconfigdir}/${f})
			if ! [ "${ts_old}" '<' "${ts_new}" ]
			then
				inform "${f}: not downgrading from '${ts_old}' to '${ts_new}'"
				continue
			fi
			cp -f ${_privgnuconfigdir}/${f} ${d}/
			inform "${f}: updated from '${ts_old}' to '${ts_new}'"
		done
	done
}

#****C* autotools.cygclass/cygautoreconf
#  SYNOPSIS
#  cygautoreconf
#  DESCRIPTION
#  Updates the autotool build system with autoreconf, which runs the tools
#  from autoconf, automake, gettext-devel, and libtool to update their
#  respective components.  Must be run in the directory containing the top-level
#  configure.ac or configure.in (usually $S).
#  NOTE
#  If, during cygautoreconf, you get a warning such as:
#     Warning: ./configure.ac may require LT_OUTPUT macro
#  then the package configure script is relying on libtool 1.5 behaviour where
#  the libtool script is generated during AC_PROG_LIBTOOL, and can therefore
#  be called anytime thereafter in the configure script.  Some packages do this
#  either to read the libtool config, or to run compiling/linking tests with
#  the libtool script.
#
#  However, with the current libtool 2.2, the libtool script is generated during
#  the AC_OUTPUT phase at the end of configure, so these configure commands will
#  not work as designed.  The simplest portable solution is to add the
#  following line after the AC_PROG_LIBTOOL call:
#    m4_ifdef([LT_OUTPUT], [LT_OUTPUT])
#  which means: if libtool 2.2 is in use, generate a libtool script early so
#  that it can be used during configure; if libtool 1.5 is in use, this line has
#  no effect.  Such a patch is suitable to be pushed upstream.
#  WARNING
#  Skipping this step is strongly discouraged.  Patches to configure.ac, aclocal
#  macros, or Makefile.am files will not take effect until their tools have been
#  regenerated.  Also, Cygwin's libtool package often includes patches which
#  are not yet available in the latest upstream release, so libraries may not
#  be built optimally, or even correctly, without this step having been run.
#  REQUIRES
#  autoconf, automake, gettext-devel, libtool
#****
cygautoreconf() {
	local d;
	local f;
	local f_in;
	local tool;
	local ac_config_files;
	local ac_config_headers;
	local ac_subdirs;
	local ac_ver;
	local autoreconf_args;
	local auxdir;
	local configure_ac;
	local config_h;
	local config_h_in;
	local gettext_version;
	local sub_configure_ac;

	if [ -f configure.ac ]
	then
		configure_ac="configure.ac";
	elif [ -f configure.in ]
	then
		configure_ac="configure.in";
	else
		error "Could not find configure.ac or configure.in";
	fi

	__config_set with_autoconf 1

	ac_subdirs="$(grep '^[^\#]*AC_CONFIG_SUBDIRS*' ${configure_ac} | sed -e 's!.*AC_CONFIG_SUBDIRS*(\[*\(.*\))!\1!g' -e 's!\]*!!g')"

	for d in . ${ac_subdirs}
	do
		if [ -d ${d} ]
		then
			if [ -f ${d}/configure.ac ]
			then
				sub_configure_ac="${d}/configure.ac";
			elif [ -f ${d}/configure.in ]
			then
				sub_configure_ac="${d}/configure.in";
			else
				error "Could not find ${d}/configure.ac or ${d}/configure.in";
			fi

			# e.g. CVS checkouts
			if [ -f ${d}/configure ]
			then
				# Assure that autotool files are writable (e.g. less)
				eval $(grep -h '^ac_config_files=' ${d}/configure)
				# eval $(grep -h '^ac_config_headers=' ${d}/configure)

				chmod +wx ${d}/configure;

				for f in ${ac_config_files} ${ac_config_headers}
				do
					case ${f} in
						*:*)	f_in=${d}/${f#*:} ;;
						*)		f_in=${d}/${f}.in ;;
					esac

					if [ -e ${f_in} ]
					then
						chmod +w ${f_in};
					fi
				done

				unset ac_config_files;
			fi

			# sometimes ACLOCAL_AMFLAGS is defined but the given directory
			# exists only in VCS (or is created by an autogen.sh) and is
			# not shipped in the tarball
			if [ -f Makefile.am ]
			then
				__parse_aclocal_amflags $(grep '^ACLOCAL_AMFLAGS' Makefile.am | sed 's/=/ = /') | xargs -r mkdir -p
			fi
			grep '^[^\#]*AC_CONFIG_MACRO_DIRS*' ${sub_configure_ac} | sed -e "s!.*AC_CONFIG_MACRO_DIRS*(\[*\(.*\))!${d}/\1!g" -e 's!\]*!!g' | xargs -r mkdir -p

			if [ ! -f ${d}/aclocal.m4 ] || $(head -n 1 ${d}/aclocal.m4 | grep -q 'generated.* by aclocal')
			then
				__config_set with_aclocal 1
			fi

			if $(grep -q 'AM_INIT_AUTOMAKE' ${sub_configure_ac})
			then
				__config_set with_automake 1
			fi

			if $(grep -Eq '(AM_GNU_GETTEXT|AM_ICONV|AC_GGZ_INTL)' ${sub_configure_ac})
			then
				cp -f /usr/share/gettext/config.rpath ${d}/
				__config_set with_gnu_gettext 1
			fi

			if $(grep -Eq '^(AC|IT)_PROG_INTLTOOL' ${sub_configure_ac})
			then
				intltoolize --automake --copy --force
				__config_set with_intltool 1
			fi

			if $(grep -Eq '(A[CM]_PROG_LIBTOOL|LT_INIT|XCB_UTIL_COMMON)' ${sub_configure_ac})
			then
				__config_set with_libtool 1
			fi

			if $(grep -q '\./libtool -' ${sub_configure_ac}) && ! $(grep -q 'LT_OUTPUT' ${sub_configure_ac})
			then
				warning "${sub_configure_ac} may require LT_OUTPUT macro"
			fi

			# Automake will exit if these are not present
			# dodoc won't install these, however, since they will be empty
			touch ${d}/{AUTHORS,ChangeLog,NEWS,README};

			# These required files can be overwritten by automake, so we
			# back them up now and restore them afterwards
			for f in COPYING INSTALL
			do
				if [ -f ${d}/${f} ]
				then
					cp ${d}/${f} ${d}/${f}.temp;
					chmod +w ${d}/${f}.temp;
				elif [ -d ${d}/${f} ]
				then
					:
				else
					touch ${d}/${f}
				fi
			done
		fi
	done

	check_prog_req autoreconf autoconf

#****v* cygautoreconf/WANT_AUTOCONF
#  DESCRIPTION
#  This variable controls the version of autoconf to be used.  Valid settings
#  are "2.1" (for the 2.13 version), "2.5" (for the 2.5x/2.6x series) and "2.7"
#  (the default, for the 2.7x series).
#****

#****v* cygautoreconf/WANT_AUTOMAKE
#  DESCRIPTION
#  This variable controls the version of automake to be used.  Valid settings
#  are currently "1.4", "1.5" through to "1.16", etc.  If undefined, the version
#  used by the preexisting automake files will be used; if nonexistant (e.g. a
#  VCS checkout), the newest available automake will be used.
#****
	export WANT_AUTOCONF;
	export WANT_AUTOMAKE;

	case "${WANT_AUTOCONF}" in
	2.1)
		check_prog_req autoconf-2.13 autoconf2.1

		if __config_equals with_libtool 1
		then
			warning "libtool is incompatible with autoconf-2.13";
		fi
		;;
	2.5|2.7|'')
		case "${WANT_AUTOCONF}" in
		2.5)
			WANT_AUTOCONF=2.5
			case $(autoconf --version 2> /dev/null | head -n 1) in
			autoconf*2.[56]?) ;;
			*)	error "autoconf2.5 is required to build this package" ;;
			esac
			;;
		2.7|'')
			WANT_AUTOCONF=2.7
			case $(autoconf --version 2> /dev/null | head -n 1) in
			autoconf*2.[7]?) ;;
			*)	error "autoconf2.7 is required to build this package" ;;
			esac
		esac

		if __config_equals with_libtool 1
		then
			case $(automake --version 2> /dev/null | head -n 1) in
			automake*\ 1.9.*|automake*\ 1.1[0-9]*) ;;
			*)  inform "libtool requires at least automake 1.9; setting WANT_AUTOMAKE"
			    WANT_AUTOMAKE=1.9 ;;
			esac
		fi

#****v* cygautoreconf/NO_ACLOCAL
#  DESCRIPTION
#  Define this variable to a boolean true value to skip running aclocal
#  during cygautoreconf.
#  WARNING
#  This is strongly discouraged, and should only be used when a package uses
#  its own aclocal macros which are not provided in the tarball *and* the
#  package does not use libtool.
#****
#****v* cygautoreconf/NO_LIBTOOLIZE
#  DESCRIPTION
#  Define this variable to a boolean true value to skip running libtoolize
#  during cygautoreconf.
#  WARNING
#  This is strongly discouraged.  Cygwin's libtool usually includes patches
#  required for optimal results on Cygwin which are not yet available in
#  even the most recent upstream releases.
#****
#****v* cygautoreconf/NO_AUTOCONF
#  DESCRIPTION
#  Define this variable to a boolean true value to skip running autoconf
#  during cygautoreconf.
#  WARNING
#  This is strongly discouraged.  Running libtoolize, changes to aclocal macros,
#  or changes to configure.ac/configure.in all require autoconf to be run for
#  a working build.
#****
#****v* cygautoreconf/NO_AUTOHEADER
#  DESCRIPTION
#  Sometimes autoreconf thinks a package uses autoheader when in fact it does
#  not, in which case cygautoreconf will fail during the autoheader stage.
#  In such cases, provided you have not patched any AC_DEFINEs, set this
#  variable to a boolean true value to skip the autoheader step.
#****
#****v* cygautoreconf/NO_AUTOMAKE
#  DESCRIPTION
#  Define this variable to a boolean true value to skip running automake
#  during cygautoreconf.
#  WARNING
#  This is strongly discouraged.  If aclocal and/or autoconf have been run,
#  running automake is required for a working build.
#****
#****v* cygautoreconf/NO_AUTOPOINT
#  DESCRIPTION
#  Define this variable to a boolean true value to skip running autopoint
#  during cygautoreconf.
#  WARNING
#  This is strongly discouraged.  Cygwin's gettext package often has patches
#  which are not yet available in even the latest official release, and earlier
#  versions of gettext are incompatible with the current libtool.
#****

		ACLOCAL=aclocal
		# not all packages require gettext-devel
		AUTOPOINT=$(which autopoint 2>/dev/null || :)

		if cross_compiling || inherited toolchain
		then
			local ccsysroot=$(${CC} -print-sysroot)
			local sysrootgtize=${ccsysroot}$(__target_prefix)/bin/autopoint
			local sysrootltize=${ccsysroot}$(__target_prefix)/bin/libtoolize
			local sysrootaclocaldir=${ccsysroot}$(__target_prefix)/share/aclocal
			if [ -x ${sysrootltize} ]
			then
				LIBTOOLIZE=${sysrootltize}
			fi
			if [ -x ${sysrootgtize} ]
			then
				AUTOPOINT=${sysrootgtize}
			fi
		fi

		# libtool 2.2+ is incompatible with gettext before 0.17
		# config.rpath is broken wrt dynamic linkage before 0.17
		if defined AUTOPOINT
		then
			eval gettext_$(grep ^version= ${AUTOPOINT})
			if ! __version_at_least 0.17 ${gettext_version}
			then
				error "cygautoreconf: at least gettext 0.17 required"
			fi
			case ${gettext_version} in
			*.*.*.*) gettext_version=${gettext_version%.*} ;;
			esac
			AUTOPOINT+=" -V ${gettext_version}"
		fi

		for tool in ACLOCAL LIBTOOLIZE AUTOCONF AUTOHEADER AUTOPOINT AUTOMAKE
		do
			if __arg_bool NO_${tool}
			then
				declare ${tool}=true
				case ${tool} in
				LIBTOOLIZE) __config_set with_libtool 0 ;;
				*) __config_set with_${tool,,} 0 ;;
				esac
			fi
			export ${tool}
		done

#****v* cygautoreconf/ACLOCAL_FLAGS
#  DESCRIPTION
#  Set this variable when a package ships with its own aclocal macros but
#  their directory is not included when running aclocal during cygautoreconf.
#  If needed, usually in the form "-I DIR" (where DIR is the relative subdirectory
#  containing the aclocal macros, often m4/).
#****
		if defined ACLOCAL_FLAGS
		then
			ACLOCAL+=" ${ACLOCAL_FLAGS}"
		fi
		;;
	*)
		error "illegal WANT_AUTOCONF value: ${WANT_AUTOCONF}"
		;;
	esac

	for d in ${sysrootaclocaldir} ${ACLOCAL_PATH//:/ }
	do
		if [ -d $d ]
		then
			autoreconf_args+=" -I $d"
		fi
	done

	autoreconf --force -i --verbose ${autoreconf_args} || error "autoreconf failed"

	for d in . ${ac_subdirs}
	do
		if [ -d ${d} ]
		then
			if [ -f ${d}/configure.ac ]
			then
				sub_configure_ac="${d}/configure.ac";
			else
				sub_configure_ac="${d}/configure.in";
			fi

			auxdir="$(grep '^[^\#]*AC_CONFIG_AUX_DIR' ${sub_configure_ac} | sed -e 's!.*AC_CONFIG_AUX_DIR(\[*\(.*\))!\1!g' -e 's!\]*!!g')"
			config_h="$(grep 'A[CM]_CONFIG_HEADERS*' ${sub_configure_ac} | sed -e 's!A[CM]_CONFIG_HEADERS*(\[*\(.*\))!\1!g' -e 's!\]*!!g')";

			if defined config_h
			then
				__config_set with_autoheader 1
			fi

			case "${config_h}" in
				'') config_h_in= ;;
				*:*) config_h_in="$(echo "${config_h//:/ }" | cut -d ' ' -f 2-)" ;;
				*) config_h_in="${config_h}.in" ;;
			esac

			for f in ${config_h_in}
			do
				if [ -e ${d}/${f} ]
				then
					touch ${d}/${f}
				fi
			done

			if [ -f ${d}/${auxdir}/config.guess ]
			then
				gnuconfigize ${d}/${auxdir}
			fi

			for f in COPYING INSTALL
			do
				if [ -f ${d}/${f}.temp ]
				then
					mv -f ${d}/${f}.temp ${d}/${f};
				fi
			done
		fi
	done
}

#****C* autotools.cygclass/cygconf
#  SYNOPSIS
#  cygconf [CONFIGURE_FLAG] [CONFIGURE_FLAG] ...
#  DESCRIPTION
#  Runs the configure script for the package.  cygconf passes configure the flags
#  necessary to install the package into the /usr prefix and according to the
#  |html <a href="https://www.pathname.com/fhs/">Filesystem Hierarchy Standard</a> and the
#  |html <a href="https://cygwin.com/packaging-package-files.html#package_contents">Cygwin package guidelines</a>.
#  In addition, all arguments to cygconf are passed to configure, followed by
#  CYGCONF_ARGS, if set.
#  NOTES
#  * cygconf should be run in or under $B, as it defines the build directory of
#    the package and creates numerous files.
#  * If the configure script is not in $S, see CYGCONF_SOURCE.
#  * cygconf is intended for configure scripts generated by, or compatible with,
#    autoconf.  Packages with handwritten configure scripts may not accept all
#    the flags used by cygconf, in which case a direct call to the configure
#    script is in order.
#****
cygconf() {
	local ac_config_files;
	local ac_config_headers;
	local confargs;
	local confdir;
	local configure;
	local confver;
	local confver_maj;
	local confver_min;
	local f;
	local foo_config;
	local prefix;

#****v* cygconf/AUTOCONF_VERSION
#  DESCRIPTION
#  In some in some rare cases, cygport cannot automatically detect the autoconf
#  version that was used to generate the configure script (typically when
#  cygautoreconf is not being used, and the autoconfigury has been heavily
#  customized).  This variable can be set to indicate the autoconf version the
#  configure script's options are compatible with e.g. "2.50", "2.60", "2.70".
#****

#****v* cygconf/CYGCONF_SOURCE
#  DESCRIPTION
#  Set this variable to the directory containing the configure script.  This
#  is only necessary when configure is not in $S and cygconf is not being run
#  in the same subdirectory of $B which under $S contains configure.  (IOW if
#  the configure script is in $S/unix and cygconf is run from $B/unix, setting
#  CYGCONF_SOURCE would not be necessary.)
#****
	if defined CYGCONF_SOURCE
	then
		if [ -x ${CYGCONF_SOURCE}/configure ]
		then
			confdir=${CYGCONF_SOURCE};
		else
			error "CYGCONF_SOURCE defined but no configure script found"
		fi
	elif [ -x ${PWD/${B}/${S}}/configure ]
	then
		confdir=${PWD/${B}/${S}};
	elif [ -x ${S}/configure ]
	then
		confdir=${S};
	else
		error "no configure script found";
	fi

	configure="${confdir}/configure"
	if [ -z ${AUTOCONF_VERSION} ]
	then
		confver="$("$configure" --version | sed -rn 's/.*(GNU Autoconf|autoconf version) ([0-9\.]+).*/\2/p')"
	else
		confver=${AUTOCONF_VERSION}
	fi
	confver_maj=${confver%%.*}
	confver_min=${confver#*.}
	confver_min=${confver_min%%.*}
	if [ -z $confver_maj ]
	then
		error "could not determine the autoconf version used to generate ./configure; perhaps set AUTOCONF_VERSION?";
	fi
	if [ $confver_maj -ne 2 ]
	then
		error "unexpected autoconf version";
	fi

	# AC_CONFIG_FILES should not be dist'ed, but it sometimes happens anyway
	eval $(grep -h '^ac_config_files=' ${configure})
	eval $(grep -h '^ac_config_headers=' ${configure})

	for f in ${ac_config_files} ${ac_config_headers}
	do
		if [ -f ${confdir}/${f} ]
		then
			inform "Removing ${f} to be regenerated by configure"
			rm -f ${confdir}/${f}
			ln -sf ${confdir/${S}/${PWD}}/${f} ${confdir}/${f}
		fi
	done

	prefix=$(__host_prefix)

	# Set default configure arguments
	confargs="--prefix=${prefix} --exec-prefix=${prefix} \
		  --localstatedir=$(__host_localstatedir) \
		  --sysconfdir=$(__host_sysconfdir)"

	if inherited multilib
	then
		confargs+=" --libdir=${prefix}/${MULTILIB_LIBDIR}"
	fi

	if [ $confver_min -ge 60 ]
	then
		# Autoconf version supports --docdir and --htmldir, which will
		# need to be specified manually.  It also supports --infodir
		# and --mandir, but the defaults for those match the FHS.
		confargs+=" --docdir=/usr/share/doc/${PN} --htmldir=/usr/share/doc/${PN}/html"
	else
		# Autoconf version does not support --docdir or --htmldir, so
		# don't specify those.  Set --infodir and --mandir, as those
		# have defaults that don't match the FHS.
		confargs+=" --infodir=${prefix}/share/info --mandir=${prefix}/share/man"
	fi

	if [ $confver_min -ge 50 ]
	then
		# Always use a cache file; prior to 2.50, this was the default,
		# thereafter it needs to be requested explicitly.
		confargs+=" -C"
	fi

	if cross_compiling || inherited toolchain
	then
		confargs+=" --build=${CBUILD} --host=${CHOST}"
		if inherited toolchain
		then
			# AM_GNU_GETTEXT is used in subdirectories of the
			# cygnus tree but not in the top-level
			confargs+=" --target=${CTARGET} --without-libiconv-prefix --without-libintl-prefix"
		fi
	fi

	if cross_compiling
	then
		# make sure that PATH checks for foo-config scripts
		# do not detect the native ones in /usr/bin
		for foo_config in $(ls /usr/bin/*[-_]config 2>/dev/null)
		do
			case ${foo_config##*/} in pkg-config) continue ; esac
			export $(echo ${foo_config##*/} | tr "a-z+-\." "A-ZX__")=no
			export ac_cv_path_$(echo ${foo_config##*/} | tr "a-z+-\." "A-ZX__")=no
		done
		for foo_config in $(ls $(__host_prefix)/bin/*[-_]config 2>/dev/null)
		do
			export $(echo ${foo_config##*/} | tr "a-z+-\." "A-ZX__")=$foo_config
			export ac_cv_path_$(echo ${foo_config##*/} | tr "a-z+-\." "A-ZX__")=$foo_config
		done
	fi

	if __config_equals with_libtool 1 && ! __host_is_embedded
	then
		confargs+=" --enable-shared --disable-static"
	fi

	# workaround lib-link.m4:AC_LIB_HAVE_LINKFLAGS's insistence on
	# linking with rpath, and stick with -l flags instead
	for l in $(grep '^with_lib[^ ]*_prefix$' ${configure})
	do
		# make sure this is not just a custom AC_ARG_WITH;
		# AC_LIB_HAVE_LINKFLAGS uses LIBNAME_PREFIX= further on in
		if grep -q $(echo ${l#with_}= | tr '[:lower:]' '[:upper:]') ${configure}
		then
			confargs+=" --${l//_/-}=no"
		fi
	done

	# AC_HAVE_MMAP fails despite a working mmap, so we force this to yes
	# (see https://sourceware.org/legacy-ml/cygwin/2004-09/msg00741.html
	# and following thread for details)
	case ${CHOST} in
	*-*-cygwin*)
		export ac_cv_func_mmap_fixed_mapped=yes ;;
	esac

	# packages that use intltool w/o glib-gettext get this wrong
	export DATADIRNAME="share"

#****v* cygconf/CYGCONF_ARGS
#  DESCRIPTION
#  Additional flags to pass to configure, as a string.
#  NOTE
#  Flags in CYGCONF_ARGS follow, and therefore override, flags passed by default
#  and as arguments to cygconf.
#****

	verbose ${configure} \
		--srcdir="${confdir}" ${confargs} "${@}" ${CYGCONF_ARGS} \
		|| error "configure failed"
}

#****o* autotools.cygclass/src_compile (autotools)
#  DEFINITION
src_compile() {
	cd ${S}
	cygautoreconf
	cd ${B}
	cygconf
	cygmake
}
#****

if ! defined _autotools_CYGCLASS_stage1_
then
	readonly -f cygautoreconf cygconf gnuconfigize __parse_aclocal_amflags
fi
