TExcelTAD
Home › Meson, Autotools & xmake › Reading & Patching Legacy ./configure Projects

Reading & Patching Legacy ./configure Projects

Sooner or later you will have to build somebody else's 15-year-old library: a dependency of your product, a tool your CI needs, an internal project whose author left. This lesson is a field guide: how to read an unfamiliar Autotools project, fix the most common configure failure, and bring an old configure.in up to date.

Step 1: read before you run

From a release tarball, you only need ./configure --help. It lists the project's own options, and the variables it honours, without running a single test. From a git checkout you usually have no configure at all: look for autogen.sh / bootstrap (the project's own wrapper around autoreconf) or run autoreconf -fi yourself.

Look at To learn
./configure --help | sed -n '/Optional Features/,$p' the project's --enable/--with options and precious variables
configure.ac (or configure.in): PKG_CHECK_MODULES, AC_CHECK_LIB, AC_CHECK_HEADER every external dependency
AC_INIT, AC_PREREQ, AM_INIT_AUTOMAKE name, version, how old the build system is
Makefile.am (top level and each SUBDIRS entry) what gets built and installed
m4/ or acinclude.m4 custom macros (often the reason autoreconf fails on a new system)

Step 2: the classic error

The most common failure, by far, is a missing dependency. Here a small project needs libquux, a library built and installed by hand into ~/deps (as a CI job might do):

bash · try it yourself
# a dependency "installed" into ~/deps by an earlier build step
mkdir -p ~/deps/include ~/deps/lib/pkgconfig q
echo 'int quux_answer(void);' > ~/deps/include/quux.h
echo 'int quux_answer(void) { return 42; }' > q/quux.c
gcc -c q/quux.c -o q/quux.o && ar rcs ~/deps/lib/libquux.a q/quux.o
printf 'prefix=%s\nName: quux\nDescription: d\nVersion: 3.1\nCflags: -I${prefix}/include\nLibs: -L${prefix}/lib -lquux\n' $HOME/deps > ~/deps/lib/pkgconfig/quux.pc
# the legacy project
mkdir -p oldapp && cd oldapp
cat > configure.ac <<'AC'
AC_INIT([oldapp], [2.4])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
AC_CHECK_HEADER([quux.h], [], [AC_MSG_ERROR([quux.h not found: install libquux-dev])])
AC_CHECK_LIB([quux], [quux_answer], [], [AC_MSG_ERROR([libquux not found])])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
printf 'bin_PROGRAMS = oldapp\noldapp_SOURCES = main.c\n' > Makefile.am
echo '#include <stdio.h>
#include <quux.h>
int main(void) { printf("answer %d\n", quux_answer()); return 0; }' > main.c
autoreconf -i 2> /dev/null
echo "--- attempt 1"
./configure 2>&1 | tail -2
echo "--- attempt 2: tell the compiler and linker where to look"
./configure CPPFLAGS="-I$HOME/deps/include" LDFLAGS="-L$HOME/deps/lib" 2>&1 | tail -3
make -j1 -s && ./oldapp
--- attempt 1
checking for quux.h... no
configure: error: quux.h not found: install libquux-dev
--- attempt 2: tell the compiler and linker where to look
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
answer 42

The fixes, in order of preference:

  1. Install the development package (apt install libquux-dev, dnf install quux-devel). The -dev/-devel package holds the headers and the .so symlink; the runtime package alone is not enough, which is the reason for most "but it is installed!" reports.
  2. If the library is somewhere non-standard, pass CPPFLAGS=-I… and LDFLAGS=-L… (for AC_CHECK_HEADER/AC_CHECK_LIB projects, as above) or PKG_CONFIG_PATH=… (for PKG_CHECK_MODULES projects). Pass them as arguments, ./configure VAR=value, not as environment variables: then config.status --recheck and automatic re-runs remember them.
  3. If the project has a --with-quux=DIR option, use it.
  4. If the check itself is wrong, read config.log (previous lesson), then override the result with a cache variable, as below.

PKG_CHECK_MODULES failures look different and even suggest the fix:

text
configure: error: Package requirements (quux >= 3.0) were not met:

Package 'quux', required by 'virtual:world', not found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables QUUX_CFLAGS
and QUUX_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

Step 3: overriding a wrong answer

Every check stores its result in a cache variable (ac_cv_…, listed at the end of config.log). Setting one on the command line skips the test. That is the standard way to fix a test that can't run when cross-compiling (tests that execute a program), or a check that gives the wrong answer on your platform:

