lint.cmake (1337B)
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 example/*.cpp example/*.hpp 16 ) 17 default(FIX NO) 18 19 set(flag --output-replacements-xml) 20 set(args OUTPUT_VARIABLE output) 21 if(FIX) 22 set(flag -i) 23 set(args "") 24 endif() 25 26 file(GLOB_RECURSE files ${PATTERNS}) 27 set(badly_formatted "") 28 set(output "") 29 string(LENGTH "${CMAKE_SOURCE_DIR}/" path_prefix_length) 30 31 foreach(file IN LISTS files) 32 execute_process( 33 COMMAND "${FORMAT_COMMAND}" --style=file "${flag}" "${file}" 34 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" 35 RESULT_VARIABLE result 36 ${args} 37 ) 38 if(NOT result EQUAL "0") 39 message(FATAL_ERROR "'${file}': formatter returned with ${result}") 40 endif() 41 if(NOT FIX AND output MATCHES "\n<replacement offset") 42 string(SUBSTRING "${file}" "${path_prefix_length}" -1 relative_file) 43 list(APPEND badly_formatted "${relative_file}") 44 endif() 45 set(output "") 46 endforeach() 47 48 if(NOT badly_formatted STREQUAL "") 49 list(JOIN badly_formatted "\n" bad_list) 50 message("The following files are badly formatted:\n\n${bad_list}\n") 51 message(FATAL_ERROR "Run again with FIX=YES to fix these files.") 52 endif()