diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 4d54565..8aea823 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,9 +4,11 @@ idf_component_register(SRCS "main.c" set(CARGO_BUILD_TYPE "release") set(CARGO_BUILD_ARG "--release") if(CONFIG_IDF_TARGET_ARCH_RISCV) - set(CARGO_TARGET "riscv32i-unknown-elf") + set(CARGO_TARGET "riscv32i-unknown-none-elf") + set(CARGO_FEATURES_ARG "") elseif(CONFIG_IDF_TARGET_ARCH_XTENSA) - set(CARGO_TARGET "xtensa-${CONFIG_IDF_TARGET}-none-elf") + set(CARGO_TARGET "xtensa-esp32-none-elf") + set(CARGO_FEATURES_ARG "--features=std") endif() set(RUST_PROJECT_DIR "${CMAKE_CURRENT_LIST_DIR}/../rustlib") @@ -19,7 +21,7 @@ ExternalProject_Add( BUILD_COMMAND ${CMAKE_COMMAND} -E env CARGO_BUILD_TARGET=${CARGO_TARGET} CARGO_BUILD_TARGET_DIR=${RUST_BUILD_DIR} - cargo build ${CARGO_BUILD_ARG} + cargo build ${CARGO_BUILD_ARG} ${CARGO_FEATURES_ARG} DOWNLOAD_COMMAND "" CONFIGURE_COMMAND "" INSTALL_COMMAND "" diff --git a/rustlib/Cargo.toml b/rustlib/Cargo.toml index 822363e..ddf3f35 100644 --- a/rustlib/Cargo.toml +++ b/rustlib/Cargo.toml @@ -8,10 +8,12 @@ edition = "2018" crate-type = ["staticlib"] #crate-type = ["rlib", "staticlib"] - [build-dependencies] bindgen = "0.58" +[features] +std = [] + [profile.dev] # Optimization level 1 is similar to "g" in gcc/clang, although for some reason # Cargo doesn't support that flag. diff --git a/rustlib/bindings.rs b/rustlib/bindings.rs deleted file mode 100644 index 995247a..0000000 --- a/rustlib/bindings.rs +++ /dev/null @@ -1,3 +0,0 @@ -/* automatically generated by rust-bindgen 0.58.1 */ - -pub const true_ : u32 = 1 ; pub const false_ : u32 = 0 ; pub const __bool_true_false_are_defined : u32 = 1 ; extern "C" { pub fn validate_param_in_c (param : :: std :: os :: raw :: c_int , value : :: std :: os :: raw :: c_int) -> bool ; } \ No newline at end of file diff --git a/rustlib/build.rs b/rustlib/build.rs index 541fc0e..af64de6 100644 --- a/rustlib/build.rs +++ b/rustlib/build.rs @@ -1,40 +1,32 @@ use std::env; -use std::fs; use std::path::{Path, PathBuf}; fn main() { let target = env::var("TARGET").unwrap(); - // let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); - + let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - // fs::remove_dir_all(&out_dir).unwrap(); - // fs::create_dir_all(&out_dir).unwrap(); - // run_cbindgen(&cargo_dir, &out_dir); + run_cbindgen(&cargo_dir, &out_dir); run_bindgen(&target, &out_dir); } -// fn run_cbindgen(_cargo_dir: &Path, _out_dir: &Path) { +fn run_cbindgen(_cargo_dir: &Path, _out_dir: &Path) { // TODO: Run cbindgen -// } +} fn run_bindgen(target: &str, out_dir: &Path) { - // if target == "xtensa-esp32-none-elf" { - // env::set_var("LLVM_CONFIG_PATH", "/usr/local/xtensa/llvm/bin/llvm-config"); - // } - let mut builder = bindgen::Builder::default(); builder = builder.header("../main/CApi.h"); - builder = builder.use_core(); - builder = builder.ctypes_prefix("ffi"); - - println!("Target: {}", target); match target { "riscv32i-unknown-none-elf" => { builder = builder.clang_arg("--target=riscv32"); + builder = builder.use_core(); + builder = builder.ctypes_prefix("crate::ffi"); } "xtensa-esp32-none-elf" => { + // Make sure that LLVM_CONFIG_PATH has been set to point to the + // Xtensa build of llvm-config. builder = builder.clang_arg("--target=xtensa-esp32-elf"); } _ => { diff --git a/rustlib/src/lib.rs b/rustlib/src/lib.rs index 3528519..952604e 100644 --- a/rustlib/src/lib.rs +++ b/rustlib/src/lib.rs @@ -1,8 +1,12 @@ -// #![no_std] +#![cfg_attr(not(feature = "std"), no_std)] +#[cfg(not(feature = "std"))] use core::panic::PanicInfo; -pub mod ffi { +/// Create aliases for FFI types for esp32c3, which doesn't have std. +#[cfg(not(feature = "std"))] +mod ffi { + #![allow(dead_code)] #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] @@ -15,28 +19,20 @@ pub mod sys { #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] - - use crate::ffi; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); } -use crate::sys::*; - - -// #[panic_handler] -// fn panic(_info: &PanicInfo) -> ! { -// loop {} -// } #[no_mangle] pub extern "C" fn add_in_rust(x: i32, y: i32) -> i32 { - - extern "C" { - fn validate_param_in_c(param: i32, value: i32) -> i32; - } - unsafe { - validate_param_in_c(0, x); - validate_param_in_c(2, y); + sys::validate_param_in_c(0, x); + sys::validate_param_in_c(2, y); } x + y } + +#[cfg(not(feature = "std"))] +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +}