Back Original

Parallel ./configure

Tavian Barnes GitHub

I'm sorry, but in the year 2025, this is ridiculous:

$ time ./configure
./configure  13.80s user 12.72s system 69% cpu 38.018 total
$ time make -j48
make -j48  12.05s user 4.70s system 593% cpu 2.822 total

I paid good money for my 24 CPU cores, but ./configure can only manage to use 69% of one of them. As a result, this random project takes about 13.5× longer to configure the build than it does to actually do the build.

The purpose of a ./configure script is basically to run the compiler a bunch of times and check which runs succeeded. In this way it can test whether particular headers, functions, struct fields, etc. exist, which lets people write portable software. This is an embarrassingly parallel problem, but Autoconf can't parallelize it, and neither can CMake, neither can Meson, etc., etc.

The problem is that most build configurations scripts pretty much look like this:

CFLAGS="-g"
if $CC $CFLAGS -Wall empty.c; then
    CFLAGS="$CFLAGS -Wall"
fi
...

: >config.h
if $CC $CFLAGS have_statx.c; then
    echo "#define HAVE_STATX 1" >>config.h
else
    echo "#define HAVE_STATX 0" >>config.h
fi
...

This is written in an inherently sequential way, but in principle many of these tests could be run in parallel. In fact, we already have an effective tool for parallelizing lots of commands (make), so let's use it. We'll have a configuration makefile that generates our Makefile and config.h:

configure.mk

# The default goal generates both outputs, and merges the logs together
config: Makefile config.h
	cat Makefile.log config.h.log >$@.log
	rm Makefile.log config.h.log

To start with, we'll save the initial values of variables like CC and CFLAGS into the Makefile:

configure.mk

# Default values, if unspecified
CC ?= cc
CPPFLAGS ?= -D_GNU_SOURCE
CFLAGS ?= -g
LDFLAGS ?=

# Export these through the environment to avoid stripping backslashes
export _CC=${CC}
export _CPPFLAGS=${CPPFLAGS}
export _CFLAGS=${CFLAGS}
export _LDFLAGS=${LDFLAGS}

Makefile:
	printf 'CC := %s\n' "$$_CC" >$@
	printf 'CPPFLAGS := %s\n' "$$_CPPFLAGS" >>$@
	printf 'CFLAGS := %s\n' "$$_CFLAGS" >>$@
	printf 'LDFLAGS := %s\n' "$$_LDFLAGS" >>$@

Using export like this avoids stripping the necessary backslashes from invocations like

$ ./configure CPPFLAGS='-DMACRO=\"string\"'

Now let's check which flags our compiler supports. We'll use this helper script:

flags.sh

#!/bin/sh

set -eu

VAR="$1"
FLAGS="$2"
shift 2

if "$@" $FLAGS; then
    printf '%s += %s\n' "$VAR" "$FLAGS"
fi

When we run

$ ./flags.sh CFLAGS -Wall cc empty.c

it will print

CFLAGS += -Wall

if cc empty.c -Wall succeeds (and nothing otherwise). We can use this to generate some makefile fragments that enable only the supported flags.

configure.mk

ALL_FLAGS = ${CPPFLAGS} ${CFLAGS} ${LDFLAGS}

# Run the compiler with the given flags, sending
#
# - stdout to foo.mk (e.g. CFLAGS += -flag)
# - stderr to foo.mk.log (e.g. error: unrecognized command-line option ‘-flag’)
# - the compiled binary to foo.mk.out
#   - but then we delete it immediately
TRY_CC = ${CC} ${ALL_FLAGS} empty.c -o $@.out >$@ 2>$@.log && rm -f $@.out $@.d

deps.mk:
	./flags.sh CPPFLAGS "-MP -MD" ${TRY_CC}
Wall.mk:
	./flags.sh CFLAGS -Wall ${TRY_CC}
pthread.mk:
	./flags.sh CFLAGS -pthread ${TRY_CC}
bind-now.mk:
	./flags.sh LDFLAGS -Wl,-z,now ${TRY_CC}

Each of these targets generates a tiny makefile fragment that's responsible for a single flag. Importantly, each one can run independently, in parallel. Once they're done, we can merge them all into the main Makefile and clean up the cruft:

configure.mk

FLAGS := \
    deps.mk \
    Wall.mk \
    pthread.mk \
    bind-now.mk

