lint.cmake (1305B)
1 cmake_minimum_required(VERSION 3.14) 2 3 macro(default name) 4 if(NOT DEFINED "${name}") 5 set("${name}" "${ARGN}") 6 endif() 7 endmacro() 8 9 default(FORMAT_COMMAND clang-format) 10 default( 11 PATTERNS 12 source/*.cpp source/*.hpp 13 include/*.hpp 14 test/*.cpp test/*.hpp 15 ) 16 default(FIX NO) 17 18 set(flag --output-replacements-xml) 19 set(args OUTPUT_VARIABLE output) 20 if(FIX) 21 set(flag -i) 22 set(args "") 23 endif() 24 25 file(GLOB_RECURSE files ${PATTERNS}) 26 set(badly_formatted "") 27 set(output "") 28 string(LENGTH "${CMAKE_SOURCE_DIR}/" path_prefix_length) 29 30 foreach(file IN LISTS files) 31 execute_process( 32 COMMAND "${FORMAT_COMMAND}" --style=file "${flag}" "${file}" 33 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" 34 RESULT_VARIABLE result 35 ${args} 36 ) 37 if(NOT result EQUAL "0") 38 message(FATAL_ERROR "'${file}': formatter returned with ${result}") 39 endif() 40 if(NOT FIX AND output MATCHES "\n<replacement offset") 41 string(SUBSTRING "${file}" "${path_prefix_length}" -1 relative_file) 42 list(APPEND badly_formatted "${relative_file}") 43 endif() 44 set(output "") 45 endforeach() 46 47 if(NOT badly_formatted STREQUAL "") 48 list(JOIN badly_formatted "\n" bad_list) 49 message("The following files are badly formatted:\n\n${bad_list}\n") 50 message(FATAL_ERROR "Run again with FIX=YES to fix these files.") 51 endif()