1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: autotools.eclass
6 # base-system@gentoo.org
7 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
8 # @BLURB: Regenerates auto* build scripts
10 # This eclass is for safely handling autotooled software packages that need to
11 # regenerate their build scripts. All functions will abort in case of errors.
13 # Note: We require GNU m4, as does autoconf. So feel free to use any features
14 # from the GNU version of m4 without worrying about other variants (i.e. BSD).
16 if [[ ${__AUTOTOOLS_AUTO_DEPEND+set} == "set" ]] ; then
17 # See if we were included already, but someone changed the value
18 # of AUTOTOOLS_AUTO_DEPEND on us. We could reload the entire
19 # eclass at that point, but that adds overhead, and it's trivial
20 # to re-order inherit in eclasses/ebuilds instead. #409611
21 if [[ ${__AUTOTOOLS_AUTO_DEPEND} != ${AUTOTOOLS_AUTO_DEPEND} ]] ; then
22 die "AUTOTOOLS_AUTO_DEPEND changed value between inherits; please inherit autotools.eclass first! ${__AUTOTOOLS_AUTO_DEPEND} -> ${AUTOTOOLS_AUTO_DEPEND}"
26 if [[ -z ${_AUTOTOOLS_ECLASS} ]]; then
31 *) die "${ECLASS}: EAPI ${EAPI} not supported" ;;
36 # @ECLASS-VARIABLE: WANT_AUTOCONF
38 # The major version of autoconf your package needs
39 : ${WANT_AUTOCONF:=latest}
41 # @ECLASS-VARIABLE: WANT_AUTOMAKE
43 # The major version of automake your package needs
44 : ${WANT_AUTOMAKE:=latest}
46 # @ECLASS-VARIABLE: WANT_LIBTOOL
48 # Do you want libtool? Valid values here are "latest" and "none".
49 : ${WANT_LIBTOOL:=latest}
51 # @ECLASS-VARIABLE: _LATEST_AUTOMAKE
55 # The latest major unstable and stable version/slot of automake available
57 # List latest unstable version first to boost testing adoption rate because
58 # most package manager dependency resolver will pick the first suitable
60 # If a newer slot is stable on any arch, and is NOT reflected in this list,
61 # then circular dependencies may arise during emerge @system bootstraps.
63 # See bug 312315 and 465732 for further information and context.
65 # Do NOT change this variable in your ebuilds!
66 # If you want to force a newer minor version, you can specify the correct
67 # WANT value by using a colon: <PV>:<WANT_AUTOMAKE>
68 _LATEST_AUTOMAKE=( 1.16.1:1.16 1.15.1:1.15 )
70 _automake_atom="sys-devel/automake"
71 _autoconf_atom="sys-devel/autoconf"
72 if [[ -n ${WANT_AUTOMAKE} ]]; then
73 case ${WANT_AUTOMAKE} in
74 # Even if the package doesn't use automake, we still need to depend
75 # on it because we run aclocal to process m4 macros. This matches
76 # the autoreconf tool, so this requirement is correct. #401605
79 # Use SLOT deps if we can. For EAPI=0, we get pretty close.
80 if [[ ${EAPI:-0} != 0 ]] ; then
81 _automake_atom="|| ( `printf '>=sys-devel/automake-%s:%s ' ${_LATEST_AUTOMAKE[@]/:/ }` )"
83 _automake_atom="|| ( `printf '>=sys-devel/automake-%s ' ${_LATEST_AUTOMAKE[@]/%:*}` )"
86 *) _automake_atom="=sys-devel/automake-${WANT_AUTOMAKE}*" ;;
91 if [[ -n ${WANT_AUTOCONF} ]] ; then
92 case ${WANT_AUTOCONF} in
93 none) _autoconf_atom="" ;; # some packages don't require autoconf at all
94 2.1) _autoconf_atom="~sys-devel/autoconf-2.13" ;;
95 # if you change the "latest" version here, change also autotools_env_setup
96 latest|2.5) _autoconf_atom=">=sys-devel/autoconf-2.69" ;;
97 *) die "Invalid WANT_AUTOCONF value '${WANT_AUTOCONF}'" ;;
102 _libtool_atom=">=sys-devel/libtool-2.4"
103 if [[ -n ${WANT_LIBTOOL} ]] ; then
104 case ${WANT_LIBTOOL} in
105 none) _libtool_atom="" ;;
107 *) die "Invalid WANT_LIBTOOL value '${WANT_LIBTOOL}'" ;;
112 # Force people (nicely) to upgrade to a newer version of gettext as
113 # older ones are known to be crappy. #496454
114 AUTOTOOLS_DEPEND="!<sys-devel/gettext-0.18.1.1-r3
120 # @ECLASS-VARIABLE: AUTOTOOLS_AUTO_DEPEND
122 # Set to 'no' to disable automatically adding to DEPEND. This lets
123 # ebuilds form conditional depends by using ${AUTOTOOLS_DEPEND} in
124 # their own DEPEND string.
125 : ${AUTOTOOLS_AUTO_DEPEND:=yes}
126 if [[ ${AUTOTOOLS_AUTO_DEPEND} != "no" ]] ; then
128 0|1|2|3|4|5|6) DEPEND=${AUTOTOOLS_DEPEND} ;;
129 7) BDEPEND=${AUTOTOOLS_DEPEND} ;;
132 __AUTOTOOLS_AUTO_DEPEND=${AUTOTOOLS_AUTO_DEPEND} # See top of eclass
134 unset _automake_atom _autoconf_atom
136 # @ECLASS-VARIABLE: AM_OPTS
139 # Additional options to pass to automake during
142 # @ECLASS-VARIABLE: AT_NOEAUTOMAKE
145 # Don't run eautomake command if set to 'yes'; only used to workaround
146 # broken packages. Generally you should, instead, fix the package to
147 # not call AM_INIT_AUTOMAKE if it doesn't actually use automake.
149 # @ECLASS-VARIABLE: AT_NOELIBTOOLIZE
152 # Don't run elibtoolize command if set to 'yes',
153 # useful when elibtoolize needs to be ran with
156 # @ECLASS-VARIABLE: AT_M4DIR
158 # Additional director(y|ies) aclocal should search
161 # @ECLASS-VARIABLE: AT_SYS_M4DIR
164 # For system integrators, a list of additional aclocal search paths.
165 # This variable gets eval-ed, so you can use variables in the definition
166 # that may not be valid until eautoreconf & friends are run.
169 # @FUNCTION: eautoreconf
171 # This function mimes the behavior of autoreconf, but uses the different
172 # eauto* functions to run the tools. It doesn't accept parameters, but
173 # the directory with include files can be specified with AT_M4DIR variable.
175 # Should do a full autoreconf - normally what most people will be interested in.
176 # Also should handle additional directories specified by AC_CONFIG_SUBDIRS.
180 # Subdirs often share a common build dir #529404. If so, we can't safely
181 # run in parallel because many tools clobber the content in there. Libtool
182 # and automake both `rm && cp` while aclocal reads the output. We might be
183 # able to handle this if we split the steps and grab locks on the dirs the
184 # tools actually write to. Then we'd run all the common tools that use
185 # those inputs. Doing this in bash does not scale easily.
186 # If we do re-enable parallel support, make sure #426512 is handled.
187 if [[ -z ${AT_NO_RECURSIVE} ]] ; then
188 # Take care of subdirs
189 for x in $(autotools_check_macro_val AC_CONFIG_SUBDIRS) ; do
190 if [[ -d ${x} ]] ; then
191 pushd "${x}" >/dev/null
192 # Avoid unsafe nested multijob_finish_one for bug #426512.
193 AT_NOELIBTOOLIZE="yes" eautoreconf || die
199 einfo "Running eautoreconf in '${PWD}' ..."
201 local m4dirs=$(autotools_check_macro_val AC_CONFIG_{AUX,MACRO}_DIR)
202 [[ -n ${m4dirs} ]] && mkdir -p ${m4dirs}
204 # Run all the tools before aclocal so we can gather the .m4 files.
206 # <tool> <was run> <command>
207 glibgettext false "autotools_run_tool glib-gettextize --copy --force"
208 gettext false "autotools_run_tool --at-missing autopoint --force"
209 # intltool must come after autopoint.
210 intltool false "autotools_run_tool intltoolize --automake --copy --force"
211 gtkdoc false "autotools_run_tool --at-missing gtkdocize --copy"
212 gnomedoc false "autotools_run_tool --at-missing gnome-doc-prepare --copy --force"
213 libtool false "_elibtoolize --auto-ltdl --install --copy --force"
215 for (( i = 0; i < ${#tools[@]}; i += 3 )) ; do
216 if _at_uses_${tools[i]} ; then
222 # Generate aclocal.m4 with our up-to-date m4 files.
223 local rerun_aclocal=false
226 # Check to see if we had macros expanded by other macros or in other
227 # m4 files that we couldn't detect early. This is uncommon, but some
228 # packages do this, so we have to handle it correctly.
229 for (( i = 0; i < ${#tools[@]}; i += 3 )) ; do
230 if ! ${tools[i+1]} && _at_uses_${tools[i]} ; then
235 ${rerun_aclocal} && eaclocal
237 if [[ ${WANT_AUTOCONF} = 2.1 ]] ; then
243 [[ ${AT_NOEAUTOMAKE} != "yes" ]] && FROM_EAUTORECONF="yes" eautomake ${AM_OPTS}
245 if [[ ${AT_NOELIBTOOLIZE} != "yes" ]] ; then
246 # Call it here to prevent failures due to elibtoolize called _before_
248 elibtoolize --force "${PWD}"
254 # @FUNCTION: _at_uses_pkg
257 # See if the specified macros are enabled.
259 if [[ -n $(autotools_check_macro "$@") ]] ; then
262 # If the trace didn't find it (perhaps because aclocal.m4 hasn't
263 # been generated yet), cheat, but be conservative.
266 args+=( -e "^[[:space:]]*${macro}\>" )
268 egrep -q "${args[@]}" configure.??
271 _at_uses_autoheader() { _at_uses_pkg A{C,M}_CONFIG_HEADER{S,}; }
272 _at_uses_automake() { _at_uses_pkg AM_INIT_AUTOMAKE; }
273 _at_uses_gettext() { _at_uses_pkg AM_GNU_GETTEXT_{,REQUIRE_}VERSION; }
274 _at_uses_glibgettext() { _at_uses_pkg AM_GLIB_GNU_GETTEXT; }
275 _at_uses_intltool() { _at_uses_pkg {AC,IT}_PROG_INTLTOOL; }
276 _at_uses_gtkdoc() { _at_uses_pkg GTK_DOC_CHECK; }
277 _at_uses_gnomedoc() { _at_uses_pkg GNOME_DOC_INIT; }
278 _at_uses_libtool() { _at_uses_pkg A{C,M}_PROG_LIBTOOL LT_INIT; }
279 _at_uses_libltdl() { _at_uses_pkg LT_CONFIG_LTDL_DIR; }
281 # @FUNCTION: eaclocal_amflags
283 # Extract the ACLOCAL_AMFLAGS value from the Makefile.am and try to handle
284 # (most) of the crazy crap that people throw at us.
286 local aclocal_opts amflags_file
288 for amflags_file in GNUmakefile.am Makefile.am GNUmakefile.in Makefile.in ; do
289 [[ -e ${amflags_file} ]] || continue
290 # setup the env in case the pkg does something crazy
291 # in their ACLOCAL_AMFLAGS. like run a shell script
292 # which turns around and runs autotools. #365401
293 # or split across multiple lines. #383525
295 aclocal_opts=$(sed -n \
296 "/^ACLOCAL_AMFLAGS[[:space:]]*=/{ \
297 # match the first line
299 # then gobble up all escaped lines
300 : nextline /\\\\$/{ n; p; b nextline; } \
302 eval aclocal_opts=\""${aclocal_opts}"\"
309 # @FUNCTION: eaclocal
311 # These functions runs the autotools using autotools_run_tool with the
312 # specified parametes. The name of the tool run is the same of the function
314 # They also force installing the support files for safety.
315 # Respects AT_M4DIR for additional directories to search for macro's.
317 [[ ! -f aclocal.m4 || -n $(grep -e 'generated.*by aclocal' aclocal.m4) ]] && \
318 autotools_run_tool --at-m4flags aclocal "$@" $(eaclocal_amflags)
321 # @FUNCTION: _elibtoolize
325 # Note the '_' prefix: avoid collision with elibtoolize() from libtool.eclass.
327 local LIBTOOLIZE=${LIBTOOLIZE:-$(type -P glibtoolize > /dev/null && echo glibtoolize || echo libtoolize)}
329 if [[ $1 == "--auto-ltdl" ]] ; then
331 _at_uses_libltdl && set -- "$@" --ltdl
334 [[ -f GNUmakefile.am || -f Makefile.am ]] && set -- "$@" --automake
336 autotools_run_tool ${LIBTOOLIZE} "$@"
339 # @FUNCTION: eautoheader
343 _at_uses_autoheader || return 0
344 autotools_run_tool --at-no-fail --at-m4flags autoheader "$@"
347 # @FUNCTION: eautoconf
351 if [[ ! -f configure.ac && ! -f configure.in ]] ; then
353 eerror "No configure.{ac,in} present in '${PWD}'!"
355 die "No configure.{ac,in} present!"
357 if [[ ${WANT_AUTOCONF} != "2.1" && -e configure.in ]] ; then
358 eqawarn "This package has a configure.in file which has long been deprecated. Please"
359 eqawarn "update it to use configure.ac instead as newer versions of autotools will die"
360 eqawarn "when it finds this file. See https://bugs.gentoo.org/426262 for details."
363 autotools_run_tool --at-m4flags autoconf "$@"
366 # @FUNCTION: eautomake
374 # - a Makefile.am type file exists
375 # - the configure script is using the AM_INIT_AUTOMAKE directive
376 for makefile_name in {GNUmakefile,{M,m}akefile}.am "" ; do
377 [[ -f ${makefile_name} ]] && break
380 _automake_version() {
381 autotools_run_tool --at-output automake --version 2>/dev/null |
382 sed -n -e '1{s:.*(GNU automake) ::p;q}'
385 if [[ -z ${makefile_name} ]] ; then
386 _at_uses_automake || return 0
388 elif [[ -z ${FROM_EAUTORECONF} && -f ${makefile_name%.am}.in ]]; then
390 local installed_automake
392 installed_automake=$(WANT_AUTOMAKE= _automake_version)
393 used_automake=$(head -n 1 < ${makefile_name%.am}.in | \
394 sed -e 's:.*by automake \(.*\) from .*:\1:')
396 if [[ ${installed_automake} != ${used_automake} ]]; then
397 ewarn "Automake used for the package (${used_automake}) differs from" \
398 "the installed version (${installed_automake})."
399 ewarn "Forcing a full rebuild of the autotools to workaround."
405 [[ -f INSTALL && -f AUTHORS && -f ChangeLog && -f NEWS && -f README ]] \
406 || extra_opts+=( --foreign )
408 # Older versions of automake do not support --force-missing. But we want
409 # to use this whenever possible to update random bundled files #133489.
410 case $(_automake_version) in
412 *) extra_opts+=( --force-missing ) ;;
415 autotools_run_tool automake --add-missing --copy "${extra_opts[@]}" "$@"
418 # @FUNCTION: eautopoint
420 # Runs autopoint (from the gettext package).
422 autotools_run_tool autopoint "$@"
425 # @FUNCTION: config_rpath_update
426 # @USAGE: [destination]
428 # Some packages utilize the config.rpath helper script, but don't
429 # use gettext directly. So we have to copy it in manually since
430 # we can't let `autopoint` do it for us.
431 config_rpath_update() {
432 local dst src=$(type -P gettext | sed 's:bin/gettext:share/gettext/config.rpath:')
434 [[ $# -eq 0 ]] && set -- $(find -name config.rpath)
435 [[ $# -eq 0 ]] && return 0
437 einfo "Updating all config.rpath files"
440 cp "${src}" "${dst}" || die
444 # @FUNCTION: autotools_env_setup
447 # Process the WANT_AUTO{CONF,MAKE} flags.
448 autotools_env_setup() {
449 # We do the "latest" → version switch here because it solves
450 # possible order problems, see bug #270010 as an example.
451 if [[ ${WANT_AUTOMAKE} == "latest" ]]; then
453 for pv in ${_LATEST_AUTOMAKE[@]/#*:} ; do
454 # Break on first hit to respect _LATEST_AUTOMAKE order.
458 hv_args="--host-root"
464 ROOT=/ has_version ${hv_args} "=sys-devel/automake-${pv}*" && export WANT_AUTOMAKE="${pv}" && break
466 [[ ${WANT_AUTOMAKE} == "latest" ]] && \
467 die "Cannot find the latest automake! Tried ${_LATEST_AUTOMAKE[*]}"
469 [[ ${WANT_AUTOCONF} == "latest" ]] && export WANT_AUTOCONF=2.5
472 # @FUNCTION: autotools_run_tool
473 # @USAGE: [--at-no-fail] [--at-m4flags] [--at-missing] [--at-output] <autotool> [tool-specific flags]
476 # Run the specified autotool helper, but do logging and error checking
477 # around it in the process.
478 autotools_run_tool() {
479 # Process our own internal flags first
480 local autofail=true m4flags=false missing_ok=false return_output=false
481 while [[ -n $1 ]] ; do
483 --at-no-fail) autofail=false;;
484 --at-m4flags) m4flags=true;;
485 --at-missing) missing_ok=true;;
486 --at-output) return_output=true;;
487 # whatever is left goes to the actual tool
493 if [[ ${EBUILD_PHASE} != "unpack" && ${EBUILD_PHASE} != "prepare" ]]; then
494 ewarn "QA Warning: running $1 in ${EBUILD_PHASE} phase"
497 if ${missing_ok} && ! type -P ${1} >/dev/null ; then
498 einfo "Skipping '$*' due $1 not installed"
504 # Allow people to pass in full paths. #549268
505 local STDERR_TARGET="${T}/${1##*/}.out"
506 # most of the time, there will only be one run, but if there are
507 # more, make sure we get unique log filenames
508 if [[ -e ${STDERR_TARGET} ]] ; then
511 STDERR_TARGET="${T}/${1##*/}-${i}.out"
512 [[ -e ${STDERR_TARGET} ]] || break
518 set -- "${1}" $(autotools_m4dir_include) "${@:2}" $(autotools_m4sysdir_include)
521 # If the caller wants to probe something, then let them do it directly.
522 if ${return_output} ; then
527 printf "***** $1 *****\n***** PWD: ${PWD}\n***** $*\n\n" > "${STDERR_TARGET}"
530 "$@" >> "${STDERR_TARGET}" 2>&1
531 if ! eend $? && ${autofail} ; then
533 eerror "Failed Running $1 !"
535 eerror "Include in your bugreport the contents of:"
537 eerror " ${STDERR_TARGET}"
539 die "Failed Running $1 !"
543 # Internal function to check for support
545 # Keep a list of all the macros we might use so that we only
546 # have to run the trace code once. Order doesn't matter.
547 ALL_AUTOTOOLS_MACROS=(
548 A{C,M}_PROG_LIBTOOL LT_INIT LT_CONFIG_LTDL_DIR
549 A{C,M}_CONFIG_HEADER{S,}
551 AC_CONFIG_AUX_DIR AC_CONFIG_MACRO_DIR
554 AM_GNU_GETTEXT_{,REQUIRE_}VERSION
555 {AC,IT}_PROG_INTLTOOL
559 autotools_check_macro() {
560 [[ -f configure.ac || -f configure.in ]] || return 0
562 # We can run in multiple dirs, so we have to cache the trace
563 # data in $PWD rather than an env var.
564 local trace_file=".__autoconf_trace_data"
565 if [[ ! -e ${trace_file} ]] || [[ ! aclocal.m4 -ot ${trace_file} ]] ; then
566 WANT_AUTOCONF="2.5" autoconf \
567 $(autotools_m4dir_include) \
568 ${ALL_AUTOTOOLS_MACROS[@]/#/--trace=} > ${trace_file} 2>/dev/null
573 has ${macro} ${ALL_AUTOTOOLS_MACROS[@]} || die "internal error: add ${macro} to ALL_AUTOTOOLS_MACROS"
574 args+=( -e ":${macro}:" )
576 grep "${args[@]}" ${trace_file}
579 # @FUNCTION: autotools_check_macro_val
580 # @USAGE: <macro> [macros]
583 # Look for a macro and extract its value.
584 autotools_check_macro_val() {
588 autotools_check_macro "${macro}" | \
589 gawk -v macro="${macro}" \
590 '($0 !~ /^[[:space:]]*(#|dnl)/) {
591 if (match($0, macro ":(.*)$", res))
599 _autotools_m4dir_include() {
600 local x include_opts flag
602 # Use the right flag to autoconf based on the version #448986
603 [[ ${WANT_AUTOCONF} == "2.1" ]] \
612 [[ ! -d ${x} ]] && ewarn "autotools.eclass: '${x}' does not exist"
613 include_opts+=" -${flag} ${x}"
620 autotools_m4dir_include() { _autotools_m4dir_include ${AT_M4DIR} ; }
621 autotools_m4sysdir_include() {
622 # First try to use the paths the system integrator has set up.
623 local paths=( $(eval echo ${AT_SYS_M4DIR}) )
625 if [[ ${#paths[@]} -eq 0 && -n ${SYSROOT} ]] ; then
626 # If they didn't give us anything, then default to the SYSROOT.
627 # This helps when cross-compiling.
628 local path="${SYSROOT}/usr/share/aclocal"
629 [[ -d ${path} ]] && paths+=( "${path}" )
631 _autotools_m4dir_include "${paths[@]}"