如何创建基于时间的 Makefile 规则?
我希望有一个 Makefile 目标,仅当目标文件早于某个时间间隔时才会重建。
举个例子,假设我有某种方法可以生成一天有效的密钥,但生成它需要相当长的时间。我可以在每次需要时重新生成它:
.PHONY: key
key:
sleep 5 && echo generated > key
foo: key
echo foo
bar: key
echo bar
但是,在一天中,我可能会多次键入 make foo
或 make bar
。每次等待都烦人,我宁愿每天只吃一次这个费用。
I would like to have a Makefile target that gets rebuilt only if the target file is older than some time interval.
As an example, say that I have some way of generating a key that is valid for one day, but generating it takes a non-trivial amount of time. I could just regenerate it each time I needed it:
.PHONY: key
key:
sleep 5 && echo generated > key
foo: key
echo foo
bar: key
echo bar
But, over the course of the day, I might type make foo
or make bar
quite a few times. Waiting each time is annoying, and I would rather just eat this cost once per day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让生成的文件依赖于一些虚拟文件,例如
key-timestamp
,然后每天执行一个 cron 作业touch
该文件。Have the generated file depend on some dummy file like
key-timestamp
, then have a cron jobtouch
that file every day.如何保留在 make 运行期间计算的哨兵文件?
How about keeping a sentinel file that is computed during the make run?
应该按预期工作,如果您无权访问
find
,则必须将其替换为类似的内容。基本上,将其替换为能够检测该密钥上次修改时间为 1 天或更早的内容。.PHONY
< /a> 将强制规则运行。我还深入研究
order-only -先决条件
,这需要手头运行两次makefile以每24小时仅更新一次密钥。不太实用。然而,这是一个技巧,我不建议将其作为 makefile 的正常使用。
Should work as expected, if you don't have access to
find
you must replace it with something similar. Basically, replace it by something that will detect that key have been last modified 1 days or more ago..PHONY
will force the rule to run.I also dig into
order-only-prerequisites
, this need to run the makefile twice at hand to update the key only one every 24 hours. Not very practical.This is however a trick I would not recommend it as a normal use of a makefile.
要在一段时间后重建 make 目标,您需要一个帮助程序文件,依赖于该文件并强制触摸帮助程序,例如(示例是每日):
To rebuild a make target after a certain time, you would need a helper file, depend on that and touch the helper by force, like (example is daily):