cmake_minimum_required(VERSION 3.14) macro(default name) if(NOT DEFINED "${name}") set("${name}" "${ARGN}") endif() endmacro() default(FORMAT_COMMAND clang-format) default( PATTERNS src/*.cpp src/*.hpp include/*.hpp test/*.cpp test/*.hpp ) default(FIX NO) set(flag --output-replacements-xml) set(args OUTPUT_VARIABLE output) if(FIX) set(flag -i) set(args "") endif() file(GLOB_RECURSE files ${PATTERNS}) set(badly_formatted "") set(output "") string(LENGTH "${CMAKE_SOURCE_DIR}/" path_prefix_length) foreach(file IN LISTS files) execute_process( COMMAND "${FORMAT_COMMAND}" --style=file "${flag}" "${file}" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" RESULT_VARIABLE result ${args} ) if(NOT result EQUAL "0") message(FATAL_ERROR "'${file}': formatter returned with ${result}") endif() if(NOT FIX AND output MATCHES "\n