All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,  Quentin Monnet <qmo@kernel.org>,
	Miguel Ojeda <ojeda@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	 Nicolas Schier <nsc@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	 Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>
Cc: linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
	 rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org,
	 llvm@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v1 1/3] tools build: Add Rust library build infrastructure and zlib-rs feature test
Date: Thu, 29 Oct 2026 09:29:56 -0700	[thread overview]
Message-ID: <20261029162958.3224280-2-irogers@google.com> (raw)
In-Reply-To: <20261029162958.3224280-1-irogers@google.com>

Add shared Rust library build and feature detection support across tools/
and implement the zlib-rs feature check:

- Add tools/build/Makefile.rust with helpers to locate Cargo crate
  sources in distro (/usr/share/cargo/registry) or user Cargo registries,
  compile .rlib and .a targets with rustc, and resolve C compiler/linker
  flags via %-cflags and %-libs targets.
- Support resolving zlib-rs via LIBZ_RS_DIR, pkg-config
  (libz-rs-sys-cdylib, zlib-rs, z_rs), or building libz_rs.a (along with
  libzlib_rs.rlib and liblibc.rlib) into $(OUTPUT)libz_rs/.
- Add tools/build/feature/test-zlib-rs.c verifying ZLIB_VER_SUBREVISION
  == 15 (0xf) and linking against inflateInit().
- Extend tools/build/Build.include and tools/build/Makefile.build with
  cfgs-to-flags, rlibs-to-externs, per-target RUST_FLAGS/RUST_ENVS, and
  $(OUTPUT)lib%.rlib: %.rs pattern rules.
- Add RUST_TARGET_FLAGS_<arch> cross-compilation triples to
  tools/scripts/Makefile.arch and the feature_rust_lib helper macro to
  tools/scripts/Makefile.include.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/build/Build.include          |  14 ++-
 tools/build/Makefile.build         |  13 ++-
 tools/build/Makefile.feature       |   9 +-
 tools/build/Makefile.rust          | 149 +++++++++++++++++++++++++++++
 tools/build/feature/.gitignore     |   1 +
 tools/build/feature/Makefile       |   9 +-
 tools/build/feature/test-zlib-rs.c |  19 ++++
 tools/scripts/Makefile.arch        |  14 +++
 tools/scripts/Makefile.include     |  10 ++
 9 files changed, 229 insertions(+), 9 deletions(-)
 create mode 100644 tools/build/Makefile.rust
 create mode 100644 tools/build/feature/test-zlib-rs.c

diff --git a/tools/build/Build.include b/tools/build/Build.include
index cd0baa7a168d..45b61c31fd4c 100644
--- a/tools/build/Build.include
+++ b/tools/build/Build.include
@@ -100,12 +100,18 @@ cxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXX
 
 ###
 # Rust flags to be used on rule definition, includes:
+# - cfgs-to-flags helper to turn cfg tokens into --cfg='...' flags
+# - rlibs-to-externs helper to map .rlib prerequisites to --extern <crate>=<path>
 # - global $(RUST_FLAGS)
-# - per target Rust flags
-# - per object Rust flags
-rust_flags_1 = $(RUST_FLAGS) $(RUST_FLAGS_$(basetarget).o) $(RUST_FLAGS_$(obj))
-rust_flags_2 = $(filter-out $(RUST_FLAGS_REMOVE_$(basetarget).o), $(rust_flags_1))
+# - per target Rust flags ($(RUST_FLAGS_<target>)) and envs ($(RUST_ENVS_<target>))
+# - per object Rust flags ($(RUST_FLAGS_<obj>)) and envs ($(RUST_ENVS_<obj>))
+cfgs-to-flags = $(patsubst %,--cfg='%',$1)
+rlibs-to-externs = $(foreach rlib,$(filter %.rlib,$1),--extern $(patsubst lib%.rlib,%,$(notdir $(rlib)))=$(rlib))
+
+rust_flags_1 = $(RUST_FLAGS) $(RUST_FLAGS_$(basetarget).o) $(RUST_FLAGS_$(notdir $@)) $(RUST_FLAGS_$(obj))
+rust_flags_2 = $(filter-out $(RUST_FLAGS_REMOVE_$(basetarget).o) $(RUST_FLAGS_REMOVE_$(notdir $@)), $(rust_flags_1))
 rust_flags   = $(filter-out $(RUST_FLAGS_REMOVE_$(obj)), $(rust_flags_2))
