Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function in prerequisite

A have a make target foo/%.bar. It matches files like:

foo/x/y/z.bar foo/a.bar

Now, I want a prerequisite prereq.o which must reside in the same folder than the .bar file. Thus, for foo/x/y/z.bar the prerequisite should be foo/x/y/prereq.o, for foo/a.bar it should be foo/prereq.o.

How to achieve this?

I tried using the dir function like this:

foo/%.bar : foo/$(dir %)prereq.o

However, this does not work because functions are evaluated before the patterns are expanded. So how does it work?

like image 739
gexicide Avatar asked Dec 20 '14 19:12

gexicide


1 Answers

You have to use .SECONDEXPANSION:

.SECONDEXPANSION:
foo/%.bar : foo/$$(dir %)prereq.o
    @echo $@ $<

The $$ in $$(dir...) is necessary so that it is not evaluated until the second expansion occurs.

like image 141
Louis Avatar answered Oct 06 '22 00:10

Louis