Reuse percent sign in Makefile prerequesite as subdirectory name
bash, makefile, unix
Solution
you can use VPATH to specify a list of directories that make should search.
exemplary makefile:
# Find all tex files
tex := $(shell find -iname '*.tex')
# Make targets out of them
PDFS := $(notdir $(tex:%.tex=%.pdf))
# specify search folder
VPATH := $(dir $(tex))
all : $(PDFS)
%.pdf : %.tex
pdflatex $<
or even better would be to use vpath (lowercase):
vpath %.tex $(dir $(tex))
it will look only for .tex files in those directories.
Problem
I'd like to learn how to re-use the % sign in my makefile target's prequisite, supposing that the target is `X.pdf`, and the prerequesite is in `X/X.tex`. To elaborate, I currently have a makefile like so: ``` all: foo.pdf %.pdf: %.tex pdflatex $*.tex ``` I additionally have a file `foo.tex`, and when I type `make` it will make `foo.pdf` by running `pdflatex foo.tex`. Now for various reasons I can't control, my directory structure has changed: ``` my_dir |- Makefile |- foo |- foo.tex ``` I'd like to modify my Makefile such that when it tries to make `X.pdf`, it looks for the file `X/X.tex`. I tried the following (I tried to put '%/%.tex' to tell it to look for `foo/foo.tex`): ``` all: foo.pdf %.pdf: %/%.tex pdflatex $*/$*.tex ``` However, this yields: ``` No rule to make target `foo.pdf', needed by `all'. Stop. ``` So does `%.pdf: $*/$*.tex`. If I change the `%/%.tex` to `foo/%.tex` it works as expected, but I don't want to hard-code the `foo` in there, because in the future I'll do `all: foo.pdf bar.pdf` and it should look for `foo/foo.tex` and `bar/bar.tex`. I'm fairly new to Makefiles (experience limited to modifying someone else's to my needs), and have never done a more-than-absolutely basic one, so if anyone could give me a pointer that would help (I don't really know what words to search for in the Makefile documentation - the only ones that looked promising were `%` and `$*`, which I couldn't get to work).