Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress and ignore output for Makefile?

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.

like image 843
Andrew Lee Avatar asked Aug 08 '12 19:08

Andrew Lee


People also ask

How do I hide the output of my makefile?

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.

How do I ignore in makefile?

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.

What is @echo in makefile?

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.


1 Answers

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:

  1. Part of the build is broken and you want to continue anyway, in which case you’ve got some learning to do. The build is broken and needs to be fixed, not bandaided in an unmaintainable way.
  2. A command returns a non-zero exit code even though it did exactly what you wanted, in which case you don’t really want Make to issue a warning.

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.

like image 76
andrewdotn Avatar answered Oct 12 '22 22:10

andrewdotn