The Makefile below has to create (multiple) output directories, and generate output in those directories, from input in the directory above. So, on entry, dirn exists, and dirn/file.foo exists. The build has to create dirn/out/file.bar.
This Makefile works when run as a single job (note that it creates the two required source directories and files in the $(shell)
). It presumably works because makedirs
is the first/leftmost prerequisite for all
. However, it doesn't work for a multi-job make (ie. make -j4
/whatever).
Any ideas on how to fix the dependencies to ensure that the output directories are made before they're required?
EDIT
I should have made it clear that I have tried various order-only prerequisite solutions, but I couldn't do this and guarantee that the target actually rebuilt (the point of order-only is generally to prevent rebuilding, not enforce dependency ordering). If you have an OO solution, please check it! Thanks.
# expected output:
# made directories
# copying dir1/out/../file.foo to dir1/out/file.bar
# copying dir2/out/../file.foo to dir2/out/file.bar
# created all output files
# done
$(shell mkdir dir1 >& /dev/null; touch dir1/file.foo; \
mkdir dir2 >& /dev/null; touch dir2/file.foo)
OUTDIRS = dir1/out dir2/out
OUTPUTS = dir1/out/file.bar dir2/out/file.bar
.DEFAULT_GOAL := all
.PHONY: makedirs $(OUTDIRS)
.SUFFIXES: .foo .bar
%.bar : ../%.foo
@echo "copying $< to $@"
@cp $< $@
all : makedirs outputs
@echo "done"
outputs : $(OUTPUTS)
@echo "created all output files"
makedirs : $(OUTDIRS)
@mkdir -p $(OUTDIRS)
@echo "made directories"
clean :
@rm -rf dir1 dir2