2

With explicit targets I can combine several rules like

foo.o bar.o: $(SOURCES)
 cc $< -o $@

This is equivalent of

foo.o: $(SOURCES)
 cc $< -o $@
bar.o: $(SOURCES)
 cc $< -o $@

But I want to use pattern rules.

I have several troff documents (man, README) and I want to generate .html and .ascii files.

Naive approach is

GROFF := groff
DOCS := man README
DOC_FILES = $(foreach doc,$(DOCS),$(doc).html $(doc).ascii)
CALL_GROFF = $(GROFF) -T$(subst $*.,,$@) -mman $< > $@
%.html %.ascii: %.doc
 $(CALL_GROFF)
.DEFAULT: all
all: $(DOC_FILES)
.PHONY: clean
clean:
 rm $(DOC_FILES)

But it doesn't work, because make believes that all files are created with one command (much like & in modern make: https://www.gnu.org/software/make/manual/html_node/Multiple-Targets.html)

Obviously I can do

GROFF := groff
DOCS := man README
DOC_FILES = $(foreach doc,$(DOCS),$(doc).html $(doc).ascii)
CALL_GROFF = $(GROFF) -T$(subst $*.,,$@) -mman $< > $@
%.ascii: %.doc
 $(CALL_GROFF)
%.html: %.doc
 $(CALL_GROFF)
.DEFAULT: all
all: $(DOC_FILES)
.PHONY: clean
clean:
 rm $(DOC_FILES)

But it is a kind of copy-paste.

Could it be solved with GNU make?

asked Sep 13, 2021 at 0:12

1 Answer 1

2

This is exactly how this works; it's a long-standing feature. From the documentation:

Pattern rules may have more than one target; however, every target must contain a % character. Pattern rules are always treated as grouped targets (see Multiple Targets in a Rule) regardless of whether they use the : or &: separator.

As example states, it was meant to deal with programs that generate more than one output in one invocation, like bison. You can either update your recipe to generate both files in one shot, or keep the rules separated as you do now.

answered Sep 13, 2021 at 8:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.