xref: /openbmc/linux/rust/Makefile (revision 1bffcea4)
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
19ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
20obj-$(CONFIG_RUST) += build_error.o
21else
22always-$(CONFIG_RUST) += build_error.o
23endif
24
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 = \
50    --cfg no_borrow \
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 \
115    --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
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
133rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
134	$(call if_changed,rustc_test_library)
135
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 \
226    --extern build_error --extern macros --extern bindings
227rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
228    rusttestlib-build_error rusttestlib-macros rusttestlib-bindings FORCE
229	$(call if_changed,rustc_test)
230	$(call if_changed,rustc_test_library)
231
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
265bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
266	$(bindgen_extra_c_flags)
267endif
268
269ifdef CONFIG_LTO
270bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
271else
272bindgen_c_flags_lto = $(bindgen_c_flags)
273endif
274
275bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
276
277quiet_cmd_bindgen = BINDGEN $@
278      cmd_bindgen = \
279	$(BINDGEN) $< $(bindgen_target_flags) \
280		--use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
281		--no-debug '.*' \
282		--size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \
283		$(bindgen_target_cflags) $(bindgen_target_extra)
284
285$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
286    $(shell grep -v '^\#\|^$$' $(srctree)/$(src)/bindgen_parameters)
287$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
288    $(src)/bindgen_parameters FORCE
289	$(call if_changed_dep,bindgen)
290
291# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
292# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
293# given it is `libclang`; but for consistency, future Clang changes and/or
294# a potential future GCC backend for `bindgen`, we disable it too.
295$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
296    --blacklist-type '.*' --whitelist-var '' \
297    --whitelist-function 'rust_helper_.*'
298$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
299    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
300$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
301    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
302$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
303	$(call if_changed_dep,bindgen)
304
305quiet_cmd_exports = EXPORTS $@
306      cmd_exports = \
307	$(NM) -p --defined-only $< \
308		| grep -E ' (T|R|D) ' | cut -d ' ' -f 3 \
309		| xargs -Isymbol \
310		echo 'EXPORT_SYMBOL_RUST_GPL(symbol);' > $@
311
312$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
313	$(call if_changed,exports)
314
315$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
316	$(call if_changed,exports)
317
318$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
319	$(call if_changed,exports)
320
321$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
322	$(call if_changed,exports)
323
324quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
325      cmd_rustc_procmacro = \
326	$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
327		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
328		--crate-type proc-macro \
329		--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
330
331# Procedural macros can only be used with the `rustc` that compiled it.
332# Therefore, to get `libmacros.so` automatically recompiled when the compiler
333# version changes, we add `core.o` as a dependency (even if it is not needed).
334$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
335	$(call if_changed_dep,rustc_procmacro)
336
337quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
338      cmd_rustc_library = \
339	OBJTREE=$(abspath $(objtree)) \
340	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
341		$(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
342		--emit=dep-info=$(depfile) --emit=obj=$@ \
343		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
344		--crate-type rlib -L$(objtree)/$(obj) \
345		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
346	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
347
348rust-analyzer:
349	$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
350		$(RUST_LIB_SRC) > $(objtree)/rust-project.json
351
352redirect-intrinsics = \
353	__eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \
354	__unorddf2 \
355	__muloti4 __multi3 \
356	__udivmodti4 __udivti3 __umodti3
357
358ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
359	# These intrinsics are defined for ARM64 and RISCV64
360	redirect-intrinsics += \
361		__ashrti3 \
362		__ashlti3 __lshrti3
363endif
364
365$(obj)/core.o: private skip_clippy = 1
366$(obj)/core.o: private skip_flags = -Dunreachable_pub
367$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
368$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
369$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs scripts/target.json FORCE
370	$(call if_changed_dep,rustc_library)
371
372$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
373$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
374	$(call if_changed_dep,rustc_library)
375
376$(obj)/alloc.o: private skip_clippy = 1
377$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
378$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
379$(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
380	$(call if_changed_dep,rustc_library)
381
382$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
383	$(call if_changed_dep,rustc_library)
384
385$(obj)/bindings.o: $(src)/bindings/lib.rs \
386    $(obj)/compiler_builtins.o \
387    $(obj)/bindings/bindings_generated.rs \
388    $(obj)/bindings/bindings_helpers_generated.rs FORCE
389	$(call if_changed_dep,rustc_library)
390
391$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
392    --extern build_error --extern macros --extern bindings
393$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
394    $(obj)/libmacros.so $(obj)/bindings.o FORCE
395	$(call if_changed_dep,rustc_library)
396
397endif # CONFIG_RUST
398