Skip to content

Commit f7e7453

Browse files
no1wudixiaoxiang781216
authored andcommittedJan 10, 2025
examples: New app to build Rust with Cargo
Build Rust applictions with cargo is the most commn way, and it's more easy to cooporate with Rust ecosystem. This example shows how to use cargo to build a simple hello world application. And please notice that you need to install nighly version of rustc to support this feature, any version after rust-lang/rust#127755 is merged, can use NuttX as cargo target directly. Build ----- To build hello_rust_cargo application, you can use any target that based on RISCV32IMAC, for example: ``` cmake -B build -DBOARD_CONFIG=rv-virt:nsh -GNinja . ``` And disable ARCH_FPU in menuconfig, since the hard coded target triple in this demo is `riscv32imac`. Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
1 parent ffd256d commit f7e7453

14 files changed

+565
-0
lines changed
 

‎CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ if(CONFIG_APPS_DIR)
3333
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
3434
include(nuttx_add_luamod)
3535
include(nuttx_add_wamrmod)
36+
include(nuttx_add_rust)
3637
nuttx_add_library(apps)
3738
if(NOT EXISTS {NUTTX_APPS_BINDIR}/dummy.c)
3839
file(TOUCH ${NUTTX_APPS_BINDIR}/dummy.c)

‎cmake/nuttx_add_rust.cmake

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# ##############################################################################
2+
# cmake/nuttx_add_rust.cmake
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
include(nuttx_parse_function_args)
22+
23+
# ~~~
24+
# Convert architecture type to Rust NuttX target
25+
#
26+
# Supported architectures:
27+
# - armv7a: armv7a-nuttx-eabi, armv7a-nuttx-eabihf
28+
# - thumbv6m: thumbv6m-nuttx-eabi
29+
# - thumbv7a: thumbv7a-nuttx-eabi, thumbv7a-nuttx-eabihf
30+
# - thumbv7m: thumbv7m-nuttx-eabi
31+
# - thumbv7em: thumbv7em-nuttx-eabihf
32+
# - thumbv8m.main: thumbv8m.main-nuttx-eabi, thumbv8m.main-nuttx-eabihf
33+
# - thumbv8m.base: thumbv8m.base-nuttx-eabi, thumbv8m.base-nuttx-eabihf
34+
# - riscv32: riscv32imc/imac/imafc-unknown-nuttx-elf
35+
# - riscv64: riscv64imac/imafdc-unknown-nuttx-elf
36+
#
37+
# Inputs:
38+
# ARCHTYPE - Architecture type (e.g. thumbv7m, riscv32)
39+
# ABITYPE - ABI type (e.g. eabi, eabihf)
40+
# CPUTYPE - CPU type (e.g. cortex-m4, sifive-e20)
41+
#
42+
# Output:
43+
# OUTPUT - Rust target triple (e.g. riscv32imac-unknown-nuttx-elf,
44+
# thumbv7m-nuttx-eabi, thumbv7em-nuttx-eabihf)
45+
# ~~~
46+
47+
function(nuttx_rust_target_triple ARCHTYPE ABITYPE CPUTYPE OUTPUT)
48+
if(ARCHTYPE MATCHES "thumb")
49+
if(ARCHTYPE MATCHES "thumbv8m")
50+
# Extract just the base architecture type (thumbv8m.main or thumbv8m.base)
51+
if(ARCHTYPE MATCHES "thumbv8m.main")
52+
set(ARCH_BASE "thumbv8m.main")
53+
elseif(ARCHTYPE MATCHES "thumbv8m.base")
54+
set(ARCH_BASE "thumbv8m.base")
55+
else()
56+
# Otherwise determine if we should use thumbv8m.main or thumbv8m.base
57+
# based on CPU type
58+
if(CPUTYPE MATCHES "cortex-m23")
59+
set(ARCH_BASE "thumbv8m.base")
60+
else()
61+
set(ARCH_BASE "thumbv8m.main")
62+
endif()
63+
endif()
64+
set(TARGET_TRIPLE "${ARCH_BASE}-nuttx-${ABITYPE}")
65+
else()
66+
set(TARGET_TRIPLE "${ARCHTYPE}-nuttx-${ABITYPE}")
67+
endif()
68+
elseif(ARCHTYPE STREQUAL "riscv32")
69+
if(CPUTYPE STREQUAL "sifive-e20")
70+
set(TARGET_TRIPLE "riscv32imc-unknown-nuttx-elf")
71+
elseif(CPUTYPE STREQUAL "sifive-e31")
72+
set(TARGET_TRIPLE "riscv32imac-unknown-nuttx-elf")
73+
elseif(CPUTYPE STREQUAL "sifive-e76")
74+
set(TARGET_TRIPLE "riscv32imafc-unknown-nuttx-elf")
75+
else()
76+
set(TARGET_TRIPLE "riscv32imc-unknown-nuttx-elf")
77+
endif()
78+
elseif(ARCHTYPE STREQUAL "riscv64")
79+
if(CPUTYPE STREQUAL "sifive-s51")
80+
set(TARGET_TRIPLE "riscv64imac-unknown-nuttx-elf")
81+
elseif(CPUTYPE STREQUAL "sifive-u54")
82+
set(TARGET_TRIPLE "riscv64imafdc-unknown-nuttx-elf")
83+
else()
84+
set(TARGET_TRIPLE "riscv64imac-unknown-nuttx-elf")
85+
endif()
86+
endif()
87+
set(${OUTPUT}
88+
${TARGET_TRIPLE}
89+
PARENT_SCOPE)
90+
endfunction()
91+
92+
# ~~~
93+
# nuttx_add_rust
94+
#
95+
# Description:
96+
# Build a Rust crate and add it as a static library to the NuttX build system
97+
#
98+
# Example:
99+
# nuttx_add_rust(
100+
# CRATE_NAME
101+
# hello
102+
# CRATE_PATH
103+
# ${CMAKE_CURRENT_SOURCE_DIR}/hello
104+
# )
105+
# ~~~
106+
107+
function(nuttx_add_rust)
108+
109+
# parse arguments into variables
110+
nuttx_parse_function_args(
111+
FUNC
112+
nuttx_add_rust
113+
ONE_VALUE
114+
CRATE_NAME
115+
CRATE_PATH
116+
REQUIRED
117+
CRATE_NAME
118+
CRATE_PATH
119+
ARGN
120+
${ARGN})
121+
122+
# Determine build profile based on CONFIG_DEBUG_FULLOPT
123+
if(CONFIG_DEBUG_FULLOPT)
124+
set(RUST_PROFILE "release")
125+
else()
126+
set(RUST_PROFILE "debug")
127+
endif()
128+
129+
# Get the Rust target triple
130+
nuttx_rust_target_triple(${LLVM_ARCHTYPE} ${LLVM_ABITYPE} ${LLVM_CPUTYPE}
131+
RUST_TARGET)
132+
133+
# Set up build directory in current binary dir
134+
set(RUST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CRATE_NAME})
135+
set(RUST_LIB_PATH
136+
${RUST_BUILD_DIR}/${RUST_TARGET}/${RUST_PROFILE}/lib${CRATE_NAME}.a)
137+
138+
# Create build directory
139+
file(MAKE_DIRECTORY ${RUST_BUILD_DIR})
140+
141+
# Add a custom command to build the Rust crate
142+
add_custom_command(
143+
OUTPUT ${RUST_LIB_PATH}
144+
COMMAND
145+
cargo build --${RUST_PROFILE} -Zbuild-std=std,panic_abort --manifest-path
146+
${CRATE_PATH}/Cargo.toml --target ${RUST_TARGET} --target-dir
147+
${RUST_BUILD_DIR}
148+
COMMENT "Building Rust crate ${CRATE_NAME}"
149+
VERBATIM)
150+
151+
# Add a custom target that depends on the built library
152+
add_custom_target(${CRATE_NAME}_build ALL DEPENDS ${RUST_LIB_PATH})
153+
154+
# Add imported library target
155+
add_library(${CRATE_NAME} STATIC IMPORTED GLOBAL)
156+
set_target_properties(${CRATE_NAME} PROPERTIES IMPORTED_LOCATION
157+
${RUST_LIB_PATH})
158+
159+
# Add the Rust library to NuttX build
160+
nuttx_add_extra_library(${RUST_LIB_PATH})
161+
162+
# Ensure the Rust library is built before linking
163+
add_dependencies(${CRATE_NAME} ${CRATE_NAME}_build)
164+
165+
endfunction()

