如何知道要更新哪些文件?

时间:2021-04-10 17:13:43

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.

我注意到,当我对一些文件进行更改并输入make时,它将运行与这些文件相关的某些命令。如果我不改变任何东西,那么make什么也不做,说这个程序是最新的。这告诉我make有一种方法可以知道自上次运行以来哪些文件被修改了。它是如何知道的?它似乎没有把任何东西放在它运行的目录中,所以它必须将这些信息存储在其他地方。

4 个解决方案

#1


25  

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.

例如,请参见stat() man页面和struct stat的st_mtime成员。

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:

它有内置的规则,告诉它(例如)如果相应的.c文件发生了更改,则需要重新生成.o文件;关于规则语法的手册部分说:

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).

过期的标准是根据先决条件(由空格分隔的文件名组成)来指定的。(通配符和归档成员(请参阅档案)在这里也被允许。)如果目标不存在,或者大于任何先决条件(通过比较最后修改时间),那么它就过时了。

#2


13  

Make is a lot more sophisticated than it appears on the surface. It has an inference engine with a sophisticated algorithm; when combined with your configuration data make is a type of program+database known as an expert system.

制作比表面看起来要复杂得多。它有一个复杂算法的推理引擎;当与您的配置数据相结合时,make是一种称为专家系统的程序+数据库类型。

Anyway, to answer your question, the check make does is a little bit tricky because there can be multiple chains of prerequisites, and those prerequisite chains can themselves be cross-linked in arbitrary ways.

无论如何,为了回答你的问题,check make做的有点棘手,因为可能有多个先决条件链,而这些先决条件链本身可以以任意的方式进行交叉链接。

So, the answer to "how does it know?" is:

所以,“它怎么知道?”的答案是:

  • make stat(2)'s the files to get their modification times
  • 使stat(2)'s文件以获得其修改时间
  • make then does a topological sort on the graph it made out of the Makefile
  • make然后对从Makefile生成的图进行拓扑排序

You are right, make doesn't normally leave any kind of cookie or other state-tracing file around. (Occasionally some Makefile's do themselves create timestamp cookies, but that's rare.) But that doesn't matter, after all, the last Make run might be unsuccessful anyway. What it does is simply compare the dates on the target files it is making with the dates on the source files. It only cares if the targets are up-to-date, it doesn't normally factor in when make was last run.

您是对的,make通常不会留下任何类型的cookie或其他状态跟踪文件。(有时候一些Makefile会自己创建时间戳cookie,但这种情况很少见。)但这并不重要,毕竟,最后一次尝试可能不会成功。它所做的只是比较目标文件上的日期和源文件上的日期。它只关心目标是最新的,它通常不会影响到最后一次运行。

#3


12  

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:

make通过检查目标文件X的修改时间(如文件系统中记录的那样)是否比它的任何依赖项都要早来确定是否需要重新构建它。例如,如果你有一个制定规则:

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.

然后它知道它需要重新构建foo。如果有的话。c,foo。h或常见。h比foo.o有更新的修改时间。这意味着执行触摸很常见。h将迫使foo。o将在下一次重建。

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.

这意味着使可以困惑如果修改时间不可靠——例如,如果你的系统时钟向后跳,或者如果你存储你的文件在某些网络文件系统和多个客户端访问他们(尤其是网络上的不同机器上的时钟不同步)。如果您正在使用make与分布在网络上的文件一起使用,那么最好运行NTP以保持时钟的正确设置。

#4


3  

It checks is to see if the date/time stamp on the source file is later than that on the corresponding intermediate file (or perhaps the output file - it's been a while since I dealt with make files). If it is then the file needs to be complied. This will then trigger the linking of the final executable.

它检查源文件上的日期/时间戳是否晚于相应的中间文件(或者输出文件,因为我处理了make文件,这已经有一段时间了)。如果是,则需要遵守该文件。这将触发最终可执行文件的链接。

#1


25  

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.

例如,请参见stat() man页面和struct stat的st_mtime成员。

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:

它有内置的规则,告诉它(例如)如果相应的.c文件发生了更改,则需要重新生成.o文件;关于规则语法的手册部分说:

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).

过期的标准是根据先决条件(由空格分隔的文件名组成)来指定的。(通配符和归档成员(请参阅档案)在这里也被允许。)如果目标不存在,或者大于任何先决条件(通过比较最后修改时间),那么它就过时了。

#2


13  

Make is a lot more sophisticated than it appears on the surface. It has an inference engine with a sophisticated algorithm; when combined with your configuration data make is a type of program+database known as an expert system.

制作比表面看起来要复杂得多。它有一个复杂算法的推理引擎;当与您的配置数据相结合时,make是一种称为专家系统的程序+数据库类型。

Anyway, to answer your question, the check make does is a little bit tricky because there can be multiple chains of prerequisites, and those prerequisite chains can themselves be cross-linked in arbitrary ways.

无论如何,为了回答你的问题,check make做的有点棘手,因为可能有多个先决条件链,而这些先决条件链本身可以以任意的方式进行交叉链接。

So, the answer to "how does it know?" is:

所以,“它怎么知道?”的答案是:

  • make stat(2)'s the files to get their modification times
  • 使stat(2)'s文件以获得其修改时间
  • make then does a topological sort on the graph it made out of the Makefile
  • make然后对从Makefile生成的图进行拓扑排序

You are right, make doesn't normally leave any kind of cookie or other state-tracing file around. (Occasionally some Makefile's do themselves create timestamp cookies, but that's rare.) But that doesn't matter, after all, the last Make run might be unsuccessful anyway. What it does is simply compare the dates on the target files it is making with the dates on the source files. It only cares if the targets are up-to-date, it doesn't normally factor in when make was last run.

您是对的,make通常不会留下任何类型的cookie或其他状态跟踪文件。(有时候一些Makefile会自己创建时间戳cookie,但这种情况很少见。)但这并不重要,毕竟,最后一次尝试可能不会成功。它所做的只是比较目标文件上的日期和源文件上的日期。它只关心目标是最新的,它通常不会影响到最后一次运行。

#3


12  

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:

make通过检查目标文件X的修改时间(如文件系统中记录的那样)是否比它的任何依赖项都要早来确定是否需要重新构建它。例如,如果你有一个制定规则:

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.

然后它知道它需要重新构建foo。如果有的话。c,foo。h或常见。h比foo.o有更新的修改时间。这意味着执行触摸很常见。h将迫使foo。o将在下一次重建。

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.

这意味着使可以困惑如果修改时间不可靠——例如,如果你的系统时钟向后跳,或者如果你存储你的文件在某些网络文件系统和多个客户端访问他们(尤其是网络上的不同机器上的时钟不同步)。如果您正在使用make与分布在网络上的文件一起使用,那么最好运行NTP以保持时钟的正确设置。

#4


3  

It checks is to see if the date/time stamp on the source file is later than that on the corresponding intermediate file (or perhaps the output file - it's been a while since I dealt with make files). If it is then the file needs to be complied. This will then trigger the linking of the final executable.

它检查源文件上的日期/时间戳是否晚于相应的中间文件(或者输出文件,因为我处理了make文件,这已经有一段时间了)。如果是,则需要遵守该文件。这将触发最终可执行文件的链接。