1 # SPDX-License-Identifier: GPL-2.0
3 obj-$(CONFIG_RUST) += core.o compiler_builtins.o
4 always-$(CONFIG_RUST) += exports_core_generated.h
6 # Missing prototypes are expected in the helpers since these are exported
7 # for Rust only, thus there is no header nor prototypes.
8 obj-$(CONFIG_RUST) += helpers.o
9 CFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations
11 always-$(CONFIG_RUST) += libmacros.so
12 no-clean-files += libmacros.so
14 always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
15 obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
16 always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
17 exports_kernel_generated.h
19 ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
20 obj-$(CONFIG_RUST) += build_error.o
22 always-$(CONFIG_RUST) += build_error.o
25 obj-$(CONFIG_RUST) += exports.o
27 # Avoids running `$(RUSTC)` for the sysroot when it may not be available.
30 # `$(rust_flags)` is passed in case the user added `--sysroot`.
31 rustc_sysroot := $(shell $(RUSTC) $(rust_flags) --print sysroot)
32 rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
33 RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
35 ifeq ($(quiet),silent_)
38 rustdoc_test_quiet=--test-args -q
39 else ifeq ($(quiet),quiet_)
41 rustdoc_test_quiet=--test-args -q
52 --cfg no_global_oom_handling \
60 quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
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 $<
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.
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.
80 rustdoc: 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
92 rustdoc-macros: private rustdoc_host = yes
93 rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
95 rustdoc-macros: $(src)/macros/lib.rs FORCE
96 $(call if_changed,rustdoc)
98 rustdoc-core: private rustc_target_flags = $(core-cfgs)
99 rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
100 $(call if_changed,rustdoc)
102 rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
103 $(call if_changed,rustdoc)
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.
109 rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
110 -Arustdoc::broken_intra_doc_links
111 rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
112 $(call if_changed,rustdoc)
114 rustdoc-kernel: private rustc_target_flags = --extern alloc \
115 --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
117 rustdoc-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)
122 quiet_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-,,$@)) $<
133 rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
134 $(call if_changed,rustc_test_library)
136 rusttestlib-macros: private rustc_target_flags = --extern proc_macro
137 rusttestlib-macros: private rustc_test_library_proc = yes
138 rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
139 $(call if_changed,rustc_test_library)
141 rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
142 $(call if_changed,rustc_test_library)
144 quiet_cmd_rustdoc_test = RUSTDOC T $<
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-,,$@) $<
154 # We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
155 # so for the moment we skip `-Cpanic=abort`.
156 quiet_cmd_rustc_test = RUSTC T $<
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)
168 rusttest: rusttest-macros rusttest-kernel
170 # This prepares a custom sysroot with our custom `alloc` instead of
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.
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.
197 quiet_cmd_rustsysroot = 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
216 rusttest-prepare: FORCE
217 $(call if_changed,rustsysroot)
219 rusttest-macros: private rustc_target_flags = --extern proc_macro
220 rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
221 rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
222 $(call if_changed,rustc_test)
223 $(call if_changed,rustdoc_test)
225 rusttest-kernel: private rustc_target_flags = --extern alloc \
226 --extern build_error --extern macros --extern bindings
227 rusttest-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)
232 ifdef CONFIG_CC_IS_CLANG
233 bindgen_c_flags = $(c_flags)
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.
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).
241 bindgen_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-%
256 # Derived from `scripts/Makefile.clang`.
257 BINDGEN_TARGET_x86 := x86_64-linux-gnu
258 BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
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.
263 bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
265 bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
266 $(bindgen_extra_c_flags)
270 bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
272 bindgen_c_flags_lto = $(bindgen_c_flags)
275 bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
277 quiet_cmd_bindgen = BINDGEN $@
279 $(BINDGEN) $< $(bindgen_target_flags) \
280 --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
282 --size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \
283 $(bindgen_target_cflags) $(bindgen_target_extra)
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)
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)
305 quiet_cmd_exports = EXPORTS $@
307 $(NM) -p --defined-only $< \
308 | grep -E ' (T|R|D) ' | cut -d ' ' -f 3 \
310 echo 'EXPORT_SYMBOL_RUST_GPL(symbol);' > $@
312 $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
313 $(call if_changed,exports)
315 $(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
316 $(call if_changed,exports)
318 $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
319 $(call if_changed,exports)
321 $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
322 $(call if_changed,exports)
324 quiet_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 $@)) $<
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)
337 quiet_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) $@)
349 $(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
350 $(RUST_LIB_SRC) > $(objtree)/rust-project.json
352 redirect-intrinsics = \
353 __eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \
356 __udivmodti4 __udivti3 __umodti3
358 ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
359 # These intrinsics are defined for ARM64 and RISCV64
360 redirect-intrinsics += \
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)
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)
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)
382 $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
383 $(call if_changed_dep,rustc_library)
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)
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)