help-make
[Top][All Lists]
Advanced

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

Re: how to create a single rule to build a directory as necessary


From: Paul Smith
Subject: Re: how to create a single rule to build a directory as necessary
Date: Tue, 22 Sep 2009 12:18:54 -0400

On Tue, 2009-09-22 at 11:44 -0400, Sam Steingold wrote:
> Hi,
> I, too, am struggling with rebuilding a directory.
> it appears that the rule:
> 
> gllib: config.status
>       mkdir -p gllib; cd gllib; make
> 
> is wrong because the script "mkdir ..." is always executed, regardless of 
> whether config.status has changed or not.
> So, what is the right way to handle this?

The short answer is that using a directory as a target in make is almost
never correct, unless it's a .PHONY target or the equivalent.

Why is that?  It's because make doesn't treat directories any different
than files, when it computes whether the item is up to date: if the time
modified on the directory is newer than all its prerequisites then the
command is not executed.  If the modification time on the directory is
older than any of its prerequisites, then the command is executed.

The problem is that the modification time on a directory means something
that is not really appropriate for make: the modification time on a
directory changes whenever the DIRECTORY ITSELF changes.  When does a
directory change?  It changes whenever any element in the directory is
renamed or deleted, or a new element is created in the directory.  In
short, if you run "ls -a" before and after some command and the output
is identical, then the directory was not modified.


So, in your case, running the command "mkdir -p gllib; cd gllib;
make" (which by the way, is not well-formed; it should be:

        mkdir -p $@ && cd $@ && $(MAKE)

), if it doesn't create, remove, or rename any files in the "gllib"
directory, will not change the modified time on the directory.  And
hence, when you run "make" again, it will see that the file
"config.status" is still newer than the directory "gllib", and it will
re-run the rule.


Your problem here is more conceptual than anything.  What, in words, are
you really trying to get make to do?  How does the upper level makefile
know, really, that it does or doesn't need to invoke the sub-make?  It
can't know that... unless it invokes the submake to see whether anything
needs to be done.

In a standard recursive make environment, sub-makes are almost always
set up as .PHONY, so they're always invoked, to be sure that their
content is up to date.  That's why recursive makes are slower,
typically, than non-recursive makes.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]