-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcmake-format.cmake
66 lines (58 loc) · 2.04 KB
/
cmake-format.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Regex-filter a git repository's files.
function(get_cmake_files)
cmake_parse_arguments("" "" "GIT_REPOSITORY_DIR;OUTPUT_LIST;REGEX" "" ${ARGN})
execute_process(
COMMAND ${GIT_PROGRAM} ls-files --cached --exclude-standard ${_GIT_REPOSITORY_DIR}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE all_files
)
cmake_policy(SET CMP0007 NEW)
string(REPLACE "\n" ";" filtered_files "${all_files}")
list(FILTER filtered_files INCLUDE REGEX ${_REGEX})
foreach(it ${filtered_files})
if(NOT EXISTS ${it} OR IS_DIRECTORY ${it})
list(REMOVE_ITEM filtered_files ${it})
endif()
endforeach()
if(CMAKE_FORMAT_EXCLUDE)
list(FILTER filtered_files EXCLUDE REGEX ${CMAKE_FORMAT_EXCLUDE})
endif()
set(${_OUTPUT_LIST}
${filtered_files}
PARENT_SCOPE
)
endfunction()
execute_process(
COMMAND git rev-parse --show-toplevel
OUTPUT_VARIABLE GIT_TOPLEVEL
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
# remove trailing whitespace from output
string(STRIP ${GIT_TOPLEVEL} GIT_TOPLEVEL)
get_cmake_files(
GIT_REPOSITORY_DIR ${GIT_TOPLEVEL} OUTPUT_LIST CMAKE_FILES REGEX
"\\.cmake$|\\.cmake\\.in$|(^|/)CMakeLists\\.txt$"
)
separate_arguments(CMAKE_FORMAT_EXTRA_ARGS)
if(CMAKE_FORMAT_TARGET STREQUAL fix-cmake-format)
execute_process(COMMAND ${CMAKE_FORMAT_PROGRAM} ${CMAKE_FORMAT_EXTRA_ARGS} -i ${CMAKE_FILES})
return()
endif()
if(CMAKE_FORMAT_TARGET STREQUAL check-cmake-format)
set(OUTPUT_QUIET_OPTION OUTPUT_QUIET)
endif()
set(formatted_cmake_file ${BINARY_DIR}/formatted.cmake)
foreach(cmake_file IN LISTS CMAKE_FILES)
set(source_cmake_file ${CMAKE_SOURCE_DIR}/${cmake_file})
execute_process(
COMMAND ${CMAKE_FORMAT_PROGRAM} ${CMAKE_FORMAT_EXTRA_ARGS} -o ${formatted_cmake_file}
${source_cmake_file}
)
execute_process(
COMMAND ${GIT_PROGRAM} diff -G. --color --no-index -- ${source_cmake_file}
${formatted_cmake_file} RESULT_VARIABLE result ${OUTPUT_QUIET_OPTION}
)
if(OUTPUT_QUIET_OPTION AND result)
message(FATAL_ERROR "${cmake_file} needs to be reformatted")
endif()
endforeach()