Skip to content

Commit 3972197

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. Signed-off-by: Huang Qi <[email protected]>
1 parent 4e3f77d commit 3972197

File tree

8 files changed

+223
-0
lines changed

8 files changed

+223
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
include(nuttx_add_library)
22+
23+
if(CONFIG_EXAMPLES_HELLO_RUST_CARGO)
24+
25+
# Call cargo build from CMakeLists and add it to the build system.
26+
# Notice we should call cargo build with add_custom_target, otherwise
27+
# cargo will be called every time when cmake is configured.
28+
29+
add_custom_command(
30+
OUTPUT ${CMAKE_BINARY_DIR}/hello_rust_cargo/riscv32imac-unknown-nuttx-elf/release/libhello.a
31+
COMMAND cargo build --release
32+
-Zbuild-std=core
33+
--manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/hello/Cargo.toml
34+
--target riscv32imac-unknown-nuttx-elf
35+
--target-dir ${CMAKE_BINARY_DIR}/hello_rust_cargo)
36+
37+
add_custom_target(hello_rust_cargo
38+
DEPENDS ${CMAKE_BINARY_DIR}/hello_rust_cargo/riscv32imac-unknown-nuttx-elf/release/libhello.a)
39+
40+
# Call cargo_build target each time when cmake is configured or generated.
41+
42+
# Add static library to the build system.
43+
nuttx_library_import(rust_hello ${CMAKE_BINARY_DIR}/hello_rust_cargo/riscv32imac-unknown-nuttx-elf/release/libhello.a)
44+
nuttx_add_extra_library(rust_hello)
45+
46+
nuttx_add_application(
47+
NAME
48+
${CONFIG_EXAMPLES_HELLO_RUST_CARGO_PROGNAME}
49+
SRCS
50+
proxy_main.c
51+
STACKSIZE
52+
${CONFIG_EXAMPLES_HELLO_STACKSIZE}
53+
PRIORITY
54+
${CONFIG_EXAMPLES_HELLO_PRIORITY})
55+
56+
endif() # CONFIG_EXAMPLES_HELLO_RUST_CARGO

examples/hello_rust_cargo/Kconfig

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

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

Lines changed: 32 additions & 0 deletions
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
Lines changed: 18 additions & 0 deletions
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"
Lines changed: 22 additions & 0 deletions
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+
}
Lines changed: 42 additions & 0 deletions
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)