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