Skip to content

Commit 39e8b76

Browse files
committed
Migrate rust related PR from apache/nuttx-app
1 parent b8a6656 commit 39e8b76

26 files changed

+992
-5
lines changed

cmake/nuttx_add_rust.cmake

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
# - x86: i686-unknown-nuttx
37+
# - x86_64: x86_64-unknown-nuttx
38+
#
39+
# Inputs:
40+
# ARCHTYPE - Architecture type (e.g. thumbv7m, riscv32)
41+
# ABITYPE - ABI type (e.g. eabi, eabihf)
42+
# CPUTYPE - CPU type (e.g. cortex-m4, sifive-e20)
43+
#
44+
# Output:
45+
# OUTPUT - Rust target triple (e.g. riscv32imac-unknown-nuttx-elf,
46+
# thumbv7m-nuttx-eabi, thumbv7em-nuttx-eabihf)
47+
# ~~~
48+
49+
function(nuttx_rust_target_triple ARCHTYPE ABITYPE CPUTYPE OUTPUT)
50+
if(ARCHTYPE STREQUAL "x86_64")
51+
set(TARGET_TRIPLE "x86_64-unknown-nuttx")
52+
elseif(ARCHTYPE STREQUAL "x86")
53+
set(TARGET_TRIPLE "i686-unknown-nuttx")
54+
elseif(ARCHTYPE MATCHES "thumb")
55+
if(ARCHTYPE MATCHES "thumbv8m")
56+
# Extract just the base architecture type (thumbv8m.main or thumbv8m.base)
57+
if(ARCHTYPE MATCHES "thumbv8m.main")
58+
set(ARCH_BASE "thumbv8m.main")
59+
elseif(ARCHTYPE MATCHES "thumbv8m.base")
60+
set(ARCH_BASE "thumbv8m.base")
61+
else()
62+
# Otherwise determine if we should use thumbv8m.main or thumbv8m.base
63+
# based on CPU type
64+
if(CPUTYPE MATCHES "cortex-m23")
65+
set(ARCH_BASE "thumbv8m.base")
66+
else()
67+
set(ARCH_BASE "thumbv8m.main")
68+
endif()
69+
endif()
70+
set(TARGET_TRIPLE "${ARCH_BASE}-nuttx-${ABITYPE}")
71+
else()
72+
set(TARGET_TRIPLE "${ARCHTYPE}-nuttx-${ABITYPE}")
73+
endif()
74+
elseif(ARCHTYPE STREQUAL "riscv32")
75+
if(CPUTYPE STREQUAL "sifive-e20")
76+
set(TARGET_TRIPLE "riscv32imc-unknown-nuttx-elf")
77+
elseif(CPUTYPE STREQUAL "sifive-e31")
78+
set(TARGET_TRIPLE "riscv32imac-unknown-nuttx-elf")
79+
elseif(CPUTYPE STREQUAL "sifive-e76")
80+
set(TARGET_TRIPLE "riscv32imafc-unknown-nuttx-elf")
81+
else()
82+
set(TARGET_TRIPLE "riscv32imc-unknown-nuttx-elf")
83+
endif()
84+
elseif(ARCHTYPE STREQUAL "riscv64")
85+
if(CPUTYPE STREQUAL "sifive-s51")
86+
set(TARGET_TRIPLE "riscv64imac-unknown-nuttx-elf")
87+
elseif(CPUTYPE STREQUAL "sifive-u54")
88+
set(TARGET_TRIPLE "riscv64imafdc-unknown-nuttx-elf")
89+
else()
90+
set(TARGET_TRIPLE "riscv64imac-unknown-nuttx-elf")
91+
endif()
92+
endif()
93+
set(${OUTPUT}
94+
${TARGET_TRIPLE}
95+
PARENT_SCOPE)
96+
endfunction()
97+
98+
# ~~~
99+
# nuttx_add_rust
100+
#
101+
# Description:
102+
# Build a Rust crate and add it as a static library to the NuttX build system
103+
#
104+
# Example:
105+
# nuttx_add_rust(
106+
# CRATE_NAME
107+
# hello
108+
# CRATE_PATH
109+
# ${CMAKE_CURRENT_SOURCE_DIR}/hello
110+
# )
111+
# ~~~
112+
113+
function(nuttx_add_rust)
114+
115+
# parse arguments into variables
116+
nuttx_parse_function_args(
117+
FUNC
118+
nuttx_add_rust
119+
ONE_VALUE
120+
CRATE_NAME
121+
CRATE_PATH
122+
REQUIRED
123+
CRATE_NAME
124+
CRATE_PATH
125+
ARGN
126+
${ARGN})
127+
128+
# Determine build profile based on CONFIG_DEBUG_FULLOPT
129+
if(CONFIG_DEBUG_FULLOPT)
130+
set(RUST_PROFILE "release")
131+
set(RUST_DEBUG_FLAGS "-Zbuild-std-features=panic_immediate_abort")
132+
else()
133+
set(RUST_PROFILE "debug")
134+
set(RUST_DEBUG_FLAGS "")
135+
endif()
136+
137+
# Get the Rust target triple
138+
nuttx_rust_target_triple(${LLVM_ARCHTYPE} ${LLVM_ABITYPE} ${LLVM_CPUTYPE}
139+
RUST_TARGET)
140+
141+
# Set up build directory in current binary dir
142+
set(RUST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CRATE_NAME})
143+
set(RUST_LIB_PATH
144+
${RUST_BUILD_DIR}/${RUST_TARGET}/${RUST_PROFILE}/lib${CRATE_NAME}.a)
145+
146+
# Create build directory
147+
file(MAKE_DIRECTORY ${RUST_BUILD_DIR})
148+
149+
# Add a custom command to build the Rust crate
150+
add_custom_command(
151+
OUTPUT ${RUST_LIB_PATH}
152+
COMMAND
153+
${CMAKE_COMMAND} -E env
154+
NUTTX_INCLUDE_DIR=${PROJECT_SOURCE_DIR}/include:${CMAKE_BINARY_DIR}/include:${CMAKE_BINARY_DIR}/include/arch
155+
cargo build --${RUST_PROFILE} -Zbuild-std=std,panic_abort
156+
${RUST_DEBUG_FLAGS} --manifest-path ${CRATE_PATH}/Cargo.toml --target
157+
${RUST_TARGET} --target-dir ${RUST_BUILD_DIR}
158+
COMMENT "Building Rust crate ${CRATE_NAME}"
159+
VERBATIM)
160+
161+
# Add a custom target that depends on the built library
162+
add_custom_target(${CRATE_NAME}_build ALL DEPENDS ${RUST_LIB_PATH})
163+
164+
# Add imported library target
165+
add_library(${CRATE_NAME} STATIC IMPORTED GLOBAL)
166+
set_target_properties(${CRATE_NAME} PROPERTIES IMPORTED_LOCATION
167+
${RUST_LIB_PATH})
168+
169+
# Add the Rust library to NuttX build
170+
nuttx_add_extra_library(${RUST_LIB_PATH})
171+
172+
# Ensure the Rust library is built before linking
173+
add_dependencies(${CRATE_NAME} ${CRATE_NAME}_build)
174+
175+
endfunction()

