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