Repository: ccache/ccache

Issue #224: Make colored warnings work for GCC out of the box
Created: 2018-02-06T12:46:32Z
Background
GCC ≥4.9 can emit colored diagnostics messages such as warnings. ccache handles this (see d875ede) according to GCC 4.9's documentation: "The default is never if GCC_COLORS environment variable isn't present in the environment, and auto otherwise." However, GCC ≥5.1 documents that "the default depends on how the compiler has been configured", which for at least Ubuntu-based and RedHat-based distributions seems to be auto without the requirement of GCC_COLORS being set. This means that ccache doesn't replicate the defacto behavior for GCC ≥5.1.
Here's an idea of how to improve things.
What to do
If the compiler is GCC:

Don't include any explicit -f(no-)diagnostics-color options in the hash.
Strip any explicit -fno-diagnostics-color and -fdiagnostics-color options from the compiler arguments and add a -fdiagnostics-color option (if not already present) to force colors.
If run_second_cpp is true, run the preprocessor without -fdiagnostics-color (since those error messages won't be used anyway).
If the compiler (in the run_second_cpp case) or preprocessor (in the !run_second_cpp case) fails with the added -fdiagnostics-color option, check if the error message includes unrecognized command line option and -fdiagnostics-color. If yes, execute again but without -fdiagnostics-color.
If run_second_cpp is false, remember if it worked to pass -fdiagnostics-color to the preprocessor. If yes, add -fdiagnostics-color when executing the compiler as well.
On a cache hit: Strip color codes from the stored stderr if -fno-diagnostics-color has been passed OR -fdiagnostics-color=never has been passed OR (stderr does not refer to a TTY AND -fdiagnostics-color has not been passed AND -fdiagnostics-color=always has not been passed).
Prove that it works by adding a test case that checks that color codes are stored in the cache and that color codes are not produced when stderr is redirected.
Remove the corresponding caveat from the documentation.

The above method will make cache misses (and preprocessed hits when run_second_cpp is false) slightly slower for GCC ≤4.8. On my system, the extra probe call takes ≈1 ms. I think that this is an OK price to pay to make colored warnings work transparently for modern GCC versions.
See also #223 which is somewhat similar but for clang.

Duplicate issue #297: 'preprocessor error' with GCC_COLORS and gcc < 4.9
Created: 2018-08-14T08:13:59Z
On my environment, call to ccache only increment 'preprocessor error' stat. After stracing ccache, I noticed that it add "-fdiagnostics-color" to gcc command line. Indeed, my environment contains GCC_COLORS. Since my gcc is < 4.9, this option make an error.
Indeed, everything works well once I unset GCC_COLOR.
After quickly looking at the code (to write a patch), it seems issue is not easy to solve. However, I think ccache should detect this issue and provide to use some hint to solve it.

Closing comment (2018-08-23T20:17:05Z): Yes, this is sort of a duplicate of #224. We currently sacrifice usability for GCC <4.9 users in order to enable colored output out of the box for GCC ≥4.9 users.