Skip to content

Commit 27808a8

Browse files
committed
ci: Add Rust for Linux
Rust for Linux, so far, has pinned the Rust compiler and `bindgen` versions. The kernel is looking into expanding that support to several versions, i.e. establishing a minimum supported version, so that the kernel can start to be more easily built. In particular, it should be possible to build the kernel using the tools provided directly by Linux distributions. In order to help achieve that goal, the Rust project has added the kernel to its Rust pre-merge CI. This commit does the same for `bindgen`. In particular, it adds a quick, build-only test of the Rust code in the kernel as an extra step in the `test` workflow. This is intended to be an end-to-end test that runs what kernel developers/users would do. In particular, it is useful to catch certain issues that go beyond the C header comparisons. For instance, it would have been able to catch an issue like the `--version` option unexpectedly requiring a header in 0.69.0 (fixed in 0.69.1) [1]. It would also have detected another issue present in 0.66.0 and 0.66.1: a panic handling certain C headers with string literals containing an interior NUL [2]. While the kernel is not really a stable test, and such an issue would still require that a proper test is added, it is nevertheless a good test case of non-trivial C headers that may trigger edge cases like that. Of course, `bindgen` may need to disable the test for different reasons, i.e. there is no expectation to block any urgent/important PR, and the kernel can also call `bindgen` differently depending on the version, i.e. we are happy to adjust on our side too. Even if it gets disabled often, we would still be in a better situation than not having the test at all. The Linux version (hash or tag) should ideally be updated from time to time (e.g. every kernel `-rc1`), and each update should only contain that change. Link: rust-lang#2678 [1] Link: rust-lang#2567 [2] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 4b3cd6c commit 27808a8

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

.github/workflows/bindgen.yml

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ jobs:
227227
BINDGEN_FEATURE_RUNTIME: ${{matrix.feature_runtime}}
228228
BINDGEN_FEATURE_EXTRA_ASSERTS: ${{matrix.feature_extra_asserts}}
229229
BINDGEN_NO_DEFAULT_FEATURES: ${{matrix.no_default_features}}
230+
BINDGEN_RUST_FOR_LINUX_TEST: ${{matrix.os == 'ubuntu-latest' && matrix.llvm_version == '16.0' && matrix.feature_extra_asserts == 0 && 1 || 0}}
230231
run: ./ci/test.sh
231232

232233
check-cfg:

ci/test.sh

+78
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,81 @@ assert_no_diff
118118

119119
# Run the integration tests
120120
(cd bindgen-integration && cargo test $CARGO_ARGS)
121+
122+
if [ "$BINDGEN_RUST_FOR_LINUX_TEST" == "1" ]; then
123+
# Run the Rust for Linux test
124+
#
125+
# This is intended to be an end-to-end test that runs what Linux kernel
126+
# developers/users would do. It is a quick, build-only test of the Rust code
127+
# in the Linux kernel.
128+
129+
# Put LLVM binaries in the path for `LLVM=1`. The LLVM `bin` directory should
130+
# go first since there are others in the Ubuntu image.
131+
export PATH="${LLVM_DIRECTORY}/bin:${PATH}"
132+
133+
# Kernel build dependency: `bindgen-cli`, which is under test.
134+
#
135+
# Using `cargo build` (and adding the two common profiles to the `$PATH`) so
136+
# that we can use `$CARGO_ARGS` as is, since `cargo install` does not support
137+
# `--release`. A cleaner alternative is using `--out-dir`, but it is unstable.
138+
# `--target-dir` could be used to isolate the test more (another test could
139+
# build the other profile which then gets picked up by `$PATH`), but it would
140+
# mean rebuilding more.
141+
(cd bindgen-cli && cargo build $CARGO_ARGS)
142+
export PATH="${PWD}/target/release:${PWD}/target/debug:${PATH}"
143+
144+
# Kernel build dependency: `libelf-dev`.
145+
sudo apt-get update
146+
sudo apt-get install libelf-dev
147+
148+
# Kernel build dependency: the Rust standard library sources.
149+
#
150+
# `rustup` is used here to install the `rust-src` component (instead of using
151+
# `actions-rs/toolchain`'s `components` option in the workflow step) since we
152+
# only need it for this test, and anyway the action installs `rustup`.
153+
rustup component add rust-src
154+
155+
# Ideally this should be updated from time to time (e.g. every kernel `-rc1`),
156+
# and each update should only contain this change.
157+
#
158+
# Both commit hashes and tags are supported.
159+
LINUX_VERSION=c13320499ba0efd93174ef6462ae8a7a2933f6e7
160+
161+
# Download Linux at a specific commit
162+
mkdir -p linux
163+
git -C linux init
164+
git -C linux remote add origin https://github.com/torvalds/linux.git
165+
git -C linux fetch --depth 1 origin ${LINUX_VERSION}
166+
git -C linux checkout FETCH_HEAD
167+
168+
# Configure Rust for Linux
169+
cat <<EOF > linux/kernel/configs/rfl-for-bindgen-ci.config
170+
# CONFIG_WERROR is not set
171+
172+
CONFIG_RUST=y
173+
174+
CONFIG_SAMPLES=y
175+
CONFIG_SAMPLES_RUST=y
176+
177+
CONFIG_SAMPLE_RUST_MINIMAL=m
178+
CONFIG_SAMPLE_RUST_PRINT=y
179+
180+
CONFIG_RUST_PHYLIB_ABSTRACTIONS=y
181+
CONFIG_AX88796B_PHY=y
182+
CONFIG_AX88796B_RUST_PHY=y
183+
184+
CONFIG_KUNIT=y
185+
CONFIG_RUST_KERNEL_DOCTESTS=y
186+
EOF
187+
188+
make -C linux LLVM=1 -j$(($(nproc) + 1)) \
189+
rustavailable \
190+
defconfig \
191+
rfl-for-bindgen-ci.config
192+
193+
# Build Rust for Linux
194+
make -C linux LLVM=1 -j$(($(nproc) + 1)) \
195+
samples/rust/rust_minimal.o \
196+
samples/rust/rust_print.o \
197+
drivers/net/phy/ax88796b_rust.o
198+
fi

0 commit comments

Comments
 (0)