[HN Gopher] Tell HN: TIL: GNU Make .DELETE_ON_ERROR
___________________________________________________________________
Tell HN: TIL: GNU Make .DELETE_ON_ERROR
If you have a make recipe which creates the target file, or
modifies the timestamp of the existing out-or-date target, and that
recipe fails, make will not delete the target. Thus it looks up-to-
date, even though the recipe failed. The manual says: _" So
generally the right thing to do is to delete the target file if the
recipe fails after beginning to change the file. make will do this
if .DELETE_ON_ERROR appears as a target. This is almost always what
you want make to do, but it is not historical practice; so for
compatibility, you must explicitly request it."_ So, hidden in the
manual is the recommendation that, it's good practice to have
.DELETE_ON_ERROR: somewhere in every GNU Makefile. An
example where this is relevant: foo: bar
generate bar > foo # redirection is used redirection will
create the file or touch the timestamp of the existing one before
the _generate_ command is run. What if bar has syntax errors and
generate fails? The foo file was touched and so looks updated. If
you run "make foo" again, Make reports that foo is up-to-date.
With .DELETE_ON_ERROR, foo will be deleted. If foo exists and its
timestamp is not changed, then it's left alone. If a recipe
creates or touches the target, and fails due to Make being
interrupted by a signal, in that case make deletes the target
without any option having to be specified.
Author : kazinator
Score : 22 points
Date : 2023-09-22 19:31 UTC (3 hours ago)
| rurban wrote:
| That's why you do foo: bar generate
| bar > tmp && mv tmp foo
|
| instead
| 10000truths wrote:
| On Linux, you can use O_TMPFILE to ensure that the temporary
| file won't linger around if an error occurs:
| foo: bar generate bar | python3 -c "import os,sys;f
| =os.open('.',os.O_RDWR|os.O_TMPFILE);os.write(f,sys.stdin.buffe
| r.read());os.link(f'/proc/self/fd/{f}','./foo')"
| admax88qqq wrote:
| Lol. Files were a mistake.
___________________________________________________________________
(page generated 2023-09-22 23:02 UTC)