Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does make know which files to update

I noticed that when I make changes to some files and then I type make, it will run certain commands related to those files. If I don't change anything, then make doesn't do anything, saying that the program is up to date. This tells me that make has a way of knowing which files were changed since it was last run. How does it know? It doesn't seem to put anything in the directory where it is run, so it must be storing this information somewhere else.

like image 524
neuromancer Avatar asked Nov 24 '09 12:11

neuromancer


People also ask

How does make know that a source code file needs to be recompiled?

How does make know that a source code file needs to be recompiled? Freshness Check: Speed is the reason we use make. For maximum speed, make does the least amount of work to determine the freshness of the compiled object code.

How makefiles are remade?

In the case of makefiles, a makefile that has a double-colon rule with a recipe but no prerequisites will be remade every time make is run, and then again after make starts over and reads the makefiles in again.


2 Answers

It inspects the file system's modification date meta information.

See, for instance, the stat() man page and the st_mtime member of the struct stat.

It has built-in rules that tells it that (for instance) a .o file needs to be re-generated if the corresponding .c file has changed; the manual section on rule syntax says:

The criterion for being out of date is specified in terms of the prerequisites, which consist of file names separated by spaces. (Wildcards and archive members (see Archives) are allowed here too.) A target is out of date if it does not exist or if it is older than any of the prerequisites (by comparison of last-modification times).

like image 85
unwind Avatar answered Sep 21 '22 21:09

unwind


make determines if target file X needs to be rebuilt, by checking to see if its modification time (as recorded in the filesystem) is older than any of its dependencies. For example, if you had a make rule like:

foo.o: foo.c foo.h common.h 

Then it knows it needs to rebuild foo.o if any of foo.c, foo.h or common.h have a newer modification time than foo.o. This means that executing touch common.h would force foo.o to be rebuilt at the next make.

This means that make can be confused if the modification times are unreliable - for example, if your system clock has jumped backwards, or if you are storing your files on certain network filesystems and have multiple clients accessing them (particularly if the clocks on the various machines on your network are not in synch). If you're using make with files distributed over a network, it's generally a good idea to run NTP to keep your clock set correctly.

like image 39
caf Avatar answered Sep 22 '22 21:09

caf