Install Rust in docker image, add Rust library and build rules (#41)

* Install Rust in docker image

* Also install thumbv7em-none-eabi target

* Install Rust in docker image

* Also install thumbv7em-none-eabi target

* Add Rust example

* Link to the Rust example

* Call function from the Rust lib

* Move PROJECT_DIR to the 'paths' section

* Fix target_f1 build

* Link to the Rust library in target_f1

* Generate cbindgen bindings

* Add forgotten dependency line

* Use panic=abort instead of eh_personality lang item

* Install Rust in docker image

* Also install thumbv7em-none-eabi target

* Add Rust example

* Link to the Rust example

* Call function from the Rust lib

* Move PROJECT_DIR to the 'paths' section

* Link to the Rust library in target_f1

* Generate cbindgen bindings

* Add forgotten dependency line

* Use panic=abort instead of eh_personality lang item

* add rust call test

Co-authored-by: aanper <mail@s3f.ru>
This commit is contained in:
Vadim Kaushan 2020-08-26 13:08:20 +03:00 committed by GitHub
parent 046a20fa0e
commit 1bec8dd23a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 478 additions and 11 deletions

View File

@ -2,9 +2,13 @@ FROM ubuntu:18.04
RUN apt-get update && apt-get install -y --no-install-recommends \
make gcc-arm-none-eabi binutils-arm-none-eabi libnewlib-arm-none-eabi \
autoconf automake libtool curl wget g++ unzip && \
autoconf automake libtool curl wget g++ unzip build-essential curl && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN apt-get update && apt-get install -y --no-install-recommends \
python python-pip libstdc++-arm-none-eabi-newlib && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --profile=minimal --target thumbv7em-none-eabi thumbv7em-none-eabihf
ENV PATH="/root/.cargo/bin:${PATH}"
ENV USER=root

View File

@ -2,6 +2,8 @@
#include "flipper.h"
#include "debug.h"
#include "flipper-core.h"
bool furi_ac_create_kill(FILE* debug_uart);
bool furi_ac_switch_exit(FILE* debug_uart);
@ -56,5 +58,11 @@ void flipper_test_app(void* p) {
fprintf(debug_uart, "[TEST] furi_mute_algorithm FAILED\n");
}
if(add(1, 2) == 3) {
fprintf(debug_uart, "[TEST] Rust add PASSED\n");
} else {
fprintf(debug_uart, "[TEST] Rust add FAILED\n");
}
furiac_exit(NULL);
}

3
core-rs/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/bindings
/target
/.idea

341
core-rs/Cargo.lock generated Normal file
View File

@ -0,0 +1,341 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "cbindgen"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e783d38a7700989e0209d0b0ed224c34ade92d3603da0cf15dc502ebada685a6"
dependencies = [
"clap",
"heck",
"log",
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
"tempfile",
"toml",
]
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "flipper-core"
version = "0.1.0"
dependencies = [
"cbindgen",
]
[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "heck"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "libc"
version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3"
[[package]]
name = "log"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [
"cfg-if",
]
[[package]]
name = "ppv-lite86"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
[[package]]
name = "proc-macro2"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "serde"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891d8d6567fe7c7f8835a3a98af4208f3846fba258c1bc3c31d6e506239f11f9"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "tempfile"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
dependencies = [
"cfg-if",
"libc",
"rand",
"redox_syscall",
"remove_dir_all",
"winapi",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "toml"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a"
dependencies = [
"serde",
]
[[package]]
name = "unicode-segmentation"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

20
core-rs/Cargo.toml Normal file
View File

@ -0,0 +1,20 @@
[package]
name = "flipper-core"
version = "0.1.0"
authors = ["Vadim Kaushan <admin@disasm.info>"]
edition = "2018"
[lib]
crate-type = ["staticlib"]
[build-dependencies]
cbindgen = "0.14"
[profile.release]
codegen-units = 1 # better optimizations
debug = true # symbols are nice and they don't increase the size on Flash
lto = true # better optimizations
panic = "abort"
[profile.dev]
panic = "abort"

13
core-rs/build.rs Normal file
View File

@ -0,0 +1,13 @@
use std::env;
use std::path::Path;
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
cbindgen::generate(&crate_dir)
.expect("Unable to generate cbindgen bindings")
.write_to_file(
Path::new(&crate_dir).join("bindings").join(format!("{}.h", pkg_name))
);
}

4
core-rs/cbindgen.toml Normal file
View File

@ -0,0 +1,4 @@
language = "C"
[export]
item_types = ["functions"]

16
core-rs/src/lib.rs Normal file
View File

@ -0,0 +1,16 @@
#![no_std]
#[no_mangle]
pub extern "C" fn add(a: u32, b: u32) -> u32 {
a + b
}
mod aux {
use core::panic::PanicInfo;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop { continue }
}
}

View File

