help-make
[Top][All Lists]
Advanced

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

Re: How does make compare the timestamp if the file does not exist


From: John Calcote
Subject: Re: How does make compare the timestamp if the file does not exist
Date: Mon, 13 Jul 2009 21:39:22 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1b3pre) Gecko/20090223 Thunderbird/3.0b2

Hello,

On 7/13/2009 8:51 PM, 雷 高 wrote:
If the target of a rule does not exist or the timestamp of the target is older than any of the prerequisites, the target is out of date. My question is that if some files of prerequisites does not exist, how can we compare the timestamp.
For example:
foo : boo
        @echo foo
boo :
        @echo boo
Then if foo already exists, but the rule for "boo" does not create the file boo, so how compare the timestamp of foo and boo.
When I run the makefile, the console prints :
boo
foo
I want to know why the make parses the makefile like this.

A more general problem is that how the make treat files which doesn't exist and can't be created by explicit rules and implicit rules.
If a file doesn't exist, it's considered "out of date" and is thus rebuilt, just as you surmised.

Make builds a dependency tree based on the rules in the makefile. It then traverses the dependency tree, starting with the first target listed in the makefile (in this case, foo). It processes the tree in a depth-first manner, not actually executing any commands until it finds the first dependency in the traversal that doesn't have any of its own dependencies. If any of the current node's (zero or more) dependencies are newer than the target, or if the target is missing (or declared "phony"), then make runs its commands and moves to the next node along the traversal path. It continues in this manner until it has returned to the original node.

This is why you see "boo" first, followed by "foo" on your screen, when you execute make. The commands for the leaf nodes are executed before those of higher level nodes in order to ensure that dependencies are built before targets that need those files.

When you don't actually build a target with a rule's commands (as in your example), then make processes higher-level targets, as if their dependencies were actually built. The comparison timestamp for those dependencies becomes "now", even if the files don't exist after their commands are executed. And "now" is always newer than a non-existent file. If the current target actually does exist, then "now" is still going to be newer than any timestamp on that file, so it will be rebuilt.

Regards,
John

reply via email to

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