bash · try it yourself
mkdir -p ov && cd ov
cat > configure.ac <<'AC'
AC_INIT([ov], [1.0])
AC_PROG_CC
AC_CHECK_FUNCS([strlcpy])
AC_CONFIG_HEADERS([config.h])
AC_OUTPUT
AC
autoreconf -i 2> /dev/null
./configure | grep strlcpy
grep STRLCPY config.h
./configure ac_cv_func_strlcpy=no | grep strlcpy
grep STRLCPY config.h
checking for strlcpy... yes
#define HAVE_STRLCPY 1
checking for strlcpy... (cached) no
/* #undef HAVE_STRLCPY */

For many overrides at once, put them in a config.site file and point CONFIG_SITE at it; distributions and cross-compilation SDKs ship one per target.

Step 4: modernising an old configure.in

Projects from the 2000s often fail or warn with today's autoconf 2.71 and automake 1.16. Typical symptoms: the input is still called configure.in, AM_INIT_AUTOMAKE(name, version) has the old two-argument form, and obsolete macros appear:

bash · try it yourself
mkdir -p old/src && cd old
cat > configure.in <<'AC'
AC_INIT(src/main.c)
AM_INIT_AUTOMAKE(oldtool, 0.9)
AM_CONFIG_HEADER(config.h)
AC_PROG_CC
AC_PROG_LIBTOOL
AC_OUTPUT(Makefile)
AC
printf 'bin_PROGRAMS = oldtool\noldtool_SOURCES = src/main.c\n' > Makefile.am
echo 'int main(void) { return 0; }' > src/main.c
echo "--- what today's tools say"
autoreconf -fi 2>&1 | grep -E 'warning|error' | grep -v 'is expanded from' | sort -u
echo "--- autoupdate rewrites obsolete autoconf macros"
mv configure.in configure.ac
autoupdate 2>&1
diff configure.ac~ configure.ac
--- what today's tools say
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am:2: warning: source file 'src/main.c' is in a subdirectory,
aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'
autoheader: warning: autoconf input should be named 'configure.ac', not 'configure.in'
automake: warning: autoconf input should be named 'configure.ac', not 'configure.in'
automake: warning: possible forward-incompatibility.
autoreconf: error: automake failed with exit status: 1
autoreconf: warning: autoconf input should be named 'configure.ac', not 'configure.in'
configure.in:2: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.
configure.in:2: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.in:3: warning: 'AM_CONFIG_HEADER': this macro is obsolete.
configure.in:5: warning: The macro `AC_PROG_LIBTOOL' is obsolete.
configure.in:6: warning: AC_OUTPUT should be used without arguments.
--- autoupdate rewrites obsolete autoconf macros
1c1,2
< AC_INIT(src/main.c)
---
> AC_INIT
> AC_CONFIG_SRCDIR([src/main.c])
5,6c6,8
< AC_PROG_LIBTOOL
< AC_OUTPUT(Makefile)
---
> LT_INIT
> AC_CONFIG_FILES([Makefile])
> AC_OUTPUT

Read the messages from the bottom up: the input file name is outdated, AM_INIT_AUTOMAKE(name, version) is deprecated, three macros are obsolete, and the old form also lacks foreign, so automake demands the GNU files (NEWS, README, AUTHORS, ChangeLog) and stops. That last error is the one that breaks the build; the rest are warnings.

autoupdate handled the autoconf side (AC_INIT(file) became AC_INIT + AC_CONFIG_SRCDIR, AC_PROG_LIBTOOL became LT_INIT, AC_OUTPUT(files) became AC_CONFIG_FILES + AC_OUTPUT). The automake macros are your job. The modern form of this file is:

m4
AC_INIT([oldtool], [0.9])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIRS([m4])
AM_INIT_AUTOMAKE([foreign subdir-objects])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CC
LT_INIT
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Keep such changes as a patch (in your fork, or as a .patch file your build applies) and send it upstream. Never edit the generated configure script by hand: the next autoreconf throws your change away, and the generated script is thousands of lines that nobody can review.

Test yourself

configure: error: libquux not found, but libquux1 is installed. What is the most likely fix?

  1. Edit configure to skip the check
  2. Install the development package (libquux-dev), which holds the headers and the .so symlink
  3. Run make anyway
  4. Run autoreconf

The library is in /opt/quux. How do you point a configure script that uses AC_CHECK_LIB at it?

  1. ./configure --prefix=/opt/quux
  2. ./configure CPPFLAGS=-I/opt/quux/include LDFLAGS=-L/opt/quux/lib
  3. export QUUX=/opt/quux
  4. ./configure --libdir=/opt/quux/lib

A configure test must run a program, which is impossible when cross-compiling. How do you supply the answer?

  1. Patch configure
  2. Set the check's cache variable, e.g. ./configure ac_cv_func_strlcpy=yes, or put it in a config.site
  3. Use --disable-tests
  4. Run configure on the target device