Makefile.am: Primaries, check & install
Makefile.am is not a Makefile you write rules in: it is a list of declarations that automake expands into a complete, GNU-standards-compliant Makefile.in with all, install, uninstall, check, clean, distclean, dist, distcheck, dependency tracking and out-of-tree support. You may still add ordinary Make rules; automake copies them through.
The naming scheme: where_PRIMARY
Every declaration is <where>_<PRIMARY> = <things>:
| Part | Examples | Meaning |
|---|---|---|
| where | bin, sbin, lib, include, pkginclude, data, pkgdata, man1, noinst, check, or a custom foo with a foodir variable |
the install directory (bindir, libdir…), or noinst (build, don't install), check (build only for make check) |
| PRIMARY | PROGRAMS, LIBRARIES, LTLIBRARIES, HEADERS, DATA, SCRIPTS, MANS, TEXINFOS |
what kind of thing it is |
Each program or library then gets its own variables, with the name canonicalised (., -, / → _): hello_SOURCES, hello_CFLAGS, hello_CPPFLAGS, hello_LDADD (libraries for programs), libfoo_a_SOURCES, libfoo_la_LIBADD (libraries for libraries), libfoo_la_LDFLAGS. A missing X_SOURCES defaults to X.c.
A program, a static helper library and tests
mkdir -p wc/src wc/tests && cd wc
cat > configure.ac <<'AC'
AC_INIT([wc], [0.5])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([foreign -Wall -Werror subdir-objects])
AM_SILENT_RULES([yes])
AC_PROG_CC
AM_PROG_AR
AC_PROG_RANLIB
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
cat > Makefile.am <<'AM'
# a convenience library: built, linked into the program and tests, never installed
noinst_LIBRARIES = libcount.a
libcount_a_SOURCES = src/count.c src/count.h
bin_PROGRAMS = wc
wc_SOURCES = src/main.c
wc_LDADD = libcount.a
dist_doc_DATA = README
# make check: build these, run TESTS, count the results
check_PROGRAMS = tests/test_count
tests_test_count_LDADD = libcount.a
TESTS = tests/test_count tests/cli.sh
XFAIL_TESTS = tests/cli.sh
EXTRA_DIST = tests/cli.sh
AM
cat > src/count.h <<'H'
int count_words(const char *s);
H
cat > src/count.c <<'SRC'
#include <ctype.h>
#include "count.h"
int count_words(const char *s) {
int n = 0, in = 0;
for (; *s; s++) { if (isspace((unsigned char)*s)) in = 0; else if (!in) { in = 1; n++; } }
return n;
}
SRC
cat > src/main.c <<'SRC'
#include <stdio.h>
#include "count.h"
int main(int argc, char **argv) { printf("%d\n", count_words(argc > 1 ? argv[1] : "")); return 0; }
SRC
cat > tests/test_count.c <<'SRC'
#include "../src/count.h"
int main(void) { return count_words(" two words ") == 2 && count_words("") == 0 ? 0 : 1; }
SRC
printf '#!/bin/sh\n# a known bug: counting words with hyphens\ntest "$(./wc "well-known fact")" = 3\n' > tests/cli.sh
chmod +x tests/cli.sh
echo "wc counts words" > README
autoreconf -i 2> /dev/null
./configure --quiet
make -j1
make -j1 check 2>&1 | grep -E '^(PASS|FAIL|XFAIL|XPASS|SKIP|ERROR|# )' CC src/main.o
CC src/count.o
AR libcount.a
ar: `u' modifier ignored since `D' is the default (see `U')
CCLD wc
PASS: tests/test_count
XFAIL: tests/cli.sh
# TOTAL: 2
# PASS: 1
# SKIP: 0
# XFAIL: 1
# FAIL: 0
# XPASS: 0
# ERROR: 0
AM_SILENT_RULES([yes])replaces full command lines with shortCC/AR/CCLDlines.make V=1shows the full commands again for one run;./configure --disable-silent-rulesmakes that the default.- The
ar: 'u' modifier ignoredline is harmless: automake's defaultARFLAGSiscru, and Ubuntu's binutils builds deterministic archives (D), where "update only newer members" makes no sense.ARFLAGS = crinMakefile.amsilences it. noinst_LIBRARIESis the standard way to share code between a program and its tests (with libtool,noinst_LTLIBRARIES).check_PROGRAMSare built only bymake check;TESTSlists what to run, programs or scripts. Automake's parallel test harness writes a.logand.trsfile per test and atest-suite.logwith every failure's output. Exit code 0 is PASS, 77 SKIP, 99 a hard ERROR.XFAIL_TESTSmarks known failures: they count asXFAILand don't fail the suite (and an unexpected pass is anXPASS, which does).dist_prefixes andEXTRA_DISTdecide what goes into the release tarball (see make distcheck). Sources and headers listed in_SOURCESare distributed automatically.
Installing with DESTDIR
The generated Makefile implements the GNU directory variables (prefix, exec_prefix, bindir, libdir, docdir…) and DESTDIR staging, just like Meson's. make uninstall removes exactly what was installed:
mkdir -p hi && cd hi
cat > configure.ac <<'AC'
AC_INIT([hi], [1.0])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
cat > Makefile.am <<'AM'
bin_PROGRAMS = hi
include_HEADERS = hi.h
dist_doc_DATA = README
man1_MANS = hi.1
EXTRA_DIST = hi.1
AM
echo 'int main(void) { return 0; }' > hi.c
echo '/* public header */' > hi.h
echo 'hi says hi' > README
printf '.TH HI 1\n.SH NAME\nhi \\- says hi\n' > hi.1
autoreconf -i 2> /dev/null
./configure --quiet --prefix=/opt/hi
make -j1 -s
make -j1 -s install DESTDIR=$PWD/stage
find stage -type f | sort
make -j1 -s uninstall DESTDIR=$PWD/stage
echo "after uninstall: $(find stage -type f | wc -l) files" /usr/bin/mkdir -p '/home/learner/hi/stage/opt/hi/bin'
/usr/bin/install -c hi '/home/learner/hi/stage/opt/hi/bin'
/usr/bin/mkdir -p '/home/learner/hi/stage/opt/hi/share/doc/hi'
/usr/bin/install -c -m 644 README '/home/learner/hi/stage/opt/hi/share/doc/hi'
/usr/bin/mkdir -p '/home/learner/hi/stage/opt/hi/include'
/usr/bin/install -c -m 644 hi.h '/home/learner/hi/stage/opt/hi/include'
/usr/bin/mkdir -p '/home/learner/hi/stage/opt/hi/share/man/man1'
/usr/bin/install -c -m 644 hi.1 '/home/learner/hi/stage/opt/hi/share/man/man1'
stage/opt/hi/bin/hi
stage/opt/hi/include/hi.h
stage/opt/hi/share/doc/hi/README
stage/opt/hi/share/man/man1/hi.1
( cd '/home/learner/hi/stage/opt/hi/bin' && rm -f hi )
( cd '/home/learner/hi/stage/opt/hi/share/doc/hi' && rm -f README )
( cd '/home/learner/hi/stage/opt/hi/include' && rm -f hi.h )
( cd '/home/learner/hi/stage/opt/hi/share/man/man1' && rm -f hi.1 )
after uninstall: 0 files
--prefix is fixed at configure time (it may be compiled into the program), while DESTDIR is applied at install time and only changes where files are copied.
Test yourself
What does check_PROGRAMS = tests/t1 mean?
- Install
t1into acheckdirectory - Build
t1only formake check, never install it - Run
t1during./configure - Check that
t1exists
A program my-tool needs extra libraries. Which variable?
my-tool_LIBSmy_tool_LDADDLDFLAGS_my_toolmy-tool_LIBADD
Which exit code makes automake's test harness record a test as SKIP?
- 0
- 1
- 77
- 99