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