4

GCCFilter is a neat perl script that allows to color the output of GCC and thus makes debugging much more fun and, more important, faster.

You can use GCCFilter with a CMake generated Makefile by calling

gccfilter -a -c make

However, this approach has some drawbacks: Delayed output of CMake status infos, no color in the CMake commands are the most obvious.

The question: Is there a way to write some CMake module that searches for gccfilter if the compiler is gcc, checks if, say COLOR_CXX is set (rather easy up to here) and then tells CMake to replace all calls to gcc by gccfilter -a -c gcc.

CMake offers the variable CMAKE_CXX_COMPILER, but changing this one will disallow CMake to find correct include paths and the like. Is there some variable we may change after the project() command that is prefixed before each call to gcc?

Thilo
  • 8,827
  • 2
  • 35
  • 56
  • By the way, GCC output should be colorized by default, and if you're running it inside a script, you can use `-fdiagnostics-color-always`. – Kyle Strand Jul 13 '15 at 15:58

1 Answers1

6

You can make CMake use gccfilter by pointing the RULE_LAUNCH_COMPILE property to a wrapper script which invokes gccfilter with the desired options.

Create an executable shell script named gccfilter_wrap in the outermost CMake project directory with the following contents:

#!/bin/sh
exec gccfilter -a -c "$@"

Be sure to set the file's executable bit. Then in your CMakeLists.txt, set the RULE_LAUNCH_COMPILE directory property before adding targets:

project (HelloWorld)

set_directory_properties(PROPERTIES RULE_LAUNCH_COMPILE
   "${PROJECT_SOURCE_DIR}/gccfilter_wrap")

add_executable(HelloWorld HelloWorld.cpp)

The generated makefile rules will then prefix each compiler invocation with the gccfilter_wrap script. Alternatively the RULE_LAUNCH_COMPILE property can also be set as a target property or as global property.

The RULE_LAUNCH_COMPILE property only works for Makefile-based CMake generators.


Edit by Thilo

This is how I finally solved the problem - basically a rephrased version of this solution:

# GCCFilter, if appliciable
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCPP)
  option(COLOR_GCC "Use GCCFilter to color compiler output messages" ON)
  set(COLOR_GCC_OPTIONS "-c -r -w" CACHE STRING "Arguments that are passed to gccfilter when output coloring is switchend on. Defaults to -c -r -w.")
  if(COLOR_GCC)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${PROJECT_SOURCE_DIR}/cmake/gccfilter ${COLOR_GCC_OPTIONS}")
  endif()
endif()
Thilo
  • 8,827
  • 2
  • 35
  • 56
sakra
  • 62,199
  • 16
  • 168
  • 151
  • 2
    Works like a charm. However, the script is not really necessary, can all be done in CMake. I edited you post to add my final solution - hope you don't mind. – Thilo Jan 19 '13 at 18:38
  • I tried both versions (wrapper and CMakeLists.txt-only) but each runs into an endless loop when the compilation is almost done (at 99% or 100% progress). Without gccfilter the compilation finishes as expected. Maybe this is related to [this question](http://stackoverflow.com/questions/18954237/gccfilter-and-and-gcc-4-7-2-does-not-work-stops-compiling). Any ideas? – jotrocken Jul 08 '14 at 08:35