wei
wei

Reputation: 6889

How does gnu Make handle the dependencies?

we currently generate a dependency file for every .o. But when do an incremental build, Make reads from the dependency file for dependencies for each .o. Is Make checking the time stamp of these dependent files and compare it with the .o? If so, is it possible to cache the status of dependencies to avoid too much I/O hit because of duplicated status checks for each object file?

for example, 
a.o: h1.h h2.h
    gcc...
b.o: h1.h h2.h
    gcc...

If we cache the status of h1.h and h2.h when it builds a.o, do we save two checks when build b.o?

I am not familiar with the make system, but is currently looking for ways to improve its performance on a large legacy C project.

Upvotes: 1

Views: 729

Answers (1)

Eldar Abusalimov
Eldar Abusalimov

Reputation: 25483

Use strace for that purpose:

strace -e trace=stat make --touch

Output of the first run (full build):

...
stat("a.o", 0x7fff70c35f00)             = -1 ENOENT (No such file or directory)
stat("h1.h", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
stat("h2.h", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
touch a.o
stat("b.o", 0x7fff70c35f00)             = -1 ENOENT (No such file or directory)
touch b.o

And the second run (incremental build):

...
stat("a.o", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
stat("h1.h", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
stat("h2.h", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
stat("b.o", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
make: Nothing to be done for `all'.

As you can see, GNU Make caches timestamps avoiding unnecessary stat syscalls. However, I guess, things are not so good in case of using recursive make.

Upvotes: 2

Related Questions