Skip to content

Commit

Permalink
make: initial Rust language support
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 authored and test committed Aug 9, 2016
1 parent 76e7d40 commit f2e2eea
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,7 @@ export CFLAGS += -DRIOT_VERSION=\"$(RIOT_VERSION_OVERRIDE)\"
else
export CFLAGS += -DRIOT_VERSION=\"$(RIOT_VERSION)\"
endif

ifeq (1,$(CARGO_BUILD))
include $(RIOTBASE)/Makefile.rust
endif
28 changes: 28 additions & 0 deletions Makefile.rust
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# RIOT rust support makefile

# make RIOT's linker hook path known to rustc
export RUST_TARGET_PATH ?= $(RIOTBASE)/dist/rust/targets

# (unfortunately with this set, Cargo somehow fails. Thus we need to keep
# Cargo's build directory in ./target)
#export CARGO_TARGET_DIR ?= $(BINDIR)/_cargo

# the rust riot crate depends on fmt
USEMODULE += fmt

ifeq ($(CPU_ARCH), cortex-m0plus)
RUST_TARGET=$(CPU_ARCH)
else ifeq ($(CPU_ARCH),native)
$(error Makefile.rust: RIOT rust on native is currently broken.)
RUST_TARGET=i586-unknown-linux-gnu
endif

ifeq (, $(RUST_TARGET))
$(error Makefile.rust: cannot determine rust target triple!)
endif

all: xargo

xargo:
@mkdir -p $(BINDIR)/_extra_libs
@PATH=$${PATH}:$(RIOTBASE)/dist/rust xargo build -v --target $(RUST_TARGET)
21 changes: 21 additions & 0 deletions dist/rust/riot-rust-linker-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

[ -z "$BINDIR" ] && {
echo "$0: BINDIR not defined!" 1&>2
exit 1
}

_EXTRA_LIBS=${BINDIR}/_extra_libs
mkdir -p ${_EXTRA_LIBS}

while true; do
[ -z "$1" ] && exit 0
[ -f "$1" ] && {
if [[ "$1" == *.o ]]; then
${AR} r ${_EXTRA_LIBS}/_objects.a $1
elif [[ "$1" == *.rlib ]]; then
cp $1 ${_EXTRA_LIBS}/$(basename $1).a
fi
}
shift
done
14 changes: 14 additions & 0 deletions dist/rust/targets/cortex-m0plus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"arch": "arm",
"cpu": "cortex-m0plus",
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
"executables": true,
"linker": "riot-rust-linker-hook",
"llvm-target": "thumbv6m-none-eabi",
"no-compiler-rt": true,
"os": "none",
"pre-link-args": [ ],
"relocation-model": "static",
"target-endian": "little",
"target-pointer-width": "32"
}
7 changes: 7 additions & 0 deletions examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "app"
version = "0.1.0"
authors = ["Kaspar Schleiser <[email protected]>"]

[dependencies]
riot = "0.1.0"
20 changes: 20 additions & 0 deletions examples/rust/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# name of your application
APPLICATION = rust

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# tested only on samr21-xpro
BOARD_WHITELIST := samr21-xpro

# If no BOARD is found in the environment, use this default:
BOARD ?= samr21-xpro

# use this to enable rust support.
# the current directory will be built using xargo.
CARGO_BUILD=1

include $(RIOTBASE)/Makefile.include
40 changes: 40 additions & 0 deletions examples/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Introduction

"Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety."

This example shows how to write a RIOT application using Rust.

# Status

As Rust's libstd is too fat for our little MCUs, it is currently only possible
to write Rust applications using ```#![no_std]```.
There's a crate on crate.io trying to map RIOT's API to rust, but that is in
very early stages.

# Prerequisites

You'll need a Rust nightly toolchain and xargo.

This should get you started:

# curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain=nightly
# . ~/.cargo/env
# cargo install xargo

# Trying the example

As always,

# BOARD=samr21-xpro make clean all flash term

is all you need.

# Internals

RIOT's make system intercepts cargo's linking step and just copies the objects
and archives cargo tries to link into RIOT's build dir, then just links
everything together.

# Known issues

- currently this works only for Atmel Sam R21
20 changes: 20 additions & 0 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![feature(lang_items)]
#![no_main]
#![no_std]

mod lang_items {
#[lang = "panic_fmt"]
extern fn panic_fmt() {}

#[lang = "eh_personality"]
fn eh_personality() {}
}

// user code starts here

extern crate riot;

#[no_mangle]
pub fn main() {
riot::fmt::print("Hello from RUST!\n");
}

0 comments on commit f2e2eea

Please sign in to comment.