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