Merge tag 'wq-for-6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[linux-block.git] / rust / Makefile
CommitLineData
2f7ab126
MO
1# SPDX-License-Identifier: GPL-2.0
2
2f7ab126
MO
3obj-$(CONFIG_RUST) += core.o compiler_builtins.o
4always-$(CONFIG_RUST) += exports_core_generated.h
5
6# Missing prototypes are expected in the helpers since these are exported
7# for Rust only, thus there is no header nor prototypes.
8obj-$(CONFIG_RUST) += helpers.o
9CFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations
10
11always-$(CONFIG_RUST) += libmacros.so
12no-clean-files += libmacros.so
13
14always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
15obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
16always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
17 exports_kernel_generated.h
18
ecaa6ddf
GG
19ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
20obj-$(CONFIG_RUST) += build_error.o
21else
22always-$(CONFIG_RUST) += build_error.o
23endif
24
2f7ab126
MO
25obj-$(CONFIG_RUST) += exports.o
26
27# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
28ifdef CONFIG_RUST
29
30# `$(rust_flags)` is passed in case the user added `--sysroot`.
31rustc_sysroot := $(shell $(RUSTC) $(rust_flags) --print sysroot)
32rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
33RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
34
35ifeq ($(quiet),silent_)
36cargo_quiet=-q
37rust_test_quiet=-q
38rustdoc_test_quiet=--test-args -q
39else ifeq ($(quiet),quiet_)
40rust_test_quiet=-q
41rustdoc_test_quiet=--test-args -q
42else
43cargo_quiet=--verbose
44endif
45
46core-cfgs = \
47 --cfg no_fp_fmt_parse
48
49alloc-cfgs = \
8909a80e 50 --cfg no_borrow \
2f7ab126
MO
51 --cfg no_fmt \
52 --cfg no_global_oom_handling \
53 --cfg no_macros \
54 --cfg no_rc \
55 --cfg no_str \
56 --cfg no_string \
57 --cfg no_sync \
58 --cfg no_thin
59
60quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
61 cmd_rustdoc = \
62 OBJTREE=$(abspath $(objtree)) \
63 $(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \
64 $(rustc_target_flags) -L$(objtree)/$(obj) \
65 --output $(objtree)/$(obj)/doc \
66 --crate-name $(subst rustdoc-,,$@) \
67 @$(objtree)/include/generated/rustc_cfg $<
68
69# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
70# can be used to specify a custom logo. However:
71# - The given value is used as-is, thus it cannot be relative or a local file
72# (unlike the non-custom case) since the generated docs have subfolders.
73# - It requires adding it to every crate.
74# - It requires changing `core` which comes from the sysroot.
75#
76# Using `-Zcrate-attr` would solve the last two points, but not the first.
77# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
78# command-like flags to solve the issue. Meanwhile, we use the non-custom case
79# and then retouch the generated files.
80rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
81 rustdoc-alloc rustdoc-kernel
82 $(Q)cp $(srctree)/Documentation/images/logo.svg $(objtree)/$(obj)/doc
83 $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(objtree)/$(obj)/doc
84 $(Q)find $(objtree)/$(obj)/doc -name '*.html' -type f -print0 | xargs -0 sed -Ei \
85 -e 's:rust-logo\.svg:logo.svg:g' \
86 -e 's:rust-logo\.png:logo.svg:g' \
87 -e 's:favicon\.svg:logo.svg:g' \
88 -e 's:<link rel="alternate icon" type="image/png" href="[./]*favicon-(16x16|32x32)\.png">::g'
89 $(Q)echo '.logo-container > img { object-fit: contain; }' \
90 >> $(objtree)/$(obj)/doc/rustdoc.css
91
92rustdoc-macros: private rustdoc_host = yes
93rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
94 --extern proc_macro
95rustdoc-macros: $(src)/macros/lib.rs FORCE
96 $(call if_changed,rustdoc)
97
98rustdoc-core: private rustc_target_flags = $(core-cfgs)
99rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
100 $(call if_changed,rustdoc)
101
102rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
103 $(call if_changed,rustdoc)
104
105# We need to allow `rustdoc::broken_intra_doc_links` because some
106# `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
107# functions. Ideally `rustdoc` would have a way to distinguish broken links
108# due to things that are "configured out" vs. entirely non-existing ones.
109rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
110 -Arustdoc::broken_intra_doc_links
111rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
112 $(call if_changed,rustdoc)
113
114rustdoc-kernel: private rustc_target_flags = --extern alloc \
ecaa6ddf 115 --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
2f7ab126
MO
116 --extern bindings
117rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
118 rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
119 $(obj)/bindings.o FORCE
120 $(call if_changed,rustdoc)
121
122quiet_cmd_rustc_test_library = RUSTC TL $<
123 cmd_rustc_test_library = \
124 OBJTREE=$(abspath $(objtree)) \
125 $(RUSTC) $(rust_common_flags) \
126 @$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
127 --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
128 --out-dir $(objtree)/$(obj)/test --cfg testlib \
129 --sysroot $(objtree)/$(obj)/test/sysroot \
130 -L$(objtree)/$(obj)/test \
131 --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
132
ecaa6ddf
GG
133rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
134 $(call if_changed,rustc_test_library)
135
2f7ab126
MO
136rusttestlib-macros: private rustc_target_flags = --extern proc_macro
137rusttestlib-macros: private rustc_test_library_proc = yes
138rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
139 $(call if_changed,rustc_test_library)
140
141rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
142 $(call if_changed,rustc_test_library)
143
144quiet_cmd_rustdoc_test = RUSTDOC T $<
145 cmd_rustdoc_test = \
146 OBJTREE=$(abspath $(objtree)) \
147 $(RUSTDOC) --test $(rust_common_flags) \
148 @$(objtree)/include/generated/rustc_cfg \
149 $(rustc_target_flags) $(rustdoc_test_target_flags) \
150 --sysroot $(objtree)/$(obj)/test/sysroot $(rustdoc_test_quiet) \
151 -L$(objtree)/$(obj)/test --output $(objtree)/$(obj)/doc \
152 --crate-name $(subst rusttest-,,$@) $<
153
154# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
155# so for the moment we skip `-Cpanic=abort`.
156quiet_cmd_rustc_test = RUSTC T $<
157 cmd_rustc_test = \
158 OBJTREE=$(abspath $(objtree)) \
159 $(RUSTC) --test $(rust_common_flags) \
160 @$(objtree)/include/generated/rustc_cfg \
161 $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
162 --sysroot $(objtree)/$(obj)/test/sysroot \
163 -L$(objtree)/$(obj)/test \
164 --crate-name $(subst rusttest-,,$@) $<; \
165 $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
166 $(rustc_test_run_flags)
167
168rusttest: rusttest-macros rusttest-kernel
169
170# This prepares a custom sysroot with our custom `alloc` instead of
171# the standard one.
172#
173# This requires several hacks:
174# - Unlike `core` and `alloc`, `std` depends on more than a dozen crates,
175# including third-party crates that need to be downloaded, plus custom
176# `build.rs` steps. Thus hardcoding things here is not maintainable.
177# - `cargo` knows how to build the standard library, but it is an unstable
178# feature so far (`-Zbuild-std`).
179# - `cargo` only considers the use case of building the standard library
180# to use it in a given package. Thus we need to create a dummy package
181# and pick the generated libraries from there.
182# - Since we only keep a subset of upstream `alloc` in-tree, we need
183# to recreate it on the fly by putting our sources on top.
184# - The usual ways of modifying the dependency graph in `cargo` do not seem
185# to apply for the `-Zbuild-std` steps, thus we have to mislead it
186# by modifying the sources in the sysroot.
187# - To avoid messing with the user's Rust installation, we create a clone
188# of the sysroot. However, `cargo` ignores `RUSTFLAGS` in the `-Zbuild-std`
189# steps, thus we use a wrapper binary passed via `RUSTC` to pass the flag.
190#
191# In the future, we hope to avoid the whole ordeal by either:
192# - Making the `test` crate not depend on `std` (either improving upstream
193# or having our own custom crate).
194# - Making the tests run in kernel space (requires the previous point).
195# - Making `std` and friends be more like a "normal" crate, so that
196# `-Zbuild-std` and related hacks are not needed.
197quiet_cmd_rustsysroot = RUSTSYSROOT
198 cmd_rustsysroot = \
199 rm -rf $(objtree)/$(obj)/test; \
200 mkdir -p $(objtree)/$(obj)/test; \
201 cp -a $(rustc_sysroot) $(objtree)/$(obj)/test/sysroot; \
202 cp -r $(srctree)/$(src)/alloc/* \
203 $(objtree)/$(obj)/test/sysroot/lib/rustlib/src/rust/library/alloc/src; \
204 echo '\#!/bin/sh' > $(objtree)/$(obj)/test/rustc_sysroot; \
205 echo "$(RUSTC) --sysroot=$(abspath $(objtree)/$(obj)/test/sysroot) \"\$$@\"" \
206 >> $(objtree)/$(obj)/test/rustc_sysroot; \
207 chmod u+x $(objtree)/$(obj)/test/rustc_sysroot; \
208 $(CARGO) -q new $(objtree)/$(obj)/test/dummy; \
209 RUSTC=$(objtree)/$(obj)/test/rustc_sysroot $(CARGO) $(cargo_quiet) \
210 test -Zbuild-std --target $(rustc_host_target) \
211 --manifest-path $(objtree)/$(obj)/test/dummy/Cargo.toml; \
212 rm $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib/*; \
213 cp $(objtree)/$(obj)/test/dummy/target/$(rustc_host_target)/debug/deps/* \
214 $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib
215
216rusttest-prepare: FORCE
217 $(call if_changed,rustsysroot)
218
219rusttest-macros: private rustc_target_flags = --extern proc_macro
220rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
221rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
222 $(call if_changed,rustc_test)
223 $(call if_changed,rustdoc_test)
224
225rusttest-kernel: private rustc_target_flags = --extern alloc \
ecaa6ddf 226 --extern build_error --extern macros --extern bindings
2f7ab126 227rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
ecaa6ddf 228 rusttestlib-build_error rusttestlib-macros rusttestlib-bindings FORCE
2f7ab126
MO
229 $(call if_changed,rustc_test)
230 $(call if_changed,rustc_test_library)
231
2f7ab126
MO
232ifdef CONFIG_CC_IS_CLANG
233bindgen_c_flags = $(c_flags)
234else
235# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
236# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
237#
238# For the moment, here we are tweaking the flags on the fly. This is a hack,
239# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
240# if we end up using one of those structs).
241bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
242 -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
243 -mindirect-branch=thunk-extern -mindirect-branch-register \
244 -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
245 -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
246 -mno-pointers-to-nested-functions -mno-string \
247 -mno-strict-align -mstrict-align \
248 -fconserve-stack -falign-jumps=% -falign-loops=% \
249 -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
250 -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
251 -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
252 -fzero-call-used-regs=% -fno-stack-clash-protection \
253 -fno-inline-functions-called-once \
254 --param=% --param asan-%
255
256# Derived from `scripts/Makefile.clang`.
257BINDGEN_TARGET_x86 := x86_64-linux-gnu
258BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
259
260# All warnings are inhibited since GCC builds are very experimental,
261# many GCC warnings are not supported by Clang, they may only appear in
262# some configurations, with new GCC versions, etc.
263bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
264
d966c3ca
AR
265# Auto variable zero-initialization requires an additional special option with
266# clang that is going to be removed sometime in the future (likely in
267# clang-18), so make sure to pass this option only if clang supports it
268# (libclang major version < 16).
269#
270# https://github.com/llvm/llvm-project/issues/44842
271# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
272ifdef CONFIG_INIT_STACK_ALL_ZERO
273libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
274ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
275bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
276endif
277endif
278
2f7ab126
MO
279bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
280 $(bindgen_extra_c_flags)
281endif
282
283ifdef CONFIG_LTO
284bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
285else
286bindgen_c_flags_lto = $(bindgen_c_flags)
287endif
288
289bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
290
291quiet_cmd_bindgen = BINDGEN $@
292 cmd_bindgen = \
293 $(BINDGEN) $< $(bindgen_target_flags) \
294 --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
295 --no-debug '.*' \
296 --size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \
297 $(bindgen_target_cflags) $(bindgen_target_extra)
298
299$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
1c5f054f 300 $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters)
2f7ab126
MO
301$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
302 $(src)/bindgen_parameters FORCE
303 $(call if_changed_dep,bindgen)
304
305# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
306# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
307# given it is `libclang`; but for consistency, future Clang changes and/or
308# a potential future GCC backend for `bindgen`, we disable it too.
309$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
310 --blacklist-type '.*' --whitelist-var '' \
311 --whitelist-function 'rust_helper_.*'
312$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
313 -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
314$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
315 sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@
316$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
317 $(call if_changed_dep,bindgen)
318
319quiet_cmd_exports = EXPORTS $@
320 cmd_exports = \
321 $(NM) -p --defined-only $< \
322 | grep -E ' (T|R|D) ' | cut -d ' ' -f 3 \
323 | xargs -Isymbol \
324 echo 'EXPORT_SYMBOL_RUST_GPL(symbol);' > $@
325
326$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
327 $(call if_changed,exports)
328
329$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
330 $(call if_changed,exports)
331
332$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
333 $(call if_changed,exports)
334
335$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
336 $(call if_changed,exports)
337
338quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
339 cmd_rustc_procmacro = \
340 $(RUSTC_OR_CLIPPY) $(rust_common_flags) \
295d8398
MY
341 --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
342 --crate-type proc-macro \
2185242f 343 --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
2f7ab126
MO
344
345# Procedural macros can only be used with the `rustc` that compiled it.
346# Therefore, to get `libmacros.so` automatically recompiled when the compiler
347# version changes, we add `core.o` as a dependency (even if it is not needed).
348$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
349 $(call if_changed_dep,rustc_procmacro)
350
351quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
352 cmd_rustc_library = \
353 OBJTREE=$(abspath $(objtree)) \
354 $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
355 $(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
295d8398
MY
356 --emit=dep-info=$(depfile) --emit=obj=$@ \
357 --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
358 --crate-type rlib -L$(objtree)/$(obj) \
2185242f 359 --crate-name $(patsubst %.o,%,$(notdir $@)) $< \
2f7ab126
MO
360 $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
361
362rust-analyzer:
363 $(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
364 $(RUST_LIB_SRC) > $(objtree)/rust-project.json
365
cb7d9def
GG
366redirect-intrinsics = \
367 __eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \
368 __unorddf2 \
369 __muloti4 __multi3 \
370 __udivmodti4 __udivti3 __umodti3
371
372ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
373 # These intrinsics are defined for ARM64 and RISCV64
374 redirect-intrinsics += \
375 __ashrti3 \
376 __ashlti3 __lshrti3
377endif
378
2f7ab126
MO
379$(obj)/core.o: private skip_clippy = 1
380$(obj)/core.o: private skip_flags = -Dunreachable_pub
cb7d9def 381$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
2f7ab126 382$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
c83b16ce 383$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs scripts/target.json FORCE
2f7ab126
MO
384 $(call if_changed_dep,rustc_library)
385
386$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
387$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
388 $(call if_changed_dep,rustc_library)
389
390$(obj)/alloc.o: private skip_clippy = 1
391$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
392$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
393$(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
394 $(call if_changed_dep,rustc_library)
395
ecaa6ddf
GG
396$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
397 $(call if_changed_dep,rustc_library)
398
2f7ab126
MO
399$(obj)/bindings.o: $(src)/bindings/lib.rs \
400 $(obj)/compiler_builtins.o \
401 $(obj)/bindings/bindings_generated.rs \
402 $(obj)/bindings/bindings_helpers_generated.rs FORCE
403 $(call if_changed_dep,rustc_library)
404
405$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
ecaa6ddf
GG
406 --extern build_error --extern macros --extern bindings
407$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
2f7ab126
MO
408 $(obj)/libmacros.so $(obj)/bindings.o FORCE
409 $(call if_changed_dep,rustc_library)
410
411endif # CONFIG_RUST