Skip to content

Commit 158a3b7

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. In addition, skip formatting for this vendored crate and take the chance to add a comment mentioning this. Link: rust-lang/rust#123431 [1] Link: rust-lang/rust#112711 [2] Link: rust-lang/rust#119651 [3] Reviewed-by: Gary Guo <[email protected]> Tested-by: Gary Guo <[email protected]> Tested-by: Jesung Yang <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent bc1565e commit 158a3b7

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,10 +1826,15 @@ rusttest: prepare
18261826
$(Q)$(MAKE) $(build)=rust $@
18271827

18281828
# Formatting targets
1829+
#
1830+
# Generated files as well as vendored crates are skipped.
18291831
PHONY += rustfmt rustfmtcheck
18301832

18311833
rustfmt:
18321834
$(Q)find $(srctree) $(RCS_FIND_IGNORE) \
1835+
\( \
1836+
-path $(srctree)/rust/proc-macro2 \
1837+
\) -prune -o \
18331838
-type f -a -name '*.rs' -a ! -name '*generated*' -print \
18341839
| xargs $(RUSTFMT) $(rustfmt_flags)
18351840

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)
@@ -125,10 +138,15 @@ rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
125138
$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
126139
echo ".logo-container > img { object-fit: contain; }" >> $$f; done
127140

141+
rustdoc-proc_macro2: private rustdoc_host = yes
142+
rustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
143+
rustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
144+
+$(call if_changed,rustdoc)
145+
128146
rustdoc-macros: private rustdoc_host = yes
129147
rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
130148
--extern proc_macro
131-
rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean FORCE
149+
rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
132150
+$(call if_changed,rustdoc)
133151

134152
# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
@@ -185,6 +203,10 @@ rusttestlib-build_error: $(src)/build_error.rs FORCE
185203
rusttestlib-ffi: $(src)/ffi.rs FORCE
186204
+$(call if_changed,rustc_test_library)
187205

206+
rusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
207+
rusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
208+
+$(call if_changed,rustc_test_library)
209+
188210
rusttestlib-macros: private rustc_target_flags = --extern proc_macro
189211
rusttestlib-macros: private rustc_test_library_proc = yes
190212
rusttestlib-macros: $(src)/macros/lib.rs FORCE
@@ -431,6 +453,11 @@ quiet_cmd_rustc_procmacrolibrary = $(RUSTC_OR_CLIPPY_QUIET) PL $@
431453
mv $(objtree)/$(obj)/$(patsubst lib%.rlib,%,$(notdir $@)).d $(depfile); \
432454
sed -i '/^\#/d' $(depfile)
433455

456+
$(obj)/libproc_macro2.rlib: private skip_clippy = 1
457+
$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
458+
$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
459+
+$(call if_changed_dep,rustc_procmacrolibrary)
460+
434461
quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
435462
cmd_rustc_procmacro = \
436463
$(RUSTC_OR_CLIPPY) $(rust_common_flags) $(rustc_target_flags) \
@@ -442,7 +469,7 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
442469
@$(objtree)/include/generated/rustc_cfg $<
443470

444471
# Procedural macros can only be used with the `rustc` that compiled it.
445-
$(obj)/$(libmacros_name): $(src)/macros/lib.rs FORCE
472+
$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib FORCE
446473
+$(call if_changed_dep,rustc_procmacro)
447474

448475
$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel
@@ -465,6 +492,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
465492
rust-analyzer:
466493
$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
467494
--cfgs='core=$(core-cfgs)' $(core-edition) \
495+
--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
468496
$(realpath $(srctree)) $(realpath $(objtree)) \
469497
$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
470498
> 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)