@@ -244,6 +244,14 @@ if(UMF_FORMAT_CODE_STYLE)
244244 clang-format )
245245 find_program (CMAKE_FORMAT NAMES cmake-format )
246246
247+ if (NOT CLANG_FORMAT AND NOT CMAKE_FORMAT )
248+ message (
249+ FATAL_ERROR
250+ "UMF_FORMAT_CODE_STYLE=ON, but neither clang-format (required version: "
251+ "${CLANG_FORMAT_REQUIRED} ) nor cmake-format (required version: "
252+ "${CMAKE_FORMAT_VERSION} ) was found." )
253+ endif ()
254+
247255 if (CLANG_FORMAT )
248256 get_program_version_major_minor (${CLANG_FORMAT} CLANG_FORMAT_VERSION )
249257 message (STATUS "Found clang-format: ${CLANG_FORMAT} "
@@ -256,6 +264,44 @@ if(UMF_FORMAT_CODE_STYLE)
256264 message (FATAL_ERROR "Required clang-format version is "
257265 "${CLANG_FORMAT_REQUIRED} " )
258266 endif ()
267+
268+ # Obtain files for clang-format check
269+ set (format_clang_glob )
270+ foreach (
271+ DIR IN
272+ ITEMS benchmark
273+ examples
274+ include
275+ src
276+ test )
277+ list (
278+ APPEND
279+ format_clang_glob
280+ "${DIR} /*.h"
281+ "${DIR} /*.hpp"
282+ "${DIR} /*.c"
283+ "${DIR} /*.cpp"
284+ "${DIR} /**/*.h"
285+ "${DIR} /**/*.hpp"
286+ "${DIR} /**/*.c"
287+ "${DIR} /**/*.cpp" )
288+ endforeach ()
289+ file (GLOB_RECURSE format_list ${format_clang_glob} )
290+
291+ message (
292+ STATUS "Adding 'clang-format-check' and 'clang-format-apply' make "
293+ "targets" )
294+
295+ add_custom_target (
296+ clang-format-check
297+ COMMAND ${CLANG_FORMAT} --style=file --dry-run -Werror
298+ ${format_list}
299+ COMMENT "Check files formatting using clang-format" )
300+
301+ add_custom_target (
302+ clang-format-apply
303+ COMMAND ${CLANG_FORMAT} --style=file --i ${format_list}
304+ COMMENT "Format files using clang-format" )
259305 endif ()
260306
261307 if (CMAKE_FORMAT )
@@ -270,103 +316,68 @@ if(UMF_FORMAT_CODE_STYLE)
270316 message (FATAL_ERROR "Required cmake-format version is"
271317 "${CMAKE_FORMAT_REQUIRED} " )
272318 endif ()
273- endif ()
274319
275- if (NOT CLANG_FORMAT AND NOT CMAKE_FORMAT )
276- message (
277- FATAL_ERROR
278- "UMF_FORMAT_CODE_STYLE=ON, but neither clang-format (required version: "
279- "${CLANG_FORMAT_REQUIRED} ) nor cmake-format (required version: "
280- "${CMAKE_FORMAT_VERSION} ) was found." )
281- endif ()
320+ # Obtain files for cmake-format check
321+ set (format_cmake_glob )
322+ foreach (
323+ DIR IN
324+ ITEMS cmake
325+ benchmark
326+ examples
327+ include
328+ src
329+ test )
330+ list (
331+ APPEND
332+ format_cmake_glob
333+ "${DIR} /CMakeLists.txt"
334+ "${DIR} /*.cmake"
335+ "${DIR} /**/CMakeLists.txt"
336+ "${DIR} /**/*.cmake" )
337+ endforeach ()
338+ file (GLOB_RECURSE format_cmake_list ${format_cmake_glob} )
339+ list (APPEND format_cmake_list "${PROJECT_SOURCE_DIR} /CMakeLists.txt" )
282340
283- # Obtain files for clang-format check
284- set (format_glob )
285- foreach (
286- DIR IN
287- ITEMS benchmark
288- examples
289- include
290- src
291- test )
292- list (
293- APPEND
294- format_glob
295- "${DIR} /*.h"
296- "${DIR} /*.hpp"
297- "${DIR} /*.c"
298- "${DIR} /*.cpp"
299- "${DIR} /**/*.h"
300- "${DIR} /**/*.hpp"
301- "${DIR} /**/*.c"
302- "${DIR} /**/*.cpp" )
303- endforeach ()
304- file (GLOB_RECURSE format_list ${format_glob} )
305-
306- message (STATUS "Adding clang-format-check and clang-format-apply make "
341+ message (
342+ STATUS "Adding 'cmake-format-check' and 'cmake-format-apply' make "
307343 "targets" )
308344
309- add_custom_target (
310- clang-format-check
311- COMMAND ${CLANG_FORMAT} --style=file --dry-run -Werror ${format_list}
312- COMMENT "Check files formatting using clang-format" )
313-
314- add_custom_target (
315- clang-format-apply
316- COMMAND ${CLANG_FORMAT} --style=file --i ${format_list}
317- COMMENT "Format files using clang-format" )
318-
319- # Obtain files for cmake-format check
320- set (format_cmake_glob )
321- foreach (
322- DIR IN
323- ITEMS examples
324- cmake
325- include
326- src
327- test
328- benchmark )
329- list (
330- APPEND
331- format_cmake_glob
332- "${DIR} /CMakeLists.txt"
333- "${DIR} /*.cmake"
334- "${DIR} /**/CMakeLists.txt"
335- "${DIR} /**/*.cmake" )
336- endforeach ()
337- file (GLOB_RECURSE format_cmake_list ${format_cmake_glob} )
338- list (APPEND format_cmake_list "${PROJECT_SOURCE_DIR} /CMakeLists.txt" )
339-
340- message (STATUS "Adding cmake-format-check and cmake-format-apply make "
341- "targets" )
345+ add_custom_target (
346+ cmake-format-check
347+ COMMAND ${CMAKE_FORMAT} --check ${format_cmake_list}
348+ COMMENT "Check CMake files formatting using cmake-format" )
342349
343- add_custom_target (
344- cmake-format-check
345- COMMAND ${CMAKE_FORMAT} --check ${format_cmake_list}
346- COMMENT "Check files formatting using cmake-format" )
347-
348- add_custom_target (
349- cmake-format-apply
350- COMMAND ${CMAKE_FORMAT} --in-place ${format_cmake_list}
351- COMMENT "Format files using cmake-format" )
352-
353- # Add a convenience target for running both clang-format and cmake-format
354- # checks/apply
355- add_custom_target (
356- format-check
357- COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
358- clang-format-check
359- COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
360- cmake-format-check
361- COMMENT "Running both clang-format-check and cmake-format-check" )
362-
363- add_custom_target (
364- format-apply
365- COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
366- clang-format-apply
367- COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
368- cmake-format-apply
369- COMMENT "Format files using clang-format and cmake-format" )
350+ add_custom_target (
351+ cmake-format-apply
352+ COMMAND ${CMAKE_FORMAT} --in-place ${format_cmake_list}
353+ COMMENT "Format Cmake files using cmake-format" )
354+ endif ()
355+
356+ # Add a convenience target for running both tools at once - available only
357+ # if both are found.
358+ if (CLANG_FORMAT AND CMAKE_FORMAT )
359+ add_custom_target (
360+ format-check
361+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
362+ clang-format-check
363+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
364+ cmake-format-check
365+ COMMENT "Running both clang-format-check and cmake-format-check" )
366+
367+ add_custom_target (
368+ format-apply
369+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
370+ clang-format-apply
371+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
372+ cmake-format-apply
373+ COMMENT "Format files using clang-format and cmake-format" )
374+ else ()
375+ message (
376+ STATUS
377+ " Convenience targets 'make format-check' and 'make format-apply' are "
378+ "not available. Use commands specific for found tools (see the log above)."
379+ )
380+ endif ()
370381endif ()
371382
372383# --------------------------------------------------------------------------- #
0 commit comments