Makefile 16 KB

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