nuttx-apps/tools/Rust.mk
Huang Qi efcfd87b44 tools/Rust: Add support for panic_immediate_abort
Summary:
- Added support for `panic_immediate_abort` in Rust builds, which causes the system to abort immediately on panic instead of unwinding the stack
- Enabled `-Zbuild-std-features=panic_immediate_abort` flag for release builds when `CONFIG_DEBUG_FULLOPT` is set
- Updated both CMake and Makefile build systems to include the new flag

Impact:
- Significantly reduces binary size (e.g., from 2270605 to 84987 bytes for riscv64imc)
- Changes panic behavior to immediate abort, which may be preferred for embedded systems
- Improves system reliability by preventing undefined behavior from stack unwinding in constrained environments
- Maintains compatibility with existing Rust code while providing a more deterministic panic handling mechanism

For example, if it is enabled, the system will panic immediately:
```
NuttShell (NSH) NuttX-12.8.0
nsh> hello_rust_cargo
{"name":"John","age":30}
{"name":"Jane","age":25}
Deserialized: Alice is 28 years old
Pretty JSON:
{
  "name": "Alice",
  "age": 28
}
riscv_exception: EXCEPTION: Illegal instruction. MCAUSE: 0000000000000002, EPC: 0000000080027df6, MTVAL: 0000000000000000
riscv_exception: PANIC!!! Exception = 0000000000000002
dump_assert_info: Current Version: NuttX  12.8.0 8e3621e059 Jan 20 2025 14:45:00 risc-v
dump_assert_info: Assertion failed panic: at file: :0 task: hello_rust_cargo process: hello_rust_cargo 0x80020588
/* Stack dump from NuttX */
```
vs the default behavior:
```
NuttShell (NSH) NuttX-12.8.0
nsh> hello_rust_cargo
{"name":"John","age":30}
{"name":"Jane","age":25}
Deserialized: Alice is 28 years old
Pretty JSON:
{
  "name": "Alice",
  "age": 28
}

thread '<unnamed>' panicked at /home/huang/Work/rust/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/src/rust/library/std/src/sys/random/unix_legacy.rs:19:10:
failed to generate random data: Os { code: 2, kind: NotFound, message: "No such file or directory" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
nsh>
```

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2025-01-20 19:48:25 +08:00

126 lines
4.2 KiB
Makefile

############################################################################
# apps/tools/Rust.mk
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
# Generate Rust target triple based on LLVM architecture configuration
#
# Uses the following LLVM variables directly:
# - LLVM_ARCHTYPE: Architecture type (e.g. thumbv7m, riscv32)
# - LLVM_ABITYPE: ABI type (e.g. eabi, eabihf)
# - LLVM_CPUTYPE: CPU type (e.g. cortex-m23, sifive-e20)
#
# Supported architectures and their target triples:
# - armv7a: armv7a-nuttx-eabi, armv7a-nuttx-eabihf
# - thumbv6m: thumbv6m-nuttx-eabi
# - thumbv7a: thumbv7a-nuttx-eabi, thumbv7a-nuttx-eabihf
# - thumbv7m: thumbv7m-nuttx-eabi
# - thumbv7em: thumbv7em-nuttx-eabihf
# - thumbv8m.main: thumbv8m.main-nuttx-eabi, thumbv8m.main-nuttx-eabihf
# - thumbv8m.base: thumbv8m.base-nuttx-eabi, thumbv8m.base-nuttx-eabihf
# - riscv32: riscv32imc/imac/imafc-unknown-nuttx-elf
# - riscv64: riscv64imac/imafdc-unknown-nuttx-elf
#
# Usage: $(call RUST_TARGET_TRIPLE)
#
# Output:
# Rust target triple (e.g. riscv32imac-unknown-nuttx-elf,
# thumbv7m-nuttx-eabi, thumbv7em-nuttx-eabihf)
define RUST_TARGET_TRIPLE
$(or \
$(and $(filter thumb%,$(LLVM_ARCHTYPE)), \
$(if $(filter thumbv8m%,$(LLVM_ARCHTYPE)), \
$(if $(filter cortex-m23,$(LLVM_CPUTYPE)),thumbv8m.base,thumbv8m.main)-nuttx-$(LLVM_ABITYPE), \
$(LLVM_ARCHTYPE)-nuttx-$(LLVM_ABITYPE) \
) \
), \
$(and $(filter riscv32,$(LLVM_ARCHTYPE)), \
riscv32$(or \
$(and $(filter sifive-e20,$(LLVM_CPUTYPE)),imc), \
$(and $(filter sifive-e31,$(LLVM_CPUTYPE)),imac), \
$(and $(filter sifive-e76,$(LLVM_CPUTYPE)),imafc), \
imc \
)-unknown-nuttx-elf \
), \
$(and $(filter riscv64,$(LLVM_ARCHTYPE)), \
riscv64$(or \
$(and $(filter sifive-s51,$(LLVM_CPUTYPE)),imac), \
$(and $(filter sifive-u54,$(LLVM_CPUTYPE)),imafdc), \
imac \
)-unknown-nuttx-elf \
) \
)
endef
# Build Rust project using cargo
#
# Usage: $(call RUST_CARGO_BUILD,cratename,prefix)
#
# Inputs:
# cratename - Name of the Rust crate (e.g. hello)
# prefix - Path prefix to the crate (e.g. path/to/project)
#
# Output:
# None, builds the Rust project
ifeq ($(CONFIG_DEBUG_FULLOPT),y)
define RUST_CARGO_BUILD
cargo build --release -Zbuild-std=std,panic_abort \
-Zbuild-std-features=panic_immediate_abort \
--manifest-path $(2)/$(1)/Cargo.toml \
--target $(call RUST_TARGET_TRIPLE)
endef
else
define RUST_CARGO_BUILD
@echo "Building Rust code with cargo..."
cargo build -Zbuild-std=std,panic_abort \
--manifest-path $(2)/$(1)/Cargo.toml \
--target $(call RUST_TARGET_TRIPLE)
endef
endif
# Clean Rust project using cargo
#
# Usage: $(call RUST_CARGO_CLEAN,cratename,prefix)
#
# Inputs:
# cratename - Name of the Rust crate (e.g. hello)
# prefix - Path prefix to the crate (e.g. path/to/project)
#
# Output:
# None, cleans the Rust project
define RUST_CARGO_CLEAN
cargo clean --manifest-path $(2)/$(1)/Cargo.toml
endef
# Get Rust binary path for given crate and path prefix
#
# Usage: $(call RUST_GET_BINDIR,cratename,prefix)
#
# Inputs:
# cratename - Name of the Rust crate (e.g. hello)
# prefix - Path prefix to the crate (e.g. path/to/project)
#
# Output:
# Path to the Rust binary (e.g. path/to/project/target/riscv32imac-unknown-nuttx-elf/release/libhello.a)
define RUST_GET_BINDIR
$(2)/$(1)/target/$(strip $(call RUST_TARGET_TRIPLE))/$(if $(CONFIG_DEBUG_FULLOPT),release,debug)/lib$(1).a
endef