Skip to main content
Redirect stderr of make command so it goes to the sed command
Source Link
make(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" ( set -o pipefail CLICOLOR_FORCE=yes_please command make "$@" 2>&1 | sed --unbuffered "${subst}" ) } 
make(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" ( set -o pipefail CLICOLOR_FORCE=yes_please command make "$@" | sed --unbuffered "${subst}" ) } 
make(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" ( set -o pipefail CLICOLOR_FORCE=yes_please command make "$@" 2>&1 | sed --unbuffered "${subst}" ) } 
Forgot to add `set -o pipefail` from my version
Source Link
make(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" ( set -o pipefail CLICOLOR_FORCE=yes_please command make "$@" | sed --unbuffered "${subst}"  ) } 
  • You have to use \x1b in sed because it doesn't understand \033.

  • & expands to the part of the line that was matched.

  • --unbuffered is necessary otherwise your output will either come out in large chunks or in a single chunk once the whole make command finishes.

  • command avoids recursively calling the make shell function.

  • "@" forwards the arguments given to the make shell function to command make. This will include VERBOSE=1 and anything else.

  • CLICOLOR_FORCE is to have the colored output from the Makefile that CMake generates which normally gets turned off if when the output is not a TTY (ex: a pipe).

  • The subshell with set -o pipefail is because with a pipe, you normally get the return code of the last command in the pipeline. Activating the option pipefail will cause the return code of the pipeline to be 0 if all commands returned 0, or the return code of the right-most command that returned a non-zero code (i.e. (exit 8) | true | (exit 9) | true would have a return code of 9). The reason for the subshell is so that the set -o pipefail won't affect the user's shell.

make(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" CLICOLOR_FORCE=yes_please command make "$@" | sed --unbuffered "${subst}" } 
  • You have to use \x1b in sed because it doesn't understand \033.

  • & expands to the part of the line that was matched.

  • --unbuffered is necessary otherwise your output will either come out in large chunks or in a single chunk once the whole make command finishes.

  • command avoids recursively calling the make shell function.

  • "@" forwards the arguments given to the make shell function to command make. This will include VERBOSE=1 and anything else.

  • CLICOLOR_FORCE is to have the colored output from the Makefile that CMake generates which normally gets turned off if when the output is not a TTY (ex: a pipe).

make(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" ( set -o pipefail CLICOLOR_FORCE=yes_please command make "$@" | sed --unbuffered "${subst}"  ) } 
  • You have to use \x1b in sed because it doesn't understand \033.

  • & expands to the part of the line that was matched.

  • --unbuffered is necessary otherwise your output will either come out in large chunks or in a single chunk once the whole make command finishes.

  • command avoids recursively calling the make shell function.

  • "@" forwards the arguments given to the make shell function to command make. This will include VERBOSE=1 and anything else.

  • CLICOLOR_FORCE is to have the colored output from the Makefile that CMake generates which normally gets turned off if when the output is not a TTY (ex: a pipe).

  • The subshell with set -o pipefail is because with a pipe, you normally get the return code of the last command in the pipeline. Activating the option pipefail will cause the return code of the pipeline to be 0 if all commands returned 0, or the return code of the right-most command that returned a non-zero code (i.e. (exit 8) | true | (exit 9) | true would have a return code of 9). The reason for the subshell is so that the set -o pipefail won't affect the user's shell.

Changed the name of my test function to 'make'
Source Link

I like to have the words 'warning' and 'error' colored so I do this in my shell startup file

mmmake(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" CLICOLOR_FORCE=yes_please command make "$@" | sed --unbuffered "${subst}" } 

With the right regexes instead of my error, warning, and undefined reference, you can probably get a pretty decent highlighting of your output going on.

Here are some details.

  • You have to use \x1b in sed because it doesn't understand \033.

  • & expands to the part of the line that was matched.

  • --unbuffered is necessary otherwise your output will either come out in large chunks or in a single chunk once the whole make command finishes.

  • command avoids recursively calling the make shell function.

  • "@" forwards the arguments given to the make shell function to command make. This will include VERBOSE=1 and anything else.

  • CLICOLOR_FORCE is to have the colored output from the Makefile that CMake generates which normally gets turned off if when the output is not a TTY (ex: a pipe).

Have a look at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors for the color codes.

Normally, the output of a Makefile generated by CMake will have color for the [ 42%] Building C object source_file.c.o and such but this can be changed in the CMakeLists.txt with set(CMAKE_COLOR_MAKEFILE [ON|OFF]).

Color also goes away if the output of make is not a tty (like make | cat. You can force the color output in that case by setting the environment variable CLICOLOR_FORCE=yes_please (or any non-empty value). However if CMAKE_COLOR_MAKEFILE is off, `CLICOLOR_FORCE will have no effect.

If the project is not yours and CMAKE_COLOR_MAKEFILE is off, then you can add some substitutions to the list to compensate.

Also note that CLICOLOR_FORCE is a variable that is inspected by other commands so putting export CLICOLOR_FORCE=yessir in your shell startup file will affect more things that you may want. I don't do it.

Reference for CMAKE_COLOR_MAKEFILE: https://cmake.org/cmake/help/latest/variable/CMAKE_COLOR_MAKEFILE.html

I like to have the words 'warning' and 'error' colored so I do this in my shell startup file

mm(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" CLICOLOR_FORCE=yes_please command make "$@" | sed --unbuffered "${subst}" } 

With the right regexes instead of my error, warning, and undefined reference, you can probably get a pretty decent highlighting of your output going on.

Here are some details.

  • You have to use \x1b in sed because it doesn't understand \033.

  • & expands to the part of the line that was matched.

  • --unbuffered is necessary otherwise your output will either come out in large chunks or in a single chunk once the whole make command finishes.

  • command avoids recursively calling the make shell function.

  • "@" forwards the arguments given to the make shell function to command make. This will include VERBOSE=1 and anything else.

  • CLICOLOR_FORCE is to have the colored output from the Makefile that CMake generates which normally gets turned off if when the output is not a TTY (ex: a pipe).

Have a look at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors for the color codes.

Normally, the output of a Makefile generated by CMake will have color for the [ 42%] Building C object source_file.c.o and such but this can be changed in the CMakeLists.txt with set(CMAKE_COLOR_MAKEFILE [ON|OFF]).

Color also goes away if the output of make is not a tty (like make | cat. You can force the color output in that case by setting the environment variable CLICOLOR_FORCE=yes_please (or any non-empty value). However if CMAKE_COLOR_MAKEFILE is off, `CLICOLOR_FORCE will have no effect.

If the project is not yours and CMAKE_COLOR_MAKEFILE is off, then you can add some substitutions to the list to compensate.

Also note that CLICOLOR_FORCE is a variable that is inspected by other commands so putting export CLICOLOR_FORCE=yessir in your shell startup file will affect more things that you may want. I don't do it.

Reference for CMAKE_COLOR_MAKEFILE: https://cmake.org/cmake/help/latest/variable/CMAKE_COLOR_MAKEFILE.html

I like to have the words 'warning' and 'error' colored so I do this in my shell startup file

make(){ local subst subst+="s/error/\x1b[1;31m&\x1b[0m/g; " subst+="s/warning/\x1b[1;33m&\x1b[0m/g; " subst+="s/undefined reference/\x1b[1;35m&\x1b[0m/g; " # For debugging # echo "subst = '$subst'" CLICOLOR_FORCE=yes_please command make "$@" | sed --unbuffered "${subst}" } 

With the right regexes instead of my error, warning, and undefined reference, you can probably get a pretty decent highlighting of your output going on.

Here are some details.

  • You have to use \x1b in sed because it doesn't understand \033.

  • & expands to the part of the line that was matched.

  • --unbuffered is necessary otherwise your output will either come out in large chunks or in a single chunk once the whole make command finishes.

  • command avoids recursively calling the make shell function.

  • "@" forwards the arguments given to the make shell function to command make. This will include VERBOSE=1 and anything else.

  • CLICOLOR_FORCE is to have the colored output from the Makefile that CMake generates which normally gets turned off if when the output is not a TTY (ex: a pipe).

Have a look at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors for the color codes.

Normally, the output of a Makefile generated by CMake will have color for the [ 42%] Building C object source_file.c.o and such but this can be changed in the CMakeLists.txt with set(CMAKE_COLOR_MAKEFILE [ON|OFF]).

Color also goes away if the output of make is not a tty (like make | cat. You can force the color output in that case by setting the environment variable CLICOLOR_FORCE=yes_please (or any non-empty value). However if CMAKE_COLOR_MAKEFILE is off, `CLICOLOR_FORCE will have no effect.

If the project is not yours and CMAKE_COLOR_MAKEFILE is off, then you can add some substitutions to the list to compensate.

Also note that CLICOLOR_FORCE is a variable that is inspected by other commands so putting export CLICOLOR_FORCE=yessir in your shell startup file will affect more things that you may want. I don't do it.

Reference for CMAKE_COLOR_MAKEFILE: https://cmake.org/cmake/help/latest/variable/CMAKE_COLOR_MAKEFILE.html

Source Link
Loading