@@ -217,24 +217,38 @@ if(UMF_FORMAT_CODE_STYLE)
217
217
find_program (CLANG_FORMAT NAMES clang-format-15 clang-format-15.0 clang-format )
218
218
find_program (CMAKE_FORMAT NAMES cmake-format )
219
219
220
- if (CLANG_FORMAT AND CMAKE_FORMAT )
220
+ if (CLANG_FORMAT )
221
221
get_program_version_major_minor (${CLANG_FORMAT} CLANG_FORMAT_VERSION )
222
- get_program_version_major_minor (${CMAKE_FORMAT} CMAKE_FORMAT_VERSION )
223
222
message (STATUS "Found clang-format: ${CLANG_FORMAT} "
224
- "(version: ${CLANG_FORMAT_VERSION} )" )
225
- message (STATUS "Found cmake-format: ${CMAKE_FORMAT} "
226
- "(version: ${CMAKE_FORMAT_VERSION} )" )
223
+ "(version: ${CLANG_FORMAT_VERSION} )" )
227
224
228
225
# Check if clang-format (in correct version) is available for code formatting.
229
226
set (CLANG_FORMAT_REQUIRED "15.0" )
230
227
if (NOT (CLANG_FORMAT_VERSION VERSION_EQUAL CLANG_FORMAT_REQUIRED ))
231
228
message (FATAL_ERROR "Required clang-format version is "
232
229
"${CLANG_FORMAT_REQUIRED} " )
233
230
endif ()
234
- else ()
235
- message (FATAL_ERROR "UMF_FORMAT_CODE_STYLE=ON, but clang-format or "
236
- "cmake-format not found (required version: "
237
- "${CLANG_FORMAT_REQUIRED} )" )
231
+ endif ()
232
+
233
+ if (CMAKE_FORMAT )
234
+ get_program_version_major_minor (${CMAKE_FORMAT} CMAKE_FORMAT_VERSION )
235
+ message (STATUS "Found cmake-format: ${CMAKE_FORMAT} "
236
+ "(version: ${CMAKE_FORMAT_VERSION} )" )
237
+
238
+ # Check if cmake-format (in correct version) is available for code
239
+ # formatting.
240
+ set (CMAKE_FORMAT_REQUIRED "0.6" )
241
+ if (NOT (CMAKE_FORMAT_VERSION VERSION_EQUAL CMAKE_FORMAT_REQUIRED ))
242
+ message (FATAL_ERROR "Required cmake-format version is"
243
+ "${CMAKE_FORMAT_REQUIRED} " )
244
+ endif ()
245
+ endif ()
246
+
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 was found." )
238
252
endif ()
239
253
240
254
# Obtain files for clang-format check
@@ -283,6 +297,12 @@ if(UMF_FORMAT_CODE_STYLE)
283
297
file (GLOB_RECURSE format_cmake_list ${format_cmake_glob} )
284
298
list (APPEND format_cmake_list "${PROJECT_SOURCE_DIR} /CMakeLists.txt" )
285
299
300
+ # Print the list of files targeted for formatting by cmake-format
301
+ message (STATUS "Files to be formatted with cmake-format:" )
302
+ foreach (file IN LISTS format_cmake_list )
303
+ message (STATUS "${file} " )
304
+ endforeach ()
305
+
286
306
message (STATUS "Adding cmake-format-check and cmake-format-apply make "
287
307
"targets" )
288
308
@@ -295,6 +315,24 @@ if(UMF_FORMAT_CODE_STYLE)
295
315
cmake-format-apply
296
316
COMMAND ${CMAKE_FORMAT} --in-place ${format_cmake_list}
297
317
COMMENT "Format files using cmake-format" )
318
+
319
+ # Add a convenience target for running both clang-format and cmake-format
320
+ # checks/apply
321
+ add_custom_target (
322
+ format-checks
323
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
324
+ clang-format-check
325
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
326
+ cmake-format-check
327
+ COMMENT "Running both clang-format-check and cmake-format-check" )
328
+
329
+ add_custom_target (
330
+ format-apply
331
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
332
+ clang-format-apply
333
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
334
+ cmake-format-apply
335
+ COMMENT "Format files using clang-format and cmake-format" )
298
336
endif ()
299
337
300
338
# Add license to the installation path
0 commit comments