DEV Community

Naveen
Naveen

Posted on

Silence the makefile recipes

Makefile is made up of recipes. Running a recipe in make, prints the recipe and also the output of every command in the recipe. Recipe can be implemented by re-directing the output to /dev/null device. But this approach has a problem. Sometimes, I wanted to read the output of the command and not be re-directed to /dev/null, especially during troubleshooting. In this situation, there is no other way but to change implementation of recipe to remove the redirection.

Makefile has a switch -s to silence the printing of the recipe during execution. I wanted to use this switch to decide for redirection.

ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
  .REDIRECT = >/dev/null 2>&1
else
  .REDIRECT =
endif

test:
    which scala $(.REDIRECT) || printf "scala is not present."
Enter fullscreen mode Exit fullscreen mode

Now, make test prints the recipe and output of the which command. But make test -s completely silences the recipe execution.

Makefile provides a way to silence the echoing of recipes but the implementation of recipe, as in this case, uses conditional redirection to silence the output of commands in the recipe.

This approach, now, becomes less noisy until recipe requires to be debugged.

Top comments (0)