examples/rust/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Kconfig

examples/rust/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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/hello_rust/Kconfig renamed to examples/rust/baremetal/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ config EXAMPLES_HELLO_RUST
77
tristate "\"Hello, Rust!\" example"
88
default n
99
---help---
10-
Enable the \"Hello, Rust!\" example
10+
Enable the \"Hello, Rust!\" example to show how to build
11+
and run a baremetal Rust application with rustc.
1112

1213
if EXAMPLES_HELLO_RUST
1314

examples/hello_rust/Make.defs renamed to examples/rust/baremetal/Make.defs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
############################################################################
2-
# apps/examples/hello_rust/Make.defs
2+
# apps/examples/rust/baremetal/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
35
#
46
# Licensed to the Apache Software Foundation (ASF) under one or more
57
# contributor license agreements. See the NOTICE file distributed with
@@ -19,5 +21,5 @@
1921
############################################################################
2022

2123
ifneq ($(CONFIG_EXAMPLES_HELLO_RUST),)
22-
CONFIGURED_APPS += $(APPDIR)/examples/hello_rust
24+
CONFIGURED_APPS += $(APPDIR)/examples/rust/baremetal
2325
endif

examples/hello_rust/Makefile renamed to examples/rust/baremetal/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
############################################################################
2-
# apps/examples/hello_rust/Makefile
2+
# apps/examples/rust/baremetal/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
35
#
46
# Licensed to the Apache Software Foundation (ASF) under one or more
57
# contributor license agreements. See the NOTICE file distributed with

examples/hello_rust/hello_rust_main.rs renamed to examples/rust/baremetal/hello_rust_main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/****************************************************************************
2-
* apps/examples/hello_rust/hello_rust_main.rs
2+
* apps/examples/rust/baremetal/hello_rust_main.rs
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
35
*
46
* Licensed to the Apache Software Foundation (ASF) under one or more
57
* contributor license agreements. See the NOTICE file distributed with

examples/rust/hello/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

0 commit comments

Comments
 (0)