When writing non-trivial makefiles, two needs occur occasionally. One is some way to collect common commands together without duplicating too much code. Another is adding color to some output, making it stand out (for example, this is done by makefiles generated by CMake).

Here's a short makefile snippet that shows how to achieve this on modern Linux terminals:

define colorecho
      @tput setaf 6
      @echo $1
      @tput sgr0
endef

Defines a makefile function named colorecho, with a single argument which is exposed via $1. This function sets the terminal color to cyan with tput, echoes its argument and resets the terminal settings to default.

Here's how this function can be called:

$(call colorecho,"Linking with" $(LD))
$(LD) $^ -o $@

Comments

comments powered by Disqus