Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call functions within order-only prerequisites?
    text
    copied!<p>Given this bit of Makefile:</p> <pre><code># for pattern matching $(OBJDIR) := build # just to see if a level of indirection will work my_dir = $(dir $(1)) $(OBJECTS) : $(OBJDIR)/% : $(HEADERS) $(SRCDIR)/% | % $(dir %) $(call my_dir,%) @echo output-only = $| </code></pre> <p>This is a <a href="http://www.gnu.org/software/make/manual/make.html#Static-Pattern" rel="nofollow">static pattern rule</a> with <a href="http://www.gnu.org/software/make/manual/make.html#Prerequisite-Types" rel="nofollow">order-only prerequisites</a>.</p> <p>Consider the target "build/utility/debug.js". The output of the above rule will be this:</p> <pre><code>output-only = utility/debug.js ./ </code></pre> <ul> <li>The first component, "utility/debug.js", is properly copied from the stem (%).</li> <li>The second component, "./", is the output of calling the <code>dir</code> function in the prerequisites list.</li> <li>The third component, an empty string, is the output of calling my <code>my_dir</code> function in the prerequisites list.</li> </ul> <p>If I change <code>my_dir</code> to this:</p> <pre><code>my_dir = $(1) </code></pre> <p>The output remains the same. If I change it to this:</p> <pre><code>my_dir = "foo" </code></pre> <p>Then make complains there is no rule to make "foo" (which is expected). It appears, then, that <code>$(1)</code> is not getting bound in the call to <code>my_dir</code>.</p> <p>What's going on? Why can't I pass the stem to a function? I have a workaround that uses secondary expansion, but I want to know why I can't write my rule this way.</p> <p><strong>EDIT:</strong> I'm new to stackoverflow, forgive me if this is not the way things are done here.</p> <p>I want $(1) because I am passing the stem as an argument to <code>my_dir</code>, as Alex pointed out.</p> <p>I don't know why it was suggested I want "$". I don't believe $ by itself expands to anything in any context.</p> <p>I know that automatic variables are only available in the recipe. I am not using an automatic variable in the prerequisites - I am using the stem:</p> <blockquote> <p>Each target is matched against the target-pattern to extract a part of the target name, called the stem. This stem is substituted into each of the prereq-patterns to make the prerequisite names (one from each prereq-pattern). <a href="http://www.gnu.org/software/make/manual/make.html#Static-Usage" rel="nofollow">- the manual</a></p> </blockquote> <p>The fact that the stem is available is demonstrated by the example: the stem expands to the correct value when used alone, but not when passed to a function.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload