Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pattern-dependent variables in dependencies in make pattern rules

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?

like image 828
RAOF Avatar asked Feb 27 '23 23:02

RAOF


1 Answers

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.

like image 75
Dan Moulding Avatar answered Apr 28 '23 05:04

Dan Moulding