help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variably determined prerequisites


From: NightStrike
Subject: Re: Variably determined prerequisites
Date: Wed, 1 Apr 2020 16:35:40 -0400

On Wed, Apr 1, 2020 at 4:23 PM Paul Smith <address@hidden> wrote:
>
> On Wed, 2020-04-01 at 16:03 -0400, NightStrike wrote:
> > I am trying to use a pattern rule to pick which variable gets added
> > to a target's prereq list.
>
> All of the confusion here is due to not quite grasping make's rules for
> expanding variables.
>
> Make operates in two stages: in the first stage all makefiles are
> parsed and an internal graph of dependency relationships are created.
> This stage is described here:
> https://www.gnu.org/software/make/manual/html_node/Parsing-Makefiles.html
>
> In the second stage, make walks that internal graph and actually runs
> recipes to try to bring targets up to date.
>
> All variables that are needed to create the graph are expanded during
> the first stage, when make is parsing makefiles.  This expansion
> happens immediately, as the line is read in from the file.
>
> This means all variables that appear in either target or prerequisite
> ists are expanded immediately in the first stage.
>
> Variables that are not needed until the second stage are not expanded
> until make tries to actually run a recipe.
>
> This is discussed in the manual, here:
> https://www.gnu.org/software/make/manual/html_node/Reading-Makefiles.html
>
> This explains your behavior.  When your variables appear in a target or
> recipe they are expanded right then, as the line is parsed.  It doesn't
> matter whether they are defined with "=" or ":=".  For pattern rules,
> since we're not actually trying to build any targets (that happens in
> the second phase), there is no way to expand the pattern value so it
> just expands the literal string "extra%" as a variable.
>
>
> You should be able to use secondary expansion (see the manual) to do
> what you want:
>
>   .SECONDEXPANSION:
>   xx%: xx%.o $$(extra$$*)
>           echo $@ $^
>
> I didn't test this.

Thank you for such a swift, detailed, and correct response!  Certainly
this is what I want, it works perfectly, and the example in the manual
is even exactly what I want to do:
https://www.gnu.org/software/make/manual/make.html#Secondary-Expansion

Thank you for helping to explain this without making me feel stupid :)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]