Benoît
Benoît

Reputation: 3543

Makefile rules using variables as input

i'd like to write a Makefile (gnu make) like the following i.e. for a given list of values stored in a variable, i'd like to run a command. Is it possible or do i have to write an ugly for loop?

The ugly rules works but the smart does not.

SERVERS:=localhost
COMMAND:=uptime
OUTPUTS:=$(patsubst %, %.output ,$(SERVERS))

.PHONY: smart ugly

smart: $(OUTPUTS)

%.output: %
        ssh -n $^ $(COMMAND) > $@ || $(RM) $@

ugly:
        for s in $(SERVERS); do \
                ssh -n $$s $(COMMAND) > $$s.output || $(RM) $$s.output; \
        done

Upvotes: 0

Views: 197

Answers (1)

thiton
thiton

Reputation: 36059

The smart rule looks for a prerequisite file % (localhost in your example), which is not present. The rule is skipped accordingly.

So, use:

%.output:

instead and it should work.

Also, you might have a look at the special .DELETEONERROR target instead of doing || rm $@. In your notation, a failed SSH still exits with exit code 0 (from the rm) and the whole make process will exit with success or (when %.output is a dependency) happily go on after failure.

Upvotes: 1

Related Questions