Ninja: Builds using cross-workspace Ccache are broken for coverage since 3.21.x
Use case:
CCache is a compiler caching tool for C++ builds that drastically reduces build time by caching compiler artifacts and making them reusable across builds.
CCache is frequently used in CI/CD environments as it essentially turns from scratch builds to incremental, speeding up builds by a factor of 5 to 10.
A compiler output frequently embeds paths of files and directories for various reasons like debugging or profiling (e.g. DW_AT_comp_dir or source file paths in gcno files).
This is an issue if we'd like to share CCache across build workspaces which are located in different folders (e.g. on a CI worker), however, with proper fine tuning, cross-workspace CCache is fully functional for debug and profile builds as well.
Issue:
Starting in CMake 3.21, Ninja generator for gcc no longer uses relative paths for specifying source file location in the gcc command line.
This was an intentional architectural change, see the issue and the release notes below.
However, this breaks cross-workspace compiler CCache, as gcno files created during profile builds are embedding source location using the path provided to the compiler upon compiling source files.
This makes gcno files containing absolute paths to the sources, hence making them unusable across workspaces (e.g. when generating coverage reports using gcovr).
Possible fix:
There could be an option to allow relative source file paths in ninja generated compiler command lines to retain the original functionality while making the current behavior default to address the issues which have motivated the change to begin with.
Would this align with the CMake architectural vision and be a welcomed change?
References:
!6148 (comment 1304536)
https://cmake.org/cmake/help/latest/release/3.21.html
https://ccache.dev/