1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: autotools.eclass
7 # base-system@gentoo.org
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 # @ECLASS-VARIABLE: WANT_AUTOCONF
33 # The major version of autoconf your package needs
34 : ${WANT_AUTOCONF:=latest}
36 # @ECLASS-VARIABLE: WANT_AUTOMAKE
38 # The major version of automake your package needs
39 : ${WANT_AUTOMAKE:=latest}
41 # @ECLASS-VARIABLE: WANT_LIBTOOL
43 # Do you want libtool? Valid values here are "latest" and "none".
44 : ${WANT_LIBTOOL:=latest}
46 # @ECLASS-VARIABLE: _LATEST_AUTOMAKE
50 # The latest major version/slot of automake available on each arch. #312315
51 # We should list both the latest stable, and the latest unstable. #465732
52 # This way the stable builds will still work, but the unstable are allowed
53 # to build & test things for us ahead of time (if they have it installed).
54 # If a newer slot is stable on any arch, and is NOT reflected in this list,
55 # then circular dependencies may arise during emerge @system bootstraps.
56 # Do NOT change this variable in your ebuilds!
57 # If you want to force a newer minor version, you can specify the correct
58 # WANT value by using a colon: <PV>:<WANT_AUTOMAKE>
59 _LATEST_AUTOMAKE=( 1.15:1.15 )
61 _automake_atom="sys-devel/automake"
62 _autoconf_atom="sys-devel/autoconf"
63 if [[ -n ${WANT_AUTOMAKE} ]]; then
64 case ${WANT_AUTOMAKE} in
65 # Even if the package doesn't use automake, we still need to depend
66 # on it because we run aclocal to process m4 macros. This matches
67 # the autoreconf tool, so this requirement is correct. #401605
70 # Use SLOT deps if we can. For EAPI=0, we get pretty close.
71 if [[ ${EAPI:-0} != 0 ]] ; then
72 _automake_atom="|| ( `printf '>=sys-devel/automake-%s:%s ' ${_LATEST_AUTOMAKE[@]/:/ }` )"
74 _automake_atom="|| ( `printf '>=sys-devel/automake-%s ' ${_LATEST_AUTOMAKE[@]/%:*}` )"
77 *) _automake_atom="=sys-devel/automake-${WANT_AUTOMAKE}*" ;;
82 if [[ -n ${WANT_AUTOCONF} ]] ; then
83 case ${WANT_AUTOCONF} in
84 none) _autoconf_atom="" ;; # some packages don't require autoconf at all
85 2.1) _autoconf_atom="=sys-devel/autoconf-${WANT_AUTOCONF}*" ;;
86 # if you change the "latest" version here, change also autotools_env_setup
87 latest|2.5) _autoconf_atom=">=sys-devel/autoconf-2.69" ;;
88 *) die "Invalid WANT_AUTOCONF value '${WANT_AUTOCONF}'" ;;
93 _libtool_atom=">=sys-devel/libtool-2.4"
94 if [[ -n ${WANT_LIBTOOL} ]] ; then
95 case ${WANT_LIBTOOL} in
96 none) _libtool_atom="" ;;
98 *) die "Invalid WANT_LIBTOOL value '${WANT_LIBTOOL}'" ;;
103 # Force people (nicely) to upgrade to a newer version of gettext as
104 # older ones are known to be crappy. #496454
105 AUTOTOOLS_DEPEND="!<sys-devel/gettext-0.18.1.1-r3
111 # @ECLASS-VARIABLE: AUTOTOOLS_AUTO_DEPEND
113 # Set to 'no' to disable automatically adding to DEPEND. This lets
114 # ebuilds former conditional depends by using ${AUTOTOOLS_DEPEND} in
115 # their own DEPEND string.
116 : ${AUTOTOOLS_AUTO_DEPEND:=yes}
117 if [[ ${AUTOTOOLS_AUTO_DEPEND} != "no" ]] ; then
118 DEPEND=${AUTOTOOLS_DEPEND}
120 __AUTOTOOLS_AUTO_DEPEND=${AUTOTOOLS_AUTO_DEPEND} # See top of eclass
122 unset _automake_atom _autoconf_atom
124 # @ECLASS-VARIABLE: AM_OPTS
127 # Additional options to pass to automake during
130 # @ECLASS-VARIABLE: AT_NOEAUTOMAKE
133 # Don't run eautomake command if set to 'yes'; only used to workaround
134 # broken packages. Generally you should, instead, fix the package to
135 # not call AM_INIT_AUTOMAKE if it doesn't actually use automake.
137 # @ECLASS-VARIABLE: AT_NOELIBTOOLIZE
140 # Don't run elibtoolize command if set to 'yes',
141 # useful when elibtoolize needs to be ran with
144 # @ECLASS-VARIABLE: AT_M4DIR
146 # Additional director(y|ies) aclocal should search
149 # @ECLASS-VARIABLE: AT_SYS_M4DIR
152 # For system integrators, a list of additional aclocal search paths.
153 # This variable gets eval-ed, so you can use variables in the definition
154 # that may not be valid until eautoreconf & friends are run.
157 # @FUNCTION: eautoreconf
159 # This function mimes the behavior of autoreconf, but uses the different
160 # eauto* functions to run the tools. It doesn't accept parameters, but
161 # the directory with include files can be specified with AT_M4DIR variable.
163 # Should do a full autoreconf - normally what most people will be interested in.
164 # Also should handle additional directories specified by AC_CONFIG_SUBDIRS.
168 # Subdirs often share a common build dir #529404. If so, we can't safely
169 # run in parallel because many tools clobber the content in there. Libtool
170 # and automake both `rm && cp` while aclocal reads the output. We might be
171 # able to handle this if we split the steps and grab locks on the dirs the
172 # tools actually write to. Then we'd run all the common tools that use
173 # those inputs. Doing this in bash does not scale easily.
174 # If we do re-enable parallel support, make sure #426512 is handled.
175 if [[ -z ${AT_NO_RECURSIVE} ]] ; then
176 # Take care of subdirs
177 for x in $(autotools_check_macro_val AC_CONFIG_SUBDIRS) ; do
178 if [[ -d ${x} ]] ; then
179 pushd "${x}" >/dev/null
180 # Avoid unsafe nested multijob_finish_one for bug #426512.
181 AT_NOELIBTOOLIZE="yes" eautoreconf || die
187 einfo "Running eautoreconf in '${PWD}' ..."
189 local m4dirs=$(autotools_check_macro_val AC_CONFIG_{AUX,MACRO}_DIR)
190 [[ -n ${m4dirs} ]] && mkdir -p ${m4dirs}
192 # Run all the tools before aclocal so we can gather the .m4 files.
194 # <tool> <was run> <command>
195 glibgettext false "autotools_run_tool glib-gettextize --copy --force"
196 gettext false "autotools_run_tool --at-missing autopoint --force"
197 # intltool must come after autopoint.
198 intltool false "autotools_run_tool intltoolize --automake --copy --force"
199 gtkdoc false "autotools_run_tool --at-missing gtkdocize --copy"
200 gnomedoc false "autotools_run_tool --at-missing gnome-doc-prepare --copy --force"
201 libtool false "_elibtoolize --install --copy --force"
203 for (( i = 0; i < ${#tools[@]}; i += 3 )) ; do
204 if _at_uses_${tools[i]} ; then
210 # Generate aclocal.m4 with our up-to-date m4 files.
211 local rerun_aclocal=false
214 # Check to see if we had macros expanded by other macros or in other
215 # m4 files that we couldn't detect early. This is uncommon, but some
216 # packages do this, so we have to handle it correctly.
217 for (( i = 0; i < ${#tools[@]}; i += 3 )) ; do
218 if ! ${tools[i+1]} && _at_uses_${tools[i]} ; then
223 ${rerun_aclocal} && eaclocal
225 if [[ ${WANT_AUTOCONF} = 2.1 ]] ; then
231 [[ ${AT_NOEAUTOMAKE} != "yes" ]] && FROM_EAUTORECONF="yes" eautomake ${AM_OPTS}
233 if [[ ${AT_NOELIBTOOLIZE} != "yes" ]] ; then
234 # Call it here to prevent failures due to elibtoolize called _before_
236 elibtoolize --force "${PWD}"
242 # @FUNCTION: _at_uses_pkg
245 # See if the specified macros are enabled.
247 if [[ -n $(autotools_check_macro "$@") ]] ; then
250 # If the trace didn't find it (perhaps because aclocal.m4 hasn't
251 # been generated yet), cheat, but be conservative.
254 args+=( -e "^[[:space:]]*${macro}\>" )
256 egrep -q "${args[@]}" configure.??
259 _at_uses_autoheader() { _at_uses_pkg A{C,M}_CONFIG_HEADER{S,}; }
260 _at_uses_automake() { _at_uses_pkg AM_INIT_AUTOMAKE; }
261 _at_uses_gettext() { _at_uses_pkg AM_GNU_GETTEXT_VERSION; }
262 _at_uses_glibgettext() { _at_uses_pkg AM_GLIB_GNU_GETTEXT; }
263 _at_uses_intltool() { _at_uses_pkg {AC,IT}_PROG_INTLTOOL; }
264 _at_uses_gtkdoc() { _at_uses_pkg GTK_DOC_CHECK; }
265 _at_uses_gnomedoc() { _at_uses_pkg GNOME_DOC_INIT; }
266 _at_uses_libtool() { _at_uses_pkg A{C,M}_PROG_LIBTOOL LT_INIT; }
268 # @FUNCTION: eaclocal_amflags
270 # Extract the ACLOCAL_AMFLAGS value from the Makefile.am and try to handle
271 # (most) of the crazy crap that people throw at us.
273 local aclocal_opts amflags_file
275 for amflags_file in GNUmakefile.am Makefile.am GNUmakefile.in Makefile.in ; do
276 [[ -e ${amflags_file} ]] || continue
277 # setup the env in case the pkg does something crazy
278 # in their ACLOCAL_AMFLAGS. like run a shell script
279 # which turns around and runs autotools. #365401
280 # or split across multiple lines. #383525
282 aclocal_opts=$(sed -n \
283 "/^ACLOCAL_AMFLAGS[[:space:]]*=/{ \
284 # match the first line
286 # then gobble up all escaped lines
287 : nextline /\\\\$/{ n; p; b nextline; } \
289 eval aclocal_opts=\""${aclocal_opts}"\"
296 # @FUNCTION: eaclocal
298 # These functions runs the autotools using autotools_run_tool with the
299 # specified parametes. The name of the tool run is the same of the function
301 # They also force installing the support files for safety.
302 # Respects AT_M4DIR for additional directories to search for macro's.
304 [[ ! -f aclocal.m4 || -n $(grep -e 'generated.*by aclocal' aclocal.m4) ]] && \
305 autotools_run_tool --at-m4flags aclocal "$@" $(eaclocal_amflags)
308 # @FUNCTION: _elibtoolize
312 # Note the '_' prefix: avoid collision with elibtoolize() from libtool.eclass.
314 local LIBTOOLIZE=${LIBTOOLIZE:-$(type -P glibtoolize > /dev/null && echo glibtoolize || echo libtoolize)}
316 [[ -f GNUmakefile.am || -f Makefile.am ]] && set -- "$@" --automake
318 autotools_run_tool ${LIBTOOLIZE} "$@"
321 # @FUNCTION: eautoheader
325 _at_uses_autoheader || return 0
326 autotools_run_tool --at-no-fail --at-m4flags autoheader "$@"
329 # @FUNCTION: eautoconf
333 if [[ ! -f configure.ac && ! -f configure.in ]] ; then
335 eerror "No configure.{ac,in} present in '${PWD}'!"
337 die "No configure.{ac,in} present!"
339 if [[ ${WANT_AUTOCONF} != "2.1" && -e configure.in ]] ; then
340 eqawarn "This package has a configure.in file which has long been deprecated. Please"
341 eqawarn "update it to use configure.ac instead as newer versions of autotools will die"
342 eqawarn "when it finds this file. See https://bugs.gentoo.org/426262 for details."
345 autotools_run_tool --at-m4flags autoconf "$@"
348 # @FUNCTION: eautomake
356 # - a Makefile.am type file exists
357 # - the configure script is using the AM_INIT_AUTOMAKE directive
358 for makefile_name in {GNUmakefile,{M,m}akefile}.am "" ; do
359 [[ -f ${makefile_name} ]] && break
362 _automake_version() {
363 autotools_run_tool --at-output automake --version 2>/dev/null |
364 sed -n -e '1{s:.*(GNU automake) ::p;q}'
367 if [[ -z ${makefile_name} ]] ; then
368 _at_uses_automake || return 0
370 elif [[ -z ${FROM_EAUTORECONF} && -f ${makefile_name%.am}.in ]]; then
372 local installed_automake
374 installed_automake=$(WANT_AUTOMAKE= _automake_version)
375 used_automake=$(head -n 1 < ${makefile_name%.am}.in | \
376 sed -e 's:.*by automake \(.*\) from .*:\1:')
378 if [[ ${installed_automake} != ${used_automake} ]]; then
379 ewarn "Automake used for the package (${used_automake}) differs from" \
380 "the installed version (${installed_automake})."
381 ewarn "Forcing a full rebuild of the autotools to workaround."
387 [[ -f INSTALL && -f AUTHORS && -f ChangeLog && -f NEWS && -f README ]] \
388 || extra_opts+=( --foreign )
390 # Older versions of automake do not support --force-missing. But we want
391 # to use this whenever possible to update random bundled files #133489.
392 case $(_automake_version) in
394 *) extra_opts+=( --force-missing ) ;;
397 autotools_run_tool automake --add-missing --copy "${extra_opts[@]}" "$@"
400 # @FUNCTION: eautopoint
402 # Runs autopoint (from the gettext package).
404 autotools_run_tool autopoint "$@"
407 # @FUNCTION: config_rpath_update
408 # @USAGE: [destination]
410 # Some packages utilize the config.rpath helper script, but don't
411 # use gettext directly. So we have to copy it in manually since
412 # we can't let `autopoint` do it for us.
413 config_rpath_update() {
414 local dst src=$(type -P gettext | sed 's:bin/gettext:share/gettext/config.rpath:')
416 [[ $# -eq 0 ]] && set -- $(find -name config.rpath)
417 [[ $# -eq 0 ]] && return 0
419 einfo "Updating all config.rpath files"
422 cp "${src}" "${dst}" || die
426 # @FUNCTION: autotools_env_setup
429 # Process the WANT_AUTO{CONF,MAKE} flags.
430 autotools_env_setup() {
431 # We do the "latest" → version switch here because it solves
432 # possible order problems, see bug #270010 as an example.
433 if [[ ${WANT_AUTOMAKE} == "latest" ]]; then
435 for pv in ${_LATEST_AUTOMAKE[@]/#*:} ; do
436 # has_version respects ROOT, but in this case, we don't want it to,
437 # thus "ROOT=/" prefix:
438 ROOT=/ has_version "=sys-devel/automake-${pv}*" && export WANT_AUTOMAKE="${pv}"
440 [[ ${WANT_AUTOMAKE} == "latest" ]] && \
441 die "Cannot find the latest automake! Tried ${_LATEST_AUTOMAKE[*]}"
443 [[ ${WANT_AUTOCONF} == "latest" ]] && export WANT_AUTOCONF=2.5
446 # @FUNCTION: autotools_run_tool
447 # @USAGE: [--at-no-fail] [--at-m4flags] [--at-missing] [--at-output] <autotool> [tool-specific flags]
450 # Run the specified autotool helper, but do logging and error checking
451 # around it in the process.
452 autotools_run_tool() {
453 # Process our own internal flags first
454 local autofail=true m4flags=false missing_ok=false return_output=false
455 while [[ -n $1 ]] ; do
457 --at-no-fail) autofail=false;;
458 --at-m4flags) m4flags=true;;
459 --at-missing) missing_ok=true;;
460 --at-output) return_output=true;;
461 # whatever is left goes to the actual tool
467 if [[ ${EBUILD_PHASE} != "unpack" && ${EBUILD_PHASE} != "prepare" ]]; then
468 ewarn "QA Warning: running $1 in ${EBUILD_PHASE} phase"
471 if ${missing_ok} && ! type -P ${1} >/dev/null ; then
472 einfo "Skipping '$*' due $1 not installed"
478 # Allow people to pass in full paths. #549268
479 local STDERR_TARGET="${T}/${1##*/}.out"
480 # most of the time, there will only be one run, but if there are
481 # more, make sure we get unique log filenames
482 if [[ -e ${STDERR_TARGET} ]] ; then
485 STDERR_TARGET="${T}/${1##*/}-${i}.out"
486 [[ -e ${STDERR_TARGET} ]] || break
492 set -- "${1}" $(autotools_m4dir_include) "${@:2}" $(autotools_m4sysdir_include)
495 # If the caller wants to probe something, then let them do it directly.
496 if ${return_output} ; then
501 printf "***** $1 *****\n***** PWD: ${PWD}\n***** $*\n\n" > "${STDERR_TARGET}"
504 "$@" >> "${STDERR_TARGET}" 2>&1
505 if ! eend $? && ${autofail} ; then
507 eerror "Failed Running $1 !"
509 eerror "Include in your bugreport the contents of:"
511 eerror " ${STDERR_TARGET}"
513 die "Failed Running $1 !"
517 # Internal function to check for support
519 # Keep a list of all the macros we might use so that we only
520 # have to run the trace code once. Order doesn't matter.
521 ALL_AUTOTOOLS_MACROS=(
522 A{C,M}_PROG_LIBTOOL LT_INIT
523 A{C,M}_CONFIG_HEADER{S,}
525 AC_CONFIG_AUX_DIR AC_CONFIG_MACRO_DIR
528 AM_GNU_GETTEXT_VERSION
529 {AC,IT}_PROG_INTLTOOL
533 autotools_check_macro() {
534 [[ -f configure.ac || -f configure.in ]] || return 0
536 # We can run in multiple dirs, so we have to cache the trace
537 # data in $PWD rather than an env var.
538 local trace_file=".__autoconf_trace_data"
539 if [[ ! -e ${trace_file} ]] || [[ ! aclocal.m4 -ot ${trace_file} ]] ; then
540 WANT_AUTOCONF="2.5" autoconf \
541 $(autotools_m4dir_include) \
542 ${ALL_AUTOTOOLS_MACROS[@]/#/--trace=} > ${trace_file} 2>/dev/null
547 has ${macro} ${ALL_AUTOTOOLS_MACROS[@]} || die "internal error: add ${macro} to ALL_AUTOTOOLS_MACROS"
548 args+=( -e ":${macro}:" )
550 grep "${args[@]}" ${trace_file}
553 # @FUNCTION: autotools_check_macro_val
554 # @USAGE: <macro> [macros]
557 # Look for a macro and extract its value.
558 autotools_check_macro_val() {
562 autotools_check_macro "${macro}" | \
563 gawk -v macro="${macro}" \
564 '($0 !~ /^[[:space:]]*(#|dnl)/) {
565 if (match($0, macro ":(.*)$", res))
573 _autotools_m4dir_include() {
574 local x include_opts flag
576 # Use the right flag to autoconf based on the version #448986
577 [[ ${WANT_AUTOCONF} == "2.1" ]] \
586 [[ ! -d ${x} ]] && ewarn "autotools.eclass: '${x}' does not exist"
587 include_opts+=" -${flag} ${x}"
594 autotools_m4dir_include() { _autotools_m4dir_include ${AT_M4DIR} ; }
595 autotools_m4sysdir_include() {
596 # First try to use the paths the system integrator has set up.
597 local paths=( $(eval echo ${AT_SYS_M4DIR}) )
599 if [[ ${#paths[@]} -eq 0 && -n ${SYSROOT} ]] ; then
600 # If they didn't give us anything, then default to the SYSROOT.
601 # This helps when cross-compiling.
602 local path="${SYSROOT}/usr/share/aclocal"
603 [[ -d ${path} ]] && paths+=( "${path}" )
605 _autotools_m4dir_include "${paths[@]}"