‎examples/rust/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Kconfig

‎examples/rust/CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ##############################################################################
2+
# apps/examples/rust/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
nuttx_add_subdirectory()
24+
nuttx_generate_kconfig(MENUDESC "Rust Examples")

‎examples/rust/Make.defs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/rust/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
include $(wildcard $(APPDIR)/examples/rust/*/Make.defs)

‎examples/rust/Makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
############################################################################
2+
# apps/examples/rust/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
MENUDESC = "Rust Examples"
24+
25+
include $(APPDIR)/Directory.mk

‎examples/rust/hello/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

‎examples/rust/hello/CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ##############################################################################
2+
# apps/examples/rust/hello/CMakeLists.txt
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
if(CONFIG_EXAMPLES_HELLO_RUST_CARGO)
22+
23+
# Build the Rust crate using nuttx_add_rust
24+
nuttx_add_rust(CRATE_NAME hello CRATE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
25+
26+
nuttx_add_application(
27+
NAME ${CONFIG_EXAMPLES_HELLO_RUST_CARGO_PROGNAME} STACKSIZE
28+
${CONFIG_EXAMPLES_HELLO_STACKSIZE} PRIORITY
29+
${CONFIG_EXAMPLES_HELLO_PRIORITY})
30+
31+
endif() # CONFIG_EXAMPLES_HELLO_RUST_CARGO

‎examples/rust/hello/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "hello"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["staticlib"]
8+
9+
[profile.dev]
10+
panic = "abort"
11+
12+
# Special hanlding for the panic! macro, can be removed once
13+
# the libstd port for NuttX is complete.
14+
[profile.release]
15+
panic = "abort"
16+
lto = true
17+
18+
[dependencies]
19+
serde = { version = "1.0", features = ["derive"] }
20+
serde_json = "1.0"
21+
22+
tokio = { version = "1", features = ["rt"] }

‎examples/rust/hello/Kconfig

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_HELLO_RUST_CARGO
7+
tristate "\"Hello, Rust!\" example with Cargo"
8+
default n
9+
---help---
10+
Enable the \"Hello, Rust!\" example using Cargo to build.
11+
12+
if EXAMPLES_HELLO_RUST_CARGO
13+
14+
config EXAMPLES_HELLO_RUST_CARGO_PROGNAME
15+
string "Program name"
16+
default "hello_rust_cargo"
17+
---help---
18+
This is the name of the program that will be used when the
19+
program is installed.
20+
21+
config EXAMPLES_HELLO_RUST_CARGO_PRIORITY
22+
int "Hello Rust task priority"
23+
default 100
24+
25+
config EXAMPLES_HELLO_RUST_CARGO_STACKSIZE
26+
int "Hello Rust stack size"
27+
default DEFAULT_TASK_STACKSIZE
28+
29+
endif

‎examples/rust/hello/Make.defs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
############################################################################
2+
# apps/examples/hello_rust_cargo/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(APPDIR)/tools/Rust.mk
22+
23+
ifneq ($(CONFIG_EXAMPLES_HELLO_RUST_CARGO),)
24+
CONFIGURED_APPS += $(APPDIR)/examples/rust/hello
25+
EXTRA_LIBS += $(call RUST_GET_BINDIR,hello,$(APPDIR)/examples/rust)
26+
endif

‎examples/rust/hello/Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
############################################################################
2+
# apps/examples/rust/hello/Makefile
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(APPDIR)/Make.defs
22+
23+
# Hello, Rust! built-in application info
24+
25+
PROGNAME = $(CONFIG_EXAMPLES_HELLO_RUST_CARGO_PROGNAME)
26+
PRIORITY = $(CONFIG_EXAMPLES_HELLO_RUST_CARGO_PRIORITY)
27+
STACKSIZE = $(CONFIG_EXAMPLES_HELLO_RUST_CARGO_STACKSIZE)
28+
MODULE = $(CONFIG_EXAMPLES_HELLO_RUST_CARGO)
29+
30+
context::
31+
$(call RUST_CARGO_BUILD,hello,$(APPDIR)/examples/rust)
32+
33+
clean::
34+
$(call RUST_CARGO_CLEAN,hello,$(APPDIR)/examples/rust)
35+
36+
include $(APPDIR)/Application.mk

‎examples/rust/hello/src/lib.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
extern crate serde;
2+
extern crate serde_json;
3+
4+
use serde::{Deserialize, Serialize};
5+
6+
#[derive(Serialize, Deserialize)]
7+
struct Person {
8+
name: String,
9+
age: u8,
10+
}
11+
12+
// Function hello_rust_cargo without manglng
13+
#[no_mangle]
14+
pub extern "C" fn hello_rust_cargo_main() {
15+
// Print hello world to stdout
16+
17+
let john = Person {
18+
name: "John".to_string(),
19+
age: 30,
20+
};
21+
22+
let json_str = serde_json::to_string(&john).unwrap();
23+
println!("{}", json_str);
24+
25+
let jane = Person {
26+
name: "Jane".to_string(),
27+
age: 25,
28+
};
29+
30+
let json_str_jane = serde_json::to_string(&jane).unwrap();
31+
println!("{}", json_str_jane);
32+
33+
let json_data = r#"
34+
{
35+
"name": "Alice",
36+
"age": 28
37+
}"#;
38+
39+
let alice: Person = serde_json::from_str(json_data).unwrap();
40+
println!("Deserialized: {} is {} years old", alice.name, alice.age);
41+
42+
let pretty_json_str = serde_json::to_string_pretty(&alice).unwrap();
43+
println!("Pretty JSON:\n{}", pretty_json_str);
44+
45+
tokio::runtime::Builder::new_current_thread()
46+
.enable_all()
47+
.build()
48+
.unwrap()
49+
.block_on(async {
50+
println!("Hello world from tokio!");
51+
});
52+
53+
loop {
54+
// Do nothing
55+
}
56+
}

‎tools/Rust.mk

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
############################################################################
2+
# apps/tools/Rust.mk
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
# Generate Rust target triple based on LLVM architecture configuration
22+
#
23+
# Uses the following LLVM variables directly:
24+
# - LLVM_ARCHTYPE: Architecture type (e.g. thumbv7m, riscv32)
25+
# - LLVM_ABITYPE: ABI type (e.g. eabi, eabihf)
26+
# - LLVM_CPUTYPE: CPU type (e.g. cortex-m23, sifive-e20)
27+
#
28+
# Supported architectures and their target triples:
29+
# - armv7a: armv7a-nuttx-eabi, armv7a-nuttx-eabihf
30+
# - thumbv6m: thumbv6m-nuttx-eabi
31+
# - thumbv7a: thumbv7a-nuttx-eabi, thumbv7a-nuttx-eabihf
32+
# - thumbv7m: thumbv7m-nuttx-eabi
33+
# - thumbv7em: thumbv7em-nuttx-eabihf
34+
# - thumbv8m.main: thumbv8m.main-nuttx-eabi, thumbv8m.main-nuttx-eabihf
35+
# - thumbv8m.base: thumbv8m.base-nuttx-eabi, thumbv8m.base-nuttx-eabihf
36+
# - riscv32: riscv32imc/imac/imafc-unknown-nuttx-elf
37+
# - riscv64: riscv64imac/imafdc-unknown-nuttx-elf
38+
#
39+
# Usage: $(call RUST_TARGET_TRIPLE)
40+
#
41+
# Output:
42+
# Rust target triple (e.g. riscv32imac-unknown-nuttx-elf,
43+
# thumbv7m-nuttx-eabi, thumbv7em-nuttx-eabihf)
44+
45+
define RUST_TARGET_TRIPLE
46+
$(or \
47+
$(and $(filter thumb%,$(LLVM_ARCHTYPE)), \
48+
$(if $(filter thumbv8m%,$(LLVM_ARCHTYPE)), \
49+
$(if $(filter cortex-m23,$(LLVM_CPUTYPE)),thumbv8m.base,thumbv8m.main)-nuttx-$(LLVM_ABITYPE), \
50+
$(LLVM_ARCHTYPE)-nuttx-$(LLVM_ABITYPE) \
51+
) \
52+
), \
53+
$(and $(filter riscv32,$(LLVM_ARCHTYPE)), \
54+
riscv32$(or \
55+
$(and $(filter sifive-e20,$(LLVM_CPUTYPE)),imc), \
56+
$(and $(filter sifive-e31,$(LLVM_CPUTYPE)),imac), \
57+
$(and $(filter sifive-e76,$(LLVM_CPUTYPE)),imafc), \
58+
imc \
59+
)-unknown-nuttx-elf \
60+
), \
61+
$(and $(filter riscv64,$(LLVM_ARCHTYPE)), \
62+
riscv64$(or \
63+
$(and $(filter sifive-s51,$(LLVM_CPUTYPE)),imac), \
64+
$(and $(filter sifive-u54,$(LLVM_CPUTYPE)),imafdc), \
65+
imac \
66+
)-unknown-nuttx-elf \
67+
) \
68+
)
69+
endef
70+
71+
# Build Rust project using cargo
72+
#
73+
# Usage: $(call RUST_CARGO_BUILD,cratename,prefix)
74+
#
75+
# Inputs:
76+
# cratename - Name of the Rust crate (e.g. hello)
77+
# prefix - Path prefix to the crate (e.g. path/to/project)
78+
#
79+
# Output:
80+
# None, builds the Rust project
81+
82+
ifeq ($(CONFIG_DEBUG_FULLOPT),y)
83+
define RUST_CARGO_BUILD
84+
cargo build --release -Zbuild-std=std,panic_abort \
85+
--manifest-path $(2)/$(1)/Cargo.toml \
86+
--target $(call RUST_TARGET_TRIPLE)
87+
endef
88+
else
89+
define RUST_CARGO_BUILD
90+
@echo "Building Rust code with cargo..."
91+
cargo build -Zbuild-std=std,panic_abort \
92+
--manifest-path $(2)/$(1)/Cargo.toml \
93+
--target $(call RUST_TARGET_TRIPLE)
94+
endef
95+
endif
96+
97+
# Clean Rust project using cargo
98+
#
99+
# Usage: $(call RUST_CARGO_CLEAN,cratename,prefix)
100+
#
101+
# Inputs:
102+
# cratename - Name of the Rust crate (e.g. hello)
103+
# prefix - Path prefix to the crate (e.g. path/to/project)
104+
#
105+
# Output:
106+
# None, cleans the Rust project
107+
108+
define RUST_CARGO_CLEAN
109+
cargo clean --manifest-path $(2)/$(1)/Cargo.toml
110+
endef
111+
112+
# Get Rust binary path for given crate and path prefix
113+
#
114+
# Usage: $(call RUST_GET_BINDIR,cratename,prefix)
115+
#
116+
# Inputs:
117+
# cratename - Name of the Rust crate (e.g. hello)
118+
# prefix - Path prefix to the crate (e.g. path/to/project)
119+
#
120+
# Output:
121+
# Path to the Rust binary (e.g. path/to/project/target/riscv32imac-unknown-nuttx-elf/release/libhello.a)
122+
123+
define RUST_GET_BINDIR
124+
$(2)/$(1)/target/$(strip $(call RUST_TARGET_TRIPLE))/$(if $(CONFIG_DEBUG_FULLOPT),release,debug)/lib$(1).a
125+
endef

0 commit comments

Comments
 (0)
Please sign in to comment.