I know that the @
prefix suppresses output from a shell command in Makefiles, and also that the -
prefix will ignore errors from a shell command. Is there a way to combine the two, i.e. a prefix that suppresses output and ignores errors? I don't think @-
or -@
works.
If you want to inhibit the display of commands during a particular make run, you can use the -s option. If you want to inhibit the display of all command lines in every run, add the special target . SILENT to your makefile . and there is no file named xyz , make halts after rm returns its exit status.
To ignore errors in a recipe line, write a ' - ' at the beginning of the line's text (after the initial tab). The ' - ' is discarded before the line is passed to the shell for execution.
The ' @ ' is discarded before the line is passed to the shell. Typically you would use this for a command whose only effect is to print something, such as an echo command to indicate progress through the makefile: @echo About to make distribution files.
Actually, @-
and -@
both do work, but will print a make: [target] Error 1 (ignored)
warning.
Instead, you can use
@command || true
or, since :
is shorthand for true
in shell,
@command ||:
This often a better thing to do, because it avoid Make’s confusing warning that an error was ignored in an invisible command.
Consider the two most common cases where you might want to ignore the return value of a command:
For the second case, consider the example of grepping for warnings in the log file produced by a command. grep
will return an error if it does not find a match, which is not what you want:
.PHONY: all one two three all: at-warning at-success or-success or-warning at-%: %.log @echo Making $@ @-grep ^Warning $< or-%: %.log @echo Making $@ @grep ^Warning $< ||: success.log: echo 'Success!' > $@ warning.log: echo 'Warning: foo' > $@ clean:: rm -f {success,warning.log}
produces:
echo 'Warning: foo' > warning.log Making at-warning Warning: foo Making at-success make: [at-success] Error 1 (ignored) Making or-success Making or-warning Warning: foo
Using @-
produces a nonsensical ignored error warning when there is success, while || true
handles both warnings and the absence of warnings without complaint.
Theoretically using || true
is slower than using @-
, but this overhead is unlikely to be a bottleneck in well-designed and -maintained build systems. The vast majority of the time should be spent building, or checking timestamps when there is nothing to build, not in running the thousands of quick commands whose return values all get ignored that would be necessary for this to have a measurable performance impact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With