Utilities
Over the versions, common utilities that help you write good code have had support added to CMake. This is usually in the form of a property and matching CMAKE_*
initialization variable. The feature is not meant to be tied to one special program, but rather any program that is somewhat similar in behavior.
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.
CCache
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>_CPPCHECK
<LANG>_CPPLINT
<LANG>_INCLUDE_WHAT_YOU_USE
Clang tidy
Here is a simple example of using Clang-Tidy:
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 (I usually chose DO_CLANG_TIDY
) instead of the CMAKE_CXX_CLANG_TIDY
variable, then add it to your target properties as you create them.
Include what you use
This is an example for using include what you use. First, you'll need to have the tool, such as in a docker container:
Then, you can pass this into your build without modifying the source:
Finally, you can collect the output and apply the fixes:
Link what you use
There is a boolean target property, LINK_WHAT_YOU_USE
, that will check for extraneous files when linking.
Clang-format
Clang-format doesn't really have an integration with CMake, unfortunately. You could make a custom target (See this post, or you can run it manually. An interesting project that I have not really tried is here; it adds a format target and even makes sure that you can't commit unformatted files.
The following two line would do that in a git repository in bash (assuming you have a .clang-format
file):
Last updated