8000 Add pio_include crate to add platformio's prelude & helpers · dzervas/platformio-arduino-rust@35b9dac · GitHub
[go: up one dir, main page]

Skip to content

Commit 35b9dac

Browse files
committed
Add pio_include crate to add platformio's prelude & helpers
1 parent d8cf4f0 commit 35b9dac

File tree

7 files changed

+138
-33
lines changed

7 files changed

+138
-33
lines changed

.cargo/config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
rustflags = [
33
"--emit", "obj",
44
"-C", "soft-float=no",
5-
"-C", "link-dead-code=yes",
65
"-C", "extra-filename=-platformio",
76
"-C", "default-linker-libraries=no",
87
]

Cargo.toml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ readme = "README.md"
55
name = "ardurust"
66
version = "0.1.0"
77

8-
# [build-dependencies]
9-
# bindgen = "*"
8+
[dependencies]
9+
pio_include = { version = "*", path = "pio_include" }
1010

1111
[lib]
12-
name = "ardurust"
12+
name = "firmware"
1313
crate-type = ["staticlib"]
1414

15-
# this lets you use `cargo fix`!
16-
# [[bin]]
17-
# name = "ardurust"
18-
# test = false
19-
# bench = false
20-
21-
#[profile.release]
22-
#codegen-units = 1 # better optimizations
23-
#debug = false # symbols are nice and they don't increase the size on Flash
24-
#lto = true # better optimizations
15+
[profile.release]
16+
codegen-units = 1 # better optimizations
17+
debug = false # symbols are nice and they don't increase the size on Flash
18+
lto = true # better optimizations

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM ubuntu:latest
2+
3+
RUN apt-get update && apt-get install -y build-essential curl python3 python3-distutils vim
4+
5+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y --default-toolchain nightly
6+
RUN python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)" && \
7+
echo 'export "PATH=$HOME/.platformio/penv/bin:$PATH"' >> ~/.profile
8+
9+
RUN ~/.cargo/bin/rustup target install thumbv7em-none-eabi
10+
11+
RUN ~/.cargo/bin/cargo install bindgen

cargo_build.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,25 @@ def ignore_main_cpp(node):
2323
# Floating point does not make a difference in target in clang, but it does in -mfloat-abi
2424
# -mfloat-abi=soft for software and hard for hardware. There's also softfp (?)
2525
# I have no idea why the nrf52, while being hard requires soft ABI. Gotta check that at some point
26-
env.Execute("""bindgen --ctypes-prefix pio_rust --use-core \
27-
--blacklist-item std::* \
28-
--blacklist-item FP_NAN \
29-
--blacklist-item FP_INFINITE \
30-
--blacklist-item FP_ZERO \
31-
--blacklist-item FP_NORMAL \
32-
--blacklist-item FP_SUBNORMAL \
33-
--blacklist-item SVCALL \
34-
--blacklist-function setup \
35-
--blacklist-function loop \
36-
-o src/platformio.rs \
37-
""" + env.get("PROJECT_PACKAGES_DIR") +"""/framework-arduinoadafruitnrf52/cores/nRF5/Arduino.h -- \
38-
-mfloat-abi=soft \
39-
-target armv7em """ + defines + " " + headers)
40-
26+
# env.Execute("""bindgen --ctypes-prefix pio_rust --use-core \
27+
# --blacklist-item std::* \
28+
# --blacklist-item FP_NAN \
29+
# --blacklist-item FP_INFINITE \
30+
# --blacklist-item FP_ZERO \
31+
# --blacklist-item FP_NORMAL \
32+
# --blacklist-item FP_SUBNORMAL \
33+
# --blacklist-item SVCALL \
34+
# --blacklist-function setup \
35+
# --blacklist-function loop \
36+
# -o src/platformio.rs \
37+
# """ + env.get("PROJECT_PACKAGES_DIR") +"""/framework-arduinoadafruitnrf52/cores/nRF5/Arduino.h -- \
38+
# -target armv7em """
39+
# + defines + " " + headers)
40+
41+
print(dir(env.get("ENV")))
42+
env.get("ENV")["CPPPATH"] = ":".join(env.get("CPPPATH"))
4143
env.Execute("cargo build --release --target=thumbv7em-none-eabihf -v")
42-
env.Append(PIOBUILDFILES=["$PROJECT_DIR/target/thumbv7em-none-eabihf/release/deps/ardurust-platformio.o"])
44+
env.Append(PIOBUILDFILES=["$PROJECT_DIR/target/thumbv7em-none-eabihf/release/deps/firmware-platformio.o"])
4345

4446
return None
4547

