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