Extending for code format checks for CI
When setting up CI pipelines, it’s often beneficial to only check whether the code complies with the established formatting rules rather than automatically modifying the source files. This ensures that any code that doesn’t meet the style guidelines is flagged, prompting the developer to fix it manually. Clang-Format supports this use case with the --dry-run
and --Werror
options, which, when combined, cause the tool to exit with a non-zero status code if any file is reformatted.
You can extend the existing CMake setup so that it includes a new custom target that only checks the code format. Here’s how to do this:
# Create custom target to check clang-format if(CLANG_FORMAT_EXECUTABLE) add_custom_target( check-clang-format COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file -Werror --dry-run ${ALL_SOURCE_FILES...