Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Jan 21, 2025
1 parent a098784 commit 15c0324
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 136 deletions.
2 changes: 1 addition & 1 deletion machine/emulator
Submodule emulator updated 208 files
3 changes: 2 additions & 1 deletion machine/rust-bindings/cartesi-machine-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ copy_uarch = []
# requires setting env vars LIBCARTESI_PATH and INCLUDECARTESI_PATH
external_cartesi = []

default = ["download_uarch"]
# default = ["download_uarch"]
default = ["build_uarch"]


[dependencies]
Expand Down
155 changes: 120 additions & 35 deletions machine/rust-bindings/cartesi-machine-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

use std::{env, path::PathBuf, process::Command};

mod feature_checks {
#[cfg(all(feature = "build_uarch", feature = "copy_uarch",))]
compile_error!("Features `build_uarch` and `copy_uarch` are mutually exclusive");

#[cfg(all(feature = "build_uarch", feature = "download_uarch"))]
compile_error!("Features `build_uarch` and `download_uarch` are mutually exclusive");

#[cfg(all(feature = "copy_uarch", feature = "download_uarch"))]
compile_error!("Features `copy_uarch`, and `download_uarch` are mutually exclusive");

#[cfg(not(any(
feature = "copy_uarch",
feature = "download_uarch",
feature = "build_uarch",
feature = "external_cartesi",
)))]
compile_error!("At least one of `build_uarch`, `copy_uarch`, `download_uarch`, and `external_cartesi` must be set");
}

fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

Expand Down Expand Up @@ -78,29 +58,33 @@ fn main() {
// machine api
let machine_bindings = bindgen::Builder::default()
.header(include_path.join("machine-c-api.h").to_str().unwrap())
.merge_extern_blocks(true)
.generate_comments(false)
.prepend_enum_name(false)
.translate_enum_integer_types(true)
.generate()
.expect("Unable to generate machine bindings");

// htif constants
let htif = bindgen::Builder::default()
.header(include_path.join("htif-defines.h").to_str().unwrap())
.generate()
.expect("Unable to generate htif bindings");
// // htif constants
// let htif = bindgen::Builder::default()
// .header(libdir_path.join("htif-defines.h").to_str().unwrap())
// .generate()
// .expect("Unable to generate htif bindings");

// pma constants
let pma = bindgen::Builder::default()
.header(include_path.join("pma-defines.h").to_str().unwrap())
.generate()
.expect("Unable to generate pma bindings");
// // pma constants
// let pma = bindgen::Builder::default()
// .header(libdir_path.join("pma-defines.h").to_str().unwrap())
// .generate()
// .expect("Unable to generate pma bindings");

// Write the bindings to the `$OUT_DIR/bindings.rs` and `$OUT_DIR/htif.rs` files.
machine_bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write machine bindings");
htif.write_to_file(out_path.join("htif.rs"))
.expect("Couldn't write htif defines");
pma.write_to_file(out_path.join("pma.rs"))
.expect("Couldn't write pma defines");
// htif.write_to_file(out_path.join("htif.rs"))
// .expect("Couldn't write htif defines");
// pma.write_to_file(out_path.join("pma.rs"))
// .expect("Couldn't write pma defines");

// Setup reruns
println!("cargo:rerun-if-changed=build.rs");
Expand Down Expand Up @@ -131,7 +115,9 @@ mod build_cm {
}
}

//
// Build and link emulator
//

// build dependencies
Command::new("make")
Expand Down Expand Up @@ -282,7 +268,106 @@ mod build_cm {
}
}

#[allow(dead_code)]
#[cfg(feature = "download_uarch")]
mod download {
use bytes::Bytes;
use std::fs::OpenOptions;
use std::{
io::{self, Read, Write},
path::Path,
process::{Command, Stdio},
};

fn download_uarch(machine_dir_path: &PathBuf) {
// apply git patche for 0.18.1
let patch_file = machine_dir_path.join("add-generated-files.diff");

download_git_patch(&patch_file, "v0.18.1");
apply_git_patch(&patch_file, machine_dir_path);
}

fn download_git_patch(patch_file: &PathBuf, target_tag: &str) {
let emulator_git_url = "https://github.com/cartesi/machine-emulator";

let patch_url = format!(
"{}/releases/download/{}/add-generated-files.diff",
emulator_git_url, target_tag,
);

// get
let diff_data = reqwest::blocking::get(patch_url)
.expect("error downloading diff of generated files")
.bytes()
.expect("error getting diff request body");

// write to file
write_bytes_to_file(patch_file.to_str().unwrap(), diff_data)
.expect("failed to write `add-generated-files.diff`");
}

fn apply_git_patch(patch_file: &Path, target_dir: &Path) {
// Open the patch file
let mut patch = fs::File::open(patch_file).expect("fail to open patch file");

// Create a command to run `patch -Np0`
let mut cmd = Command::new("patch")
.arg("-Np0")
.stdin(Stdio::piped())
.current_dir(target_dir)
.spawn()
.expect("fail to spawn patch command");

// Write the contents of the patch file to the command's stdin
if let Some(ref mut stdin) = cmd.stdin {
let mut buffer = Vec::new();
patch
.read_to_end(&mut buffer)
.expect("fail to read patch content");
stdin
.write_all(&buffer)
.expect("fail to write patch to pipe");
}

// Wait for the command to complete
let status = cmd.wait().expect("fail to wait for patch command");

if !status.success() {
eprintln!("Patch command failed with status: {:?}", status);
}
}

fn write_bytes_to_file(path: &str, data: Bytes) -> io::Result<()> {
let mut file = OpenOptions::new()
.write(true)
.create(true)
.open(path)
.expect(&format!("failed to open file {}", path));

file.write_all(&data)?;
file.flush() // Ensure all data is written to disk
}
}

mod feature_checks {
#[cfg(all(feature = "build_uarch", feature = "copy_uarch",))]
compile_error!("Features `build_uarch` and `copy_uarch` are mutually exclusive");

#[cfg(all(feature = "build_uarch", feature = "download_uarch"))]
compile_error!("Features `build_uarch` and `download_uarch` are mutually exclusive");

#[cfg(all(feature = "copy_uarch", feature = "download_uarch"))]
compile_error!("Features `copy_uarch`, and `download_uarch` are mutually exclusive");

#[cfg(not(any(
feature = "copy_uarch",
feature = "download_uarch",
feature = "build_uarch",
feature = "external_cartesi",
)))]
compile_error!("At least one of `build_uarch`, `copy_uarch`, `download_uarch`, and `external_cartesi` must be set");
}

#[allow(unused)]
fn clean(path: &PathBuf) {
// clean build artifacts
Command::new("make")
Expand Down
4 changes: 2 additions & 2 deletions machine/rust-bindings/cartesi-machine-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

extern crate link_cplusplus;
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
include!(concat!(env!("OUT_DIR"), "/htif.rs"));
include!(concat!(env!("OUT_DIR"), "/pma.rs"));
// include!(concat!(env!("OUT_DIR"), "/htif.rs"));
// include!(concat!(env!("OUT_DIR"), "/pma.rs"));
Loading

0 comments on commit 15c0324

Please sign in to comment.