Patch for colormake to properly handle redirecting std err

Alec Jacobson

February 10, 2012

weblog/

I started using colormake which is a great little perl script that wraps the make command, colorizing its output. I liked it so much that I quickly decided to make an alias in my bash .profile so that whenever I called make I would actually run colormake:
alias make=colormake
Note: The original colormake script is called cmake, but I renamed it to colormake to avoid conflict with the cmake. This was going fine until I want to actually play with my typical make output. One thing I like to be able to do is redirect all of my errors to a file:
make 2>make.err
My alias to colormake broke this because the first thing the colormake wrapper does is redirect all of make's std err output to std out. Here's my patch to the colormake (cmake) script that more properly deals with stderr. I also provide a fix to the errors from using stty incorrectly (though I actually don't like the extra feature of colormake which trims commands to fit the window):
#!/bin/bash
#
# Wrapper around make to colorize its output.
#
# This patch fixes problems with calling stty in a script and redirecting std
# err
#

## use terminal size but avoid "stty: stdin isn't a terminal" warning from stty 
#STTY_SIZE=`stty size 2>/dev/null`
# Don't pass a terminal size (soft wrap entire commands)
STTY_SIZE=""

if [ -t 1 ];
then
  # Std out is tty
  if [ -t 2 ];
  then
    # Std err is tty
    make $* 2>&1 | colormake.pl $STTY_SIZE                                                              
    ret_val=${PIPESTATUS[0]}
  else
    # Std err is not tty
    # only pipe std out to colormake
    make $* | colormake.pl $STTY_SIZE                                                              
    ret_val=${PIPESTATUS[0]}
  fi
else
  # Std out is not tty
  make $*
  ret_val=$? 
fi                                                                                        
exit $ret_val
Update: Using PIPESTATUS I made sure that this script returns the return code of the make call.