Skip to content

Commit

Permalink
Merge pull request #8 from Xila-Project/Feature/WASM
Browse files Browse the repository at this point in the history
Feature/wasm
  • Loading branch information
AlixANNERAUD authored Jun 12, 2024
2 parents 01cd61a + 3dd93be commit 9327507
Show file tree
Hide file tree
Showing 74 changed files with 2,220 additions and 414 deletions.
14 changes: 11 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110
rustflags = [
"--cfg",
"espidf_time64",
] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110

[target.xtensa-esp32-espidf]
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110
rustflags = [
"--cfg",
"espidf_time64",
] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110

[env]
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "v5.1.1"
ESP_IDF_VERSION = "v5.1.1"

DEP_LV_CONFIG_PATH = { value = "Modules/Graphics/include", relative = true }
26 changes: 19 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
workspace = { members = ["Modules/Virtual_machine"] }
[workspace]
members = [
"Modules/File_system",
"Modules/Bindings",
"Modules/Shared",
"Modules/Virtual_machine",
"Modules/Virtual_machine/Tests/WASM_test",
"Modules/Bindings/Tests/WASM_test", "Modules/Graphics", "Modules/Screen", "Modules/Task",
]

[package]
name = "Xila"
Expand Down Expand Up @@ -36,6 +44,16 @@ embassy = [
[dependencies]
Virtual_machine = { path = "Modules/Virtual_machine" }

[profile.release.package.File_system_bindings_WASM_test]
# lto = true
opt-level = 's'
strip = true

[profile.release.package.Virtual_machine_WASM_test]
# lto = true
opt-level = 's'
strip = true

[target.'cfg( target_os = "espidf" )'.dependencies]
esp-idf-sys = { version = "0.34.1", features = ["binstart"] }
esp-idf-hal = "0.43.1"
Expand All @@ -45,17 +63,11 @@ embedded-sdmmc = "0.7.0"
embedded-hal = "1.0.0"
fatfs = { version = "0.3.6" }
byteorder = "1.5.0"
lvgl = { version="0.6.2", default-features=false, features = ["embedded-graphics", "unsafe_no_autoinit"] }
lvgl-sys = { version="0.6.2" }
embedded-graphics = "0.8.1"

[build-dependencies]
embuild = { version = "0.31.4", features = ["espidf"] }

[target.'cfg(target_arch = "x86_64")'.dependencies]
byteorder = "1.5.0"
sdl2 = { version = "0.36.0", features = [] }
lvgl = { version="0.6.2", features = [] }
embedded-graphics = "0.8.1"
log = { version = "0.4" }
env_logger = { version = "0.11" }
67 changes: 32 additions & 35 deletions Export.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@

clear && echo "Setting up environment variables for Rust cross-compilation"

# Variables

Linux_environment_variables="DEP_LV_CONFIG_PATH=\"$PWD/Modules/LVGL\""
Linux_target="--target x86_64-unknown-linux-gnu"
Windows_target="--target x86_64-pc-windows-gnu"

ESP32_environment_variables="MCU=esp32 DEP_LV_CONFIG_PATH=\"$PWD/Modules/LVGL\""
ESP32_target="--target xtensa-esp32-espidf --features ESP32 -Z build-std=std,panic_abort"
ESP32_S3_environment_variables="MCU=esp32s3 DEP_LV_CONFIG_PATH=\"$PWD/Modules/LVGL\""
ESP32_S3_target="--target xtensa-esp32s3-espidf --features ESP32_S3 -Z build-std=std,panic_abort"

Cargo="cargo"

# Remove all aliases
{
unalias build_linux
Expand All @@ -38,37 +25,47 @@ unalias clean
unalias format
} &> /dev/null

# Aliases
# Variables

Linux_environment_variables="DEP_LV_CONFIG_PATH=\"$PWD/Modules/LVGL\""
Linux_target="--target x86_64-unknown-linux-gnu"
Windows_target="--target x86_64-pc-windows-gnu"

ESP32_environment_variables="MCU=esp32 DEP_LV_CONFIG_PATH=\"$PWD/Modules/LVGL\""
ESP32_target="--target xtensa-esp32-espidf --features ESP32 -Z build-std=std,panic_abort"
ESP32_S3_environment_variables="MCU=esp32s3 DEP_LV_CONFIG_PATH=\"$PWD/Modules/LVGL\""
ESP32_S3_target="--target xtensa-esp32s3-espidf --features ESP32_S3 -Z build-std=std,panic_abort"

Cargo="cargo"
Cargo_esp="cargo +esp"
Clear="clear &&"
Cargo_build="$Cargo build"
alias build_linux="$Clear $Linux_environment_variables $Cargo_build $Linux_target"
alias build_windows="$Clear $Cargo_build $Windows_target"
alias build_esp32="$Clear $ESP32_environment_variables $Cargo_build $ESP32_target"
alias build_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_build $ESP32_S3_target"

Cargo_run="$Cargo run"
alias run_linux="$Clear $Linux_environment_variables $Cargo_run $Linux_target"
alias run_windows="$Clear $Cargo_run $Windows_target"
alias run_esp32="$Clear $ESP32_environment_variables $Cargo_run $ESP32_target"
alias run_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_run $ESP32_S3_target"
# Aliases
alias build_linux="$Clear $Linux_environment_variables $Cargo build $Linux_target"
alias run_linux="$Clear $Linux_environment_variables $Cargo run $Linux_target"
alias test_linux="$Clear RUST_MIN_STACK=8388608 $Linux_environment_variables $Cargo test $Linux_target"
alias check_linux="$Clear $Linux_environment_variables $Cargo clippy $Linux_target"

alias build_windows="$Clear $Cargo build $Windows_target"
alias run_windows="$Clear $Cargo run $Windows_target"
alias test_windows="$Clear $Cargo test $Windows_target"
alias check_windows="$Clear $Cargo check $Windows_target"

Cargo_test="RUST_MIN_STACK=8388608 $Cargo test"
alias test_linux="$Clear $Linux_environment_variables $Cargo_test $Linux_target"
alias test_windows="$Clear $Cargo_test $Windows_target"
alias test_esp32="$Clear $ESP32_environment_variables $Cargo_test $ESP32_target"
alias test_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_test $ESP32_S3_target"
alias build_esp32="$Clear $ESP32_environment_variables $Cargo_esp build $ESP32_target"
alias run_esp32="$Clear $ESP32_environment_variables $Cargo_esp run $ESP32_target"
alias test_esp32="$Clear $ESP32_environment_variables $Cargo_esp test $ESP32_target"
alias check_esp32="$Clear $ESP32_environment_variables $Cargo_esp clippy $ESP32_target"

Cargo_check="$Cargo clippy"
alias check_linux="$Clear $Linux_environment_variables $Cargo_check $Linux_target"
alias check_windows="$Clear $Cargo_check $Windows_target"
alias check_esp32="$Clear $ESP32_environment_variables $Cargo_check $ESP32_target"
alias check_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_check $ESP32_S3_target"
alias build_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_esp build $ESP32_S3_target"
alias run_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_esp run $ESP32_S3_target"
alias test_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_esp test $ESP32_S3_target"
alias check_esp32_s3="$Clear $ESP32_S3_environment_variables $Cargo_esp clippy $ESP32_S3_target"

alias clean="$Cargo clean"

alias doc="$Linux_environment_variables $Cargo doc"

alias format="$Cargo fmt"
alias format="$Cargo fmt --all"

export RUST_BACKTRACE=1

Expand Down
28 changes: 28 additions & 0 deletions Modules/Bindings/Build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![allow(non_snake_case)]
use std::{env, process::Command};

fn main() -> Result<(), ()> {
println!("cargo:rerun-if-changed=Tests/WASM_test/src/main.rs");
println!("cargo:rerun-if-changed=Tests/WASM_test/Cargo.toml");

// TODO : Add a check for test mode

if env::var("RUSTUP_TOOLCHAIN").unwrap().contains("esp") {
println!("cargo:warning=Build of WASM tests are disabled for esp toolchain.");
return Ok(());
}

let output = Command::new("cargo")
.args(["build", "--release"])
.current_dir("Tests/WASM_test")
.output()
.unwrap();

if !output.status.success() {
println! {"cargo:warning=stderr: {}", String::from_utf8_lossy(&output.stderr)};
println! {"cargo:warning=status: {}", output.status};
return Err(());
}

Ok(())
}
15 changes: 15 additions & 0 deletions Modules/Bindings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "Bindings"
version = "0.1.0"
edition = "2021"
build = "Build.rs"

[dependencies]
File_system = { path = "../File_system" }
Virtual_machine = { path = "../Virtual_machine" }
Binding_tool = { path = "../../../Binding_tool", features = ["Native"] }
Shared = { path = "../Shared" }

[[test]]
name = "File_system_bindings_tests"
path = "Tests/File_system.rs"
25 changes: 25 additions & 0 deletions Modules/Bindings/Tests/File_system.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]

