c++cstatic-analysisclang-tidyccache

Is it possible to accelerate clang-tidy using ccache or similar?


Since employing ccache on our CI server, we find that the bottleneck in terms of build time is now our static analysis pass, that uses clang-tidy, among other tools. Does anyone know of a way to accelerate clang-tidy in a similar way to how ccache does so with a regular compiler?


Solution

  • Thank you for the question and everybody for the answers.

    After struggling to make ejfitzgerald/clang-tidy-cache working w/o any decent documentation or help I've given a try matus-chochlik/ctcache. It's indeed much easier in installation and configuration.

    Here's a snippet we use to automatically use it with cmake when it's installed

    find_program (CLANG_TIDY_CACHE_PATH NAMES "clang-tidy-cache")
    if (CLANG_TIDY_CACHE_PATH)
        find_program (_CLANG_TIDY_PATH NAMES "clang-tidy" "clang-tidy-15" "clang-tidy-14" "clang-tidy-13" "clang-tidy-12")
    
        # Why do we use ';' here?
        # It's a cmake black magic: https://cmake.org/cmake/help/latest/prop_tgt/LANG_CLANG_TIDY.html#prop_tgt:%3CLANG%3E_CLANG_TIDY
        # The CLANG_TIDY_PATH is passed to CMAKE_CXX_CLANG_TIDY, which follows CXX_CLANG_TIDY syntax.
        set (CLANG_TIDY_PATH "${CLANG_TIDY_CACHE_PATH};${_CLANG_TIDY_PATH}" CACHE STRING "A combined command to run clang-tidy with caching wrapper")
    else ()
        find_program (CLANG_TIDY_PATH NAMES "clang-tidy" "clang-tidy-15" "clang-tidy-14" "clang-tidy-13" "clang-tidy-12")
    endif ()
    
    set (CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_PATH}")
    

    With it, if clang-tidy-cache is in the path, it will be used together with any found clang-tidy.

    During the build, we define CTCACHE_DIR and preserve them between builds.

    In our CI it caused reducing the linting time from 330 minutes to 22, so it's almost 15x faster!