configure.ac: Checks & config.h
configure.ac is a shell script with M4 macros in it. autoconf expands the macros (each one becomes dozens or hundreds of lines of portable shell) and copies plain shell code through unchanged. The result is configure.
The skeleton
Nearly every configure.ac follows the same order:
AC_PREREQ([2.69]) # minimum autoconf version
AC_INIT([name], [version], [bug-address]) # package metadata
AC_CONFIG_SRCDIR([src/main.c]) # a file that proves we're in the right directory
AC_CONFIG_AUX_DIR([build-aux]) # put compile, install-sh, missing… here, not in the root
AC_CONFIG_MACRO_DIRS([m4]) # local and copied .m4 macros
AM_INIT_AUTOMAKE([foreign -Wall -Werror subdir-objects])
# checks for programs
AC_PROG_CC
# checks for libraries
AC_SEARCH_LIBS([cos], [m])
# checks for header files
AC_CHECK_HEADERS([sys/epoll.h sys/event.h])
# checks for types, structures, compiler characteristics
AC_CHECK_SIZEOF([long])
# checks for library functions
AC_CHECK_FUNCS([strlcpy reallocarray])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT # write config.status and run it[…] is M4 quoting: always quote macro arguments, or M4 may expand words inside them. dnl starts a comment that disappears from configure; # comments are copied into it.
Checks and their results
mkdir -p probe && cd probe
cat > configure.ac <<'AC'
AC_INIT([probe], [0.1])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
AC_SEARCH_LIBS([cos], [m])
AC_CHECK_HEADERS([sys/epoll.h sys/event.h])
AC_CHECK_SIZEOF([long])
AC_CHECK_FUNCS([strlcpy reallocarray no_such_function])
AC_CHECK_DECLS([O_CLOEXEC], [], [], [[#include <fcntl.h>]])
AC_DEFINE([PROBE_FEATURE_LEVEL], [3], [How fancy the probe is])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
printf 'bin_PROGRAMS = probe\nprobe_SOURCES = probe.c\n' > Makefile.am
echo 'int main(void) { return 0; }' > probe.c
autoreconf -i 2> /dev/null
./configure | sed -n '/checking for library containing cos/,/^configure: creating/p' | grep -vE 'checking for (stdio|stdlib|string|strings|inttypes|stdint|sys/stat|sys/types|unistd|wchar)\.h'
echo "--- config.h (comments removed)"
grep -E '^#(define|undef| *undef)|^/\* #undef' config.h | grep -vE 'PACKAGE|STDC|HAVE_(STDINT|STDIO|STDLIB|STRING|STRINGS|INTTYPES|SYS_STAT|SYS_TYPES|UNISTD|WCHAR)_H'
echo "--- the libraries the program will link"
grep '^LIBS =' Makefilechecking for library containing cos... -lm
checking for sys/epoll.h... yes
checking for sys/event.h... no
checking size of long... 8
checking for strlcpy... yes
checking for reallocarray... yes
checking for no_such_function... no
checking for gcc options needed to detect all undeclared functions... none needed
checking whether O_CLOEXEC is declared... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
--- config.h (comments removed)
#define HAVE_DECL_O_CLOEXEC 1
/* #undef HAVE_NO_SUCH_FUNCTION */
#define HAVE_REALLOCARRAY 1
#define HAVE_STRLCPY 1
#define HAVE_SYS_EPOLL_H 1
/* #undef HAVE_SYS_EVENT_H */
#define PROBE_FEATURE_LEVEL 3
#define SIZEOF_LONG 8
#define VERSION "0.1"
--- the libraries the program will link
LIBS = -lm
What each kind of check leaves behind:
| Macro | Result in config.h |
Other effect |
|---|---|---|
AC_CHECK_HEADERS([x/y.h]) |
#define HAVE_X_Y_H 1 or /* #undef HAVE_X_Y_H */ |
|
AC_CHECK_FUNCS([f]) |
HAVE_F |
|
AC_CHECK_DECLS([SYM], …) |
HAVE_DECL_SYM set to 1 or 0 (always defined) |
|
AC_CHECK_SIZEOF([long]) |
SIZEOF_LONG 8 |
|
AC_SEARCH_LIBS([cos], [m]) |
adds -lm to LIBS if cos needs it |
|
AC_CHECK_LIB([m], [cos]) |
HAVE_LIBM |
always adds -lm to LIBS when found |
AC_DEFINE([NAME], [value], [description]) |
#define NAME value, with the description as a comment |
|
AC_SUBST([VAR]) |
replaces @VAR@ in every AC_CONFIG_FILES template |
The template config.h.in was written by autoheader, which knows every symbol a check might define; config.status turns each #undef X into a #define or a comment. The code then uses #ifdef HAVE_SYS_EPOLL_H, exactly like the Meson version.
A model of what configure does
Under the hood, every check is the same loop: build a test program, try it, record the answer in a cache variable (ac_cv_header_sys_epoll_h=yes), and turn the answer into a #define. This JavaScript model runs that loop against a fake "system" and writes a config.h. Change the system (remove strlcpy, add sys/event.h) and run it again:
// What the "compiler" can find on this machine.
const system = {
headers: new Set(['stdio.h', 'string.h', 'sys/epoll.h', 'unistd.h']),
functions: new Set(['strlcpy', 'reallocarray', 'malloc']),
sizes: { int: 4, long: 8, 'void *': 8 },
};
const cache = {}; // ac_cv_* variables (configure can save them in config.cache)
const defines = []; // lines for config.h
const macro = (s) => s.toUpperCase().replace(/[^A-Z0-9]/g, '_');
function check(kind, name, test, label = `checking for ${name}`) {
const key = `ac_cv_${kind}_${name.replace(/[^a-z0-9]/gi, '_')}`;
const cached = key in cache;
if (!cached) cache[key] = test();
console.log(`${label}... ${cached ? '(cached) ' : ''}${cache[key]}`);
return cache[key];
}
function AC_CHECK_HEADERS(list) {
for (const h of list) {
const ok = check('header', h, () => (system.headers.has(h) ? 'yes' : 'no'));
defines.push(ok === 'yes' ? `#define HAVE_${macro(h)} 1` : `/* #undef HAVE_${macro(h)} */`);
}
}
function AC_CHECK_FUNCS(list) {
for (const f of list) {
const ok = check('func', f, () => (system.functions.has(f) ? 'yes' : 'no'));
defines.push(ok === 'yes' ? `#define HAVE_${macro(f)} 1` : `/* #undef HAVE_${macro(f)} */`);
}
}
function AC_CHECK_SIZEOF(type) {
const n = check('sizeof', type, () => String(system.sizes[type] ?? 0), `checking size of ${type}`);
defines.push(`#define SIZEOF_${macro(type)} ${n}`);
}
AC_CHECK_HEADERS(['sys/epoll.h', 'sys/event.h', 'unistd.h']);
AC_CHECK_HEADERS(['unistd.h']); // a second check is answered from the cache
AC_CHECK_FUNCS(['strlcpy', 'no_such_function']);
AC_CHECK_SIZEOF('long');
AC_CHECK_SIZEOF('void *');
console.log('\n/* config.h */');
const name = (line) => line.match(/(HAVE|SIZEOF)_\w+/)[0];
for (const line of [...new Set(defines)].sort((a, b) => name(a).localeCompare(name(b)))) console.log(line);checking for sys/epoll.h... yes
checking for sys/event.h... no
checking for unistd.h... yes
checking for unistd.h... (cached) yes
checking for strlcpy... yes
checking for no_such_function... no
checking size of long... 8
checking size of void *... 8
/* config.h */
/* #undef HAVE_NO_SUCH_FUNCTION */
#define HAVE_STRLCPY 1
#define HAVE_SYS_EPOLL_H 1
/* #undef HAVE_SYS_EVENT_H */
#define HAVE_UNISTD_H 1
#define SIZEOF_LONG 8
#define SIZEOF_VOID__ 8
The real configure does exactly this, with the cache variables visible in config.log and settable from the command line, which matters when a test gives the wrong answer (for example when cross-compiling): ./configure ac_cv_func_strlcpy=no skips the test and uses your answer. The legacy projects lesson uses that trick.
Test yourself
After AC_CHECK_HEADERS([sys/event.h]) on Linux, what does config.h contain?
#define HAVE_SYS_EVENT_H 0/* #undef HAVE_SYS_EVENT_H */- Nothing at all
#error sys/event.h missing
What is the difference between AC_SEARCH_LIBS([cos], [m]) and AC_CHECK_LIB([m], [cos])?
- None
AC_SEARCH_LIBSfirst tries without any library and adds-lmonly if needed;AC_CHECK_LIBalways links-lmwhen it hascosAC_CHECK_LIBdoesn't touchLIBSAC_SEARCH_LIBSis for C++ only
Why are macro arguments written in square brackets, as in AC_CHECK_FUNCS([strlcpy])?
- They are optional arguments
[]are M4 quotes; they stop M4 from expanding words inside the argument- They make an array
- Autoconf 2.71 requires JSON