TExcelTAD
Home › Meson, Autotools & xmake › Toolkit: autoscan, autoupdate & Friends

Toolkit: autoscan, autoupdate & Friends

Autoconf and automake ship a set of small helper programs. You will rarely need them every day, but they save hours when you start an Autotools build for existing code, upgrade an old one, or debug what a macro expanded into.

Tool Package Does
autoscan autoconf scans C sources for portability-relevant headers and functions, writes configure.scan as a starting configure.ac
autoupdate autoconf rewrites obsolete macros in configure.ac to their modern forms
ifnames autoconf lists the #if/#ifdef identifiers in sources: the HAVE_* macros the code expects
autoheader autoconf writes config.h.in from configure.ac (run by autoreconf)
autom4te autoconf the M4 driver with a cache (autom4te.cache/); autoconf --trace uses it
aclocal automake collects third-party macros into aclocal.m4
libtoolize libtool copies libtool's scripts and macros into the project
autoreconf autoconf runs all of the above in the right order; -v shows what it runs
config.guess / config.sub shipped in build-aux/ detect and canonicalise system triplets (x86_64-pc-linux-gnu)

Starting from existing code: autoscan and ifnames

bash · try it yourself
mkdir -p legacy && cd legacy
cat > main.c <<'SRC'
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#ifdef HAVE_STRLCPY
#  define COPY(d, s, n) strlcpy(d, s, n)
#else
#  define COPY(d, s, n) (strncpy(d, s, n), d[(n) - 1] = 0)
#endif
int main(void) {
    char buf[8]; struct timeval tv;
    COPY(buf, "hello world", sizeof buf);
    gettimeofday(&tv, NULL);
    printf("%s %d\n", buf, access("/", F_OK));
    return 0;
}
SRC
autoscan
echo "--- configure.scan"
grep -vE '^#|^
  

 configure.scan
echo "--- ifnames: what the code expects configure to define"
ifnames main.c
--- configure.scan
AC_PREREQ([2.71])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CC
AC_CHECK_HEADERS([sys/time.h unistd.h])
AC_CHECK_FUNCS([gettimeofday])
AC_OUTPUT
--- ifnames: what the code expects configure to define
HAVE_STRLCPY main.c

configure.scan is a draft: rename it to configure.ac, fill in AC_INIT, add AM_INIT_AUTOMAKE and AC_CONFIG_FILES, and add the checks autoscan can't infer, here AC_CHECK_FUNCS([strlcpy]), which ifnames revealed. autoscan also writes autoscan.log, with notes on why it suggested each check.

Upgrading: autoupdate

The legacy projects lesson showed autoupdate on a whole configure.in. It also takes single files and leaves a backup (configure.ac~). Run it, read the diff, and delete what it adds "to ensure that your configure script's behavior did not change" once you have checked it is not needed.

Seeing what autoreconf does, and what a macro expands to

bash · try it yourself
mkdir -p tiny && cd tiny
cat > configure.ac <<'AC'
AC_INIT([tiny], [1.0])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
AC_CHECK_FUNCS([strlcpy])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
printf 'bin_PROGRAMS = tiny\n' > Makefile.am
echo 'int main(void) { return 0; }' > tiny.c
echo "--- autoreconf -v: the steps, in order"
autoreconf -vi 2>&1 | grep -E "^autoreconf: running" | sed 's/  */ /g'
echo "--- autoconf --trace: which macros are called, from where"
autoconf --trace=AC_CHECK_FUNCS --trace=AC_CONFIG_FILES
echo "--- configure is big"
wc -l < configure.ac
wc -l < configure
echo "--- system triplets"
sh $(ls /usr/share/automake-*/config.guess | head -1)     # projects copy it into build-aux/ (AC_CANONICAL_HOST)
--- autoreconf -v: the steps, in order
autoreconf: running: aclocal
autoreconf: running: /usr/bin/autoconf
autoreconf: running: automake --add-missing --copy --no-force
--- autoconf --trace: which macros are called, from where
configure.ac:4:AC_CHECK_FUNCS:strlcpy
configure.ac:5:AC_CONFIG_FILES:Makefile
--- configure is big
6
5533
--- system triplets
x86_64-pc-linux-gnu

Six lines of configure.ac became more than 5,000 lines of portable shell. autoreconf ran only the tools this project needs: no autoheader (there is no AC_CONFIG_HEADERS) and no libtoolize (no LT_INIT). --trace is the way to answer "where does this macro get called" in a large project with many included .m4 files, without reading the generated script.

Other tools around Autotools

  • gnulib: a library of portable replacement functions and modules (gnulib-tool --import strlcpy) that comes with the matching configure checks; GNU projects use it to run on old and odd systems.
  • autoconf-archive: hundreds of community macros (AX_CHECK_COMPILE_FLAG, AX_PTHREAD, AX_CXX_COMPILE_STDCXX, AX_CODE_COVERAGE). Copy the ones you use into m4/, so builders don't need the archive installed.
  • pkg.m4 (from pkg-config): PKG_CHECK_MODULES, PKG_PROG_PKG_CONFIG, PKG_INSTALLDIR (sets pkgconfigdir portably).
  • autoreconf -fi vs autogen.sh: many projects wrap autoreconf in an autogen.sh or bootstrap script that also fetches gnulib or submodules; read it before running it.

Test yourself

You have a directory of C files and no build system. Which tool writes a first draft of configure.ac?

  1. autoupdate
  2. autoscan
  3. aclocal
  4. autoheader

What does ifnames main.c print?

  1. The functions defined in main.c
  2. The identifiers used in #if/#ifdef lines, i.e. the macros the code expects to be defined
  3. The include files
  4. The generated config.h

Where should a project keep third-party macros such as AX_PTHREAD from autoconf-archive?

  1. Nowhere; require builders to install autoconf-archive
  2. Copied into the project's m4/ directory (with AC_CONFIG_MACRO_DIRS([m4]))
  3. In configure
  4. In /usr/share/aclocal only