1
votes

I have a makefile containing rules to build the system, tests, and run them. The last item is executed by just calling a shell script. Among other things, this prevents me from running the tests in parallel.

I have the following variables:

TEST_SRC=$(wildcard tests/*.c)
TESTS=$(patsubst %.c,%,${TEST_SRC})

and it builds the tests with the rule

$(TESTS): %: %.c
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

Is it possible (and if so, how?) to create a rule "tests" that, when run, will execute each item in the $TESTS variable?

2

2 Answers

5
votes

You could do it this way:

# a separate target to run each test
RUN_TESTS = $(addprefix run_, $(TESTS))

# a pattern rule for those targets, which will remake the test iff neccessary
.PHONY: $(RUN_TESTS)
$(RUN_TESTS):run_%:%
  ./$<

# One Rule to Ring Them All, One Rule to... sorry.
.PHONY: tests
tests: $(RUN_TESTS)
0
votes

I believe that a rule like this:

run_tests: $(TESTS)

Should do the trick:

$ make run_tests

It will not execute anything, but it will have $(TESTS) as a dependency and run them first.