开发者

How can I create a time-based Makefile rule?

开发者 https://www.devze.com 2022-12-15 08:52 出处:网络
I would like to have a Makefile target that gets rebuilt only if the target file is older than some time interval.

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 gener开发者_如何学编程ating 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.


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


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 $@


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


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

精彩评论

暂无评论...
验证码 换一张
取 消