forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The idea is to integrate rust into the existing build system as well as possible and to not use a separate buildsystem like cargo or xargo. Makefile.base has been modified to include support for main.rs and lib.rs files and a Makefile.rust has been added which sets a few rust related variables. This is very loosly based on RIOT-OS#5740
- Loading branch information
Showing
25 changed files
with
327 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Function to get `lib*` name for a module. This is | ||
# needed because rustc expects files passed to | ||
# `--extern` to match the pattern `lib*.rlib`. | ||
define libname | ||
$(shell dirname $1)/lib$(shell basename $1) | ||
endef | ||
|
||
# Set module name and library name (see above). | ||
MODULE ?= $(shell basename $(CURDIR)) | ||
ifeq (lib,$(CRATE_TYPE)) | ||
LIBNAME = $(call libname,$(MODULE)) | ||
endif | ||
|
||
# Set crate metadata in case they aren't set. | ||
CRATE_TYPE ?= bin | ||
CRATE_NAME ?= $(shell basename $(MODULE)) | ||
|
||
# Make our custom targets known to rustc. | ||
export RUST_TARGET_PATH ?= $(RIOTBASE)/dist/rust | ||
|
||
# Set the rust target accordingly. | ||
ifeq ($(BOARD),native) | ||
RUST_TARGET=i586-unknown-linux-gnu | ||
else | ||
RUST_TARGET=$(CPU_ARCH) | ||
endif | ||
|
||
# Flags to pass to the rust compiler. | ||
RUSTC_FLAGS += --crate-name $(CRATE_NAME) \ | ||
--crate-type $(CRATE_TYPE) \ | ||
--target $(RUST_TARGET) \ | ||
--extern core=$(BINDIR)/libcore.rlib | ||
|
||
# Include dependencies in `RUSTC_FLAGS`. | ||
RUSTMODULE ?= $(filter rust/%, $(USEMODULE)) | ||
RUSTC_FLAGS += $(foreach dep,$(RUSTMODULE),--extern \ | ||
$(shell basename $(dep))=$(BINDIR)/$(call libname,$(dep)).rlib) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m0", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"llvm-target": "thumbv6m-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"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, | ||
"llvm-target": "thumbv6m-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m1", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"llvm-target": "thumbv6m-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m3", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"llvm-target": "thumbv7m-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m4", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"features": "+soft-float", | ||
"llvm-target": "thumbv7em-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m4", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"llvm-target": "thumbv7em-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m7", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"features": "+soft-float", | ||
"llvm-target": "thumbv7em-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m7", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"features": "+fp-only-sp", | ||
"llvm-target": "thumbv7em-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"arch": "arm", | ||
"cpu": "cortex-m7", | ||
"data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", | ||
"executables": true, | ||
"llvm-target": "thumbv7em-none-eabi", | ||
"no-compiler-rt": true, | ||
"os": "none", | ||
"pre-link-args": [], | ||
"relocation-model": "static", | ||
"target-endian": "little", | ||
"target-pointer-width": "32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 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 | ||
|
||
# The boards needs to support rust. | ||
FEATURES_REQUIRED += rust_support | ||
|
||
# If no BOARD is found in the environment, use this default: | ||
BOARD ?= native | ||
|
||
# Since we built rust with `#![no_std]` we need libcore. | ||
USEPKG += libcore | ||
|
||
# Module dependencies. | ||
USEMODULE += rust/common # Provides lang_items | ||
USEMODULE += rust/fmt # Wrapper for sys/fmt | ||
USEMODULE += fmt # Needed by the wrapper | ||
|
||
include $(RIOTBASE)/Makefile.include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Introduction | ||
|
||
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]`. However, crates | ||
to map RIOT's API to rust are available in `sys/rust`. | ||
|
||
# Prerequisites | ||
|
||
Since building binary packages with `#![no_std]` is currently not | ||
supported by the rust stable channel you need a nightly rust toolchain. | ||
|
||
After installing the nightly rust toolchain including cargo you should | ||
be good to go. | ||
|
||
# Trying the example | ||
|
||
As always, | ||
|
||
# make BOARD=native all term | ||
|
||
is all you need. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
extern crate fmt; | ||
extern crate common; | ||
|
||
use fmt::Stdout; | ||
use core::fmt::Write; | ||
|
||
#[no_mangle] | ||
pub fn main() { | ||
let mut out = Stdout {}; | ||
out.write_str("Hello World!\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
PKG_NAME = libcore | ||
PKG_URL = https://github.com/rust-lang/rust | ||
PKG_VERSION = $(shell ./version.sh) | ||
|
||
.PHONY: all | ||
|
||
all: $(BINDIR)/$(PKG_NAME).a | ||
$(BINDIR)/$(PKG_NAME).a: $(BINDIR)/$(PKG_NAME).rlib | ||
$(AD)ln -fs $< $@ | ||
|
||
$(BINDIR)/$(PKG_NAME).rlib: git-download | ||
$(AD)rustc --crate-name core --crate-type lib \ | ||
--target $(RUST_TARGET) -o $@ \ | ||
$(PKG_BUILDDIR)/src/libcore/lib.rs | ||
|
||
include $(RIOTBASE)/Makefile.rust | ||
include $(RIOTBASE)/pkg/pkg.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
|
||
RUST_VERSION="" | ||
if ! command -v rustc >/dev/null 2>&1; then | ||
echo "Missing rustc, please install it" 2>&1 | ||
exit 1 | ||
fi | ||
|
||
RUST_VERSION="$(rustc --version | cut -d ' ' -f2-)" | ||
if [ "$(echo ${RUST_VERSION} | cut -d '-' -f2 \ | ||
| cut -d ' ' -f1)" = "nightly" ]; then | ||
RUST_VERSION="$(echo ${RUST_VERSION} | cut -d ' ' -f2 | tr -d '()')" | ||
fi | ||
|
||
echo "${RUST_VERSION}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Modules in this directory should be prefixed with a `rust/`. | ||
MODULE = rust/$(shell basename $(CURDIR)) | ||
|
||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(RIOTBASE)/sys/rust/Makefile.crate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[lang = "panic_fmt"] | ||
extern fn panic_fmt() {} | ||
|
||
#[lang = "eh_personality"] | ||
fn eh_personality() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![feature(lang_items)] | ||
#![no_std] | ||
|
||
pub mod lang_items; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(RIOTBASE)/sys/rust/Makefile.crate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![no_std] | ||
|
||
use core::fmt; | ||
use core::fmt::Result; | ||
use core::result; | ||
|
||
extern { | ||
fn print(ptr: *const u8, len: usize) -> (); | ||
} | ||
|
||
// A handle to the global standard output stream of the current process. | ||
// | ||
// This type is similar to `std::io::Stdout` except that it implements | ||
// `core::fmt::Write` instead of `std::io::Write`. | ||
pub struct Stdout; | ||
|
||
impl fmt::Write for Stdout { | ||
fn write_str(&mut self, s: &str) -> Result { | ||
unsafe { print(s.as_ptr(), s.len()); } | ||
result::Result::Ok(()) | ||
} | ||
} |