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