Makefile 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. # SPDX-License-Identifier: GPL-2.0
  2. # Where to place rustdoc generated documentation
  3. rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
  4. obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o
  5. 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/helpers.o
  9. CFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
  10. always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
  11. obj-$(CONFIG_RUST) += bindings.o pin_init.o kernel.o
  12. always-$(CONFIG_RUST) += exports_helpers_generated.h \
  13. exports_bindings_generated.h exports_kernel_generated.h
  14. always-$(CONFIG_RUST) += uapi/uapi_generated.rs
  15. obj-$(CONFIG_RUST) += uapi.o
  16. ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
  17. obj-$(CONFIG_RUST) += build_error.o
  18. else
  19. always-$(CONFIG_RUST) += build_error.o
  20. endif
  21. obj-$(CONFIG_RUST) += exports.o
  22. always-$(CONFIG_RUST) += libproc_macro2.rlib libquote.rlib libsyn.rlib
  23. always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
  24. always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
  25. obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
  26. obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
  27. always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs
  28. ifndef CONFIG_UML
  29. always-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs
  30. endif
  31. # Avoids running `$(RUSTC)` when it may not be available.
  32. ifdef CONFIG_RUST
  33. libmacros_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name macros --crate-type proc-macro - </dev/null)
  34. libmacros_extension := $(patsubst libmacros.%,%,$(libmacros_name))
  35. libpin_init_internal_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name pin_init_internal --crate-type proc-macro - </dev/null)
  36. libpin_init_internal_extension := $(patsubst libpin_init_internal.%,%,$(libpin_init_internal_name))
  37. always-$(CONFIG_RUST) += $(libmacros_name) $(libpin_init_internal_name)
  38. # `$(rust_flags)` is passed in case the user added `--sysroot`.
  39. rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
  40. rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
  41. RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
  42. ifneq ($(quiet),)
  43. rust_test_quiet=-q
  44. rustdoc_test_quiet=--test-args -q
  45. rustdoc_test_kernel_quiet=>/dev/null
  46. endif
  47. cfgs-to-flags = $(patsubst %,--cfg='%',$1)
  48. core-cfgs := \
  49. no_fp_fmt_parse
  50. core-edition := $(if $(call rustc-min-version,108700),2024,2021)
  51. core-skip_flags := \
  52. --edition=2021 \
  53. -Wunreachable_pub \
  54. -Wrustdoc::unescaped_backticks
  55. core-flags := \
  56. --edition=$(core-edition) \
  57. $(call cfgs-to-flags,$(core-cfgs))
  58. proc_macro2-cfgs := \
  59. feature="proc-macro" \
  60. wrap_proc_macro \
  61. $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
  62. # Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
  63. proc_macro2-flags := \
  64. --cap-lints=allow \
  65. -Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
  66. $(call cfgs-to-flags,$(proc_macro2-cfgs))
  67. quote-cfgs := \
  68. feature="proc-macro"
  69. quote-skip_flags := \
  70. --edition=2021
  71. quote-flags := \
  72. --edition=2018 \
  73. --cap-lints=allow \
  74. --extern proc_macro2 \
  75. $(call cfgs-to-flags,$(quote-cfgs))
  76. # `extra-traits`, `fold` and `visit` may be enabled if needed.
  77. syn-cfgs := \
  78. feature="clone-impls" \
  79. feature="derive" \
  80. feature="full" \
  81. feature="parsing" \
  82. feature="printing" \
  83. feature="proc-macro" \
  84. feature="visit-mut"
  85. syn-flags := \
  86. --cap-lints=allow \
  87. --extern proc_macro2 \
  88. --extern quote \
  89. $(call cfgs-to-flags,$(syn-cfgs))
  90. pin_init_internal-cfgs := \
  91. kernel
  92. pin_init_internal-flags := \
  93. --extern proc_macro2 \
  94. --extern quote \
  95. --extern syn \
  96. $(call cfgs-to-flags,$(pin_init_internal-cfgs))
  97. pin_init-cfgs := \
  98. kernel
  99. pin_init-flags := \
  100. --extern pin_init_internal \
  101. --extern macros \
  102. $(call cfgs-to-flags,$(pin_init-cfgs))
  103. # `rustdoc` did not save the target modifiers, thus workaround for
  104. # the time being (https://github.com/rust-lang/rust/issues/144521).
  105. rustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
  106. # Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465).
  107. doctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer)
  108. # `rustc` recognizes `--remap-path-prefix` since 1.26.0, but `rustdoc` only
  109. # since Rust 1.81.0. Moreover, `rustdoc` ICEs on out-of-tree builds since Rust
  110. # 1.82.0 (https://github.com/rust-lang/rust/issues/138520). Thus workaround both
  111. # issues skipping the flag. The former also applies to `RUSTDOC TK`.
  112. quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
  113. cmd_rustdoc = \
  114. OBJTREE=$(abspath $(objtree)) \
  115. $(RUSTDOC) $(filter-out $(skip_flags) --remap-path-prefix=% --remap-path-scope=%, \
  116. $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
  117. $(rustc_target_flags) -L$(objtree)/$(obj) \
  118. -Zunstable-options --generate-link-to-definition \
  119. --output $(rustdoc_output) \
  120. --crate-name $(subst rustdoc-,,$@) \
  121. $(rustdoc_modifiers_workaround) \
  122. $(if $(rustdoc_host),,--sysroot=/dev/null) \
  123. @$(objtree)/include/generated/rustc_cfg $<
  124. # The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
  125. # can be used to specify a custom logo. However:
  126. # - The given value is used as-is, thus it cannot be relative or a local file
  127. # (unlike the non-custom case) since the generated docs have subfolders.
  128. # - It requires adding it to every crate.
  129. # - It requires changing `core` which comes from the sysroot.
  130. #
  131. # Using `-Zcrate-attr` would solve the last two points, but not the first.
  132. # The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
  133. # command-like flags to solve the issue. Meanwhile, we use the non-custom case
  134. # and then retouch the generated files.
  135. rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
  136. rustdoc-kernel rustdoc-pin_init
  137. $(Q)grep -Ehro '<a href="srctree/([^"]+)"' $(rustdoc_output) | \
  138. cut -d'"' -f2 | cut -d/ -f2- | while read f; do \
  139. if [ ! -e "$(srctree)/$$f" ]; then \
  140. echo "warning: srctree/ link to $$f does not exist"; \
  141. fi \
  142. done
  143. $(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
  144. $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
  145. $(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
  146. -e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
  147. -e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
  148. -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
  149. -e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
  150. $(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
  151. echo ".logo-container > img { object-fit: contain; }" >> $$f; done
  152. rustdoc-proc_macro2: private rustdoc_host = yes
  153. rustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
  154. rustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
  155. +$(call if_changed,rustdoc)
  156. rustdoc-quote: private rustdoc_host = yes
  157. rustdoc-quote: private rustc_target_flags = $(quote-flags)
  158. rustdoc-quote: private skip_flags = $(quote-skip_flags)
  159. rustdoc-quote: $(src)/quote/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
  160. +$(call if_changed,rustdoc)
  161. rustdoc-syn: private rustdoc_host = yes
  162. rustdoc-syn: private rustc_target_flags = $(syn-flags)
  163. rustdoc-syn: $(src)/syn/lib.rs rustdoc-clean rustdoc-quote FORCE
  164. +$(call if_changed,rustdoc)
  165. rustdoc-macros: private rustdoc_host = yes
  166. rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
  167. --extern proc_macro --extern proc_macro2 --extern quote --extern syn
  168. rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 \
  169. rustdoc-quote rustdoc-syn FORCE
  170. +$(call if_changed,rustdoc)
  171. # Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
  172. # not be needed -- see https://github.com/rust-lang/rust/pull/128307.
  173. rustdoc-core: private skip_flags = $(core-skip_flags)
  174. rustdoc-core: private rustc_target_flags = $(core-flags)
  175. rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE
  176. +$(call if_changed,rustdoc)
  177. # Even if `rustdoc` targets are not kernel objects, they should still be
  178. # treated as such so that we pass the same flags. Otherwise, for instance,
  179. # `rustdoc` will complain about missing sanitizer flags causing an ABI mismatch.
  180. rustdoc-compiler_builtins: private is-kernel-object := y
  181. rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
  182. +$(call if_changed,rustdoc)
  183. rustdoc-ffi: private is-kernel-object := y
  184. rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
  185. +$(call if_changed,rustdoc)
  186. rustdoc-pin_init_internal: private rustdoc_host = yes
  187. rustdoc-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
  188. --extern proc_macro --crate-type proc-macro
  189. rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
  190. rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
  191. +$(call if_changed,rustdoc)
  192. rustdoc-pin_init: private rustdoc_host = yes
  193. rustdoc-pin_init: private rustc_target_flags = $(pin_init-flags) \
  194. --extern alloc --cfg feature=\"alloc\"
  195. rustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
  196. rustdoc-macros FORCE
  197. +$(call if_changed,rustdoc)
  198. rustdoc-kernel: private is-kernel-object := y
  199. rustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
  200. --extern build_error --extern macros \
  201. --extern bindings --extern uapi
  202. rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
  203. rustdoc-pin_init rustdoc-compiler_builtins $(obj)/$(libmacros_name) \
  204. $(obj)/bindings.o FORCE
  205. +$(call if_changed,rustdoc)
  206. rustdoc-clean: FORCE
  207. $(Q)rm -rf $(rustdoc_output)
  208. quiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $<
  209. cmd_rustc_test_library = \
  210. OBJTREE=$(abspath $(objtree)) \
  211. $(RUSTC_OR_CLIPPY) $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
  212. @$(objtree)/include/generated/rustc_cfg \
  213. --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
  214. --out-dir $(objtree)/$(obj)/test --cfg testlib \
  215. -L$(objtree)/$(obj)/test \
  216. --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
  217. rusttestlib-build_error: $(src)/build_error.rs FORCE
  218. +$(call if_changed,rustc_test_library)
  219. rusttestlib-ffi: $(src)/ffi.rs FORCE
  220. +$(call if_changed,rustc_test_library)
  221. rusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
  222. rusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
  223. +$(call if_changed,rustc_test_library)
  224. rusttestlib-quote: private skip_flags = $(quote-skip_flags)
  225. rusttestlib-quote: private rustc_target_flags = $(quote-flags)
  226. rusttestlib-quote: $(src)/quote/lib.rs rusttestlib-proc_macro2 FORCE
  227. +$(call if_changed,rustc_test_library)
  228. rusttestlib-syn: private rustc_target_flags = $(syn-flags)
  229. rusttestlib-syn: $(src)/syn/lib.rs rusttestlib-quote FORCE
  230. +$(call if_changed,rustc_test_library)
  231. rusttestlib-macros: private rustc_target_flags = --extern proc_macro \
  232. --extern proc_macro2 --extern quote --extern syn
  233. rusttestlib-macros: private rustc_test_library_proc = yes
  234. rusttestlib-macros: $(src)/macros/lib.rs \
  235. rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
  236. +$(call if_changed,rustc_test_library)
  237. rusttestlib-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
  238. --extern proc_macro
  239. rusttestlib-pin_init_internal: private rustc_test_library_proc = yes
  240. rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
  241. rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
  242. +$(call if_changed,rustc_test_library)
  243. rusttestlib-pin_init: private rustc_target_flags = $(pin_init-flags)
  244. rusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
  245. rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE
  246. +$(call if_changed,rustc_test_library)
  247. rusttestlib-kernel: private rustc_target_flags = --extern ffi \
  248. --extern build_error --extern macros --extern pin_init \
  249. --extern bindings --extern uapi
  250. rusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
  251. rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
  252. $(obj)/bindings.o FORCE
  253. +$(call if_changed,rustc_test_library)
  254. rusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init
  255. rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
  256. +$(call if_changed,rustc_test_library)
  257. rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
  258. rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
  259. +$(call if_changed,rustc_test_library)
  260. quiet_cmd_rustdoc_test = RUSTDOC T $<
  261. cmd_rustdoc_test = \
  262. RUST_MODFILE=test.rs \
  263. OBJTREE=$(abspath $(objtree)) \
  264. $(RUSTDOC) --test $(rust_common_flags) \
  265. -Zcrate-attr='feature(used_with_arg)' \
  266. @$(objtree)/include/generated/rustc_cfg \
  267. $(rustc_target_flags) $(rustdoc_test_target_flags) \
  268. $(rustdoc_test_quiet) \
  269. -L$(objtree)/$(obj)/test --output $(rustdoc_output) \
  270. --crate-name $(subst rusttest-,,$@) $<
  271. quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
  272. cmd_rustdoc_test_kernel = \
  273. rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
  274. mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
  275. OBJTREE=$(abspath $(objtree)) \
  276. $(RUSTDOC) --test $(filter-out --remap-path-prefix=% --remap-path-scope=%,$(rust_flags)) \
  277. -L$(objtree)/$(obj) --extern ffi --extern pin_init \
  278. --extern kernel --extern build_error --extern macros \
  279. --extern bindings --extern uapi \
  280. --no-run --crate-name kernel -Zunstable-options \
  281. --sysroot=/dev/null \
  282. $(doctests_modifiers_workaround) \
  283. --test-builder $(objtree)/scripts/rustdoc_test_builder \
  284. $< $(rustdoc_test_kernel_quiet); \
  285. $(objtree)/scripts/rustdoc_test_gen
  286. %/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
  287. $(src)/kernel/lib.rs $(obj)/kernel.o \
  288. $(objtree)/scripts/rustdoc_test_builder \
  289. $(objtree)/scripts/rustdoc_test_gen FORCE
  290. +$(call if_changed,rustdoc_test_kernel)
  291. # We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
  292. # so for the moment we skip `-Cpanic=abort`.
  293. quiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T $<
  294. cmd_rustc_test = \
  295. OBJTREE=$(abspath $(objtree)) \
  296. $(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \
  297. @$(objtree)/include/generated/rustc_cfg \
  298. $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
  299. -L$(objtree)/$(obj)/test \
  300. --crate-name $(subst rusttest-,,$@) $<; \
  301. $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
  302. $(rustc_test_run_flags)
  303. rusttest: rusttest-macros
  304. rusttest-macros: private rustc_target_flags = --extern proc_macro \
  305. --extern macros --extern kernel --extern pin_init \
  306. --extern proc_macro2 --extern quote --extern syn
  307. rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
  308. rusttest-macros: $(src)/macros/lib.rs \
  309. rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE
  310. +$(call if_changed,rustc_test)
  311. +$(call if_changed,rustdoc_test)
  312. ifdef CONFIG_CC_IS_CLANG
  313. bindgen_c_flags = $(c_flags)
  314. else
  315. # bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
  316. # plugin backend and/or the Clang driver would be perfectly compatible with GCC.
  317. #
  318. # For the moment, here we are tweaking the flags on the fly. This is a hack,
  319. # and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
  320. # if we end up using one of those structs).
  321. bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
  322. -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
  323. -mindirect-branch=thunk-extern -mindirect-branch-register \
  324. -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
  325. -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
  326. -mno-pointers-to-nested-functions -mno-string \
  327. -mno-strict-align -mstrict-align -mdirect-extern-access \
  328. -mexplicit-relocs -mno-check-zero-division \
  329. -fconserve-stack -falign-jumps=% -falign-loops=% \
  330. -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
  331. -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
  332. -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
  333. -fzero-call-used-regs=% -fno-stack-clash-protection \
  334. -fno-inline-functions-called-once -fsanitize=bounds-strict \
  335. -fstrict-flex-arrays=% -fmin-function-alignment=% \
  336. -fzero-init-padding-bits=% -mno-fdpic \
  337. -fdiagnostics-show-context -fdiagnostics-show-context=% \
  338. --param=% --param asan-% -fno-isolate-erroneous-paths-dereference
  339. # Derived from `scripts/Makefile.clang`.
  340. BINDGEN_TARGET_x86 := x86_64-linux-gnu
  341. BINDGEN_TARGET_arm64 := aarch64-linux-gnu
  342. BINDGEN_TARGET_arm := arm-linux-gnueabi
  343. BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf
  344. BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH))
  345. BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
  346. # All warnings are inhibited since GCC builds are very experimental,
  347. # many GCC warnings are not supported by Clang, they may only appear in
  348. # some configurations, with new GCC versions, etc.
  349. bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
  350. # Auto variable zero-initialization requires an additional special option with
  351. # clang that is going to be removed sometime in the future (likely in
  352. # clang-18), so make sure to pass this option only if clang supports it
  353. # (libclang major version < 16).
  354. #
  355. # https://github.com/llvm/llvm-project/issues/44842
  356. # https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
  357. ifdef CONFIG_INIT_STACK_ALL_ZERO
  358. libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
  359. ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
  360. bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
  361. endif
  362. endif
  363. bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
  364. $(bindgen_extra_c_flags)
  365. endif
  366. ifdef CONFIG_LTO
  367. bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
  368. else
  369. bindgen_c_flags_lto = $(bindgen_c_flags)
  370. endif
  371. # `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
  372. # prototypes for functions like `memcpy` -- if this flag is not passed,
  373. # `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
  374. # architecture instead of generating `usize`.
  375. bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
  376. # Each `bindgen` release may upgrade the list of Rust target versions. By
  377. # default, the highest stable release in their list is used. Thus we need to set
  378. # a `--rust-target` to avoid future `bindgen` releases emitting code that
  379. # `rustc` may not understand. On top of that, `bindgen` does not support passing
  380. # an unknown Rust target version.
  381. #
  382. # Therefore, the Rust target for `bindgen` can be only as high as the minimum
  383. # Rust version the kernel supports and only as high as the greatest stable Rust
  384. # target supported by the minimum `bindgen` version the kernel supports (that
  385. # is, if we do not test the actual `rustc`/`bindgen` versions running).
  386. #
  387. # Starting with `bindgen` 0.71.0, we will be able to set any future Rust version
  388. # instead, i.e. we will be able to set here our minimum supported Rust version.
  389. quiet_cmd_bindgen = BINDGEN $@
  390. cmd_bindgen = \
  391. $(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.68 \
  392. --use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
  393. --no-debug '.*' --enable-function-attribute-detection \
  394. -o $@ -- $(bindgen_c_flags_final) -DMODULE \
  395. $(bindgen_target_cflags) $(bindgen_target_extra)
  396. $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
  397. $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
  398. $(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
  399. sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
  400. $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
  401. $(src)/bindgen_parameters FORCE
  402. $(call if_changed_dep,bindgen)
  403. $(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
  404. $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
  405. $(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
  406. $(src)/bindgen_parameters FORCE
  407. $(call if_changed_dep,bindgen)
  408. # See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
  409. # with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
  410. # given it is `libclang`; but for consistency, future Clang changes and/or
  411. # a potential future GCC backend for `bindgen`, we disable it too.
  412. $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
  413. --blocklist-type '.*' --allowlist-var '' \
  414. --allowlist-function 'rust_helper_.*'
  415. $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
  416. -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
  417. $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
  418. sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@
  419. $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
  420. $(call if_changed_dep,bindgen)
  421. rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
  422. quiet_cmd_exports = EXPORTS $@
  423. cmd_exports = \
  424. $(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
  425. $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
  426. $(call if_changed,exports)
  427. # Even though Rust kernel modules should never use the bindings directly,
  428. # symbols from the `bindings` crate and the C helpers need to be exported
  429. # because Rust generics and inlined functions may not get their code generated
  430. # in the crate where they are defined. Other helpers, called from non-inline
  431. # functions, may not be exported, in principle. However, in general, the Rust
  432. # compiler does not guarantee codegen will be performed for a non-inline
  433. # function either. Therefore, we export all symbols from helpers and bindings.
  434. # In the future, this may be revisited to reduce the number of exports after
  435. # the compiler is informed about the places codegen is required.
  436. $(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
  437. $(call if_changed,exports)
  438. $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
  439. $(call if_changed,exports)
  440. $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
  441. $(call if_changed,exports)
  442. quiet_cmd_rustc_procmacrolibrary = $(RUSTC_OR_CLIPPY_QUIET) PL $@
  443. cmd_rustc_procmacrolibrary = \
  444. $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
  445. $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
  446. --emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib -O \
  447. --out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
  448. --crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<
  449. $(obj)/libproc_macro2.rlib: private skip_clippy = 1
  450. $(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
  451. $(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
  452. +$(call if_changed_dep,rustc_procmacrolibrary)
  453. $(obj)/libquote.rlib: private skip_clippy = 1
  454. $(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags)
  455. $(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags)
  456. $(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE
  457. +$(call if_changed_dep,rustc_procmacrolibrary)
  458. $(obj)/libsyn.rlib: private skip_clippy = 1
  459. $(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags)
  460. $(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE
  461. +$(call if_changed_dep,rustc_procmacrolibrary)
  462. quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
  463. cmd_rustc_procmacro = \
  464. $(RUSTC_OR_CLIPPY) $(rust_common_flags) $(rustc_target_flags) \
  465. -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
  466. -Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
  467. --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
  468. --crate-type proc-macro -L$(objtree)/$(obj) \
  469. --crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) \
  470. @$(objtree)/include/generated/rustc_cfg $<
  471. # Procedural macros can only be used with the `rustc` that compiled it.
  472. $(obj)/$(libmacros_name): private rustc_target_flags = \
  473. --extern proc_macro2 --extern quote --extern syn
  474. $(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
  475. $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
  476. +$(call if_changed_dep,rustc_procmacro)
  477. $(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags)
  478. $(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
  479. $(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
  480. +$(call if_changed_dep,rustc_procmacro)
  481. # `rustc` requires `-Zunstable-options` to use custom target specifications
  482. # since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
  483. quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
  484. cmd_rustc_library = \
  485. OBJTREE=$(abspath $(objtree)) \
  486. $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
  487. $(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
  488. --emit=dep-info=$(depfile) --emit=obj=$@ \
  489. --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
  490. --crate-type rlib -L$(objtree)/$(obj) \
  491. --crate-name $(patsubst %.o,%,$(notdir $@)) $< \
  492. --sysroot=/dev/null \
  493. -Zunstable-options \
  494. $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
  495. $(cmd_objtool)
  496. rust-analyzer:
  497. $(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
  498. --cfgs='core=$(core-cfgs)' $(core-edition) \
  499. --cfgs='proc_macro2=$(proc_macro2-cfgs)' \
  500. --cfgs='quote=$(quote-cfgs)' \
  501. --cfgs='syn=$(syn-cfgs)' \
  502. $(realpath $(srctree)) $(realpath $(objtree)) \
  503. $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
  504. > rust-project.json
  505. redirect-intrinsics = \
  506. __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
  507. __adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
  508. __muloti4 __multi3 \
  509. __udivmodti4 __udivti3 __umodti3
  510. ifdef CONFIG_ARM
  511. # Add eabi initrinsics for ARM 32-bit
  512. redirect-intrinsics += \
  513. __aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
  514. __aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
  515. __aeabi_uldivmod
  516. endif
  517. ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
  518. # These intrinsics are defined for ARM64 and RISCV64
  519. redirect-intrinsics += \
  520. __ashrti3 \
  521. __ashlti3 __lshrti3
  522. endif
  523. ifdef CONFIG_MODVERSIONS
  524. cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
  525. $(call rust_exports,$@,"%s\n") | \
  526. scripts/gendwarfksyms/gendwarfksyms \
  527. $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
  528. $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
  529. $@ >> $(dot-target).cmd)
  530. endif
  531. define rule_rustc_library
  532. $(call cmd_and_fixdep,rustc_library)
  533. $(call cmd,gen_objtooldep)
  534. $(call cmd,gendwarfksyms)
  535. endef
  536. define rule_rust_cc_library
  537. $(call if_changed_rule,cc_o_c)
  538. $(call cmd,force_checksrc)
  539. $(call cmd,gendwarfksyms)
  540. endef
  541. # helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
  542. # versions are calculated for the helpers too.
  543. $(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
  544. +$(call if_changed_rule,rust_cc_library)
  545. # Disable symbol versioning for exports.o to avoid conflicts with the actual
  546. # symbol versions generated from Rust objects.
  547. $(obj)/exports.o: private skip_gendwarfksyms = 1
  548. $(obj)/core.o: private skip_clippy = 1
  549. $(obj)/core.o: private skip_flags = $(core-skip_flags)
  550. $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
  551. $(obj)/core.o: private rustc_target_flags = $(core-flags)
  552. $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
  553. $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
  554. +$(call if_changed_rule,rustc_library)
  555. ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
  556. $(obj)/core.o: scripts/target.json
  557. endif
  558. KCOV_INSTRUMENT_core.o := n
  559. $(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
  560. $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
  561. $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
  562. +$(call if_changed_rule,rustc_library)
  563. $(obj)/pin_init.o: private skip_gendwarfksyms = 1
  564. $(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags)
  565. $(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
  566. $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
  567. +$(call if_changed_rule,rustc_library)
  568. # Even if normally `build_error` is not a kernel object, it should still be
  569. # treated as such so that we pass the same flags. Otherwise, for instance,
  570. # `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
  571. $(obj)/build_error.o: private is-kernel-object := y
  572. $(obj)/build_error.o: private skip_gendwarfksyms = 1
  573. $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
  574. +$(call if_changed_rule,rustc_library)
  575. $(obj)/ffi.o: private skip_gendwarfksyms = 1
  576. $(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
  577. +$(call if_changed_rule,rustc_library)
  578. $(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
  579. $(obj)/bindings.o: $(src)/bindings/lib.rs \
  580. $(obj)/ffi.o \
  581. $(obj)/pin_init.o \
  582. $(obj)/bindings/bindings_generated.rs \
  583. $(obj)/bindings/bindings_helpers_generated.rs FORCE
  584. +$(call if_changed_rule,rustc_library)
  585. $(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
  586. $(obj)/uapi.o: private skip_gendwarfksyms = 1
  587. $(obj)/uapi.o: $(src)/uapi/lib.rs \
  588. $(obj)/ffi.o \
  589. $(obj)/pin_init.o \
  590. $(obj)/uapi/uapi_generated.rs FORCE
  591. +$(call if_changed_rule,rustc_library)
  592. $(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
  593. --extern build_error --extern macros --extern bindings --extern uapi
  594. $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
  595. $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o FORCE
  596. +$(call if_changed_rule,rustc_library)
  597. ifdef CONFIG_JUMP_LABEL
  598. $(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
  599. endif
  600. ifndef CONFIG_UML
  601. ifdef CONFIG_BUG
  602. $(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
  603. endif
  604. endif
  605. endif # CONFIG_RUST