+rust_envs    = $(RUST_ENVS_$(notdir $@)) $(RUST_ENVS_$(obj))
 
 ###
 ## HOSTCC C flags
diff --git a/tools/build/Makefile.build b/tools/build/Makefile.build
index ad69efdd4e85..50c3eb57d34b 100644
--- a/tools/build/Makefile.build
+++ b/tools/build/Makefile.build
@@ -79,12 +79,15 @@ quiet_cmd_host_ld_multi = HOSTLD  $@
                           xargs $(HOSTLD) -r -o $@,rm -f $@; $(HOSTAR) rcs $@)
 
 rust_common_cmd = \
-	$(RUSTC) $(rust_flags) \
-	--crate-type staticlib -L $(objtree)/rust/ \
+	$(rust_envs) $(RUSTC) $(rust_flags) \
+	-L $(objtree)/rust/ $(call rlibs-to-externs,$^) \
 	--emit=dep-info=$(depfile),link
 
+quiet_cmd_rustc_rlib_rs = $(RUSTC) $(quiet_modtag) $@
+      cmd_rustc_rlib_rs = $(rust_common_cmd) --crate-type rlib --crate-name $(patsubst lib%.rlib,%,$(notdir $@)) -o $@ $<
+
 quiet_cmd_rustc_a_rs = $(RUSTC) $(quiet_modtag) $@
-      cmd_rustc_a_rs = $(rust_common_cmd) -o $@ -g $< $(cmd_objtool)
+      cmd_rustc_a_rs = $(rust_common_cmd) --crate-type staticlib -o $@ -g $< $(cmd_objtool)
 
 ifneq ($(filter $(obj),$(hostprogs)),)
   host = host_
@@ -117,6 +120,10 @@ $(OUTPUT)%.s: %.c FORCE
 
 # it's recommended to build a static rust library, when a foreight (to rust)
 # linker is used.
+$(OUTPUT)lib%.rlib: %.rs FORCE
+	$(call rule_mkdir)
+	$(call if_changed_dep,rustc_rlib_rs)
+
 $(OUTPUT)%.a: %.rs FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,rustc_a_rs)
diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature
index 4d37d11f674c..c372600ae9e4 100644
--- a/tools/build/Makefile.feature
+++ b/tools/build/Makefile.feature
@@ -5,10 +5,11 @@ ifneq ($(OUTPUT),)
   OUTPUT_FEATURES = $(OUTPUT)feature/
   $(shell mkdir -p $(OUTPUT_FEATURES))
 endif
+RUST_OUTPUT ?= $(abspath $(or $(OUTPUT),$(CURDIR)))/
 
 feature_check = $(eval $(feature_check_code))
 define feature_check_code
-  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CC="$(CC)" CXX="$(CXX)" CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" CXXFLAGS="$(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
+  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) RUST_OUTPUT="$(RUST_OUTPUT)" CC="$(CC)" CXX="$(CXX)" RUSTC="$(RUSTC)" $(if $(PKG_CONFIG),PKG_CONFIG="$(PKG_CONFIG)") CFLAGS="$(CLANG_CROSS_FLAGS) $(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" CXXFLAGS="$(CLANG_CROSS_FLAGS) $(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))" RUST_FLAGS="$(RUST_FLAGS)" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
 endef
 
 feature_set = $(eval $(feature_set_code))
