Skip to content

Commit 1eb301b

Browse files
committed
rust: proc-macro2: enable support in kbuild
With all the new files in place and ready from the new crate, enable the support for it in the build system. `proc_macro_byte_character` and `proc_macro_c_str_literals` were stabilized in Rust 1.79.0 [1] and were implemented earlier than our minimum Rust version (1.78) [2][3]. Thus just enable them instead of using the `cfg` that `proc-macro2` uses to emulate them in older compilers. Link: rust-lang/rust#123431 [1] Link: rust-lang/rust#112711 [2] Link: rust-lang/rust#119651 [3] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 41b03c3 commit 1eb301b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,9 @@ PHONY += rustfmt rustfmtcheck
18301830

18311831
rustfmt:
18321832
$(Q)find $(srctree) $(RCS_FIND_IGNORE) \
1833+
\( \
1834+
-path $(srctree)/rust/proc-macro2 \
1835+
\) -prune -o \
18331836
-type f -a -name '*.rs' -a ! -name '*generated*' -print \
18341837
| xargs $(RUSTFMT) $(rustfmt_flags)
18351838

rust/Makefile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ endif
2727

2828
obj-$(CONFIG_RUST) += exports.o
2929

30+
always-$(CONFIG_RUST) += libproc_macro2.rlib
31+
3032
always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
3133
always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
3234

@@ -76,6 +78,17 @@ core-flags := \
7678
--edition=$(core-edition) \
7779
$(call cfgs-to-flags,$(core-cfgs))
7880

81+
proc_macro2-cfgs := \
82+
feature="proc-macro" \
83+
wrap_proc_macro \
84+
$(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
85+
86+
# Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
87+
proc_macro2-flags := \
88+
--cap-lints=allow \
89+
-Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
90+
$(call cfgs-to-flags,$(proc_macro2-cfgs))
91+
7992
# `rustdoc` did not save the target modifiers, thus workaround for
8093
# the time being (https://github.com/rust-lang/rust/issues/144521).
8194
rustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
@@ -128,10 +141,15 @@ rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
128141
$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
129142
echo ".logo-container > img { object-fit: contain; }" >> $$f; done
130143

144+
rustdoc-proc_macro2: private rustdoc_host = yes
145+
rustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
146+
rustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
147+
+$(call if_changed,rustdoc)
148+
131149
rustdoc-macros: private rustdoc_host = yes
132150
rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
133151
--extern proc_macro
134-
rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean FORCE
152+
rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
135153
+$(call if_changed,rustdoc)
136154

137155
# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
@@ -194,6 +212,10 @@ rusttestlib-build_error: $(src)/build_error.rs FORCE
194212
rusttestlib-ffi: $(src)/ffi.rs FORCE
195213
+$(call if_changed,rustc_test_library)
196214

215+
rusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
216+
rusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
217+
+$(call if_changed,rustc_test_library)
218+
197219
rusttestlib-macros: private rustc_target_flags = --extern proc_macro
198220
rusttestlib-macros: private rustc_test_library_proc = yes
199221
rusttestlib-macros: $(src)/macros/lib.rs FORCE
@@ -440,6 +462,11 @@ quiet_cmd_rustc_hostlibrary = $(RUSTC_OR_CLIPPY_QUIET) H $@
440462
mv $(objtree)/$(obj)/$(patsubst lib%.rlib,%,$(notdir $@)).d $(depfile); \
441463
sed -i '/^\#/d' $(depfile)
442464

465+
$(obj)/libproc_macro2.rlib: private skip_clippy = 1
466+
$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
467+
$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
468+
+$(call if_changed_dep,rustc_hostlibrary)
469+
443470
quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
444471
cmd_rustc_procmacro = \
445472
$(RUSTC_OR_CLIPPY) $(rust_common_flags) $(rustc_target_flags) \
@@ -451,7 +478,7 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
451478
@$(objtree)/include/generated/rustc_cfg $<
452479

453480
# Procedural macros can only be used with the `rustc` that compiled it.
454-
$(obj)/$(libmacros_name): $(src)/macros/lib.rs FORCE
481+
$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib FORCE
455482
+$(call if_changed_dep,rustc_procmacro)
456483

457484
$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel
@@ -474,6 +501,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
474501
rust-analyzer:
475502
$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
476503
--cfgs='core=$(core-cfgs)' $(core-edition) \
504+
--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
477505
$(realpath $(srctree)) $(realpath $(objtree)) \
478506
$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
479507
> rust-project.json

scripts/generate_rust_analyzer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def append_sysroot_crate(
8686
[],
8787
)
8888

89+
append_crate(
90+
"proc_macro2",
91+
srctree / "rust" / "proc-macro2" / "lib.rs",
92+
["core", "alloc", "std", "proc_macro"],
93+
cfg=crates_cfgs["proc_macro2"],
94+
)
95+
8996
append_crate(
9097
"macros",
9198
srctree / "rust" / "macros" / "lib.rs",

0 commit comments

Comments
 (0)