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