tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: make: creating a file's parent directory



On Tue, Jul 14, 2015 at 07:18:20PM +0200, Edgar Fu? wrote:
 > There must be an easier way doing what I want.
 > 
 > I have a rule generating dir/file and I need to make sure dir exists before 
 > that rule is run.
 > Now it's tempting to write
 > 
 > dir:
 > 	test -d ${.TARGET} ||?${INSTALL} -d ... ${.TARGET}
 > 
 > dir/file: dir ...
 > 	... >${.TARGET}
 > 
 > but that causes dir/file's rule to be re-run if you touch dir/another_file, 
 > thus updating dir's time stamp.

Yes, in general nothing good comes of depending on directories in
make. It gets worse if you want to make multiple levels of
directories, when it becomes tempting to have each layer depend on the
layer above it.

I don't think there are any particularly good ways - you can do
something like

dir:
	mkdir dir

.dir.stamp:
	@[ -d dir ] || $(MAKE) dir

dir/file: .dir.stamp
	... >${.TARGET}

because then the directory is only depended on if it doesn't exist,
which is relatively harmless. (The reason to have the third layer of
rule for "dir" and to make it explicitly, instead of just running
mkdir in the .dir.stamp rule, is if you want to have trees or multiple
related directories or other complications.)

It sucks. Make ought to be able to reason separately about "exists"
and "up to date" -- this also comes up when dealing with autodepend
rules.

-- 
David A. Holland
dholland%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index