6

I'd like to define a GNU make pattern rule with the dependencies in a pattern-dependent variable. What I'd like is something like this:

%.exe : $(%_EXE_SOURCES) $(%_EXE_RESOURCES)
 $(CSC_V)$(CSC) $(CSCFLAGS) $($*_EXE_CSCFLAGS) -target:exe \
 -out:$@ $($*_EXE_SOURCES) $($*_EXE_RESOURCES)

And to later define something like

FOO_EXE_SOURCES = src/Foo.cs
all: Foo.exe

The rule presented works to build; in the body of the rule the $($*_EXE_SOURCES) variable is expanded to $(FOO_EXE_SOURCES), which expands to src/Foo.cs. The dependencies don't expand properly, however; changing src/Foo.cs does not cause Foo.exe to be rebuilt.

I suspect that this can't actually be done in make, but perhaps someone has a work-alike make fragment?

Dan Moulding
223k25 gold badges103 silver badges101 bronze badges
asked Dec 6, 2009 at 23:22

1 Answer 1

9
+50

You could use "secondary expansion". Something like this should accomplish what you are looking for:

Foo_EXE_SOURCES := foo.cs bar.cs baz.cs
all: Foo.exe
.SECONDEXPANSION:
%.exe: $$($$*_EXE_SOURCES)
 $(CSC_V)$(CSC) $(CSCFLAGS) $($*_EXE_CSCFLAGS) -target:exe \
 -out:$@ $($*_EXE_SOURCES) $($*_EXE_RESOURCES)

Enabling secondary expansion allows the use of automatic variables (i.e. $* in this case) in the prerequesites list, which is something that would otherwise not work.

answered Dec 7, 2009 at 3:13
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. That looks like it's exactly what I'm after.

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.