Makefile processing files with same extension -
this seems related how write makefile target , source files have same extension?. in question extensions same, input , output files seem in same directory , filenames being conditionally renamed.
i have large collection of .txt files in ../src/ need processed, , dumped ./ (which directory called target/) txt files of same name. want use make, files in ../src/ have been changed updated in ./. prototype working before put real code in.
my makefile in ./ follows:
dir = ../src inputs = $(wildcard $(dir)/*.txt) outputs = $(patsubst $(dir)/%.txt,%.txt,$(inputs)) all: $(outputs) .phony: $(inputs) check: @echo "dir = $(dir)" @echo "inputs = $(inputs)" @echo "outputs = $(outputs)" %.txt: $(dir)/%.txt sed -e "s/test/cat/g" "$<" > $@ for now, contents of ../src/ test1.txt , test2.txt. makefile stands now, running make test2.txt generates file expected.
target/ $ make test2.txt sed -e "s/test/cat/g" "../src/test2.txt" > test2.txt running make check shows inputs , outputs correctly.
target/ $ make check dir = ../src inputs = ../src/test1.txt ../src/test2.txt outputs = test1.txt test2.txt if run make all, generates every file, every time. expected .phony $(inputs) line in there.
if remove .phony $(inputs) target, make gets bound in trying find target make ../src/test1.txt , keeps prefixing $(dir) in front of until makes long of filename , gives up.
make: stat: ../src/../src/../src/ [repeat few pages] ../src/../src/test1.txt: file name long make: stat: ../src/../src/../src/ [repeat few pages] ../src/../src/../src/test1.txt: file name long make: *** no rule make target `../src/../src/../src/[repeat]../src/../src/test1.txt', needed `../src/[repeat]../src/../src/test1.txt'. stop. it never processing test2.txt.
as drafting this, had idea remove ../ dir, , relocate makefile parent both src/ , target/. approach seems work, isn't ideal. there chain of these makefiles, each pulling 1 directory another.
is there way keep makefile in 'target/' along generated destination files, , base destination files off of in relative path?
replace
%.txt: $(dir)/%.txt with:
${curdir}/%.txt: $(dir)/%.txt this way %.txt not match .txt file in directory. in other words, limit rule's scope files in ${curdir}/ , prevents endless recursion.
see §10.5.4 how patterns match more details.
it practice avoid relative paths:
dir = $(abspath ../src)
Comments
Post a Comment