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.
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
autowithout the requirement ofGCC_COLORSbeing 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:
-f(no-)diagnostics-coloroptions in the hash.-fno-diagnostics-colorand-fdiagnostics-coloroptions from the compiler arguments and add a-fdiagnostics-coloroption (if not already present) to force colors.run_second_cppis true, run the preprocessor without-fdiagnostics-color(since those error messages won't be used anyway).run_second_cppcase) or preprocessor (in the!run_second_cppcase) fails with the added-fdiagnostics-coloroption, check if the error message includesunrecognized command line optionand-fdiagnostics-color. If yes, execute again but without-fdiagnostics-color.run_second_cppis false, remember if it worked to pass-fdiagnostics-colorto the preprocessor. If yes, add-fdiagnostics-colorwhen executing the compiler as well.-fno-diagnostics-colorhas been passed OR-fdiagnostics-color=neverhas been passed OR (stderr does not refer to a TTY AND-fdiagnostics-colorhas not been passed AND-fdiagnostics-color=alwayshas not been passed).The above method will make cache misses (and preprocessed hits when
run_second_cppis 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.