xref: /kernel/linux/linux-6.6/rust/Makefile (revision 62306a36)
162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci# Where to place rustdoc generated documentation
462306a36Sopenharmony_cirustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciobj-$(CONFIG_RUST) += core.o compiler_builtins.o
762306a36Sopenharmony_cialways-$(CONFIG_RUST) += exports_core_generated.h
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci# Missing prototypes are expected in the helpers since these are exported
1062306a36Sopenharmony_ci# for Rust only, thus there is no header nor prototypes.
1162306a36Sopenharmony_ciobj-$(CONFIG_RUST) += helpers.o
1262306a36Sopenharmony_ciCFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_cialways-$(CONFIG_RUST) += libmacros.so
1562306a36Sopenharmony_cino-clean-files += libmacros.so
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cialways-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
1862306a36Sopenharmony_ciobj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
1962306a36Sopenharmony_cialways-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
2062306a36Sopenharmony_ci    exports_kernel_generated.h
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cialways-$(CONFIG_RUST) += uapi/uapi_generated.rs
2362306a36Sopenharmony_ciobj-$(CONFIG_RUST) += uapi.o
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
2662306a36Sopenharmony_ciobj-$(CONFIG_RUST) += build_error.o
2762306a36Sopenharmony_cielse
2862306a36Sopenharmony_cialways-$(CONFIG_RUST) += build_error.o
2962306a36Sopenharmony_ciendif
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciobj-$(CONFIG_RUST) += exports.o
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cialways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
3462306a36Sopenharmony_cialways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
3762306a36Sopenharmony_ciobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
4062306a36Sopenharmony_ciifdef CONFIG_RUST
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci# `$(rust_flags)` is passed in case the user added `--sysroot`.
4362306a36Sopenharmony_cirustc_sysroot := $(shell $(RUSTC) $(rust_flags) --print sysroot)
4462306a36Sopenharmony_cirustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
4562306a36Sopenharmony_ciRUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciifeq ($(quiet),silent_)
4862306a36Sopenharmony_cicargo_quiet=-q
4962306a36Sopenharmony_cirust_test_quiet=-q
5062306a36Sopenharmony_cirustdoc_test_quiet=--test-args -q
5162306a36Sopenharmony_cirustdoc_test_kernel_quiet=>/dev/null
5262306a36Sopenharmony_cielse ifeq ($(quiet),quiet_)
5362306a36Sopenharmony_cirust_test_quiet=-q
5462306a36Sopenharmony_cirustdoc_test_quiet=--test-args -q
5562306a36Sopenharmony_cirustdoc_test_kernel_quiet=>/dev/null
5662306a36Sopenharmony_cielse
5762306a36Sopenharmony_cicargo_quiet=--verbose
5862306a36Sopenharmony_ciendif
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cicore-cfgs = \
6162306a36Sopenharmony_ci    --cfg no_fp_fmt_parse
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cialloc-cfgs = \
6462306a36Sopenharmony_ci    --cfg no_borrow \
6562306a36Sopenharmony_ci    --cfg no_fmt \
6662306a36Sopenharmony_ci    --cfg no_global_oom_handling \
6762306a36Sopenharmony_ci    --cfg no_macros \
6862306a36Sopenharmony_ci    --cfg no_rc \
6962306a36Sopenharmony_ci    --cfg no_str \
7062306a36Sopenharmony_ci    --cfg no_string \
7162306a36Sopenharmony_ci    --cfg no_sync \
7262306a36Sopenharmony_ci    --cfg no_thin
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciquiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
7562306a36Sopenharmony_ci      cmd_rustdoc = \
7662306a36Sopenharmony_ci	OBJTREE=$(abspath $(objtree)) \
7762306a36Sopenharmony_ci	$(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \
7862306a36Sopenharmony_ci		$(rustc_target_flags) -L$(objtree)/$(obj) \
7962306a36Sopenharmony_ci		--output $(rustdoc_output) \
8062306a36Sopenharmony_ci		--crate-name $(subst rustdoc-,,$@) \
8162306a36Sopenharmony_ci		@$(objtree)/include/generated/rustc_cfg $<
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
8462306a36Sopenharmony_ci# can be used to specify a custom logo. However:
8562306a36Sopenharmony_ci#   - The given value is used as-is, thus it cannot be relative or a local file
8662306a36Sopenharmony_ci#     (unlike the non-custom case) since the generated docs have subfolders.
8762306a36Sopenharmony_ci#   - It requires adding it to every crate.
8862306a36Sopenharmony_ci#   - It requires changing `core` which comes from the sysroot.
8962306a36Sopenharmony_ci#
9062306a36Sopenharmony_ci# Using `-Zcrate-attr` would solve the last two points, but not the first.
9162306a36Sopenharmony_ci# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
9262306a36Sopenharmony_ci# command-like flags to solve the issue. Meanwhile, we use the non-custom case
9362306a36Sopenharmony_ci# and then retouch the generated files.
9462306a36Sopenharmony_cirustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
9562306a36Sopenharmony_ci    rustdoc-alloc rustdoc-kernel
9662306a36Sopenharmony_ci	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
9762306a36Sopenharmony_ci	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
9862306a36Sopenharmony_ci	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
9962306a36Sopenharmony_ci		-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
10062306a36Sopenharmony_ci		-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
10162306a36Sopenharmony_ci		-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g'
10262306a36Sopenharmony_ci	$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
10362306a36Sopenharmony_ci		echo ".logo-container > img { object-fit: contain; }" >> $$f; done
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cirustdoc-macros: private rustdoc_host = yes
10662306a36Sopenharmony_cirustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
10762306a36Sopenharmony_ci    --extern proc_macro
10862306a36Sopenharmony_cirustdoc-macros: $(src)/macros/lib.rs FORCE
10962306a36Sopenharmony_ci	$(call if_changed,rustdoc)
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cirustdoc-core: private rustc_target_flags = $(core-cfgs)
11262306a36Sopenharmony_cirustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
11362306a36Sopenharmony_ci	$(call if_changed,rustdoc)
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cirustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
11662306a36Sopenharmony_ci	$(call if_changed,rustdoc)
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci# We need to allow `rustdoc::broken_intra_doc_links` because some
11962306a36Sopenharmony_ci# `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
12062306a36Sopenharmony_ci# functions. Ideally `rustdoc` would have a way to distinguish broken links
12162306a36Sopenharmony_ci# due to things that are "configured out" vs. entirely non-existing ones.
12262306a36Sopenharmony_cirustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
12362306a36Sopenharmony_ci    -Arustdoc::broken_intra_doc_links
12462306a36Sopenharmony_cirustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
12562306a36Sopenharmony_ci	$(call if_changed,rustdoc)
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cirustdoc-kernel: private rustc_target_flags = --extern alloc \
12862306a36Sopenharmony_ci    --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
12962306a36Sopenharmony_ci    --extern bindings --extern uapi
13062306a36Sopenharmony_cirustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
13162306a36Sopenharmony_ci    rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
13262306a36Sopenharmony_ci    $(obj)/bindings.o FORCE
13362306a36Sopenharmony_ci	$(call if_changed,rustdoc)
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciquiet_cmd_rustc_test_library = RUSTC TL $<
13662306a36Sopenharmony_ci      cmd_rustc_test_library = \
13762306a36Sopenharmony_ci	OBJTREE=$(abspath $(objtree)) \
13862306a36Sopenharmony_ci	$(RUSTC) $(rust_common_flags) \
13962306a36Sopenharmony_ci		@$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
14062306a36Sopenharmony_ci		--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
14162306a36Sopenharmony_ci		--out-dir $(objtree)/$(obj)/test --cfg testlib \
14262306a36Sopenharmony_ci		--sysroot $(objtree)/$(obj)/test/sysroot \
14362306a36Sopenharmony_ci		-L$(objtree)/$(obj)/test \
14462306a36Sopenharmony_ci		--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cirusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
14762306a36Sopenharmony_ci	$(call if_changed,rustc_test_library)
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cirusttestlib-macros: private rustc_target_flags = --extern proc_macro
15062306a36Sopenharmony_cirusttestlib-macros: private rustc_test_library_proc = yes
15162306a36Sopenharmony_cirusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
15262306a36Sopenharmony_ci	$(call if_changed,rustc_test_library)
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cirusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
15562306a36Sopenharmony_ci	$(call if_changed,rustc_test_library)
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cirusttestlib-uapi: $(src)/uapi/lib.rs rusttest-prepare FORCE
15862306a36Sopenharmony_ci	$(call if_changed,rustc_test_library)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciquiet_cmd_rustdoc_test = RUSTDOC T $<
16162306a36Sopenharmony_ci      cmd_rustdoc_test = \
16262306a36Sopenharmony_ci	OBJTREE=$(abspath $(objtree)) \
16362306a36Sopenharmony_ci	$(RUSTDOC) --test $(rust_common_flags) \
16462306a36Sopenharmony_ci		@$(objtree)/include/generated/rustc_cfg \
16562306a36Sopenharmony_ci		$(rustc_target_flags) $(rustdoc_test_target_flags) \
16662306a36Sopenharmony_ci		--sysroot $(objtree)/$(obj)/test/sysroot $(rustdoc_test_quiet) \
16762306a36Sopenharmony_ci		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
16862306a36Sopenharmony_ci		--crate-name $(subst rusttest-,,$@) $<
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ciquiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
17162306a36Sopenharmony_ci      cmd_rustdoc_test_kernel = \
17262306a36Sopenharmony_ci	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
17362306a36Sopenharmony_ci	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
17462306a36Sopenharmony_ci	OBJTREE=$(abspath $(objtree)) \
17562306a36Sopenharmony_ci	$(RUSTDOC) --test $(rust_flags) \
17662306a36Sopenharmony_ci		@$(objtree)/include/generated/rustc_cfg \
17762306a36Sopenharmony_ci		-L$(objtree)/$(obj) --extern alloc --extern kernel \
17862306a36Sopenharmony_ci		--extern build_error --extern macros \
17962306a36Sopenharmony_ci		--extern bindings --extern uapi \
18062306a36Sopenharmony_ci		--no-run --crate-name kernel -Zunstable-options \
18162306a36Sopenharmony_ci		--test-builder $(objtree)/scripts/rustdoc_test_builder \
18262306a36Sopenharmony_ci		$< $(rustdoc_test_kernel_quiet); \
18362306a36Sopenharmony_ci	$(objtree)/scripts/rustdoc_test_gen
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
18662306a36Sopenharmony_ci    $(src)/kernel/lib.rs $(obj)/kernel.o \
18762306a36Sopenharmony_ci    $(objtree)/scripts/rustdoc_test_builder \
18862306a36Sopenharmony_ci    $(objtree)/scripts/rustdoc_test_gen FORCE
18962306a36Sopenharmony_ci	$(call if_changed,rustdoc_test_kernel)
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
19262306a36Sopenharmony_ci# so for the moment we skip `-Cpanic=abort`.
19362306a36Sopenharmony_ciquiet_cmd_rustc_test = RUSTC T  $<
19462306a36Sopenharmony_ci      cmd_rustc_test = \
19562306a36Sopenharmony_ci	OBJTREE=$(abspath $(objtree)) \
19662306a36Sopenharmony_ci	$(RUSTC) --test $(rust_common_flags) \
19762306a36Sopenharmony_ci		@$(objtree)/include/generated/rustc_cfg \
19862306a36Sopenharmony_ci		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
19962306a36Sopenharmony_ci		--sysroot $(objtree)/$(obj)/test/sysroot \
20062306a36Sopenharmony_ci		-L$(objtree)/$(obj)/test \
20162306a36Sopenharmony_ci		--crate-name $(subst rusttest-,,$@) $<; \
20262306a36Sopenharmony_ci	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
20362306a36Sopenharmony_ci		$(rustc_test_run_flags)
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_cirusttest: rusttest-macros rusttest-kernel
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci# This prepares a custom sysroot with our custom `alloc` instead of
20862306a36Sopenharmony_ci# the standard one.
20962306a36Sopenharmony_ci#
21062306a36Sopenharmony_ci# This requires several hacks:
21162306a36Sopenharmony_ci#   - Unlike `core` and `alloc`, `std` depends on more than a dozen crates,
21262306a36Sopenharmony_ci#     including third-party crates that need to be downloaded, plus custom
21362306a36Sopenharmony_ci#     `build.rs` steps. Thus hardcoding things here is not maintainable.
21462306a36Sopenharmony_ci#   - `cargo` knows how to build the standard library, but it is an unstable
21562306a36Sopenharmony_ci#     feature so far (`-Zbuild-std`).
21662306a36Sopenharmony_ci#   - `cargo` only considers the use case of building the standard library
21762306a36Sopenharmony_ci#     to use it in a given package. Thus we need to create a dummy package
21862306a36Sopenharmony_ci#     and pick the generated libraries from there.
21962306a36Sopenharmony_ci#   - Since we only keep a subset of upstream `alloc` in-tree, we need
22062306a36Sopenharmony_ci#     to recreate it on the fly by putting our sources on top.
22162306a36Sopenharmony_ci#   - The usual ways of modifying the dependency graph in `cargo` do not seem
22262306a36Sopenharmony_ci#     to apply for the `-Zbuild-std` steps, thus we have to mislead it
22362306a36Sopenharmony_ci#     by modifying the sources in the sysroot.
22462306a36Sopenharmony_ci#   - To avoid messing with the user's Rust installation, we create a clone
22562306a36Sopenharmony_ci#     of the sysroot. However, `cargo` ignores `RUSTFLAGS` in the `-Zbuild-std`
22662306a36Sopenharmony_ci#     steps, thus we use a wrapper binary passed via `RUSTC` to pass the flag.
22762306a36Sopenharmony_ci#
22862306a36Sopenharmony_ci# In the future, we hope to avoid the whole ordeal by either:
22962306a36Sopenharmony_ci#   - Making the `test` crate not depend on `std` (either improving upstream
23062306a36Sopenharmony_ci#     or having our own custom crate).
23162306a36Sopenharmony_ci#   - Making the tests run in kernel space (requires the previous point).
23262306a36Sopenharmony_ci#   - Making `std` and friends be more like a "normal" crate, so that
23362306a36Sopenharmony_ci#     `-Zbuild-std` and related hacks are not needed.
23462306a36Sopenharmony_ciquiet_cmd_rustsysroot = RUSTSYSROOT
23562306a36Sopenharmony_ci      cmd_rustsysroot = \
23662306a36Sopenharmony_ci	rm -rf $(objtree)/$(obj)/test; \
23762306a36Sopenharmony_ci	mkdir -p $(objtree)/$(obj)/test; \
23862306a36Sopenharmony_ci	cp -a $(rustc_sysroot) $(objtree)/$(obj)/test/sysroot; \
23962306a36Sopenharmony_ci	cp -r $(srctree)/$(src)/alloc/* \
24062306a36Sopenharmony_ci		$(objtree)/$(obj)/test/sysroot/lib/rustlib/src/rust/library/alloc/src; \
24162306a36Sopenharmony_ci	echo '\#!/bin/sh' > $(objtree)/$(obj)/test/rustc_sysroot; \
24262306a36Sopenharmony_ci	echo "$(RUSTC) --sysroot=$(abspath $(objtree)/$(obj)/test/sysroot) \"\$$@\"" \
24362306a36Sopenharmony_ci		>> $(objtree)/$(obj)/test/rustc_sysroot; \
24462306a36Sopenharmony_ci	chmod u+x $(objtree)/$(obj)/test/rustc_sysroot; \
24562306a36Sopenharmony_ci	$(CARGO) -q new $(objtree)/$(obj)/test/dummy; \
24662306a36Sopenharmony_ci	RUSTC=$(objtree)/$(obj)/test/rustc_sysroot $(CARGO) $(cargo_quiet) \
24762306a36Sopenharmony_ci		test -Zbuild-std --target $(rustc_host_target) \
24862306a36Sopenharmony_ci		--manifest-path $(objtree)/$(obj)/test/dummy/Cargo.toml; \
24962306a36Sopenharmony_ci	rm $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib/*; \
25062306a36Sopenharmony_ci	cp $(objtree)/$(obj)/test/dummy/target/$(rustc_host_target)/debug/deps/* \
25162306a36Sopenharmony_ci		$(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_cirusttest-prepare: FORCE
25462306a36Sopenharmony_ci	$(call if_changed,rustsysroot)
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_cirusttest-macros: private rustc_target_flags = --extern proc_macro
25762306a36Sopenharmony_cirusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
25862306a36Sopenharmony_cirusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
25962306a36Sopenharmony_ci	$(call if_changed,rustc_test)
26062306a36Sopenharmony_ci	$(call if_changed,rustdoc_test)
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cirusttest-kernel: private rustc_target_flags = --extern alloc \
26362306a36Sopenharmony_ci    --extern build_error --extern macros --extern bindings --extern uapi
26462306a36Sopenharmony_cirusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
26562306a36Sopenharmony_ci    rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
26662306a36Sopenharmony_ci    rusttestlib-uapi FORCE
26762306a36Sopenharmony_ci	$(call if_changed,rustc_test)
26862306a36Sopenharmony_ci	$(call if_changed,rustc_test_library)
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ciifdef CONFIG_CC_IS_CLANG
27162306a36Sopenharmony_cibindgen_c_flags = $(c_flags)
27262306a36Sopenharmony_cielse
27362306a36Sopenharmony_ci# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
27462306a36Sopenharmony_ci# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
27562306a36Sopenharmony_ci#
27662306a36Sopenharmony_ci# For the moment, here we are tweaking the flags on the fly. This is a hack,
27762306a36Sopenharmony_ci# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
27862306a36Sopenharmony_ci# if we end up using one of those structs).
27962306a36Sopenharmony_cibindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
28062306a36Sopenharmony_ci	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
28162306a36Sopenharmony_ci	-mindirect-branch=thunk-extern -mindirect-branch-register \
28262306a36Sopenharmony_ci	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
28362306a36Sopenharmony_ci	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
28462306a36Sopenharmony_ci	-mno-pointers-to-nested-functions -mno-string \
28562306a36Sopenharmony_ci	-mno-strict-align -mstrict-align \
28662306a36Sopenharmony_ci	-fconserve-stack -falign-jumps=% -falign-loops=% \
28762306a36Sopenharmony_ci	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
28862306a36Sopenharmony_ci	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
28962306a36Sopenharmony_ci	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
29062306a36Sopenharmony_ci	-fzero-call-used-regs=% -fno-stack-clash-protection \
29162306a36Sopenharmony_ci	-fno-inline-functions-called-once -fsanitize=bounds-strict \
29262306a36Sopenharmony_ci	-fstrict-flex-arrays=% \
29362306a36Sopenharmony_ci	--param=% --param asan-%
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci# Derived from `scripts/Makefile.clang`.
29662306a36Sopenharmony_ciBINDGEN_TARGET_x86	:= x86_64-linux-gnu
29762306a36Sopenharmony_ciBINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci# All warnings are inhibited since GCC builds are very experimental,
30062306a36Sopenharmony_ci# many GCC warnings are not supported by Clang, they may only appear in
30162306a36Sopenharmony_ci# some configurations, with new GCC versions, etc.
30262306a36Sopenharmony_cibindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci# Auto variable zero-initialization requires an additional special option with
30562306a36Sopenharmony_ci# clang that is going to be removed sometime in the future (likely in
30662306a36Sopenharmony_ci# clang-18), so make sure to pass this option only if clang supports it
30762306a36Sopenharmony_ci# (libclang major version < 16).
30862306a36Sopenharmony_ci#
30962306a36Sopenharmony_ci# https://github.com/llvm/llvm-project/issues/44842
31062306a36Sopenharmony_ci# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
31162306a36Sopenharmony_ciifdef CONFIG_INIT_STACK_ALL_ZERO
31262306a36Sopenharmony_cilibclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
31362306a36Sopenharmony_ciifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
31462306a36Sopenharmony_cibindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
31562306a36Sopenharmony_ciendif
31662306a36Sopenharmony_ciendif
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_cibindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
31962306a36Sopenharmony_ci	$(bindgen_extra_c_flags)
32062306a36Sopenharmony_ciendif
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ciifdef CONFIG_LTO
32362306a36Sopenharmony_cibindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
32462306a36Sopenharmony_cielse
32562306a36Sopenharmony_cibindgen_c_flags_lto = $(bindgen_c_flags)
32662306a36Sopenharmony_ciendif
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_cibindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ciquiet_cmd_bindgen = BINDGEN $@
33162306a36Sopenharmony_ci      cmd_bindgen = \
33262306a36Sopenharmony_ci	$(BINDGEN) $< $(bindgen_target_flags) \
33362306a36Sopenharmony_ci		--use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
33462306a36Sopenharmony_ci		--no-debug '.*' \
33562306a36Sopenharmony_ci		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
33662306a36Sopenharmony_ci		$(bindgen_target_cflags) $(bindgen_target_extra)
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
33962306a36Sopenharmony_ci    $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters)
34062306a36Sopenharmony_ci$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
34162306a36Sopenharmony_ci    $(src)/bindgen_parameters FORCE
34262306a36Sopenharmony_ci	$(call if_changed_dep,bindgen)
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
34562306a36Sopenharmony_ci    $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters)
34662306a36Sopenharmony_ci$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
34762306a36Sopenharmony_ci    $(src)/bindgen_parameters FORCE
34862306a36Sopenharmony_ci	$(call if_changed_dep,bindgen)
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
35162306a36Sopenharmony_ci# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
35262306a36Sopenharmony_ci# given it is `libclang`; but for consistency, future Clang changes and/or
35362306a36Sopenharmony_ci# a potential future GCC backend for `bindgen`, we disable it too.
35462306a36Sopenharmony_ci$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
35562306a36Sopenharmony_ci    --blocklist-type '.*' --allowlist-var '' \
35662306a36Sopenharmony_ci    --allowlist-function 'rust_helper_.*'
35762306a36Sopenharmony_ci$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
35862306a36Sopenharmony_ci    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
35962306a36Sopenharmony_ci$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
36062306a36Sopenharmony_ci    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
36162306a36Sopenharmony_ci$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
36262306a36Sopenharmony_ci	$(call if_changed_dep,bindgen)
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ciquiet_cmd_exports = EXPORTS $@
36562306a36Sopenharmony_ci      cmd_exports = \
36662306a36Sopenharmony_ci	$(NM) -p --defined-only $< \
36762306a36Sopenharmony_ci		| grep -E ' (T|R|D) ' | cut -d ' ' -f 3 \
36862306a36Sopenharmony_ci		| xargs -Isymbol \
36962306a36Sopenharmony_ci		echo 'EXPORT_SYMBOL_RUST_GPL(symbol);' > $@
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
37262306a36Sopenharmony_ci	$(call if_changed,exports)
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
37562306a36Sopenharmony_ci	$(call if_changed,exports)
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
37862306a36Sopenharmony_ci	$(call if_changed,exports)
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
38162306a36Sopenharmony_ci	$(call if_changed,exports)
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ciquiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
38462306a36Sopenharmony_ci      cmd_rustc_procmacro = \
38562306a36Sopenharmony_ci	$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
38662306a36Sopenharmony_ci		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
38762306a36Sopenharmony_ci		--crate-type proc-macro \
38862306a36Sopenharmony_ci		--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci# Procedural macros can only be used with the `rustc` that compiled it.
39162306a36Sopenharmony_ci# Therefore, to get `libmacros.so` automatically recompiled when the compiler
39262306a36Sopenharmony_ci# version changes, we add `core.o` as a dependency (even if it is not needed).
39362306a36Sopenharmony_ci$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
39462306a36Sopenharmony_ci	$(call if_changed_dep,rustc_procmacro)
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ciquiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
39762306a36Sopenharmony_ci      cmd_rustc_library = \
39862306a36Sopenharmony_ci	OBJTREE=$(abspath $(objtree)) \
39962306a36Sopenharmony_ci	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
40062306a36Sopenharmony_ci		$(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
40162306a36Sopenharmony_ci		--emit=dep-info=$(depfile) --emit=obj=$@ \
40262306a36Sopenharmony_ci		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
40362306a36Sopenharmony_ci		--crate-type rlib -L$(objtree)/$(obj) \
40462306a36Sopenharmony_ci		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
40562306a36Sopenharmony_ci	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_cirust-analyzer:
40862306a36Sopenharmony_ci	$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
40962306a36Sopenharmony_ci		--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
41062306a36Sopenharmony_ci		$(abs_srctree) $(abs_objtree) \
41162306a36Sopenharmony_ci		$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
41262306a36Sopenharmony_ci		$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ciredirect-intrinsics = \
41562306a36Sopenharmony_ci	__addsf3 __eqsf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __unordsf2 \
41662306a36Sopenharmony_ci	__adddf3 __ledf2 __ltdf2 __muldf3 __unorddf2 \
41762306a36Sopenharmony_ci	__muloti4 __multi3 \
41862306a36Sopenharmony_ci	__udivmodti4 __udivti3 __umodti3
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ciifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
42162306a36Sopenharmony_ci	# These intrinsics are defined for ARM64 and RISCV64
42262306a36Sopenharmony_ci	redirect-intrinsics += \
42362306a36Sopenharmony_ci		__ashrti3 \
42462306a36Sopenharmony_ci		__ashlti3 __lshrti3
42562306a36Sopenharmony_ciendif
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci$(obj)/core.o: private skip_clippy = 1
42862306a36Sopenharmony_ci$(obj)/core.o: private skip_flags = -Dunreachable_pub
42962306a36Sopenharmony_ci$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
43062306a36Sopenharmony_ci$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
43162306a36Sopenharmony_ci$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs scripts/target.json FORCE
43262306a36Sopenharmony_ci	$(call if_changed_dep,rustc_library)
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
43562306a36Sopenharmony_ci$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
43662306a36Sopenharmony_ci	$(call if_changed_dep,rustc_library)
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci$(obj)/alloc.o: private skip_clippy = 1
43962306a36Sopenharmony_ci$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
44062306a36Sopenharmony_ci$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
44162306a36Sopenharmony_ci$(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
44262306a36Sopenharmony_ci	$(call if_changed_dep,rustc_library)
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
44562306a36Sopenharmony_ci	$(call if_changed_dep,rustc_library)
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci$(obj)/bindings.o: $(src)/bindings/lib.rs \
44862306a36Sopenharmony_ci    $(obj)/compiler_builtins.o \
44962306a36Sopenharmony_ci    $(obj)/bindings/bindings_generated.rs \
45062306a36Sopenharmony_ci    $(obj)/bindings/bindings_helpers_generated.rs FORCE
45162306a36Sopenharmony_ci	$(call if_changed_dep,rustc_library)
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci$(obj)/uapi.o: $(src)/uapi/lib.rs \
45462306a36Sopenharmony_ci    $(obj)/compiler_builtins.o \
45562306a36Sopenharmony_ci    $(obj)/uapi/uapi_generated.rs FORCE
45662306a36Sopenharmony_ci	$(call if_changed_dep,rustc_library)
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
45962306a36Sopenharmony_ci    --extern build_error --extern macros --extern bindings --extern uapi
46062306a36Sopenharmony_ci$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
46162306a36Sopenharmony_ci    $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
46262306a36Sopenharmony_ci	$(call if_changed_dep,rustc_library)
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ciendif # CONFIG_RUST
465