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