theicfire/makefiletutorial

Prerequisite target for compiling % wildcard

Closed this issue · 3 comments

In chapter https://makefiletutorial.com/#automatic-variables-and-wildcards I can not find an example how to require some other target before processing % pattern rule.

For example, I want to execute bootstrap rule before passing .in files to echo.

default: *.txt

bootstrap: boot.txt

# Define a pattern rule that compiles every .in file into a .txt file
%.txt: bootstrap %.in
        echo $^ $@

This executes the command passing "bootstrap" string to it, which I did not expect.

echo bootstrap xxx.in xxx.txt

You can put bootstrap as a prereq for the default target. Also, boot.txt is an invalid prereq, because there is no target for it.

You also made the unfortunate mistake of using * without the wildcard. See this:
image

Here's my best guess as to what you want:

default: bootstrap $(wildcard *.in)

boot.txt:
	echo "hi" > boot.txt

bootstrap: boot.txt

# Define a pattern rule that compiles every .in file into a .txt file
%.in: FORCE
	echo $@
	echo $(patsubst %.in,%.txt,$@)

FORCE: 

I ended up using the hack with $< which only passes the first parameter.

-%.txt: bootstrap %.in
-        echo $^ $@
+%.txt: %.in bootstrap
+        echo $< $@

Nice! Glad to hear it.