@@ -128,6 +129,8 @@ FEATURE_TESTS_EXTRA :=                  \
          libdebuginfod			\
          clang-bpf-co-re		\
          bpftool-skeletons		\
+         zlib-rs			\
+         rust				\
          libunwind			\
          libunwind-debug-frame		\
          $(foreach arch,$(LIBUNWIND_ARCHS),libunwind-$(arch) libunwind-debug-frame-$(arch))
@@ -149,6 +152,7 @@ FEATURE_DISPLAY ?=              \
          libcapstone            \
          llvm-perf              \
          zlib                   \
+         zlib-rs                \
          lzma                   \
          bpf			\
          libaio			\
@@ -216,6 +220,9 @@ ifeq ($(feature-all), 1)
   $(call feature_check,compile-x32)
   $(call feature_check,bionic)
   $(call feature_check,babeltrace2-ctf-writer)
+  ifneq ($(filter zlib-rs,$(FEATURE_TESTS)),)
+    $(call feature_check,zlib-rs)
+  endif
   $(call feature_check,libunwind)
   $(call feature_check,libunwind-debug-frame)
   $(foreach arch,$(LIBUNWIND_ARCHS),$(call feature_check,libunwind-$(arch)))
diff --git a/tools/build/Makefile.rust b/tools/build/Makefile.rust
new file mode 100644
index 000000000000..cd9e0915b929
--- /dev/null
+++ b/tools/build/Makefile.rust
@@ -0,0 +1,149 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Shared rules and helpers for discovering and building Rust libraries
+# (either from pre-installed packages/pkg-config or from local Cargo crate
+# sources) for tools/ builds and feature detection.
+
+ifeq ($(pound),)
+include $(dir $(lastword $(MAKEFILE_LIST)))../scripts/Makefile.include
+endif
+
+ifeq ($(PKG_CONFIG),)
+  ifneq (, $(shell which $(CROSS_COMPILE)pkg-config 2>/dev/null))
+    PKG_CONFIG := $(CROSS_COMPILE)pkg-config
+  else
+    PKG_CONFIG := pkg-config
+
+    ifdef CROSS_COMPILE
+      ifeq ($(PKG_CONFIG_LIBDIR)$(PKG_CONFIG_PATH)$(PKG_CONFIG_SYSROOT_DIR),)
+        CROSS_ARCH = $(notdir $(CROSS_COMPILE:%-=%))
+        PKG_CONFIG_LIBDIR := /usr/local/$(CROSS_ARCH)/lib/pkgconfig/
+        PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/lib/$(CROSS_ARCH)/pkgconfig/
+        PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/lib/$(CROSS_ARCH)/pkgconfig/
+        PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/local/share/pkgconfig/
+        PKG_CONFIG_LIBDIR := $(PKG_CONFIG_LIBDIR):/usr/share/pkgconfig/
+        export PKG_CONFIG_LIBDIR
+      endif
+    endif
+  endif
+endif
+
+ifeq ($(findstring -static,${LDFLAGS}),-static)
+  ifeq ($(findstring --static,$(PKG_CONFIG)),)
+    PKG_CONFIG += --static
+  endif
+endif
+
+# Search both distro-packaged Cargo registries (e.g. Debian/Ubuntu
+# /usr/share/cargo/registry from librust-*-dev packages) and user Cargo
+# registries ($CARGO_HOME/registry/src/* or ~/.cargo/registry/src/*).
+# Require a digit after "<crate>-" so prefixes like "libc-" do not match
+# unrelated crates such as "libc-print-*", and use version sorting (sort -V)
+# on the "<crate>-<version>" directory basename when multiple versions exist.
+CARGO_REGISTRY_DIR ?= /usr/share/cargo/registry $(wildcard $(or $(CARGO_HOME),$(HOME)/.cargo)/registry/src/*)
+cargo_crate_dirs = $(strip $(foreach dir,$(CARGO_REGISTRY_DIR),$(wildcard $(dir)/$(1)-[0-9]*)))
+cargo_crate_dir = $(if $(word 2,$(call cargo_crate_dirs,$(1))),$(shell printf '%s\n' $(call cargo_crate_dirs,$(1)) | awk -F/ '{print $$NF, $$0}' | sort -k1,1V | tail -n1 | cut -d' ' -f2-),$(call cargo_crate_dirs,$(1)))
+
+# Extract CARGO_PKG_VERSION* environment variables expected by crates using
+# env!("CARGO_PKG_VERSION*") from the "<crate>-<version>" directory name.
+define cargo_pkg_envs
+CARGO_PKG_VERSION=$(patsubst $(1)-%,%,$(notdir $(2))) \
+CARGO_PKG_VERSION_MAJOR=$(word 1,$(subst ., ,$(patsubst $(1)-%,%,$(notdir $(2))))) \
+CARGO_PKG_VERSION_MINOR=$(word 2,$(subst ., ,$(patsubst $(1)-%,%,$(notdir $(2)))))
+endef
+
+# Convert a whitespace-separated list of cfg tokens into --cfg='...' flags.
+cfgs-to-flags = $(patsubst %,--cfg='%',$1)
+
+# Map .rlib prerequisites to explicit --extern <crate>=<path.rlib> flags.
+# Passing explicit paths avoids -L ambiguity with sysroot crates (e.g. E0464
+# when both sysroot and local liblibc.rlib exist).
+rlibs-to-externs = $(foreach rlib,$(filter %.rlib,$1),--extern $(patsubst lib%.rlib,%,$(notdir $(rlib)))=$(rlib))
+rustc_common_flags := -C opt-level=3 -C relocation-model=pic
+
+cmd_rustc_rlib = \
+	$(rustc_target_envs) $(RUSTC) $(RUST_FLAGS) $(rustc_common_flags) $(rustc_target_flags) \
+	$(call rlibs-to-externs,$^) --crate-type rlib --crate-name $(patsubst lib%.rlib,%,$(notdir $@)) \
+	-o $@ $(filter %.rs,$^) > $(@:.rlib=.make.output) 2>&1
+
+cmd_rustc_staticlib = \
+	$(rustc_target_envs) $(RUSTC) $(RUST_FLAGS) $(rustc_common_flags) $(rustc_target_flags) \
+	$(call rlibs-to-externs,$^) --crate-type staticlib --crate-name $(patsubst lib%.a,%,$(notdir $@)) \
+	-o $@ $(filter %.rs,$^) > $(@:.a=.make.output) 2>&1
+
+# Helper targets invoked by $(call feature_rust_lib,<name>) to query the
+# resolved compiler and linker flags for a Rust library feature.
+%-cflags:
+	@echo $(FEATURE_CFLAGS-$*)
+
+%-libs:
+	@echo $(FEATURE_LIBS-$*)
+
+# Place compiled Rust library artifacts in their own subdirectory under the
+# build output root (e.g. $(OUTPUT)libz_rs/, alongside libapi/, libbpf/, etc.)
+# rather than inside $(OUTPUT)feature/.
+RUST_OUTPUT ?= $(patsubst %/feature/,%/,$(OUTPUT))
+LIBZ_RS_OUTPUT ?= $(RUST_OUTPUT)libz_rs/
+
+$(LIBZ_RS_OUTPUT):
+	$(Q)mkdir -p $@
+
+# Build chain for libz_rs.a from Cargo crate sources:
+#   libc (liblibc.rlib) -> zlib-rs (libzlib_rs.rlib) -> libz-rs-sys (libz_rs.a)
+LIBC_RS_DIR ?= $(call cargo_crate_dir,libc)
+libc-cfgs := feature="default" feature="std"
+libc-flags := --edition=2021 $(call cfgs-to-flags,$(libc-cfgs))
+
+$(LIBZ_RS_OUTPUT)liblibc.rlib: private rustc_target_flags = $(libc-flags)
+$(LIBZ_RS_OUTPUT)liblibc.rlib: $(wildcard $(LIBC_RS_DIR)/src/lib.rs) | $(LIBZ_RS_OUTPUT)
+	@$(if $<,$(cmd_rustc_rlib),false)
+
+ZLIB_RS_DIR ?= $(call cargo_crate_dir,zlib-rs)
+zlib_rs-cfgs := feature="__internal-api" feature="c-allocator" feature="rust-allocator" feature="std"
+zlib_rs-flags := --edition=2021 $(call cfgs-to-flags,$(zlib_rs-cfgs))
+
+$(LIBZ_RS_OUTPUT)libzlib_rs.rlib: private rustc_target_flags = $(zlib_rs-flags)
+$(LIBZ_RS_OUTPUT)libzlib_rs.rlib: $(wildcard $(ZLIB_RS_DIR)/src/lib.rs) | $(LIBZ_RS_OUTPUT)
+	@$(if $<,$(cmd_rustc_rlib),false)
+
+# Enable export-symbols, c-allocator, std, and gz so libz_rs.a exports the
+# full standard zlib C ABI (inflate*, deflate*, gz*, etc.) without symbol
+# prefixing.
+LIBZ_RS_SYS_DIR ?= $(call cargo_crate_dir,libz-rs-sys)
+libz_rs_sys-cfgs := feature="export-symbols" feature="c-allocator" feature="std" feature="gz"
+libz_rs_sys-flags := --edition=2021 $(call cfgs-to-flags,$(libz_rs_sys-cfgs))
+libz_rs_sys-envs := $(call cargo_pkg_envs,libz-rs-sys,$(LIBZ_RS_SYS_DIR))
+
+$(LIBZ_RS_OUTPUT)libz_rs.a: private rustc_target_envs = $(libz_rs_sys-envs)
+$(LIBZ_RS_OUTPUT)libz_rs.a: private rustc_target_flags = $(libz_rs_sys-flags)
+$(LIBZ_RS_OUTPUT)libz_rs.a: $(wildcard $(LIBZ_RS_SYS_DIR)/src/lib.rs) $(LIBZ_RS_OUTPUT)libzlib_rs.rlib $(LIBZ_RS_OUTPUT)liblibc.rlib | $(LIBZ_RS_OUTPUT)
+	@$(if $(filter %.rs,$^),$(cmd_rustc_staticlib),false)
+
+# Resolve zlib-rs flags and libraries in priority order:
+#   1. Explicit LIBZ_RS_DIR=/path/to/prefix override
+#   2. pkg-config (libz-rs-sys-cdylib, zlib-rs, or z_rs)
+#   3. Local Cargo crate sources built into $(LIBZ_RS_OUTPUT)libz_rs.a
+ifdef LIBZ_RS_DIR
+  FEATURE_CFLAGS-zlib-rs := -I$(LIBZ_RS_DIR)/include
+  FEATURE_LIBS-zlib-rs := -L$(LIBZ_RS_DIR)/lib -lz_rs
+endif
+
+ifeq ($(FEATURE_LIBS-zlib-rs),)
+  ZLIB_RS_PC := $(shell $(PKG_CONFIG) --exists libz-rs-sys-cdylib 2>/dev/null && echo libz-rs-sys-cdylib || \
+                        ($(PKG_CONFIG) --exists zlib-rs 2>/dev/null && echo zlib-rs) || \
+                        ($(PKG_CONFIG) --exists z_rs 2>/dev/null && echo z_rs))
+  ifneq ($(ZLIB_RS_PC),)
+    FEATURE_CFLAGS-zlib-rs := $(shell $(PKG_CONFIG) --cflags $(ZLIB_RS_PC) 2>/dev/null)
+    FEATURE_LIBS-zlib-rs := $(shell $(PKG_CONFIG) --libs $(ZLIB_RS_PC) 2>/dev/null)
+  endif
+endif
+
+ifeq ($(FEATURE_LIBS-zlib-rs),)
+  ifneq ($(and $(ZLIB_RS_DIR),$(LIBZ_RS_SYS_DIR),$(LIBC_RS_DIR)),)
+    FEATURE_CFLAGS-zlib-rs := -I$(LIBZ_RS_SYS_DIR)/include
+    FEATURE_STATIC-zlib-rs := $(LIBZ_RS_OUTPUT)libz_rs.a
+    FEATURE_LIBS-zlib-rs := $(FEATURE_STATIC-zlib-rs)
+  endif
+endif
+
+zlib-rs-libs: $(FEATURE_STATIC-zlib-rs)
diff --git a/tools/build/feature/.gitignore b/tools/build/feature/.gitignore
index 15fcd34acdb9..2fb933f84bd0 100644
--- a/tools/build/feature/.gitignore
+++ b/tools/build/feature/.gitignore
@@ -2,3 +2,4 @@
 *.d
 *.bin
 *.output
+libz_rs/
diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 10340a537224..ff41e3d3569a 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -50,6 +50,7 @@ FILES=                                          \
          test-compile-32.bin                    \
          test-compile-x32.bin                   \
          test-zlib.bin                          \
+         test-zlib-rs.bin                       \
          test-lzma.bin                          \
          test-bpf.bin                           \
          test-libbpf.bin                        \
@@ -301,6 +302,11 @@ $(OUTPUT)test-compile-x32.bin:
 $(OUTPUT)test-zlib.bin:
 	$(BUILD) -lz
 
+include ../Makefile.rust
+
+$(OUTPUT)test-zlib-rs.bin: $(FEATURE_STATIC-zlib-rs)
+	$(BUILD) $(FEATURE_CFLAGS-zlib-rs) $(FEATURE_LIBS-zlib-rs) -lpthread -ldl -lm
+
 $(OUTPUT)test-lzma.bin:
 	$(BUILD) -llzma
 
@@ -374,4 +380,5 @@ $(OUTPUT)test-rust.bin:
 ###############################
 
 clean:
-	rm -f $(FILES) $(OUTPUT)*.d $(FILES:.bin=.make.output)
+	rm -f $(FILES) $(OUTPUT)*.d $(OUTPUT)*.make.output
+	rm -rf $(LIBZ_RS_OUTPUT)
diff --git a/tools/build/feature/test-zlib-rs.c b/tools/build/feature/test-zlib-rs.c
new file mode 100644
index 000000000000..b8729c5135bb
--- /dev/null
+++ b/tools/build/feature/test-zlib-rs.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <zlib.h>
+
+/*
+ * libz-rs-sys's bundled zlib.h defines ZLIB_VER_SUBREVISION to 15 (0xf) to
+ * distinguish itself from standard C zlib (which defines it to 0). Verify
+ * we are including the zlib-rs header and can link against its zlib C ABI.
+ */
+#if !defined(ZLIB_VER_SUBREVISION) || ZLIB_VER_SUBREVISION != 15
+#error "Not zlib-rs header"
+#endif
+
+int main(void)
+{
+	z_stream zs;
+
+	inflateInit(&zs);
+	return 0;
+}
diff --git a/tools/scripts/Makefile.arch b/tools/scripts/Makefile.arch
index ed5f008d400e..d5f25f3be555 100644
--- a/tools/scripts/Makefile.arch
+++ b/tools/scripts/Makefile.arch
@@ -50,3 +50,17 @@ ifeq ($(LP64), 1)
 else
   IS_64_BIT := 0
 endif
+
+# Map kernel ARCH to rustc --target triples when cross-compiling tools/.
+ifneq ($(CROSS_COMPILE),)
+  RUST_TARGET_FLAGS_arm		:= arm-unknown-linux-gnueabi
+  RUST_TARGET_FLAGS_arm64	:= aarch64-unknown-linux-gnu
+  RUST_TARGET_FLAGS_loongarch	:= loongarch64-unknown-linux-gnu
+  RUST_TARGET_FLAGS_m68k	:= m68k-unknown-linux-gnu
+  RUST_TARGET_FLAGS_mips	:= mipsel-unknown-linux-gnu
+  RUST_TARGET_FLAGS_powerpc	:= powerpc64le-unknown-linux-gnu
+  RUST_TARGET_FLAGS_riscv	:= riscv64gc-unknown-linux-gnu
+  RUST_TARGET_FLAGS_s390	:= s390x-unknown-linux-gnu
+  RUST_TARGET_FLAGS_x86		:= x86_64-unknown-linux-gnu
+  RUST_TARGET_FLAGS_x86_64	:= x86_64-unknown-linux-gnu
+endif
diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
index 378387f62960..72394d245d5d 100644
--- a/tools/scripts/Makefile.include
+++ b/tools/scripts/Makefile.include
@@ -111,6 +111,16 @@ SYSTEM_BPFTOOL	?= bpftool
 
 RUSTC		?= rustc
 
+# Query tools/build/Makefile.rust for a Rust library feature's CFLAGS and LIBS
+# (building any required staticlib such as libz_rs.a under $(RUST_OUTPUT)), and
+# populate FEATURE_CFLAGS-<feat> and FEATURE_LIBS-<feat>.
+define feature_rust_lib_code
+  FEATURE_RUST_OUTPUT := $$(if $$(RUST_OUTPUT),$$(RUST_OUTPUT),$$(abspath $$(or $$(OUTPUT),$$(CURDIR)))/)
+  FEATURE_CFLAGS-$(1) := $$(shell $$(MAKE) -s --no-print-directory -f $$(srctree)/tools/build/Makefile.rust $$(if $$(PKG_CONFIG),PKG_CONFIG="$$(PKG_CONFIG)") LDFLAGS="$$(LDFLAGS)" $(1)-cflags)
+  FEATURE_LIBS-$(1) := $$(shell $$(MAKE) -s --no-print-directory -f $$(srctree)/tools/build/Makefile.rust RUST_OUTPUT="$$(FEATURE_RUST_OUTPUT)" $$(if $$(PKG_CONFIG),PKG_CONFIG="$$(PKG_CONFIG)") RUSTC="$$(RUSTC)" RUST_FLAGS="$$(RUST_FLAGS)" LDFLAGS="$$(LDFLAGS)" $(1)-libs)
+endef
+feature_rust_lib = $(eval $(feature_rust_lib_code))
+
 ifeq ($(CC_NO_CLANG), 1)
 EXTRA_WARNINGS += -Wstrict-aliasing=3
 
-- 
2.56.0.313.g2479322e68-goog


  reply	other threads:[~2026-10-29 16:30 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29 16:29 [RFC PATCH v1 0/3] tools: Add Rust library build infrastructure and prefer zlib-rs Ian Rogers
2026-10-29 16:29 ` Ian Rogers [this message]
2026-10-29 16:57   ` [RFC PATCH v1 1/3] tools build: Add Rust library build infrastructure and zlib-rs feature test sashiko-bot
2026-10-29 16:29 ` [RFC PATCH v1 2/3] perf build: Prefer zlib-rs over zlib when available Ian Rogers
2026-10-29 16:50   ` sashiko-bot
2026-10-29 16:29 ` [RFC PATCH v1 3/3] bpftool: " Ian Rogers
2026-10-29 17:06   ` sashiko-bot
2026-10-30  0:14   ` Quentin Monnet
2026-10-29 17:32 ` [RFC PATCH v1 0/3] tools: Add Rust library build infrastructure and prefer zlib-rs Miguel Ojeda
2026-10-29 18:23   ` Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261029162958.3224280-2-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=justinstitt@google.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nsc@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=qmo@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.