Skip to content

Commit 40fcbfe

Browse files
authored
Merge pull request #637 from ChinYikMing/dtsfmt
Leverage dtsfmt as the standard formatter for DTS and DTSI files
2 parents 3a168b7 + 1048c2d commit 40fcbfe

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

.ci/check-format.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
set -u -o pipefail
66

7-
REPO_ROOT="$(git rev-parse --show-toplevel)"
8-
97
# Use git ls-files to exclude submodules and untracked files
108
C_SOURCES=()
119
while IFS= read -r file; do
@@ -58,4 +56,20 @@ else
5856
PY_FORMAT_EXIT=0
5957
fi
6058

61-
exit $((C_FORMAT_EXIT + SH_FORMAT_EXIT + PY_FORMAT_EXIT))
59+
DTS_SOURCES=()
60+
while IFS= read -r file; do
61+
[ -n "$file" ] && DTS_SOURCES+=("$file")
62+
done < <(git ls-files -- '*.dts' '*.dtsi')
63+
64+
if [ ${#DTS_SOURCES[@]} -gt 0 ]; then
65+
echo "Checking DTS/DTSI files..."
66+
DTS_FORMAT_EXIT=0
67+
for dts_src in "${DTS_SOURCES[@]}"; do
68+
dtsfmt --check "${dts_src}"
69+
DTS_FORMAT_EXIT=$((DTS_FORMAT_EXIT + $?))
70+
done
71+
else
72+
DTS_FORMAT_EXIT=0
73+
fi
74+
75+
exit $((C_FORMAT_EXIT + SH_FORMAT_EXIT + PY_FORMAT_EXIT + DTS_FORMAT_EXIT))

.dtsfmtrc.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
indent_str = " "
2+
warn_on_unhandled_tokens = true

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ jobs:
594594
run: |
595595
sudo apt-get install -q=2 clang-format-18 shfmt python3-pip
596596
pip3 install black==25.1.0
597+
curl -LSfs https://go.mskelton.dev/dtsfmt/install | sh -s -- -y
597598
.ci/check-newline.sh
598599
.ci/check-format.sh
599600
shell: bash

CONTRIBUTING.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,24 @@ Software requirement:
5050
* [clang-format](https://clang.llvm.org/docs/ClangFormat.html) version 18.
5151
* [shfmt](https://github.com/mvdan/sh).
5252
* [black](https://github.com/psf/black) version 25.1.0.
53+
* [dtsfmt](https://github.com/mskelton/dtsfmt).
5354

5455
To maintain a uniform style across languages, run:
5556
* `clang-format -i *.{c,h}` to apply the project’s C/C++ formatting rules from the up-to-date .clang-format file.
5657
* `shfmt -w $(find . -type f -name "*.sh")` to clean and standardize all shell scripts.
5758
* `black .` to enforce a consistent, idiomatic layout for Python code.
58-
* `make format` to automatically run all three formatters.
59+
* `dtsfmt ./src/devices/` to enforce a consistent layout for device tree source (and include).
60+
* `make format` to automatically run all four formatters.
61+
62+
## Coding Style for Device Tree Source and Device Tree Source Include
63+
64+
Device Tree Source (and Include) must be clean, consistent, and portable. The following `dtsfmt` rules(check `.dtsfmtrc.toml` file) are enforced project-wide:
65+
* Use spaces for indentation.
66+
* Indent with 4 spaces.
67+
* Use multi-line comments.
68+
* Use Unix-style line endings (LF).
69+
* Remove trailing whitespace at the end of lines.
70+
* Ensure the file ends with a newline.
5971

6072
## Coding Style for Shell Script
6173

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ CLANG_FORMAT := $(shell which clang-format-18 2>/dev/null)
492492

493493
SHFMT := $(shell which shfmt 2>/dev/null)
494494

495+
DTSFMT := $(shell which dtsfmt 2>/dev/null)
496+
495497
BLACK := $(shell which black 2>/dev/null)
496498
BLACK_VERSION := $(if $(strip $(BLACK)),$(shell $(BLACK) --version | head -n 1 | awk '{print $$2}'),)
497499
BLACK_MAJOR := $(shell echo $(BLACK_VERSION) | cut -f1 -d.)
@@ -520,6 +522,15 @@ else
520522
$(Q)$(SHFMT) -w $(shell find . \( $(SUBMODULES_PRUNE_PATHS) -o -path \"./$(OUT)\" \) \
521523
-prune -o -name "*.sh" -print)
522524
endif
525+
ifeq ($(DTSFMT),)
526+
$(error dtsfmt not found. Install dtsfmt and try again)
527+
else
528+
$(Q)for dts_src in $$(find . \( $(SUBMODULES_PRUNE_PATHS) -o -path "./$(OUT)" \) \
529+
-prune -o \( -name "*.dts" -o -name "*.dtsi" \) -print); \
530+
do \
531+
$(DTSFMT) $$dts_src; \
532+
done
533+
endif
523534
ifeq ($(BLACK),)
524535
$(error black not found. Install black version 25.1.0 or above and try again)
525536
else

src/devices/minimal.dts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88
aliases {
99
serial0 = "/soc@F0000000/serial@4000000";
1010
};
11-
1211
chosen {
1312
bootargs = "earlycon console=ttyS0";
1413
stdout-path = "serial0";
1514
linux,initrd-start = <INITRD_START>;
1615
linux,initrd-end = <INITRD_END>;
1716
};
18-
1917
cpus {
2018
#address-cells = <1>;
2119
#size-cells = <0>;
2220
timebase-frequency = <65000000>;
21+
2322
cpu0: cpu@0 {
2423
device_type = "cpu";
2524
compatible = "riscv";
2625
reg = <0>;
2726
riscv,isa = "rv32ima";
2827
mmu-type = "riscv,sv32";
28+
2929
cpu0_intc: interrupt-controller {
3030
#interrupt-cells = <1>;
3131
#address-cells = <0>;
@@ -34,13 +34,11 @@
3434
};
3535
};
3636
};
37-
3837
sram: memory@0 {
3938
device_type = "memory";
4039
reg = <MEM_START MEM_END>;
4140
reg-names = "sram0";
4241
};
43-
4442
soc: soc@F0000000 {
4543
#address-cells = <1>;
4644
#size-cells = <1>;
@@ -57,13 +55,14 @@
5755
interrupts-extended = <&cpu0_intc 9>;
5856
riscv,ndev = <31>;
5957
};
60-
6158
serial@4000000 {
6259
compatible = "ns16550";
6360
reg = <0x4000000 0x100000>;
6461
interrupts = <1>;
6562
no-loopback-test;
66-
clock-frequency = <5000000>; /* the baudrate divisor is ignored */
63+
clock-frequency = <5000000>;
64+
65+
/* the baudrate divisor is ignored */
6766
};
6867

6968
/*

0 commit comments

Comments
 (0)