Makefile: ${FLAGS}
	printf 'CC := %s\n' "$$_CC" >$@
	...
	cat ${FLAGS} >>$@
	cat ${FLAGS:%=%.log} >$@.log
	rm ${FLAGS} ${FLAGS:%=%.log}

The last part to add to the Makefile is the part that actually builds our application. We can write a simple makefile like this:

main.mk

OBJS := main.o

app: ${OBJS}
	${CC} ${CFLAGS} ${LDFLAGS} ${OBJS} -o $@

${OBJS}:
	${CC} ${CPPFLAGS} ${CFLAGS} -c ${@:.o=.c} -o $@

-include ${OBJS:.o=.d}

And append it to the Makefile after all the flags:


We also want to generate a config.h file, which defines macros that tell us whether certain libraries/headers/functions/struct fields/etc. exist. We can do this by test-compiling some simple C programs. As an example, these programs check for the various ways to learn about a file's creation timestamp:

#include <fcntl.h>
#include <sys/stat.h>

int main(void) {
	struct statx stx;
	return statx(AT_FDCWD, ".", 0, STATX_BTIME, &stx);
}
#include <sys/stat.h>

int main(void) {
	struct stat sb = {0};
	return sb.st_birthtim.tv_sec;
}
#include <sys/stat.h>

int main(void) {
	struct stat sb = {0};
	return sb.st_birthtimespec.tv_sec;
}
#include <sys/stat.h>

int main(void) {
	struct stat sb = {0};
	return sb.__st_birthtim.tv_sec;
}
This helper script:

define.sh

#!/bin/sh

set -eu

MACRO=$1
shift

if "$@"; then
    printf '#define %s 1\n' "$MACRO"
else
    printf '#define %s 0\n' "$MACRO"
fi

will output things like

#define HAVE_STATX 1

or

#define HAVE_ST_BIRTHTIM 0

depending on whether the build succeeds. We can use it in a makefile like this:

configure.mk

# Use a recursive make to pick up our auto-detected *FLAGS from above
config.h: Makefile
	+${MAKE} -f header.mk $@

header.mk

# Get the final *FLAGS values from the Makefile
include Makefile

# We first generate a lot of small headers, before merging them into one big one
HEADERS := \
    have_statx.h \
    have_st_birthtim.h \
    have_st_birthtimespec.h \
    have___st_birthtim.h

# Strip .h and capitalize the macro name
MACRO = $$(printf '%s' ${@:.h=} | tr 'a-z' 'A-Z')

ALL_FLAGS = ${CPPFLAGS} ${CFLAGS} ${LDFLAGS}

${HEADERS}:
	./define.sh ${MACRO} ${CC} ${ALL_FLAGS} ${@:.h=.c} -o $@.out >$@ 2>$@.log
	rm -f $@.out $@.d

And to join them all together (along with a header guard):

header.mk

config.h: ${HEADERS}
	printf '#ifndef CONFIG_H\n' >$@
	printf '#define CONFIG_H\n' >>$@
	cat ${HEADERS} >>$@
	printf '#endif\n' >>$@
	cat ${HEADERS:%=%.log} >$@.log
	rm ${HEADERS} ${HEADERS:%=%.log}

The last step is to wrap configure.mk in a shell script, so people can run ./configure like they're used to:

configure

#!/bin/sh

set -eu

# Guess a good number for make -j<N>
jobs() {
    {
        nproc \
            || sysctl -n hw.ncpu \
            || getconf _NPROCESSORS_ONLN \
            || echo 1
    } 2>/dev/null
}

# Default to MAKE=make
MAKE="${MAKE-make}"

# Set MAKEFLAGS to -j$(jobs) if it's unset
export MAKEFLAGS="${MAKEFLAGS--j$(jobs)}"

$MAKE -r -f configure.mk "$@"

I put together a simple proof-of-concept GitHub repository that contains the full version of all these files if you want to copy-paste. The demo app prints file creation times, if it can figure out how to on your platform.

I've also been using a similar build system in bfs for a while, if you want to see a larger example. The performance benefit is substantial:

$ time ./configure
./configure  1.44s user 1.78s system 802% cpu 0.401 total
tavianator@tachyon $ time make -j48
make -j48  1.89s user 0.64s system 817% cpu 0.310 total

Of course, a lot of the benefit comes from just doing less configuration steps, but the 802% CPU use is a tremendous improvement over everything else I've tried.