I searched quite a bit to find the solution for this but no luck.
I have some source code that produces HTML and am using a Makefile for this. The directory structure is:
main/
main/Makefile
main/src
main/src/dox
main/src/dox/generate.rb
...
The relevant portion of the Makefile is:
sources := $(shell find src -name "*rb" )
pages := $(sources:src/%.rb=%.html)
all: $(pages)
%.html: src/%.rb
ruby $< > $@
make -dr produces:
(snip)
Considering target file 'dox/generate.html'.
File 'dox/generate.html' does not exist.
Looking for an implicit rule for 'dox/generate.html'.
Trying pattern rule with stem 'generate'.
Trying implicit prerequisite 'dox/src/generate.rb'.
Trying pattern rule with stem 'generate'.
Trying implicit prerequisite 'dox/src/generate.rb'.
Looking for a rule with intermediate file 'dox/src/generate.rb'.
Avoiding implicit rule recursion.
No implicit rule found for 'dox/generate.html'.
Finished prerequisites of target file 'dox/generate.html'.
Must remake target 'dox/generate.html'.
make: *** No rule to make target 'dox/generate.html', needed by 'build'. Stop.`
How can I correct this?
Clearly, make is looking for the source file in the wrong directory, but the rule seems very simple and straightforward.
"Wrong" in the sense of "not the directory you want it to look in", of course. There is no actual misbehavior here, in that
makeis behaving exactly as documented.When
makeconsiders the pattern rule in the question for building targetdox/generate.html, the pattern matches with stemdox/generate. When the target pattern does not have any literal directory components, as in this case, the leading directory portion of that stem gets special treatment:(GNU make manual)
This is how GNU
makesupports pattern rules structured for building targets in the same directory as their corresponding source, whatever directory that may be. This is the traditional approach formake-based build systems.And that is why you see
makelooking for filedox/src/generate.rbwhen the stem isdox/generate.htmland the prerequisite pattern issrc/%.html.It's unclear what other files you want to build with this rule, but possibly this is what you're looking for:
When, as there, the target pattern does have an explicit leading directory component, the stem is treated as a flat string, with no special treatment of directory components.