Skip to content

Commit 1f96140

Browse files
[CMake] Add black formatter for Python files
and enable checking it in CI.
1 parent 34c584d commit 1f96140

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

.github/workflows/pr_push.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
- name: Install apt packages
9595
run: |
9696
sudo apt-get update
97-
sudo apt-get install -y cmake clang-format-15 cmake-format libhwloc-dev
97+
sudo apt-get install -y black cmake clang-format-15 cmake-format libhwloc-dev
9898
9999
- name: Configure CMake
100100
run: >
@@ -112,6 +112,9 @@ jobs:
112112
- name: Check cmake-format
113113
run: cmake --build build --target cmake-format-check
114114

115+
- name: Check Python formatting
116+
run: cmake --build build --target black-format-check
117+
115118
DocsBuild:
116119
name: Build docs
117120
runs-on: ubuntu-latest

CMakeLists.txt

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ option(UMF_BUILD_EXAMPLES "Build UMF examples" ON)
2525
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
2626
option(UMF_DEVELOPER_MODE "Enable developer checks, treats warnings as errors"
2727
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)
2932
option(USE_ASAN "Enable AddressSanitizer checks" OFF)
3033
option(USE_UBSAN "Enable UndefinedBehaviorSanitizer checks" OFF)
3134
option(USE_TSAN "Enable ThreadSanitizer checks" OFF)
@@ -255,13 +258,16 @@ if(UMF_FORMAT_CODE_STYLE)
255258
find_program(CLANG_FORMAT NAMES clang-format-15 clang-format-15.0
256259
clang-format)
257260
find_program(CMAKE_FORMAT NAMES cmake-format)
261+
find_program(BLACK NAMES black)
258262

259-
if(NOT CLANG_FORMAT AND NOT CMAKE_FORMAT)
263+
if(NOT CLANG_FORMAT
264+
AND NOT CMAKE_FORMAT
265+
AND NOT BLACK)
260266
message(
261267
FATAL_ERROR
262268
"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.")
265271
endif()
266272

267273
if(CLANG_FORMAT)
@@ -365,24 +371,52 @@ if(UMF_FORMAT_CODE_STYLE)
365371
COMMENT "Format Cmake files using cmake-format")
366372
endif()
367373

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)
371401
add_custom_target(
372402
format-check
373403
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
374404
clang-format-check
375405
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
376406
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")
378410

379411
add_custom_target(
380412
format-apply
381413
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
382414
clang-format-apply
383415
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target
384416
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")
386420
else()
387421
message(
388422
STATUS

CONTRIBUTING.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ To enable additional checks (including `-Werror` / `/WX` compilation flag), swit
6262
["CMake standard options"](./README.md#cmake-standard-options) section in the top-level Readme.
6363

6464
### Code style
65-
We use `clang-format` to verify and apply code style changes to source files.
65+
We use `clang-format` to verify and apply code style changes to C/C++ source files.
6666
To see all rules we require, please take a look at `.clang-format` file in the
6767
root directory of this repository. Similarly, we use `cmake-format` tool and
6868
`.cmake-format` file to verify and apply code style changes to CMake files.
69+
For Python source files we use `black` tool.
6970

7071
To enable code style checks and re-formatting, CMake option `UMF_FORMAT_CODE_STYLE` has to
7172
be switched on. You'll then have additional CMake targets available.
@@ -74,7 +75,7 @@ To verify correct coding style of your changes execute (assuming `build` is your
7475

7576
```bash
7677
$ cmake -B build -DUMF_FORMAT_CODE_STYLE=ON
77-
$ cmake --build build --target format-checks
78+
$ cmake --build build --target format-check
7879
```
7980

8081
We run these checks in our Continuous Integration (CI). So, if any issues were found,
@@ -87,9 +88,10 @@ $ cmake --build build --target format-apply
8788
# Remember to review introduced changes
8889
```
8990

90-
If you wish to use only `clang-format` or only `cmake-format`, you can execute the corresponding
91-
`clang-format-check` and `clang-format-apply` for source files, or `cmake-format-check` and
92-
`cmake-format-apply` for CMake files, respectively.
91+
If you wish to use only `clang-format`, or `cmake-format`, or `black`, you can execute the corresponding
92+
`clang-format-check` and `clang-format-apply` for C/C++ source files, or `cmake-format-check` and
93+
`cmake-format-apply` for CMake files, or `black-format-check` and `black-format-apply` for Python
94+
source files, respectively.
9395

9496
**NOTE**: We use specific versions of formatting tools to ensure consistency across the project. The required versions are:
9597
- clang-format version **15.0**, which can be installed with the command: `python -m pip install clang-format==15.0.7`.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ List of options provided by CMake:
102102
| UMF_BUILD_EXAMPLES | Build UMF examples | ON/OFF | ON |
103103
| UMF_ENABLE_POOL_TRACKING | Build UMF with pool tracking | ON/OFF | ON |
104104
| UMF_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF |
105-
| UMF_FORMAT_CODE_STYLE | Add clang and cmake -format-check and -format-apply targets to make | ON/OFF | OFF |
105+
| UMF_FORMAT_CODE_STYLE | Add clang, cmake, and black -format-check and -format-apply targets to make | ON/OFF | OFF |
106106
| USE_ASAN | Enable AddressSanitizer checks | ON/OFF | OFF |
107107
| USE_UBSAN | Enable UndefinedBehaviorSanitizer checks | ON/OFF | OFF |
108108
| USE_TSAN | Enable ThreadSanitizer checks | ON/OFF | OFF |

third_party/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Formatting the source code
33
clang-format==15.0.7
44
cmake-format==0.6.13
5+
black==23.7.0
56
# Generating HTML documentation
67
pygments==2.15.1
78
sphinxcontrib_applehelp==1.0.4

0 commit comments

Comments
 (0)