Skip to content

Commit 90b5c3c

Browse files
[CMake] Add clang/cmake formatting commands only if tools are available
1 parent 2047208 commit 90b5c3c

File tree

1 file changed

+103
-92
lines changed

1 file changed

+103
-92
lines changed

CMakeLists.txt

Lines changed: 103 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ if(UMF_FORMAT_CODE_STYLE)
247247
clang-format)
248248
find_program(CMAKE_FORMAT NAMES cmake-format)
249249

250+
if(NOT CLANG_FORMAT AND NOT CMAKE_FORMAT)
251+
message(
252+
FATAL_ERROR
253+
"UMF_FORMAT_CODE_STYLE=ON, but neither clang-format (required version: "
254+
"${CLANG_FORMAT_REQUIRED}) nor cmake-format (required version: "
255+
"${CMAKE_FORMAT_VERSION}) was found.")
256+
endif()
257+
250258
if(CLANG_FORMAT)
251259
get_program_version_major_minor(${CLANG_FORMAT} CLANG_FORMAT_VERSION)
252260
message(STATUS "Found clang-format: ${CLANG_FORMAT} "
@@ -259,6 +267,44 @@ if(UMF_FORMAT_CODE_STYLE)
259267
message(FATAL_ERROR "Required clang-format version is "
260268
"${CLANG_FORMAT_REQUIRED}")
261269
endif()
270+
271+
# Obtain files for clang-format check
272+
set(format_clang_glob)
273+
foreach(
274+
DIR IN
275+
ITEMS benchmark
276+
examples
277+
include
278+
src
279+
test)
280+
list(
281+
APPEND
282+
format_clang_glob
283+
"${DIR}/*.h"
284+
"${DIR}/*.hpp"
285+
"${DIR}/*.c"
286+
"${DIR}/*.cpp"
287+
"${DIR}/**/*.h"
288+
"${DIR}/**/*.hpp"
289+
"${DIR}/**/*.c"
290+
"${DIR}/**/*.cpp")
291+
endforeach()
292+
file(GLOB_RECURSE format_list ${format_clang_glob})
293+
294+
message(
295+
STATUS "Adding 'clang-format-check' and 'clang-format-apply' make "
296+
"targets")
297+
298+
add_custom_target(
299+
clang-format-check
300+
COMMAND ${CLANG_FORMAT} --style=file --dry-run -Werror
301+
${format_list}
302+
COMMENT "Check files formatting using clang-format")
303+
304+
add_custom_target(
305+
clang-format-apply
306+
COMMAND ${CLANG_FORMAT} --style=file --i ${format_list}
307+
COMMENT "Format files using clang-format")
262308
endif()
263309

264310
if(CMAKE_FORMAT)
@@ -273,103 +319,68 @@ if(UMF_FORMAT_CODE_STYLE)
273319
message(FATAL_ERROR "Required cmake-format version is"
274320
"${CMAKE_FORMAT_REQUIRED}")
275321
endif()
276-
endif()
277322

278-
if(NOT CLANG_FORMAT AND NOT CMAKE_FORMAT)
279-
message(
280-
FATAL_ERROR
281-
"UMF_FORMAT_CODE_STYLE=ON, but neither clang-format (required version: "
282-
"${CLANG_FORMAT_REQUIRED}) nor cmake-format (required version: "
283-
"${CMAKE_FORMAT_VERSION}) was found.")
284-
endif()
323+
# Obtain files for cmake-format check
324+
set(format_cmake_glob)
325+
foreach(
326+
DIR IN
327+
ITEMS cmake
328+
benchmark
329+
examples
330+
include
331+
src
332+
test)
333+
list(
334+
APPEND
335+
format_cmake_glob
336+
"${DIR}/CMakeLists.txt"
337+
"${DIR}/*.cmake"
338+
"${DIR}/**/CMakeLists.txt"
339+
"${DIR}/**/*.cmake")
340+
endforeach()
341+
file(GLOB_RECURSE format_cmake_list ${format_cmake_glob})
342+
list(APPEND format_cmake_list "${PROJECT_SOURCE_DIR}/CMakeLists.txt")
285343

286-
# Obtain files for clang-format check
287-
set(format_glob)
288-
foreach(
289-
DIR IN
290-
ITEMS benchmark
291-
examples
292-
include
293-
src
294-
test)
295-
list(
296-
APPEND
297-
format_glob
298-
"${DIR}/*.h"
299-
"${DIR}/*.hpp"
300-
"${DIR}/*.c"
301-
"${DIR}/*.cpp"
302-
"${DIR}/**/*.h"
303-
"${DIR}/**/*.hpp"
304-
"${DIR}/**/*.c"
305-
"${DIR}/**/*.cpp")
306-
endforeach()
307-
file(GLOB_RECURSE format_list ${format_glob})
308-
309-
message(STATUS "Adding clang-format-check and clang-format-apply make "
344+
message(
345+
STATUS "Adding 'cmake-format-check' and 'cmake-format-apply' make "
310346
"targets")
311347

312-
add_custom_target(
313-
clang-format-check
314-
COMMAND ${CLANG_FORMAT} --style=file --dry-run -Werror ${format_list}
315-
COMMENT "Check files formatting using clang-format")
316-
317-
add_custom_target(
318-
clang-format-apply
319-
COMMAND ${CLANG_FORMAT} --style=file --i ${format_list}
320-
COMMENT "Format files using clang-format")
321-
322-
# Obtain files for cmake-format check
323-
set(format_cmake_glob)
324-
foreach(
325-
DIR IN
326-
ITEMS examples
327-
cmake
328-
include
329-
src
330-
test
331-
benchmark)
332-
list(
333-
APPEND
334-
format_cmake_glob
335-
"${DIR}/CMakeLists.txt"
336-
"${DIR}/*.cmake"
337-
"${DIR}/**/CMakeLists.txt"
338-
"${DIR}/**/*.cmake")
339-
endforeach()
340-
file(GLOB_RECURSE format_cmake_list ${format_cmake_glob})
341-
list(APPEND format_cmake_list "${PROJECT_SOURCE_DIR}/CMakeLists.txt")
342-
343-
message(STATUS "Adding cmake-format-check and cmake-format-apply make "
344-
"targets")
348+
add_custom_target(
349+
cmake-format-check
350+
COMMAND ${CMAKE_FORMAT} --check ${format_cmake_list}
351+
COMMENT "Check CMake files formatting using cmake-format")
345352

346-
add_custom_target(
347-
cmake-format-check
348-
COMMAND ${CMAKE_FORMAT} --check ${format_cmake_list}
349-
COMMENT "Check files formatting using cmake-format")
350-
351-
add_custom_target(
352-
cmake-format-apply
353-
COMMAND ${CMAKE_FORMAT} --in-place ${format_cmake_list}
354-
COMMENT "Format files using cmake-format")
355-
356-
# Add a convenience target for running both clang-format and cmake-format
357-
# checks/apply
358-
add_custom_target(
359-
format-check
360-
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
361-
clang-format-check
362-
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
363-
cmake-format-check
364-
COMMENT "Running both clang-format-check and cmake-format-check")
365-
366-
add_custom_target(
367-
format-apply
368-
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
369-
clang-format-apply
370-
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
371-
cmake-format-apply
372-
COMMENT "Format files using clang-format and cmake-format")
353+
add_custom_target(
354+
cmake-format-apply
355+
COMMAND ${CMAKE_FORMAT} --in-place ${format_cmake_list}
356+
COMMENT "Format Cmake files using cmake-format")
357+
endif()
358+
359+
# Add a convenience target for running both tools at once - available only
360+
# if both are found.
361+
if(CLANG_FORMAT AND CMAKE_FORMAT)
362+
add_custom_target(
363+
format-check
364+
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
365+
clang-format-check
366+
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
367+
cmake-format-check
368+
COMMENT "Running both clang-format-check and cmake-format-check")
369+
370+
add_custom_target(
371+
format-apply
372+
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
373+
clang-format-apply
374+
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
375+
cmake-format-apply
376+
COMMENT "Format files using clang-format and cmake-format")
377+
else()
378+
message(
379+
STATUS
380+
" Convenience targets 'make format-check' and 'make format-apply' are "
381+
"not available. Use commands specific for found tools (see the log above)."
382+
)
383+
endif()
373384
endif()
374385

375386
# Add license to the installation path

0 commit comments

Comments
 (0)