Skip to content

Commit d6c4e3f

Browse files
committed
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`. TODO: 1. Add support for Rust in CMake based system 2. Upstream https://github.com/no1wudi/libc to rust-lang/libc 3. Port libstd of Rust to NuttX, which blocked by 2.
1 parent 12012f2 commit d6c4e3f

File tree

8 files changed

+218
-0
lines changed

8 files changed

+218
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# ##############################################################################
2+
# apps/examples/hello_rust_cargo/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+
# Call cargo build from CMakeLists and add it to the build system.
22+
# Notice we should call cargo build with add_custom_target, otherwise
23+
# cargo will be called every time when cmake is configured.
24+
25+
add_custom_target(cargo-build ALL
26+
COMMAND cargo build --release
27+
-Zbuild-std=core
28+
--manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/hello/Cargo.toml
29+
--target riscv32imac-unknown-nuttx-elf
30+
)
31+
32+
# Let target apps depends on cargo-build, so that cargo will be called before
33+
# building the target.
34+
add_dependencies(apps cargo-build)
35+
36+
# Add static library apps/examples/hello_rust_cargo/hello/target/riscv32imac-unknown-nuttx-elf/release/libhello.a
37+
# to the build system.
38+
nuttx_library_import(hello ${CMAKE_CURRENT_SOURCE_DIR}/hello/target/riscv32imac-unknown-nuttx-elf/release/libhello.a)
39+
nuttx_add_extra_library(hello)
40+
41+
if(CONFIG_EXAMPLES_HELLO_RUST_CARGO)
42+
nuttx_add_application(
43+
NAME
44+
${CONFIG_EXAMPLES_HELLO_RUST_CARGO_PROGNAME}
45+
SRCS
46+
proxy_main.c
47+
STACKSIZE
48+
${CONFIG_EXAMPLES_HELLO_STACKSIZE}
49+
PRIORITY
50+
${CONFIG_EXAMPLES_HELLO_PRIORITY})
51+
endif()

examples/hello_rust_cargo/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/hello_rust_cargo/Make.defs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
ifneq ($(CONFIG_EXAMPLES_HELLO_RUST_CARGO),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/hello_rust_cargo
23+
endif

examples/hello_rust_cargo/Makefile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
############################################################################
2+
# apps/examples/hello_rust/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+
# Do not suppport building this application from Makefile.
31+
32+
include $(APPDIR)/Application.mk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "hello"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["staticlib"]
8+
9+
[dependencies]
10+
libc = { git = "https://github.com/no1wudi/libc", branch = "libc-0.2" }
11+
12+
[profile.dev]
13+
panic ="abort"
14+
15+
# Special hanlding for the panic! macro, can be removed once
16+
# the libstd port for NuttX is complete.
17+
[profile.release]
18+
panic ="abort"
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// no-std libraray for the Rust programming language
2+
#![no_std]
3+
4+
extern crate libc;
5+
6+
// Function hello_rust_cargo without manglng
7+
#[no_mangle]
8+
pub extern "C" fn rust_main() {
9+
// Print hello world to stdout
10+
unsafe {
11+
libc::printf(
12+
"Hello World from Rust build with Cargo!\n\0"
13+
.as_ptr()
14+
.cast(),
15+
);
16+
}
17+
}
18+
19+
#[panic_handler]
20+
fn panic(_info: &core::panic::PanicInfo) -> ! {
21+
loop {}
22+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/****************************************************************************
2+
* apps/examples/hello_rust_cargo/proxy_main.c
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+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
#include <stdio.h>
27+
28+
/****************************************************************************
29+
* Public Functions
30+
****************************************************************************/
31+
32+
extern void rust_main(void);
33+
34+
/****************************************************************************
35+
* main
36+
****************************************************************************/
37+
38+
int main(int argc, FAR char *argv[])
39+
{
40+
rust_main();
41+
return 0;
42+
}

0 commit comments

Comments
 (0)