Skip to content

Commit 1a91efd

Browse files
committed
[rust][rust_hello] Very basic Rust hello world app
This creates an very basic rust hello world app. The app declaration is done through unsafe C, but this is sufficient to demonstrate building and linking a rust app into LK. Signed-off-by: David Brown <[email protected]>
1 parent 64cdefc commit 1a91efd

File tree

7 files changed

+63
-0
lines changed

7 files changed

+63
-0
lines changed

app/rust_hello/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Rust sample hello world application crate.
2+
3+
[package]
4+
name = "rust_hello"
5+
version = "0.1.0"
6+
edition = "2024"
7+
description = """
8+
Rust LK hello world application.
9+
"""
10+
license = "MIT"
11+
12+
[dependencies.lk]
13+
version = "0.1.0"
14+
path = "../../rust/lk"
15+
16+
[dependencies.log]
17+
version = "0.4.27"

app/rust_hello/rules.mk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LOCAL_DIR := $(GET_LOCAL_DIR)
2+
3+
RUST_CRATES += $(LOCAL_DIR)
4+
$(info "Adding rust crate $(LOCAL_DIR)")
5+
6+
MODULE := $(LOCAL_DIR)
7+
8+
MODULES += $(MODULE)
9+
10+
MODULE :=

app/rust_hello/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Sample application.
2+
3+
#![no_std]
4+
5+
use core::ffi::c_void;
6+
7+
use lk::sys::{APP_FLAG_NO_AUTOSTART, app_descriptor};
8+
9+
pub fn must_link() {}
10+
11+
/// Manual, and unsafe declration of an app.
12+
#[unsafe(link_section = "apps")]
13+
#[used]
14+
static APP_RUST_HELLO: app_descriptor = app_descriptor {
15+
name: c"rust_hello".as_ptr(),
16+
init: Some(init),
17+
entry: Some(main),
18+
flags: APP_FLAG_NO_AUTOSTART,
19+
stack_size: 0,
20+
};
21+
22+
extern "C" fn init(_desc: *const app_descriptor) {
23+
log::info!("Rust hello app init");
24+
}
25+
26+
extern "C" fn main(_desc: *const app_descriptor, _args: *mut c_void) {
27+
log::info!("Rust hello app main");
28+
}

project/qemu-virt-arm64-test.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# main project for qemu-aarch64
22
MODULES += \
33
app/shell \
4+
app/rust_hello \
45
lib/uefi \
56

67
include project/virtual/test.mk

rust/lk-sys/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ fn main() -> anyhow::Result<()> {
5858
.allowlist_type("lk_init_struct")
5959
.allowlist_function("register_int_handler")
6060
.allowlist_function("unmask_interrupt")
61+
.allowlist_type("app_descriptor")
62+
.allowlist_item("APP_FLAG.*")
6163
.generate()?;
6264

6365
bindings.write_to_file(out_path.join("bindings.rs"))?;

rust/lk-sys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::ffi::c_char;
1515
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
1616

1717
unsafe impl Sync for lk_init_struct {}
18+
unsafe impl Sync for app_descriptor {}
1819

1920
// lk_init_level constants have large gaps between them and some modules
2021
// add or subtract from these constants to indicate that it wants to run

rust/lk-sys/wrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@
2828

2929
#include <platform/interrupts.h>
3030

31+
#if WITH_APP
32+
#include <app.h>
33+
#endif
34+
3135
// #include "error.h"

0 commit comments

Comments
 (0)