@ -28,6 +28,7 @@ OPT = -Og
#######################################
# paths
#######################################
PROJECT_DIR = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
# Build path
BUILD_DIR = build
@ -134,6 +135,10 @@ endif
# Add C_SOURCES +=, C_DEFS += or CPP_SOURCES += here
C_SOURCES += ../applications/tests/furiac_test.c
C_SOURCES += ../applications/tests/furi_record_test.c
C_SOURCES += ../applications/tests/test_index.c
#######################################
# binaries
#######################################
@ -155,7 +160,26 @@ SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# Rust library
#######################################
RUST_LIB_SRC = $(realpath $(PROJECT_DIR)/../core-rs)
RUST_LIB_NAME = flipper_core
RUST_LIB_TARGET = thumbv7em-none-eabihf
RUST_LIB_FLAGS = --target=$(RUST_LIB_TARGET)
ifeq ($(DEBUG), 1)
RUST_LIB_PATH = $(RUST_LIB_SRC)/target/$(RUST_LIB_TARGET)/debug
else
RUST_LIB_FLAGS += --release
RUST_LIB_PATH = $(RUST_LIB_SRC)/target/$(RUST_LIB_TARGET)/release
endif
RUST_LIB_CMD = cd $(RUST_LIB_SRC) && cargo build $(RUST_LIB_FLAGS)
#######################################
# CFLAGS
#######################################
@ -193,7 +217,8 @@ C_INCLUDES = \
-IDrivers/CMSIS/Device/ST/STM32L4xx/Include \
-IDrivers/CMSIS/Include \
-IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc \
-IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc
-IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc \
-I../core-rs/bindings
# compile gcc flags
@ -218,8 +243,8 @@ CPPFLAGS = -fno-threadsafe-statics
LDSCRIPT = STM32L476RGTx_FLASH.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LIBS = -lc -lm -lnosys -l$(RUST_LIB_NAME)
LIBDIR = -L$(RUST_LIB_PATH)
LDFLAGS = $(MCU) -specs=nano.specs -specs=nosys.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
# default action: build all
@ -258,7 +283,10 @@ $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(BUILD_DIR)/%.pb.c $(BUILD_DIR)/%.pb.h: ../flipper_proto/%.proto
$(PROTOC) $(PROTOC_OPTS) --nanopb_out=$(BUILD_DIR) $<
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a:
$(RUST_LIB_CMD)
$(BUILD_DIR)/$(TARGET).elf: $(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a $(OBJECTS) Makefile
$(CPP) $(OBJECTS) $(LDFLAGS) -o $@
$(SZ) $@
@ -276,10 +304,12 @@ $(BUILD_DIR):
#######################################
clean:
-rm -fR $(BUILD_DIR)
cd $(RUST_LIB_SRC) && cargo clean
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
-include $(wildcard $(RUST_LIB_PATH)/*.d)
# *** EOF ***

View File

@ -13,6 +13,7 @@ OPT = -Og
#######################################
# paths
#######################################
PROJECT_DIR = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
# Build path
BUILD_DIR = build
@ -70,7 +71,26 @@ CP = objcopy
SZ = size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# Rust library
#######################################
RUST_LIB_SRC = $(realpath $(PROJECT_DIR)/../core-rs)
RUST_LIB_NAME = flipper_core
RUST_LIB_TARGET = x86_64-unknown-linux-gnu
RUST_LIB_FLAGS = --target=$(RUST_LIB_TARGET)
ifeq ($(DEBUG), 1)
RUST_LIB_PATH = $(RUST_LIB_SRC)/target/$(RUST_LIB_TARGET)/debug
else
RUST_LIB_FLAGS += --release
RUST_LIB_PATH = $(RUST_LIB_SRC)/target/$(RUST_LIB_TARGET)/release
endif
RUST_LIB_CMD = cd $(RUST_LIB_SRC) && cargo build $(RUST_LIB_FLAGS)
#######################################
# CFLAGS
#######################################
@ -80,7 +100,8 @@ BIN = $(CP) -O binary -S
C_INCLUDES = \
-IInc \
-I../applications \
-I../core
-I../core \
-I../core-rs/bindings
# compile gcc flags
CFLAGS = $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -pthread
@ -100,8 +121,8 @@ CPPFLAGS = -fno-threadsafe-statics
#######################################
# libraries
LIBS = -lc -lm
LIBDIR =
LIBS = -lc -lm -l$(RUST_LIB_NAME)
LIBDIR = -L$(RUST_LIB_PATH)
LDFLAGS = $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -pthread
# default action: build all
@ -132,7 +153,10 @@ $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)
$(CPP) -c $(CFLAGS) $(CPPFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(BUILD_DIR)/$(TARGET): $(OBJECTS) Makefile
$(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a:
$(RUST_LIB_CMD)
$(BUILD_DIR)/$(TARGET): $(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a $(OBJECTS) Makefile
$(CPP) $(OBJECTS) $(LDFLAGS) -o $@
$(SZ) $@
@ -144,10 +168,12 @@ $(BUILD_DIR):
#######################################
clean:
-rm -fR $(BUILD_DIR)
cd $(RUST_LIB_SRC) && cargo clean
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
-include $(wildcard $(RUST_LIB_PATH)/*.d)
# *** EOF ***

View File

@ -5,8 +5,10 @@ Local fw build entry point.
*/
void app();
unsigned int add(unsigned int a, unsigned int b);
int main() {
add(2, 2);
app();
return 0;