Skip to content

Commit 9646c0e

Browse files
authored
Merge pull request #627 from ojeda/rust-test-ci
Build system review
2 parents 1648677 + cbdf925 commit 9646c0e

File tree

4 files changed

+54
-46
lines changed

4 files changed

+54
-46
lines changed

Makefile

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,11 +1162,7 @@ export MODULES_NSDEPS := $(extmod_prefix)modules.nsdeps
11621162
ifeq ($(KBUILD_EXTMOD),)
11631163
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/
11641164
core-$(CONFIG_BLOCK) += block/
1165-
1166-
# Keep this one as an `ifdef` block since its `Makefile` runs `rustc`.
1167-
ifdef CONFIG_RUST
1168-
core-y += rust/
1169-
endif
1165+
core-$(CONFIG_RUST) += rust/
11701166

11711167
vmlinux-dirs := $(patsubst %/,%,$(filter %/, \
11721168
$(core-y) $(core-m) $(drivers-y) $(drivers-m) \
@@ -1269,6 +1265,7 @@ prepare0: archprepare
12691265
$(Q)$(MAKE) $(build)=scripts/mod
12701266
$(Q)$(MAKE) $(build)=.
12711267
ifdef CONFIG_RUST
1268+
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust-is-available.sh -v
12721269
$(Q)$(MAKE) $(build)=rust
12731270
endif
12741271

@@ -1583,7 +1580,7 @@ MRPROPER_FILES += include/config include/generated \
15831580
certs/x509.genkey \
15841581
vmlinux-gdb.py \
15851582
*.spec \
1586-
rust/*_generated.h rust/*_generated.rs rust/libmacros.so
1583+
rust/libmacros.so
15871584

15881585
# clean - Delete most, but leave enough to build external modules
15891586
#
@@ -1608,6 +1605,9 @@ $(mrproper-dirs):
16081605

16091606
mrproper: clean $(mrproper-dirs)
16101607
$(call cmd,rmfiles)
1608+
@find . $(RCS_FIND_IGNORE) \
1609+
\( -name '*.rmeta' \) \
1610+
-type f -print | xargs rm -f
16111611

16121612
# distclean
16131613
#
@@ -1786,7 +1786,7 @@ $(DOC_TARGETS):
17861786
# "Is Rust available?" target
17871787
PHONY += rustavailable
17881788
rustavailable:
1789-
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust-is-available.sh -v
1789+
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust-is-available.sh -v && echo >&2 "Rust is available!"
17901790

17911791
# Documentation target
17921792
#
@@ -1992,7 +1992,6 @@ clean: $(clean-dirs)
19921992
$(call cmd,rmfiles)
19931993
@find $(if $(KBUILD_EXTMOD), $(KBUILD_EXTMOD), .) $(RCS_FIND_IGNORE) \
19941994
\( -name '*.[aios]' -o -name '*.ko' -o -name '.*.cmd' \
1995-
-o -name '*.rmeta' \
19961995
-o -name '*.ko.*' \
19971996
-o -name '*.dtb' -o -name '*.dtbo' -o -name '*.dtb.S' -o -name '*.dt.yaml' \
19981997
-o -name '*.dwo' -o -name '*.lst' \

init/Kconfig

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,6 @@ config RUST
20602060
bool "Rust support"
20612061
depends on RUST_IS_AVAILABLE
20622062
depends on ARM64 || CPU_32v6 || CPU_32v6K || (PPC64 && CPU_LITTLE_ENDIAN) || X86_64 || RISCV
2063-
depends on !COMPILE_TEST
20642063
depends on !MODVERSIONS
20652064
depends on !GCC_PLUGIN_RANDSTRUCT
20662065
default n
@@ -2078,14 +2077,14 @@ config RUST
20782077
If unsure, say N.
20792078

20802079
config RUSTC_VERSION_TEXT
2081-
depends on RUST
20822080
string
2083-
default $(shell,$(RUSTC) --version)
2081+
depends on RUST
2082+
default $(shell,command -v $(RUSTC) >/dev/null 2>&1 && $(RUSTC) --version || echo n)
20842083

20852084
config BINDGEN_VERSION_TEXT
2086-
depends on RUST
20872085
string
2088-
default $(shell,$(BINDGEN) --version)
2086+
depends on RUST
2087+
default $(shell,command -v $(BINDGEN) >/dev/null 2>&1 && $(BINDGEN) --version || echo n)
20892088

20902089
#
20912090
# Place an empty function call at each tracepoint site. Can be

rust/Makefile

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
obj-$(CONFIG_RUST) += core.o compiler_builtins.o helpers.o
4-
extra-$(CONFIG_RUST) += exports_core_generated.h
4+
always-$(CONFIG_RUST) += exports_core_generated.h
55

6-
extra-$(CONFIG_RUST) += libmacros.so
6+
always-$(CONFIG_RUST) += libmacros.so
7+
no-clean-files += libmacros.so
78

8-
extra-$(CONFIG_RUST) += bindings_generated.rs bindings_helpers_generated.rs
9+
always-$(CONFIG_RUST) += bindings_generated.rs bindings_helpers_generated.rs
910
obj-$(CONFIG_RUST) += alloc.o kernel.o
10-
extra-$(CONFIG_RUST) += exports_alloc_generated.h exports_kernel_generated.h
11+
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_kernel_generated.h
1112

1213
ifdef CONFIG_RUST_BUILD_ASSERT_DENY
13-
extra-$(CONFIG_RUST) += build_error.o
14+
always-$(CONFIG_RUST) += build_error.o
1415
else
1516
obj-$(CONFIG_RUST) += build_error.o
1617
endif
1718

1819
obj-$(CONFIG_RUST) += exports.o
1920

21+
# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
22+
ifdef CONFIG_RUST
23+
2024
ifeq ($(quiet),silent_)
2125
cargo_quiet=-q
2226
rust_test_quiet=-q
@@ -302,8 +306,8 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
302306
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
303307

304308
# `$(rust_flags)` is passed in case the user added `--sysroot`.
305-
rustc_sysroot = $(shell $(RUSTC) $(rust_flags) --print sysroot)
306-
rustc_host_target = $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
309+
rustc_sysroot := $(shell $(RUSTC) $(rust_flags) --print sysroot)
310+
rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
307311
RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
308312

309313
rust-analyzer:
@@ -335,14 +339,14 @@ $(objtree)/rust/kernel.o: $(srctree)/rust/kernel/lib.rs $(objtree)/rust/alloc.o
335339
$(objtree)/rust/bindings_helpers_generated.rs FORCE
336340
$(call if_changed_dep,rustc_library)
337341

338-
# Targets that need to expand twice
339-
.SECONDEXPANSION:
340342
$(objtree)/rust/core.o: private skip_clippy = 1
341343
$(objtree)/rust/core.o: private skip_flags = -Dunreachable_pub --edition=2021
342344
$(objtree)/rust/core.o: private rustc_target_flags = $(core-cfgs) --edition=2018
343-
$(objtree)/rust/core.o: $$(RUST_LIB_SRC)/core/src/lib.rs FORCE
345+
$(objtree)/rust/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
344346
$(call if_changed_dep,rustc_library)
345347

346348
rustdoc-core: private rustc_target_flags = $(core-cfgs)
347-
rustdoc-core: $$(RUST_LIB_SRC)/core/src/lib.rs FORCE
349+
rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
348350
$(call if_changed,rustdoc)
351+
352+
endif # CONFIG_RUST

scripts/rust-is-available.sh

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ fi
4444
# Check that the Rust compiler version is suitable.
4545
#
4646
# Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
47-
rust_compiler_version=$("$RUSTC" --version | cut -f2 -d' ' | cut -f1 -d'-')
47+
rust_compiler_version=$( \
48+
LC_ALL=C "$RUSTC" --version 2>/dev/null \
49+
| head -n 1 \
50+
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
51+
)
4852
rust_compiler_min_version=$($min_tool_version rustc)
4953
rust_compiler_cversion=$(get_canonical_version $rust_compiler_version)
5054
rust_compiler_min_cversion=$(get_canonical_version $rust_compiler_min_version)
@@ -69,7 +73,11 @@ fi
6973
# Check that the Rust bindings generator is suitable.
7074
#
7175
# Non-stable and distributions' versions may have a version suffix, e.g. `-dev`.
72-
rust_bindings_generator_version=$("$BINDGEN" --version | cut -f2 -d' ' | cut -f1 -d'-')
76+
rust_bindings_generator_version=$( \
77+
LC_ALL=C "$BINDGEN" --version 2>/dev/null \
78+
| head -n 1 \
79+
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
80+
)
7381
rust_bindings_generator_min_version=$($min_tool_version bindgen)
7482
rust_bindings_generator_cversion=$(get_canonical_version $rust_bindings_generator_version)
7583
rust_bindings_generator_min_cversion=$(get_canonical_version $rust_bindings_generator_min_version)
@@ -92,15 +100,11 @@ if [ "$1" = -v ] && [ "$rust_bindings_generator_cversion" -gt "$rust_bindings_ge
92100
fi
93101

94102
# Check that the `libclang` used by the Rust bindings generator is suitable.
95-
#
96-
# There may be a better way to do this in the future,
97-
# see https://github.com/rust-lang/rust-bindgen/issues/2138
98-
bindgen_libclang_full_version=$(\
99-
"$BINDGEN" $(dirname $0)/rust-is-available-bindgen-libclang.h 2>&1 >/dev/null \
100-
| grep -F 'clang version' \
101-
| sed -E 's:^.*(clang version .*) \[.*$:\1:' \
103+
bindgen_libclang_version=$( \
104+
LC_ALL=C "$BINDGEN" $(dirname $0)/rust-is-available-bindgen-libclang.h 2>&1 >/dev/null \
105+
| grep -F 'clang version ' \
106+
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
102107
)
103-
bindgen_libclang_version=$(echo "$bindgen_libclang_full_version" | cut -f3 -d' ')
104108
bindgen_libclang_min_version=$($min_tool_version llvm)
105109
bindgen_libclang_cversion=$(get_canonical_version $bindgen_libclang_version)
106110
bindgen_libclang_min_cversion=$(get_canonical_version $bindgen_libclang_min_version)
@@ -115,17 +119,24 @@ if [ "$bindgen_libclang_cversion" -lt "$bindgen_libclang_min_cversion" ]; then
115119
exit 1
116120
fi
117121

118-
# If the C compiler is Clang, then we can also check whether its full version
119-
# matches exactly the `libclang` used by the Rust bindings generator.
122+
# If the C compiler is Clang, then we can also check whether its version
123+
# matches the `libclang` version used by the Rust bindings generator.
124+
#
125+
# In the future, we might be able to perform a full version check, see
126+
# https://github.com/rust-lang/rust-bindgen/issues/2138.
120127
if [ "$1" = -v ]; then
121128
cc_name=$($(dirname $0)/cc-version.sh "$CC" | cut -f1 -d' ')
122129
if [ "$cc_name" = Clang ]; then
123-
clang_full_version=$("$CC" --version | grep -F 'clang version')
124-
if [ "$clang_full_version" != "$bindgen_libclang_full_version" ]; then
130+
clang_version=$( \
131+
LC_ALL=C "$CC" --version 2>/dev/null \
132+
| head -n 1 \
133+
| grep -oE '[0-9]+\.[0-9]+\.[0-9]+' \
134+
)
135+
if [ "$clang_version" != "$bindgen_libclang_version" ]; then
125136
echo >&2 "***"
126-
echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') full version does not match Clang's. This may be a problem."
127-
echo >&2 "*** libclang: $bindgen_libclang_full_version"
128-
echo >&2 "*** Clang: $clang_full_version"
137+
echo >&2 "*** libclang (used by the Rust bindings generator '$BINDGEN') version does not match Clang's. This may be a problem."
138+
echo >&2 "*** libclang version: $bindgen_libclang_version"
139+
echo >&2 "*** Clang version: $clang_version"
129140
echo >&2 "***"
130141
fi
131142
fi
@@ -146,8 +157,3 @@ if [ ! -e "$rustc_src_core" ]; then
146157
fi
147158
exit 1
148159
fi
149-
150-
# Success!
151-
if [ "$1" = -v ]; then
152-
echo >&2 "Rust is available!"
153-
fi

0 commit comments

Comments
 (0)