Skip to content

Commit 8a14ed6

Browse files
authored
Merge pull request #94 from Rust-for-Linux/rust-clippy
Clippy support
2 parents 33aa8fa + 27bc0cc commit 8a14ed6

File tree

6 files changed

+92
-26
lines changed

6 files changed

+92
-26
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ jobs:
131131
git checkout $(rustc -vV | grep -F 'commit-hash' | awk '{print $2}')
132132
git submodule update --init library
133133
134+
# Setup: clippy
135+
- run: rustup component add clippy
136+
134137
# Setup: bindgen
135138
- run: cargo install --version 0.56.0 bindgen
136139

@@ -178,6 +181,9 @@ jobs:
178181
- run: ls -l ${{ env.BUILD_DIR }}drivers/char/rust_example.o ${{ env.BUILD_DIR }}drivers/char/rust_example_3.ko ${{ env.BUILD_DIR }}rust/*.o ${{ env.BUILD_DIR }}vmlinux ${{ env.BUILD_DIR }}${{ env.IMAGE_PATH }}
179182
- run: size ${{ env.BUILD_DIR }}drivers/char/rust_example.o ${{ env.BUILD_DIR }}drivers/char/rust_example_3.ko ${{ env.BUILD_DIR }}rust/*.o ${{ env.BUILD_DIR }}vmlinux
180183

184+
# Clippy
185+
- run: make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 CLIPPY=1
186+
181187
# Docs
182188
- run: make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 rustdoc
183189

Documentation/rust/coding.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ individual files, and does not require a kernel configuration. Sometimes it may
4242
even work with broken code.
4343

4444

45+
Extra lints
46+
-----------
47+
48+
While ``rustc`` is a very helpful compiler, some extra lints and analysis are
49+
available via ``clippy``, a Rust linter. To enable it, pass ``CLIPPY=1`` to
50+
the same invocation you use for compilation, e.g.::
51+
52+
make ARCH=... CROSS_COMPILE=... CC=... -j... CLIPPY=1
53+
54+
At the moment, we do not enforce a "clippy-free" compilation, so you can treat
55+
the output the same way as the extra warning levels for C, e.g. like ``W=2``.
56+
Still, we use the default configuration, which is relatively conservative, so
57+
it is a good idea to read any output it may produce from time to time and fix
58+
the pointed out issues. The list of enabled lists will be likely tweaked over
59+
time, and extra levels may end up being introduced, e.g. ``CLIPPY=2``.
60+
61+
4562
Abstractions vs. bindings
4663
-------------------------
4764

Documentation/rust/quick-start.rst

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ Quick Start
44
===========
55

66
This document describes how to get started with kernel development in Rust.
7+
If you have worked previously with Rust, this will only take a moment.
78

89

9-
Requirements
10-
------------
10+
Requirements: Building
11+
----------------------
1112

12-
This section explains how to fetch the requirements to work with Rust.
13-
If you have worked previously with Rust, this will only take a moment.
13+
This section explains how to fetch the tools needed for building.
1414

1515
Some of these requirements might be available from your Linux distribution
1616
under names like ``rustc``, ``rust-src``, ``rust-bindgen``, etc. However,
@@ -64,11 +64,19 @@ which means you will need a recent LLVM installed; like when you compile
6464
the kernel with ``CC=clang`` or ``LLVM=1``.
6565

6666

67+
Requirements: Developing
68+
------------------------
69+
70+
This section explains how to fetch the tools needed for developing. That is,
71+
if you only want to build the kernel, you do not need them.
72+
73+
6774
rustfmt
6875
*******
6976

7077
The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
71-
including the generated C bindings.
78+
including the generated C bindings (for details, please see
79+
:ref:`Documentation/rust/coding.rst <rust_coding>`).
7280

7381
If you are using ``rustup``, its ``default`` profile already installs the tool,
7482
so you should be good to go. If you are using another profile, you can install
@@ -79,12 +87,29 @@ the component manually::
7987
The standalone installers also come with ``rustfmt``.
8088

8189

90+
clippy
91+
******
92+
93+
``clippy`` is a Rust linter. Installing it allows you to get extra warnings
94+
for Rust code passing ``CLIPPY=1`` to ``make`` (for details, please see
95+
:ref:`Documentation/rust/coding.rst <rust_coding>`).
96+
97+
If you are using ``rustup``, its ``default`` profile already installs the tool,
98+
so you should be good to go. If you are using another profile, you can install
99+
the component manually::
100+
101+
rustup component add clippy
102+
103+
The standalone installers also come with ``clippy``.
104+
105+
82106
rustdoc
83107
*******
84108

85-
Optionally, if you install the ``rustdoc`` tool, then you will be able
86-
to generate HTML documentation for Rust code, including for the libraries
87-
(crates) inside ``rust/`` that are used by the rest of the kernel.
109+
If you install the ``rustdoc`` tool, then you will be able to generate pretty
110+
HTML documentation for Rust code, including for the libraries (crates) inside
111+
``rust/`` that are used by the rest of the kernel (for details, please see
112+
:ref:`Documentation/rust/docs.rst <rust_docs>`).
88113

89114
If you are using ``rustup``, its ``default`` profile already installs the tool,
90115
so you should be good to go. If you are using another profile, you can install

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ ifndef KBUILD_CHECKSRC
212212
KBUILD_CHECKSRC = 0
213213
endif
214214

215+
# Enable "clippy" (a linter) as part of the Rust compilation.
216+
#
217+
# Use 'make CLIPPY=1' to enable it.
218+
ifeq ("$(origin CLIPPY)", "command line")
219+
KBUILD_CLIPPY := $(CLIPPY)
220+
endif
221+
215222
# Use make M=dir or set the environment variable KBUILD_EXTMOD to specify the
216223
# directory of external module to build. Setting M= takes precedence.
217224
ifeq ("$(origin M)", "command line")
@@ -446,6 +453,7 @@ STRIP = $(CROSS_COMPILE)strip
446453
endif
447454
RUSTC = rustc
448455
RUSTFMT = rustfmt
456+
CLIPPY_DRIVER = clippy-driver
449457
BINDGEN = bindgen
450458
PAHOLE = pahole
451459
RESOLVE_BTFIDS = $(objtree)/tools/bpf/resolve_btfids/resolve_btfids
@@ -516,7 +524,15 @@ KBUILD_LDFLAGS_MODULE :=
516524
KBUILD_LDFLAGS :=
517525
CLANG_FLAGS :=
518526

519-
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC BINDGEN
527+
ifeq ($(KBUILD_CLIPPY),1)
528+
CLIPPY_QUIET_TAG := CLIPPY$(space)
529+
else
530+
CLIPPY_QUIET_TAG :=
531+
CLIPPY_DRIVER :=
532+
endif
533+
export CLIPPY_QUIET_TAG
534+
535+
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC CLIPPY_DRIVER BINDGEN
520536
export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
521537
export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
522538
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
@@ -1653,7 +1669,7 @@ help:
16531669
@echo ' kselftest to existing .config.'
16541670
@echo ''
16551671
@echo 'Rust targets:'
1656-
@echo ' rustfmt - Reformat all the Rust code in the kernel.'
1672+
@echo ' rustfmt - Reformat all the Rust code in the kernel'
16571673
@echo ' rustfmtcheck - Checks if all the Rust code in the kernel'
16581674
@echo ' is formatted, printing a diff otherwise.'
16591675
@echo ' rustdoc - Generate Rust documentation'

rust/Makefile

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@ extra-$(CONFIG_RUST) += exports_kernel_generated.h
88

99
RUSTDOC = rustdoc
1010

11-
rustdoc_flags = $(filter-out --emit=% --out-dir=%, $(rustc_flags))
12-
1311
quiet_cmd_rustdoc = RUSTDOC $<
1412
cmd_rustdoc = \
1513
RUST_BINDINGS_FILE=$(abspath $(objtree)/rust/bindings_generated.rs) \
16-
$(RUSTDOC) $(rustdoc_flags) $(rustdoc_target_flags) -L $(objtree)/rust/ \
14+
$(RUSTDOC) $(filter-out --emit=%, $(rustc_flags)) \
15+
$(rustdoc_target_flags) -L $(objtree)/rust/ \
1716
--output $(objtree)/rust/doc --crate-name $(subst rustdoc-,,$@) \
1817
-Wmissing-docs @$(objtree)/include/generated/rustc_cfg $<
1918

2019
rustdoc: rustdoc-module rustdoc-kernel
2120

22-
rustdoc-module: rustdoc_target_flags = --crate-type proc-macro \
21+
rustdoc-module: private rustdoc_target_flags = --crate-type proc-macro \
2322
--extern proc_macro
2423
rustdoc-module: $(srctree)/rust/module.rs FORCE
2524
$(call if_changed,rustdoc)
2625

27-
rustdoc-kernel: rustdoc_target_flags = --extern alloc \
26+
rustdoc-kernel: private rustdoc_target_flags = --extern alloc \
2827
--extern module=$(objtree)/rust/libmodule.so
2928
rustdoc-kernel: $(srctree)/rust/kernel/lib.rs rustdoc-module \
3029
$(objtree)/rust/libmodule.so $(objtree)/rust/bindings_generated.rs FORCE
@@ -74,23 +73,23 @@ quiet_cmd_exports = EXPORTS $@
7473
| xargs -n1 -Isymbol \
7574
echo 'EXPORT_SYMBOL$(exports_target_type)(symbol);' > $@
7675

77-
$(objtree)/rust/exports_core_generated.h: exports_target_type := _RUST
76+
$(objtree)/rust/exports_core_generated.h: private exports_target_type := _RUST
7877
$(objtree)/rust/exports_core_generated.h: $(objtree)/rust/core.o FORCE
7978
$(call if_changed,exports)
8079

81-
$(objtree)/rust/exports_alloc_generated.h: exports_target_type := _RUST
80+
$(objtree)/rust/exports_alloc_generated.h: private exports_target_type := _RUST
8281
$(objtree)/rust/exports_alloc_generated.h: $(objtree)/rust/alloc.o FORCE
8382
$(call if_changed,exports)
8483

85-
$(objtree)/rust/exports_kernel_generated.h: exports_target_type := _RUST_GPL
84+
$(objtree)/rust/exports_kernel_generated.h: private exports_target_type := _RUST_GPL
8685
$(objtree)/rust/exports_kernel_generated.h: $(objtree)/rust/kernel.o FORCE
8786
$(call if_changed,exports)
8887

8988
# `-Cpanic=unwind -Cforce-unwind-tables=y` overrides `rustc_flags` in order to
9089
# avoid the https://github.com/rust-lang/rust/issues/82320 rustc crash.
91-
quiet_cmd_rustc_procmacro = RUSTC P $@
90+
quiet_cmd_rustc_procmacro = RUSTC P $(CLIPPY_QUIET_TAG)$@
9291
cmd_rustc_procmacro = \
93-
$(RUSTC) $(rustc_flags) --emit=dep-info,link --extern proc_macro \
92+
$(CLIPPY_DRIVER) $(RUSTC) $(rustc_flags) --emit=dep-info,link --extern proc_macro \
9493
-Cpanic=unwind -Cforce-unwind-tables=y \
9594
--crate-type proc-macro --out-dir $(objtree)/rust/ \
9695
--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<; \
@@ -100,10 +99,11 @@ quiet_cmd_rustc_procmacro = RUSTC P $@
10099
$(objtree)/rust/libmodule.so: $(srctree)/rust/module.rs FORCE
101100
$(call if_changed_dep,rustc_procmacro)
102101

103-
quiet_cmd_rustc_library = RUSTC L $@
102+
quiet_cmd_rustc_library = RUSTC L $(if $(skip_clippy),,$(CLIPPY_QUIET_TAG))$@
104103
cmd_rustc_library = \
105104
RUST_BINDINGS_FILE=$(abspath $(objtree)/rust/bindings_generated.rs) \
106-
$(RUSTC) $(rustc_flags) $(rustc_cross_flags) $(rustc_target_flags) \
105+
$(if $(skip_clippy),,$(CLIPPY_DRIVER)) $(RUSTC) $(rustc_flags) \
106+
$(rustc_cross_flags) $(rustc_target_flags) \
107107
--crate-type rlib --out-dir $(objtree)/rust/ -L $(objtree)/rust/ \
108108
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
109109
mv $(objtree)/rust/$(patsubst %.o,%,$(notdir $@)).d $(depfile); \
@@ -115,20 +115,22 @@ rustc_sysroot = $(shell $(RUSTC) $(rustc_flags) --print sysroot)
115115
rustc_src = $(rustc_sysroot)/lib/rustlib/src/rust
116116

117117
.SECONDEXPANSION:
118+
$(objtree)/rust/core.o: private skip_clippy = 1
118119
$(objtree)/rust/core.o: $$(rustc_src)/library/core/src/lib.rs FORCE
119120
$(call if_changed_dep,rustc_library)
120121

121-
$(objtree)/rust/compiler_builtins.o: rustc_objcopy = -w -W '__*'
122+
$(objtree)/rust/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
122123
$(objtree)/rust/compiler_builtins.o: $(srctree)/rust/compiler_builtins.rs \
123124
$(objtree)/rust/core.o FORCE
124125
$(call if_changed_dep,rustc_library)
125126

127+
$(objtree)/rust/alloc.o: private skip_clippy = 1
126128
$(objtree)/rust/alloc.o: $$(rustc_src)/library/alloc/src/lib.rs \
127129
$(objtree)/rust/compiler_builtins.o FORCE
128130
$(call if_changed_dep,rustc_library)
129131

130132
# ICE on `--extern module`: https://github.com/rust-lang/rust/issues/56935
131-
$(objtree)/rust/kernel.o: rustc_target_flags = --extern alloc \
133+
$(objtree)/rust/kernel.o: private rustc_target_flags = --extern alloc \
132134
--extern module=$(objtree)/rust/libmodule.so
133135
$(objtree)/rust/kernel.o: $(srctree)/rust/kernel/lib.rs $(objtree)/rust/alloc.o \
134136
$(objtree)/rust/libmodule.so $(objtree)/rust/bindings_generated.rs FORCE

scripts/Makefile.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ $(obj)/%.lst: $(src)/%.c FORCE
301301

302302
rustc_cross_flags := --target=$(srctree)/arch/$(SRCARCH)/rust/target.json
303303

304-
quiet_cmd_rustc_o_rs = RUSTC $(quiet_modtag) $@
304+
quiet_cmd_rustc_o_rs = RUSTC $(CLIPPY_QUIET_TAG)$(quiet_modtag) $@
305305
cmd_rustc_o_rs = \
306306
RUST_MODFILE=$(modfile) \
307-
$(RUSTC) $(rustc_flags) $(rustc_cross_flags) \
307+
$(CLIPPY_DRIVER) $(RUSTC) $(rustc_flags) $(rustc_cross_flags) \
308308
--extern alloc --extern kernel \
309309
--crate-type rlib --out-dir $(obj) -L $(objtree)/rust/ \
310310
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \

0 commit comments

Comments
 (0)