@@ -25,7 +25,10 @@ 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 (
29
+ UMF_FORMAT_CODE_STYLE
30
+ "Add clang, cmake, and black -format-check and -format-apply targets to make"
31
+ OFF )
29
32
option (USE_ASAN "Enable AddressSanitizer checks" OFF )
30
33
option (USE_UBSAN "Enable UndefinedBehaviorSanitizer checks" OFF )
31
34
option (USE_TSAN "Enable ThreadSanitizer checks" OFF )
@@ -255,13 +258,16 @@ if(UMF_FORMAT_CODE_STYLE)
255
258
find_program (CLANG_FORMAT NAMES clang-format-15 clang-format-15.0
256
259
clang-format )
257
260
find_program (CMAKE_FORMAT NAMES cmake-format )
261
+ find_program (BLACK NAMES black )
258
262
259
- if (NOT CLANG_FORMAT AND NOT CMAKE_FORMAT )
263
+ if (NOT CLANG_FORMAT
264
+ AND NOT CMAKE_FORMAT
265
+ AND NOT BLACK )
260
266
message (
261
267
FATAL_ERROR
262
268
"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." )
269
+ "${CLANG_FORMAT_REQUIRED} ), nor cmake-format (required version: "
270
+ "${CMAKE_FORMAT_VERSION} ), nor black was found." )
265
271
endif ()
266
272
267
273
if (CLANG_FORMAT )
@@ -365,24 +371,52 @@ 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' make targets"
383
+ )
384
+
385
+ add_custom_target (
386
+ black-format-check
387
+ COMMAND ${BLACK} --check --verbose ${CMAKE_SOURCE_DIR}
388
+ COMMENT "Check Python files formatting using black formatter" )
389
+
390
+ add_custom_target (
391
+ black-format-apply
392
+ COMMAND ${BLACK} ${CMAKE_SOURCE_DIR}
393
+ COMMENT "Format Python files using black formatter" )
394
+ endif ()
395
+
396
+ # Add a convenience target for running all tools at once - available only if
397
+ # all are found.
398
+ if (CLANG_FORMAT
399
+ AND CMAKE_FORMAT
400
+ AND BLACK )
371
401
add_custom_target (
372
402
format-check
373
403
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
374
404
clang-format-check
375
405
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
376
406
cmake-format-check
377
- COMMENT "Running both clang-format-check and cmake-format-check" )
407
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
408
+ black-format-check
409
+ COMMENT "Running all formatting checks" )
378
410
379
411
add_custom_target (
380
412
format-apply
381
413
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
382
414
clang-format-apply
383
415
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
384
416
cmake-format-apply
385
- COMMENT "Format files using clang-format and cmake-format" )
417
+ COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
418
+ black-format-apply
419
+ COMMENT "Format C/C++, CMake, and Python files" )
386
420
else ()
387
421
message (
388
422
STATUS
0 commit comments