CCache and Utilities

    All of these take ; separated values (a standard list in CMake) that describe the program and options that you should run on the source files of this target.

    Set the CMAKE_<LANG>_COMPILER_LAUNCHER variable or the <LANG>_COMPILER_LAUNCHER property on a target to use something like CCache to “wrap” the compilation of the target. Support for CCache has been expanding in the latest versions of CMake. In practice, this tends to look like this:

    Utilities

    Set the following properties or CMAKE_* initializer variables to the command line for the tools. Most of them are limited to C or CXX with make or ninja generators.

    • <LANG>_CLANG_TIDY: CMake 3.6+
    • <LANG>_CPPLINT

    Here is a simple example of using Clang-Tidy:

    1. cmake -S . -B build-tidy -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);-fix"
    2. cmake --build build -j 1

    The -fix part is optional, and will modify your source files to try to fix the tidy warning issued. If you are working in a git repository, this is fairly safe as you can see what has changed. However, make sure you do not run your makefile/ninja build in parallel! This will not work very well at all if it tries to fix the same header twice.

    If you want to explicitly use the target form to ensure you only call this on your local targets, you can set a variable (maybe something like ) instead of the CMAKE_CXX_CLANG_TIDY variable, then add it to your target properties as you create them. You can find clang-tidy in your path like this:

    Include what you use

    1. build # cmake -S . -B build-iwyu -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=include-what-you-use

    Finally, you can collect the output and (optionally) apply the fixes:

    (You should check the fixes first, or touch them up after applying!)

    There is a boolean target property, LINK_WHAT_YOU_USE, that will check for extraneous files when linking.

    Clang-format

    The following two line would do that in a git repository in bash (assuming you have a .clang-format file):