From patchwork Fri Feb 20 00:19:12 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1906 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1771546801; bh=KhQjKx6OBUUo/E8plJwn2sW1VCwudyMItBfACuiCCUs=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=O1K14rXNO9ggAmX2nfLZvvyecerK/dls3Q61GM4aZ9ZA38DQlH91U8zVmu6zzmxpt WwdJkrgGbvssZZj2L/FYpNq+XPnyA6lcPV0ig1pLedSwiBXaVRsrUQwCRjWsOk7pbD OiBiTomOFCSP5miYXIjAzw+hJ7ah+7gZQf66QgJ9SoWWhfeuKd+NCW4yr+p5Msxow4 NTr9pzdQFBcPpXMg+1L61C807BIODWWcO6XzMUa5MWIgbiZyQmBhF8V0/Ps0lXZY+D Jj6uzapH6ZrD9qlECqg/H7f+9PklfPw8JTdw1xEn0mGv+mTMzkzMVlIh6cZdl5nb6c tNF3MAvIa6k6Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 74FBB69C8F for ; Thu, 19 Feb 2026 17:20:01 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id f8g3QsjYSuKC for ; Thu, 19 Feb 2026 17:20:01 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1771546801; bh=KhQjKx6OBUUo/E8plJwn2sW1VCwudyMItBfACuiCCUs=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=O1K14rXNO9ggAmX2nfLZvvyecerK/dls3Q61GM4aZ9ZA38DQlH91U8zVmu6zzmxpt WwdJkrgGbvssZZj2L/FYpNq+XPnyA6lcPV0ig1pLedSwiBXaVRsrUQwCRjWsOk7pbD OiBiTomOFCSP5miYXIjAzw+hJ7ah+7gZQf66QgJ9SoWWhfeuKd+NCW4yr+p5Msxow4 NTr9pzdQFBcPpXMg+1L61C807BIODWWcO6XzMUa5MWIgbiZyQmBhF8V0/Ps0lXZY+D Jj6uzapH6ZrD9qlECqg/H7f+9PklfPw8JTdw1xEn0mGv+mTMzkzMVlIh6cZdl5nb6c tNF3MAvIa6k6Q== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 6330169C0B for ; Thu, 19 Feb 2026 17:20:01 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1771546800; bh=PDNhFFaRxGWEZLE0DCxDupMb9cylnfZg8oGGE7gNF08=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wp4TNWGx6Y1rGKp94eIFGyVbe8RCU8tSS/h824fsfRsyoqoLWtPUKszheNkQ/46eZ Vol0tmXPQosRnuYq3AAOzh4FI4tS5emZt7Uxv79fY3n9Su5pp5tyM17hER5xw+g8Dg +eteXbzRD1SOW5L112F4C0FZrObzaB2SDO/F+Xlub1XCfpllizyfesLxHS6LiZ4qGE RrLS5dxlQQqZiHSDVbqh6KjmmsVqzZw6+Bt1soZKxkMn2ZjcaHZugOOUHqM6N8AC7V s5rp7ax62G1of0oAwVC169JqrEyYrFqPHT37/fkGIyisaw0NP+9dHzeQ0mlFAUeG/7 YyCN5NxlaHkYQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 639BD69C0B; Thu, 19 Feb 2026 17:20:00 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id O6qjCdTnnwWA; Thu, 19 Feb 2026 17:20:00 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1771546796; bh=AwVIMGtg8hFmilJVQkSmePWpYL1KjGDNKPI7tXxikOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=onRXqKQZ3hJO6S+2wSiX3pURPZfabf5y000PTuw+ucv79uokRmrwaAtbR3PvlXlJo sAEEBtXnziy/95KR0kVpuAPPWQjFOBuP1pqjre++PPLwVqJ0kC8SI/93GVGBDJ7qT2 QNb/ro9Km9EXBvIcQ0tyluSXztQBZoc+VA12/X71kXYQ78lfa/F1zyr2oB+bwqG5/O WwArol+uM7s9KLecIYZlojuxeDk7x/yk2VWU2yLmnZIBBRMpKDw9q652T2po6GJOv2 7WJpVshMZ3dTV1TabhPB1WHCA8qveQNSFHk4FHh8nwFlqoZzJWuKRvdMTowjBzOY4f zMiZNgDMEnydg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id DCC6D69C7C; Thu, 19 Feb 2026 17:19:55 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Thu, 19 Feb 2026 17:19:12 -0700 Message-ID: <20260220001926.2366140-5-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260220001926.2366140-1-sjg@u-boot.org> References: <20260220001926.2366140-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: GOL6URKMCZHJ2WCGCDWGMQ4IGCHOIFZE X-Message-ID-Hash: GOL6URKMCZHJ2WCGCDWGMQ4IGCHOIFZE X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass , "Claude Opus 4 . 6" X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 04/13] ulib: Add Rust demo example List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add a #![no_std] Rust equivalent of the C ulib demo, compiled to an object file with rustc and linked into U-Boot via the existing u-boot-link mechanism. The Rust demo calls the same C helpers (demo_show_banner, demo_show_footer, demo_add_numbers) via FFI and produces identical output, so assert_demo_output() works unchanged. Use core::ptr::addr_of!() rather than a reference cast to access the extern static version_string, since the compiler cannot prove that an extern static is non-null and would emit a call to an undefined panic symbol. Add CONFIG_RUST_EXAMPLES Kconfig option to gate the Rust examples. When enabled, rustc must be available with the appropriate target for the architecture. Clear MAKEFLAGS when invoking rustc so that it does not inherit make's --jobserver-auth file descriptors, which would produce a spurious warning on every compilation. Co-developed-by: Claude Opus 4.6 Signed-off-by: Simon Glass --- examples/Kconfig | 13 +++++++++ examples/ulib/rust_demo.rs | 55 +++++++++++++++++++++++++++++++++++ scripts/Makefile.ulib-example | 17 +++++++++++ 3 files changed, 85 insertions(+) create mode 100644 examples/ulib/rust_demo.rs diff --git a/examples/Kconfig b/examples/Kconfig index 5738d555d22..7f099808908 100644 --- a/examples/Kconfig +++ b/examples/Kconfig @@ -9,6 +9,19 @@ config EXAMPLES U-Boot provides an legacy API for standalone applications. Examples are provided in directory examples/. +config RUST_EXAMPLES + bool "Build Rust example programs" + depends on EXAMPLES + help + Build example programs written in Rust alongside the C + examples. The Rust demo calls the same C helpers via FFI + and produces identical output. + + Requires a Rust toolchain (rustc) with the appropriate + cross-compilation target for the architecture, e.g. + x86_64-unknown-none for x86_64 or aarch64-unknown-none + for ARM64. + config EXAMPLES_STANDALONE bool "Compile standalone examples" depends on !SANDBOX diff --git a/examples/ulib/rust_demo.rs b/examples/ulib/rust_demo.rs new file mode 100644 index 00000000000..7a85658520d --- /dev/null +++ b/examples/ulib/rust_demo.rs @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0+ +// +// Rust demo program showing U-Boot library functionality +// +// Demonstrates calling C helper functions from Rust via FFI, producing +// identical output to demo.c so assert_demo_output() works unchanged. +// +// Copyright 2026 Canonical Ltd. +// Written by Simon Glass + +#![no_std] +#![no_main] + +use core::ffi::c_int; + +extern "C" { + fn printf(fmt: *const u8, ...) -> c_int; + fn demo_show_banner(); + fn demo_show_footer(); + fn demo_add_numbers(a: c_int, b: c_int) -> c_int; + static version_string: u8; +} + +#[no_mangle] +pub extern "C" fn ulib_has_main() -> bool { + true +} + +fn demo_run() -> c_int { + unsafe { + demo_show_banner(); + // Use addr_of!() rather than &version_string to avoid a + // null-pointer check: &T must be non-null, but the compiler + // cannot prove that for an extern static, so it emits a call + // to an undefined panic symbol that crashes ld.bfd on aarch64. + printf( + b"U-Boot version: %s\n\0".as_ptr(), + core::ptr::addr_of!(version_string), + ); + printf(b"\n\0".as_ptr()); + demo_add_numbers(42, 13); + demo_show_footer(); + } + 0 +} + +#[no_mangle] +pub extern "C" fn main() -> c_int { + demo_run() +} + +#[panic_handler] +fn panic(_: &core::panic::PanicInfo) -> ! { + loop {} +} diff --git a/scripts/Makefile.ulib-example b/scripts/Makefile.ulib-example index 796b74b0c14..18d526f5b6b 100644 --- a/scripts/Makefile.ulib-example +++ b/scripts/Makefile.ulib-example @@ -19,11 +19,28 @@ PHONY += examples_$(EXAMPLE_ARCH) ULIB_EXAMPLES := demo +# --- Rust examples --- +RUSTC := rustc +RUST_TARGET := $(RUST_TARGET_$(EXAMPLE_ARCH)) + +ifeq ($(CONFIG_RUST_EXAMPLES),y) +ULIB_EXAMPLES += rust-demo +endif + quiet_cmd_u-boot-example = LD $@ cmd_u-boot-example = $(call u-boot-link,$(example-objs),$@.map) +quiet_cmd_rustc_obj = RUSTC $@ + cmd_rustc_obj = \ + mkdir -p $(dir $@) && \ + MAKEFLAGS= $(RUSTC) --edition 2021 --emit=obj -o $@ --target=$(RUST_TARGET) $< + +examples/ulib/rust_demo.o: examples/ulib/rust_demo.rs FORCE + $(call if_changed,rustc_obj) + # Per-example object lists (matches examples/ulib/Kbuild) example-demo-objs := examples/ulib/demo.o examples/ulib/demo_helper.o +example-rust-demo-objs := examples/ulib/rust_demo.o examples/ulib/demo_helper.o # Generate link rule for each example define example_link_rule