Monday, March 16, 2015

Multiple test programs, one makefile

I've been writing tests using glib's test framework. Some of them got quite large, so I split them up into multiple files with their own main()'s. Here's my makefile to build and test up to 10 test binaries in one go:
T:=$(patsubst %.c,%,$(filter tests-%,$(wildcard *.c)))  # make all test progs
#T=test1 test2  # or list them by hand
OBJECTS:=$(patsubst %.c,%.o,$(wildcard ../*.c))  # CUT is back one dir
#OBJECTS=mysource1.o mysource2.o  # you can also just list these too

CFLAGS=-Wall -Werror `pkg-config --cflags glib-2.0`
CFLAGS+=-D_GNU_SOURCE -D_BSD_SOURCE  # asprintf, strdup
CFLAGS+=-Idummy_includes
LDLIBS=`pkg-config --libs glib-2.0`
CC=gcc -std=c99

TFLAGS=--quiet  # suppress per test binary output
#TFLAGS=--verbose  # report success per test case
TESTER=gtester

.PHONY: all
all: $(T)

.PHONY: rebuild
rebuild: clean all

.PHONY: check
check: $(T)
 @$(TEST) $(TFLAGS) $(T) && echo "ALL OK"

.PHONY: clean
clean:
 rm -f $(OBJECTS) $(T)

$(T): $(OBJECTS)
Now it's just make check and you get a simple "ALL OK", or a big ugly error. By the way, make sure all the rule definitions start with actual tabs, make doesn't like spaces..

No comments :