pio_include/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "pio_include"
3+
version = "0.1.0"
4+
authors = ["Dimitris Zervas <dzervas@dzervas.gr>"]
5+
edition = "2018"
6+
7+
[lib]
8+
proc-macro = true
9+
10+
[dependencies]
11+
bindgen = "*"
12+
syn = "*"

pio_include/src/lib.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
extern crate bindgen;
2+
extern crate proc_macro;
3+
extern crate syn;
4+
5+
use std::env;
6+
use std::path::{Path, PathBuf};
7+
8+
use proc_macro::TokenStream;
9+
use syn::{parse_macro_input, LitStr};
10+
11+
#[proc_macro]
12+
pub fn pio_include(input: TokenStream) -> TokenStream {
13+
let header = parse_macro_input!(input as LitStr);
14+
let header_str = header.value();
15+
16+
let include_paths = env::var("CPPPATH").expect("Unable to read CPPPATH environment variable");
17+
let mut try_path = header_str.clone();
18+
let mut header_path = Path::new(&header_str);
19+
20+
for path in include_paths.split(":") {
21+
if header_path.exists() { break; }
22+
23+
try_path = format!("{}/{}", path, header_str);
24+
25+
header_path = Path::new(&try_path);
26+
}
27+
28+
if !header_path.exists() { panic!(format!("Header file {} was not found", header_str)); }
29+
30+
// The bindgen::Builder is the main entry point
31+
// to bindgen, and lets you build up options for
32+
// the resulting bindings.
33+
// For more options check https://llvm.org/docs/HowToCrossCompileLLVM.html
34+
// Probably need:
35+
// -DCMAKE_CROSSCOMPILING=True
36+
// -DCMAKE_CXX_FLAGS='<CC FLAGS>'
37+
println!("Generating bindings for {}", &try_path);
38+
let bindings = bindgen::Builder::default()
39+
.header(try_path)
40+
// .parse_callbacks(Box::new(bindgen::CargoCallbacks))
41+
42+
.ctypes_prefix("pio_rust")
43+
.use_core()
44+
.blacklist_item("std::*")
45+
.blacklist_item("FP_NAN")
46+
.blacklist_item("FP_INFINITE")
47+
.blacklist_item("FP_ZERO")
48+
.blacklist_item("FP_NORMAL")
49+
.blacklist_item("FP_SUBNORMAL")
50+
.blacklist_item("SVCALL")
51+
.blacklist_function("setup")
52+
.blacklist_function("loop")
53+
.clang_arg("-DCMAKE_CROSSCOMPILING=True")
54+
.clang_arg("-DCMAKE_CXX_FLAGS=-Wl,--gc-sections,--relax -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16")
55+
// .clang_arg("-mfloat-abi").clang_arg("hard")
56+
// .clang_arg("-target").clang_arg("armv7em")
57+
// .clang_arg("-sysroot").clang_arg("~/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/")
58+
.rustfmt_bindings(false)
59+
60+
.generate()
61+
.expect("Unable to generate bindings");
62+
63+
// Write the bindings to the $OUT_DIR/bindings.rs file.
64+
// let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
65+
// let out_file = out_path.join(format!("{}.rs", header_str));
66+
67+
// println!("Writing bindings to {:?}", &out_file);
68+
69+
// bindings
70+
// .write_to_file(out_file)
71+
// .expect("Couldn't write bindings!");
72+
73+
// println!("cargo:rerun-if-changed=build.rs");
74+
75+
// (quote!{
76+
// concat!("include!(\"", #header_str, ".rs\");")
77+
// }).into()
78+
// format!("include!(\"{}\");", header_str)
79+
format!("include!(\"platformio.rs\");")
80+
.parse().unwrap()
81+
}

src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
#![feature(lang_items)]
22
#![no_std]
3-
#![allow(dead_code)]
3+
#![allow(non_upper_case_globals)]
44
#![allow(non_snake_case)]
55
#![allow(non_camel_case_types)]
6-
#![allow(non_upper_case_globals)]
76

7+
#[allow(dead_code)]
88
mod pio_rust;
99

10-
include!("platformio.rs");
10+
extern crate pio_include;
11+
use pio_include::pio_include;
12+
pio_include!("Arduino.h");
13+
14+
// include!("platformio.rs");
1115

1216
#[no_mangle]
17+
#[allow(dead_code)]
1318
pub extern "C" fn setup() {
1419
unsafe {
1520
pinMode(19, 1);
1621
}
1722
}
1823

1924
#[no_mangle]
25+
#[allow(dead_code)]
2026
pub extern "C" fn r#loop() {
2127
unsafe {
2228
digitalWrite(19, 1);

0 commit comments

Comments
 (0)
0