How to target multiple directories with a single M

2019-07-04 16:28发布

问题:

I'm using GNU Make to build three different editions of a static html document.

I use Less as a CSS preprocessor.

My directory structure looks like this:

Makefile
160x600/style.less
300x250/style.less
728x90/style.less

This is my Makefile:

LESSC=lessc -x # use -x for debugging

.PHONY: all clean

all: 160x600 300x250 728x90

%.css: %.less
    $(LESSC) $< > $@

160x600: 160x600/style.css
300x250: 300x250/style.css
728x90: 728x90/style.css

clean:
    rm -f 160x600/*.css
    rm -f 300x250/*.css
    rm -f 728x90/*.css

This way, I can use make 160x600 to build style.css from style.less.

But I don't want to explicitly list a target rule for each directory. Instead, I tried adding this rule instead of the three directory specific ones:

%: %/style.css

But that does not work. I assume it's clear from that example what my goal is. Is there a way to accept any directory as a target, so that I just have to list the directory names in the all: rule?

回答1:

use static pattern rule:

res_dirs = 160x600 300x250 728x90

$(res_dirs): %: %/style.css


标签: makefile less