based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

lint.cmake (1299B)


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