TExcelTAD
Home › Meson, Autotools & xmake › --enable, --with & config.log

--enable, --with & config.log

A configure script's options are its user interface. Autoconf has two families with fixed meanings, and you should respect the convention:

Option Meaning Macro
--enable-FEATURE[=ARG] / --disable-FEATURE turn an optional feature of this package on or off AC_ARG_ENABLE
--with-PACKAGE[=ARG] / --without-PACKAGE use (or not) an external package, optionally at a path AC_ARG_WITH
VAR=value precious variables: CC, CFLAGS, CPPFLAGS, LDFLAGS, LIBS, PKG_CONFIG_PATH… AC_ARG_VAR

An optional feature and an optional library

This configure.ac has a --enable-debug-log feature and a --with-zlib dependency found through pkg-config (PKG_CHECK_MODULES, from pkg.m4, which comes with pkg-config). The default is "check", the convention used by most GNU packages: use zlib if it is there, fail only if the user explicitly asked for it.

bash · try it yourself
mkdir -p tool && cd tool
cat > configure.ac <<'AC'
AC_INIT([tool], [1.0])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
AC_PROG_CC

AC_ARG_ENABLE([debug-log],
  [AS_HELP_STRING([--enable-debug-log], [print debug messages @<:@default=no@:>@])],
  [], [enable_debug_log=no])
AS_IF([test "x$enable_debug_log" = xyes],
  [AC_DEFINE([DEBUG_LOG], [1], [Define to print debug messages])])

AC_ARG_WITH([zlib],
  [AS_HELP_STRING([--with-zlib], [compress output with zlib @<:@default=check@:>@])],
  [], [with_zlib=check])
AS_IF([test "x$with_zlib" != xno],
  [PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2],
     [AC_DEFINE([HAVE_ZLIB], [1], [Define if zlib is available])],
     [AS_IF([test "x$with_zlib" = xyes],
        [AC_MSG_ERROR([--with-zlib was given, but zlib was not found])])])])

AC_MSG_NOTICE([debug log: $enable_debug_log, zlib: $with_zlib])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
printf 'bin_PROGRAMS = tool\ntool_SOURCES = tool.c\ntool_CFLAGS = $(ZLIB_CFLAGS)\ntool_LDADD = $(ZLIB_LIBS)\n' > Makefile.am
echo 'int main(void) { return 0; }' > tool.c
autoreconf -i 2> /dev/null
echo "--- ./configure --help (project options)"
./configure --help | sed -n '/^Optional Features/,/^Some influential/p' | grep -E 'debug-log|zlib|^Optional'
echo "--- default"
./configure | grep -E 'zlib|NOTICE|notice|debug log'
grep -E 'DEBUG_LOG|HAVE_ZLIB' config.h
echo "--- --enable-debug-log --without-zlib"
./configure --enable-debug-log --without-zlib | grep -E 'zlib|debug log'
grep -E 'DEBUG_LOG|HAVE_ZLIB' config.h
echo "--- --with-zlib"
./configure --with-zlib 2>&1 | tail -2
--- ./configure --help (project options)
Optional Features:
  --enable-debug-log      print debug messages [default=no]
Optional Packages:
  --with-zlib             compress output with zlib [default=check]
--- default
checking for zlib >= 1.2... no
configure: debug log: no, zlib: check
/* #undef DEBUG_LOG */
/* #undef HAVE_ZLIB */
--- --enable-debug-log --without-zlib
configure: debug log: yes, zlib: no
#define DEBUG_LOG 1
/* #undef HAVE_ZLIB */
--- --with-zlib
checking for zlib >= 1.2... no
configure: error: --with-zlib was given, but zlib was not found

Details worth copying:

  • The help strings use AS_HELP_STRING so --help is formatted like every other option; @<:@ and @:>@ are M4's way to write literal [ and ] ("quadrigraphs").
  • test "x$var" = xyes: the x prefix guards against empty values and values that start with -, a portability habit from old shells that configure scripts keep.
  • PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2], …) sets ZLIB_CFLAGS and ZLIB_LIBS and substitutes them into Makefiles, where tool_CFLAGS / tool_LDADD pick them up.
  • When nothing is found and the user asked for it, the message is the classic configure: error: … line, and configure exits with status 1.

Reading config.log

When configure gives a surprising answer, config.log says why. It records the command line, the environment, and for every test the program, the exact compiler command and the compiler's output. Search it for the checking … line you are curious about:

bash · try it yourself
mkdir -p tool && cd tool
cat > configure.ac <<'AC'
AC_INIT([tool], [1.0])
AC_PROG_CC
AC_CHECK_HEADERS([sys/event.h])
AC_CHECK_LIB([z], [deflate])
AC_OUTPUT
AC
autoconf
./configure CFLAGS=-O2 > /dev/null
echo "--- header of config.log"
sed -n '/^  \$ /p' config.log
echo "--- the failed sys/event.h test"
sed -n '/checking for sys\/event.h/,/result: no/p' config.log | grep -vE '^\| #define' | head -12
echo "--- the failed zlib test"
grep -A3 'checking for deflate in -lz' config.log
echo "--- the cached answers"
grep -E '^ac_cv_(header_sys_event_h|lib_z_deflate)=' config.log
--- header of config.log
  $ ./configure CFLAGS=-O2
  $ ./config.status
--- the failed sys/event.h test
configure:3235: checking for sys/event.h
configure:3235: gcc -c -O2  conftest.c >&5
conftest.c:47:10: fatal error: sys/event.h: No such file or directory
   47 | #include <sys/event.h>
      |          ^~~~~~~~~~~~~
compilation terminated.
configure:3235: $? = 1
configure: failed program was:
| /* confdefs.h */
| /* end confdefs.h.  */
| #include <stddef.h>
| #ifdef HAVE_STDIO_H
--- the failed zlib test
configure:3242: checking for deflate in -lz
configure:3265: gcc -o conftest -O2   conftest.c -lz   >&5
/usr/bin/ld: cannot find -lz: No such file or directory
collect2: error: ld returned 1 exit status
--- the cached answers
ac_cv_header_sys_event_h=no
ac_cv_lib_z_deflate=no

What to read:

  1. The first lines: the exact command line (config.status appends a second section with its own command line when it runs). When someone sends you a failed build, this tells you how they configured it.
  2. For each check: configure:LINE: checking for …, the compiler command (gcc -c -O2 conftest.c >&5), the compiler's error messages (fatal error: sys/event.h: No such file), the failing program (configure: failed program was: followed by conftest.c, prefixed with | ), and result: no.
  3. At the end: the cache variables (ac_cv_*) and output variables (CFLAGS, LIBS). You can force a cache variable from the command line, as the next lessons show.

Half of all "configure says X is missing but it is installed" problems are explained by the compiler error in config.log: a header that needs another header first, a library that needs -lpthread, a CFLAGS with -Werror that turns a warning in the test program into a failure.

Test yourself

Which option style should turn on an optional feature of the package itself?

  1. --with-feature
  2. --enable-feature
  3. FEATURE=yes
  4. --feature

What variables does PKG_CHECK_MODULES([ZLIB], [zlib]) define when zlib is found?

  1. HAVE_ZLIB only
  2. ZLIB_CFLAGS and ZLIB_LIBS
  3. ZLIB_DIR
  4. LIBS only

configure reports checking for sys/event.h... no. Where do you find the compiler's actual error message?

  1. config.status
  2. config.log, under that check
  3. configure --help
  4. Makefile