tags:

views:

27

answers:

2

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?

A: 

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.

Nathan Fellman
$(TESTS) is the rule that _builds_ the tests. It would be troublesome if I wanted to rerun tests, since that'd mean recompiling them.
Tordek
More to the point, this will not actually run the tests.
Beta
+2  A: 

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)
Beta
Thank you very much!
Tordek