use Bindings::File_system_bindings;
use File_system::{Drivers::Native::File_system_type, Prelude::File_system_traits};
use Virtual_machine::{Data_type, Instantiate_test_environment, WasmValue};

#[test]
fn Integration_test() {
let Binary_buffer = include_bytes!(
"../../../target/wasm32-unknown-unknown/release/File_system_bindings_WASM_test.wasm"
);

let mut Native_file_system = File_system_type::New();

Native_file_system.Initialize().unwrap();

let User_data = Data_type::New(&Native_file_system);

let (_Runtime, _Module, Instance) =
Instantiate_test_environment(Binary_buffer, File_system_bindings {}, &User_data);

assert_eq!(Instance.Call_main(&vec![]).unwrap(), WasmValue::I32(0))
}
10 changes: 10 additions & 0 deletions Modules/Bindings/Tests/WASM_test/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build]
target = "wasm32-unknown-unknown"
rustflags = [
"-C", "link-arg=--initial-memory=65536",
"-C", "link-arg=-zstack-size=8192",
"-C", "link-arg=--export=__heap_base",
"-C", "link-arg=--export=__data_end",
"-C", "link-arg=--strip-all",
"-C", "lto=yes",
]
10 changes: 10 additions & 0 deletions Modules/Bindings/Tests/WASM_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "File_system_bindings_WASM_test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Binding_tool = { path = "../../../../../Binding_tool", features = ["WASM"] }
wee_alloc = "0.4.5"
43 changes: 43 additions & 0 deletions Modules/Bindings/Tests/WASM_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#![allow(non_snake_case)]

use Binding_tool::Bind_function_WASM;

#[Bind_function_WASM]
fn Open_file(Path: &str, Mode: u32, File_identifier: &mut u16) -> u32 {}

#[Bind_function_WASM]
fn Read_file(File_identifier: u16, Buffer: &mut [u8], Read_size: &mut u32) -> u32 {}

extern crate wee_alloc;

// Use `wee_alloc` as the global allocator saving kilobytes of code size
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

fn main() -> Result<(), ()> {
let mut File_identifier = 1;

let Result = Open_file("test.txt", 1, &mut File_identifier);

if Result != 0 {
return Err(());
}

let mut Buffer = [0; 1024];

let mut Read_size = 0;

let Result = Read_file(File_identifier, &mut Buffer, &mut Read_size);

if Result != 0 {
return Err(());
}

let String = String::from_utf8_lossy(&Buffer[..Read_size as usize]);

if String != "Hello World!" {
return Err(());
}

Ok(())
}
1 change: 1 addition & 0 deletions Modules/Bindings/Xila/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Loading

0 comments on commit 9327507

Please sign in to comment.