makefilegnu-make

Pair source and target in Make rule


It's been a very long time since I had to use Make, and I remember there was a trick to do this, but my memory fails me, and the vocabulary is too general for the search...

So:

SOURCES := $(wildcard */*.org)
JIRAS := $(patsubst %.org,%.jira,$(SOURCES))

.PHONY : all

all : $(JIRAS)

$(JIRAS) : $(SOURCES)
# $< is wrong here, because it will always be the first source
    emacs $< --batch -l $(CURDIR)/org2jira -f export-to-jira --kill

As the comment suggests, the $< part is wrong. I think there was a way to split this into two rules, and then Make would invoke it pairwise on the matching files, but after trying some combinations, I just cannot remember what it was.

If it makes it easier, the desired code can be written in Shell:

find . -type f -name '*.org' -exec emacs {} --batch -l $(pwd)/org2jira -f export-to-jira --kill \;

NB. I don't care if solution is GNUMake-specific.


Solution

  • You want to use a pattern rule.

    Like:

    SOURCES := $(wildcard */*.org)
    JIRAS := $(patsubst %.org,%.jira,$(SOURCES))
    
    .PHONY : all
    
    all : $(JIRAS)
    
    %.jira : %.org
            emacs $< --batch -l $(CURDIR)/org2jira -f export-to-jira --kill