@@ -25,7 +25,9 @@ option(UMF_BUILD_EXAMPLES "Build UMF examples" ON)
25
25
option (UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON )
26
26
option (UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors"
27
27
OFF )
28
- option (UMF_FORMAT_CODE_STYLE "Format UMF code with clang-format" OFF )
28
+ option (UMF_FORMAT_CODE_STYLE
29
+ "Add clang, cmake, and black -format-check and -format-apply targets"
30
+ OFF )
29
31
option (USE_ASAN "Enable AddressSanitizer checks" OFF )
30
32
option (USE_UBSAN "Enable UndefinedBehaviorSanitizer checks" OFF )
31
33
option (USE_TSAN "Enable ThreadSanitizer checks" OFF )
@@ -255,13 +257,19 @@ if(UMF_FORMAT_CODE_STYLE)
255
257
find_program (CLANG_FORMAT NAMES clang-format-15 clang-format-15.0
256
258
clang-format )
257
259
find_program (CMAKE_FORMAT NAMES cmake-format )
260
+ find_program (BLACK NAMES black )
261
+
262
+ set (CLANG_FORMAT_REQUIRED "15.0" )
263
+ set (CMAKE_FORMAT_REQUIRED "0.6" )
258
264
259
- if (NOT CLANG_FORMAT AND NOT CMAKE_FORMAT )
265
+ if (NOT CLANG_FORMAT
266
+ AND NOT CMAKE_FORMAT
267
+ AND NOT BLACK )
260
268
message (
261
269
FATAL_ERROR
262
270
"UMF_FORMAT_CODE_STYLE=ON, but neither clang-format (required version: "
263
- "${CLANG_FORMAT_REQUIRED} ) nor cmake-format (required version: "
264
- "${CMAKE_FORMAT_VERSION} ) was found." )
271
+ "${CLANG_FORMAT_REQUIRED} ), nor cmake-format (required version: "
272
+ "${CMAKE_FORMAT_REQUIRED} ), nor black was found." )
265
273
endif ()
266
274
267
275
if (CLANG_FORMAT )
@@ -271,7 +279,6 @@ if(UMF_FORMAT_CODE_STYLE)
271
279
272
280
# Check if clang-format (in correct version) is available for code
273
281
# formatting.
274
- set (CLANG_FORMAT_REQUIRED "15.0" )
275
282
if (NOT (CLANG_FORMAT_VERSION VERSION_EQUAL CLANG_FORMAT_REQUIRED ))
276
283
message (FATAL_ERROR "Required clang-format version is "
277
284
"${CLANG_FORMAT_REQUIRED} " )
@@ -301,8 +308,8 @@ if(UMF_FORMAT_CODE_STYLE)
301
308
file (GLOB_RECURSE format_list ${format_clang_glob} )
302
309
303
310
message (
304
- STATUS "Adding 'clang-format-check' and 'clang-format-apply' make "
305
- " targets" )
311
+ STATUS
312
+ "Adding 'clang-format-check' and 'clang-format-apply' targets" )
306
313
307
314
add_custom_target (
308
315
clang-format-check
@@ -312,7 +319,7 @@ if(UMF_FORMAT_CODE_STYLE)
312
319
313
320
add_custom_target (
314
321
clang-format-apply
315
- COMMAND ${CLANG_FORMAT} --style=file -- i ${format_list}
322
+ COMMAND ${CLANG_FORMAT} --style=file -i ${format_list}
316
323
COMMENT "Format files using clang-format" )
317
324
endif ()
318
325
@@ -323,7 +330,6 @@ if(UMF_FORMAT_CODE_STYLE)
323
330
324
331
# Check if cmake-format (in correct version) is available for cmake
325
332
# files formatting.
326
- set (CMAKE_FORMAT_REQUIRED "0.6" )
327
333
if (NOT (CMAKE_FORMAT_VERSION VERSION_EQUAL CMAKE_FORMAT_REQUIRED ))
328
334
message (FATAL_ERROR "Required cmake-format version is"
329
335
"${CMAKE_FORMAT_REQUIRED} " )
@@ -351,8 +357,8 @@ if(UMF_FORMAT_CODE_STYLE)
351
357
list (APPEND format_cmake_list "${PROJECT_SOURCE_DIR} /CMakeLists.txt" )
352
358
353
359
message (
354
- STATUS "Adding 'cmake-format-check' and 'cmake-format-apply' make "
355
- " targets" )
360
+ STATUS
361
+ "Adding 'cmake-format-check' and 'cmake-format-apply' targets" )
356
362
357
363
add_custom_target (
358
364
cmake-format-check
@@ -365,28 +371,59 @@ if(UMF_FORMAT_CODE_STYLE)
365
371
COMMENT "Format Cmake files using cmake-format" )
366
372
endif ()
367
373
368
- # Add a convenience target for running both tools at once - available only
369
- # if both are found.
370
- if (CLANG_FORMAT AND CMAKE_FORMAT )
374
+ if (BLACK )
375
+ # black should maintain backward compatibility, we don't have to require
376
+ # a specific version
377
+ get_program_version_major_minor (${BLACK} BLACK_VERSION )
378
+ message (STATUS "Found black: ${BLACK} (version: ${BLACK_VERSION} )" )
379
+
380
+ message (
381
+ STATUS
382
+ "Adding 'black-format-check' and 'black-format-apply' targets" )
383
+
384
+ add_custom_target (
385
+ black-format-check
386
+ COMMAND ${BLACK} --check --verbose ${CMAKE_SOURCE_DIR}
387
+ COMMENT "Check Python files formatting using black formatter" )
388
+
389
+ add_custom_target (
390
+ black-format-apply
391
+ COMMAND ${BLACK} ${CMAKE_SOURCE_DIR}
392
+ COMMENT "Format Python files using black formatter" )
393
+ endif ()
394
+
395
+ # Add a convenience target for running all tools at once - available only if
396
+ # all are found.
397
+ if (CLANG_FORMAT
398
+ AND CMAKE_FORMAT
399
+ AND BLACK )
371
400
add_custom_target (
372
401
format-check
373
402
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
374
403
clang-format-check
375
404
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
376
405
cmake-format-check
377
- COMMENT "Running both clang-format-check and cmake-format-check" )
406
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
407
+ black-format-check
408
+ COMMENT "Running all formatting checks" )
378
409
379
410
add_custom_target (
380
411
format-apply
381
412
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
382
413
clang-format-apply
383
414
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
384
415
cmake-format-apply
385
- COMMENT "Format files using clang-format and cmake-format" )
416
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
417
+ black-format-apply
418
+ COMMENT "Format C/C++, CMake, and Python files" )
419
+ message (
420
+ STATUS
421
+ " Adding convenience targets 'format-check' and 'format-apply'."
422
+ )
386
423
else ()
387
424
message (
388
425
STATUS
389
- " Convenience targets 'make format-check' and 'make format-apply' are "
426
+ " Convenience targets 'format-check' and 'format-apply' are "
390
427
"not available. Use commands specific for found tools (see the log above)."
391
428
)
392
429
endif ()
0 commit comments