如何创建基于时间的 Makefile 规则?

发布于 2024-08-16 22:05:18 字数 337 浏览 4 评论 0原文

我希望有一个 Makefile 目标,仅当目标文件早于某个时间间隔时才会重建。

举个例子,假设我有某种方法可以生成一天有效的密钥,但生成它需要相当长的时间。我可以在每次需要时重新生成它:

.PHONY: key
key:
    sleep 5 && echo generated > key
foo: key
    echo foo
bar: key
    echo bar

但是,在一天中,我可能会多次键入 make foomake 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁的新欢旧爱 2024-08-23 22:05:18

让生成的文件依赖于一些虚拟文件,例如 key-timestamp,然后每天执行一个 cron 作业touch 该文件。

Have the generated file depend on some dummy file like key-timestamp, then have a cron job touch that file every day.

oО清风挽发oО 2024-08-23 22:05:18

如何保留在 make 运行期间计算的哨兵文件?

sentinel_file_prefix  = sentinel_file.stamp
sentinel_file         = $(sentinel_file_prefix).$(shell date +%Y%m%d)

file_to_regenerate_every_day: $(sentinel_file)
    echo Usual make recipe

$(sentinel_file):
    - rm $(sentinel_file_prefix).* 
    touch $@

How about keeping a sentinel file that is computed during the make run?

sentinel_file_prefix  = sentinel_file.stamp
sentinel_file         = $(sentinel_file_prefix).$(shell date +%Y%m%d)

file_to_regenerate_every_day: $(sentinel_file)
    echo Usual make recipe

$(sentinel_file):
    - rm $(sentinel_file_prefix).* 
    touch $@
幽梦紫曦~ 2024-08-23 22:05:18
.PHONY: key.update key.check

foo: key.check
    echo foo

bar: key.check
    echo bar

key.check: key
    @find key -mmin +1440 -exec make key.update \;

key:
    make key.update

key.update:
    sleep 5 && echo generated > key

应该按预期工作,如果您无权访问 find,则必须将其替换为类似的内容。基本上,将其替换为能够检测该密钥上次修改时间为 1 天或更早的内容。

.PHONY< /a> 将强制规则运行。

我还深入研究 order-only -先决条件,这需要手头运行两次makefile以每24小时仅更新一次密钥。不太实用。

然而,这是一个技巧,我不建议将其作为 makefile 的正常使用。

.PHONY: key.update key.check

foo: key.check
    echo foo

bar: key.check
    echo bar

key.check: key
    @find key -mmin +1440 -exec make key.update \;

key:
    make key.update

key.update:
    sleep 5 && echo generated > key

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.

二智少女猫性小仙女 2024-08-23 22:05:18

要在一段时间后重建 make 目标,您需要一个帮助程序文件,依赖于该文件并强制触摸帮助程序,例如(示例是每日):

target:  target.helper
    script-or-rule

target.helper: FORCE
    @touch -d -1day $@

FORCE:

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

target:  target.helper
    script-or-rule

target.helper: FORCE
    @touch -d -1day $@

FORCE:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文