Speeding up builds with clang-tidy, clang-analyzer and iwyu
for our builds currently clang-tidy is currently the limiting factor in terms of speed.
There is one utility out there which tries to cache the output of clang-tidy, however it seems to be stale: https://github.com/ejfitzgerald/clang-tidy-cache
Yet, we have the impression that our case is much simpler:
We use clang-tidy with the warnings-as errors flag. Hence, the compiler is only ever started if there are no clang-tidy warnings. IMHO this implies that there can only be a ccache hit, if clang-tidy generated no output and hence the output of clang-tidy does not need to be cached. This assumes that cmake does not launch clang-tidy and clang in parallel but only launches clang/the compiler if clang-tidy passed successfully.
The overall problem was already discussed with Joel Rosdahl from ccache in this thread: https://email@example.com/msg01496.html
For a good implementation the following should work for compiling a given Class.o file:
- Create a Class.clang-tidy file next to Class.o using clang-tidy --dump-config (available as of which version?)
- Add Class.clang-tidy to configuration of extra_files_to_hash of ccache using the command line syntax: --add-extra-files=Class.clang-tidy
- try to use ccache directly, providing the --fail-on-cache-miss flag which causes ccache to exit if there is a cache miss
- if ccache failed: 4.1. launch clang-tidy 4.2. launch clang
The user should be able to select whether clang-tidy is run on a cash miss or not by setting: CMAKE_CLANG_TIDY_ON_CCACHE_HIT=OFF which by default is ON for backwards compatibility.
This approach could later be applied to any linting / static analysis tool for C++:
- use the tool with warnings-as-errors
- add the configuration of the tool (and the tool itself??) to the ccache extra files
- run it only if ccache has no hit
- run the compiler only if all static analysis passed and cache the result using ccache
Limitations / problems:
- This will only work if the proper version of ccache is installed, since the two necessary flags in ccache do yet need to be implemented.
- The user could in theory manually run ccache --add-extra-files=Class.clang-tidy which will circumnavigate the mechanism. However this seems hard to get right even if you really want to do so.
What do you think of this approach? Anything which was missed / needs to be considered?