Makefile 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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) += libmacros.so
  11. no-clean-files += libmacros.so
  12. always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
  13. obj-$(CONFIG_RUST) += bindings.o kernel.o
  14. always-$(CONFIG_RUST) += exports_helpers_generated.h \
  15. exports_bindings_generated.h exports_kernel_generated.h
  16. always-$(CONFIG_RUST) += uapi/uapi_generated.rs
  17. obj-$(CONFIG_RUST) += uapi.o
  18. ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
  19. obj-$(CONFIG_RUST) += build_error.o
  20. else
  21. always-$(CONFIG_RUST) += build_error.o
  22. endif
  23. obj-$(CONFIG_RUST) += exports.o
  24. always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
  25. always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
  26. obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
  27. obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
  28. # Avoids running `$(RUSTC)` for the sysroot when it may not be available.
  29. ifdef CONFIG_RUST
  30. # `$(rust_flags)` is passed in case the user added `--sysroot`.
  31. rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
  32. rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
  33. RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
  34. ifneq ($(quiet),)
  35. rust_test_quiet=-q
  36. rustdoc_test_quiet=--test-args -q
  37. rustdoc_test_kernel_quiet=>/dev/null
  38. endif
  39. core-cfgs = \
  40. --cfg no_fp_fmt_parse
  41. core-edition := $(if $(call rustc-min-version,108700),2024,2021)
  42. # `rustdoc` did not save the target modifiers, thus workaround for
  43. # the time being (https://github.com/rust-lang/rust/issues/144521).
  44. rustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
  45. quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
  46. cmd_rustdoc = \
  47. OBJTREE=$(abspath $(objtree)) \
  48. $(RUSTDOC) $(filter-out $(skip_flags),$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
  49. $(rustc_target_flags) -L$(objtree)/$(obj) \
  50. -Zunstable-options --generate-link-to-definition \
  51. --output $(rustdoc_output) \
  52. --crate-name $(subst rustdoc-,,$@) \
  53. $(rustdoc_modifiers_workaround) \
  54. $(if $(rustdoc_host),,--sysroot=/dev/null) \
  55. @$(objtree)/include/generated/rustc_cfg $<
  56. # The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
  57. # can be used to specify a custom logo. However:
  58. # - The given value is used as-is, thus it cannot be relative or a local file
  59. # (unlike the non-custom case) since the generated docs have subfolders.
  60. # - It requires adding it to every crate.
  61. # - It requires changing `core` which comes from the sysroot.
  62. #
  63. # Using `-Zcrate-attr` would solve the last two points, but not the first.
  64. # The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
  65. # command-like flags to solve the issue. Meanwhile, we use the non-custom case
  66. # and then retouch the generated files.
  67. rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
  68. rustdoc-kernel
  69. $(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
  70. $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
  71. $(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
  72. -e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
  73. -e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
  74. -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
  75. -e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
  76. $(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
  77. echo ".logo-container > img { object-fit: contain; }" >> $$f; done
  78. rustdoc-macros: private rustdoc_host = yes
  79. rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
  80. --extern proc_macro
  81. rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean FORCE
  82. +$(call if_changed,rustdoc)
  83. # Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
  84. # not be needed -- see https://github.com/rust-lang/rust/pull/128307.
  85. rustdoc-core: private skip_flags = --edition=2021 -Wrustdoc::unescaped_backticks
  86. rustdoc-core: private rustc_target_flags = --edition=$(core-edition) $(core-cfgs)
  87. rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE
  88. +$(call if_changed,rustdoc)
  89. rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
  90. +$(call if_changed,rustdoc)
  91. rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
  92. +$(call if_changed,rustdoc)
  93. rustdoc-kernel: private rustc_target_flags = --extern ffi \
  94. --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
  95. --extern bindings --extern uapi
  96. rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
  97. rustdoc-compiler_builtins $(obj)/libmacros.so \
  98. $(obj)/bindings.o FORCE
  99. +$(call if_changed,rustdoc)
  100. rustdoc-clean: FORCE
  101. $(Q)rm -rf $(rustdoc_output)
  102. quiet_cmd_rustc_test_library = RUSTC TL $<
  103. cmd_rustc_test_library = \
  104. OBJTREE=$(abspath $(objtree)) \
  105. $(RUSTC) $(rust_common_flags) \
  106. @$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
  107. --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
  108. --out-dir $(objtree)/$(obj)/test --cfg testlib \
  109. -L$(objtree)/$(obj)/test \
  110. --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
  111. rusttestlib-build_error: $(src)/build_error.rs FORCE
  112. +$(call if_changed,rustc_test_library)
  113. rusttestlib-ffi: $(src)/ffi.rs FORCE
  114. +$(call if_changed,rustc_test_library)
  115. rusttestlib-macros: private rustc_target_flags = --extern proc_macro
  116. rusttestlib-macros: private rustc_test_library_proc = yes
  117. rusttestlib-macros: $(src)/macros/lib.rs FORCE
  118. +$(call if_changed,rustc_test_library)
  119. rusttestlib-kernel: private rustc_target_flags = --extern ffi \
  120. --extern build_error --extern macros \
  121. --extern bindings --extern uapi
  122. rusttestlib-kernel: $(src)/kernel/lib.rs \
  123. rusttestlib-bindings rusttestlib-uapi rusttestlib-build_error \
  124. $(obj)/libmacros.so $(obj)/bindings.o FORCE
  125. +$(call if_changed,rustc_test_library)
  126. rusttestlib-bindings: private rustc_target_flags = --extern ffi
  127. rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi FORCE
  128. +$(call if_changed,rustc_test_library)
  129. rusttestlib-uapi: private rustc_target_flags = --extern ffi
  130. rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi FORCE
  131. +$(call if_changed,rustc_test_library)
  132. quiet_cmd_rustdoc_test = RUSTDOC T $<
  133. cmd_rustdoc_test = \
  134. OBJTREE=$(abspath $(objtree)) \
  135. $(RUSTDOC) --test $(rust_common_flags) \
  136. -Zcrate-attr='feature(used_with_arg)' \
  137. @$(objtree)/include/generated/rustc_cfg \
  138. $(rustc_target_flags) $(rustdoc_test_target_flags) \
  139. $(rustdoc_test_quiet) \
  140. -L$(objtree)/$(obj)/test --output $(rustdoc_output) \
  141. --crate-name $(subst rusttest-,,$@) $<
  142. quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
  143. cmd_rustdoc_test_kernel = \
  144. rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
  145. mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
  146. OBJTREE=$(abspath $(objtree)) \
  147. $(RUSTDOC) --test $(rust_flags) \
  148. -L$(objtree)/$(obj) --extern ffi --extern kernel \
  149. --extern build_error --extern macros \
  150. --extern bindings --extern uapi \
  151. --no-run --crate-name kernel -Zunstable-options \
  152. --sysroot=/dev/null \
  153. $(rustdoc_modifiers_workaround) \
  154. --test-builder $(objtree)/scripts/rustdoc_test_builder \
  155. $< $(rustdoc_test_kernel_quiet); \
  156. $(objtree)/scripts/rustdoc_test_gen
  157. %/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
  158. $(src)/kernel/lib.rs $(obj)/kernel.o \
  159. $(objtree)/scripts/rustdoc_test_builder \
  160. $(objtree)/scripts/rustdoc_test_gen FORCE
  161. +$(call if_changed,rustdoc_test_kernel)
  162. # We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
  163. # so for the moment we skip `-Cpanic=abort`.
  164. quiet_cmd_rustc_test = RUSTC T $<
  165. cmd_rustc_test = \
  166. OBJTREE=$(abspath $(objtree)) \
  167. $(RUSTC) --test $(rust_common_flags) \
  168. @$(objtree)/include/generated/rustc_cfg \
  169. $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
  170. -L$(objtree)/$(obj)/test \
  171. --crate-name $(subst rusttest-,,$@) $<; \
  172. $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
  173. $(rustc_test_run_flags)
  174. rusttest: rusttest-macros rusttest-kernel
  175. rusttest-macros: private rustc_target_flags = --extern proc_macro \
  176. --extern macros --extern kernel
  177. rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
  178. rusttest-macros: $(src)/macros/lib.rs \
  179. rusttestlib-macros rusttestlib-kernel FORCE
  180. +$(call if_changed,rustc_test)
  181. +$(call if_changed,rustdoc_test)
  182. rusttest-kernel: private rustc_target_flags = --extern ffi \
  183. --extern build_error --extern macros --extern bindings --extern uapi
  184. rusttest-kernel: $(src)/kernel/lib.rs rusttestlib-ffi rusttestlib-kernel \
  185. rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
  186. rusttestlib-uapi FORCE
  187. +$(call if_changed,rustc_test)
  188. ifdef CONFIG_CC_IS_CLANG
  189. bindgen_c_flags = $(c_flags)
  190. else
  191. # bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
  192. # plugin backend and/or the Clang driver would be perfectly compatible with GCC.
  193. #
  194. # For the moment, here we are tweaking the flags on the fly. This is a hack,
  195. # and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
  196. # if we end up using one of those structs).
  197. bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
  198. -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
  199. -mindirect-branch=thunk-extern -mindirect-branch-register \
  200. -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
  201. -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
  202. -mno-pointers-to-nested-functions -mno-string \
  203. -mno-strict-align -mstrict-align -mdirect-extern-access \
  204. -mexplicit-relocs -mno-check-zero-division \
  205. -fconserve-stack -falign-jumps=% -falign-loops=% \
  206. -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
  207. -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
  208. -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
  209. -fzero-call-used-regs=% -fno-stack-clash-protection \
  210. -fno-inline-functions-called-once -fsanitize=bounds-strict \
  211. -fstrict-flex-arrays=% -fmin-function-alignment=% \
  212. -fzero-init-padding-bits=% -mno-fdpic \
  213. --param=% --param asan-%
  214. # Derived from `scripts/Makefile.clang`.
  215. BINDGEN_TARGET_x86 := x86_64-linux-gnu
  216. BINDGEN_TARGET_arm64 := aarch64-linux-gnu
  217. BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf
  218. BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
  219. # All warnings are inhibited since GCC builds are very experimental,
  220. # many GCC warnings are not supported by Clang, they may only appear in
  221. # some configurations, with new GCC versions, etc.
  222. bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
  223. # Auto variable zero-initialization requires an additional special option with
  224. # clang that is going to be removed sometime in the future (likely in
  225. # clang-18), so make sure to pass this option only if clang supports it
  226. # (libclang major version < 16).
  227. #
  228. # https://github.com/llvm/llvm-project/issues/44842
  229. # https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
  230. ifdef CONFIG_INIT_STACK_ALL_ZERO
  231. libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
  232. ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
  233. bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
  234. endif
  235. endif
  236. bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
  237. $(bindgen_extra_c_flags)
  238. endif
  239. ifdef CONFIG_LTO
  240. bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
  241. else
  242. bindgen_c_flags_lto = $(bindgen_c_flags)
  243. endif
  244. # `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
  245. # prototypes for functions like `memcpy` -- if this flag is not passed,
  246. # `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
  247. # architecture instead of generating `usize`.
  248. bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
  249. # Each `bindgen` release may upgrade the list of Rust target versions. By
  250. # default, the highest stable release in their list is used. Thus we need to set
  251. # a `--rust-target` to avoid future `bindgen` releases emitting code that
  252. # `rustc` may not understand. On top of that, `bindgen` does not support passing
  253. # an unknown Rust target version.
  254. #
  255. # Therefore, the Rust target for `bindgen` can be only as high as the minimum
  256. # Rust version the kernel supports and only as high as the greatest stable Rust
  257. # target supported by the minimum `bindgen` version the kernel supports (that
  258. # is, if we do not test the actual `rustc`/`bindgen` versions running).
  259. #
  260. # Starting with `bindgen` 0.71.0, we will be able to set any future Rust version
  261. # instead, i.e. we will be able to set here our minimum supported Rust version.
  262. quiet_cmd_bindgen = BINDGEN $@
  263. cmd_bindgen = \
  264. $(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.68 \
  265. --use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
  266. --no-debug '.*' --enable-function-attribute-detection \
  267. -o $@ -- $(bindgen_c_flags_final) -DMODULE \
  268. $(bindgen_target_cflags) $(bindgen_target_extra)
  269. $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
  270. $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
  271. $(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
  272. sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
  273. $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
  274. $(src)/bindgen_parameters FORCE
  275. $(call if_changed_dep,bindgen)
  276. $(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
  277. $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
  278. $(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
  279. $(src)/bindgen_parameters FORCE
  280. $(call if_changed_dep,bindgen)
  281. # See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
  282. # with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
  283. # given it is `libclang`; but for consistency, future Clang changes and/or
  284. # a potential future GCC backend for `bindgen`, we disable it too.
  285. $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
  286. --blocklist-type '.*' --allowlist-var '' \
  287. --allowlist-function 'rust_helper_.*'
  288. $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
  289. -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
  290. $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
  291. sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@
  292. $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
  293. $(call if_changed_dep,bindgen)
  294. quiet_cmd_exports = EXPORTS $@
  295. cmd_exports = \
  296. $(NM) -p --defined-only $< \
  297. | awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
  298. $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
  299. $(call if_changed,exports)
  300. # Even though Rust kernel modules should never use the bindings directly,
  301. # symbols from the `bindings` crate and the C helpers need to be exported
  302. # because Rust generics and inlined functions may not get their code generated
  303. # in the crate where they are defined. Other helpers, called from non-inline
  304. # functions, may not be exported, in principle. However, in general, the Rust
  305. # compiler does not guarantee codegen will be performed for a non-inline
  306. # function either. Therefore, we export all symbols from helpers and bindings.
  307. # In the future, this may be revisited to reduce the number of exports after
  308. # the compiler is informed about the places codegen is required.
  309. $(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
  310. $(call if_changed,exports)
  311. $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
  312. $(call if_changed,exports)
  313. $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
  314. $(call if_changed,exports)
  315. quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
  316. cmd_rustc_procmacro = \
  317. $(RUSTC_OR_CLIPPY) $(rust_common_flags) \
  318. -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
  319. -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
  320. --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
  321. --crate-type proc-macro \
  322. --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
  323. # Procedural macros can only be used with the `rustc` that compiled it.
  324. $(obj)/libmacros.so: $(src)/macros/lib.rs FORCE
  325. +$(call if_changed_dep,rustc_procmacro)
  326. quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
  327. cmd_rustc_library = \
  328. OBJTREE=$(abspath $(objtree)) \
  329. $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
  330. $(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
  331. --emit=dep-info=$(depfile) --emit=obj=$@ \
  332. --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
  333. --crate-type rlib -L$(objtree)/$(obj) \
  334. --crate-name $(patsubst %.o,%,$(notdir $@)) $< \
  335. --sysroot=/dev/null \
  336. $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
  337. $(cmd_objtool)
  338. rust-analyzer:
  339. $(Q)$(srctree)/scripts/generate_rust_analyzer.py \
  340. --cfgs='core=$(core-cfgs)' $(core-edition) \
  341. $(realpath $(srctree)) $(realpath $(objtree)) \
  342. $(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
  343. $(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
  344. redirect-intrinsics = \
  345. __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
  346. __adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
  347. __muloti4 __multi3 \
  348. __udivmodti4 __udivti3 __umodti3
  349. ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
  350. # These intrinsics are defined for ARM64 and RISCV64
  351. redirect-intrinsics += \
  352. __ashrti3 \
  353. __ashlti3 __lshrti3
  354. endif
  355. define rule_rustc_library
  356. $(call cmd_and_fixdep,rustc_library)
  357. $(call cmd,gen_objtooldep)
  358. endef
  359. $(obj)/core.o: private skip_clippy = 1
  360. $(obj)/core.o: private skip_flags = --edition=2021 -Wunreachable_pub
  361. $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
  362. $(obj)/core.o: private rustc_target_flags = --edition=$(core-edition) $(core-cfgs)
  363. $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
  364. $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
  365. +$(call if_changed_rule,rustc_library)
  366. ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
  367. $(obj)/core.o: scripts/target.json
  368. endif
  369. $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
  370. $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
  371. +$(call if_changed_rule,rustc_library)
  372. $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
  373. +$(call if_changed_rule,rustc_library)
  374. $(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
  375. +$(call if_changed_rule,rustc_library)
  376. $(obj)/bindings.o: private rustc_target_flags = --extern ffi
  377. $(obj)/bindings.o: $(src)/bindings/lib.rs \
  378. $(obj)/ffi.o \
  379. $(obj)/bindings/bindings_generated.rs \
  380. $(obj)/bindings/bindings_helpers_generated.rs FORCE
  381. +$(call if_changed_rule,rustc_library)
  382. $(obj)/uapi.o: private rustc_target_flags = --extern ffi
  383. $(obj)/uapi.o: $(src)/uapi/lib.rs \
  384. $(obj)/ffi.o \
  385. $(obj)/uapi/uapi_generated.rs FORCE
  386. +$(call if_changed_rule,rustc_library)
  387. $(obj)/kernel.o: private rustc_target_flags = --extern ffi \
  388. --extern build_error --extern macros --extern bindings --extern uapi
  389. $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \
  390. $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
  391. +$(call if_changed_rule,rustc_library)
  392. endif # CONFIG_RUST