openvm_benchmarks_utils/
lib.rs1use std::{
2 fs::read,
3 path::{Path, PathBuf},
4};
5
6use cargo_metadata::Package;
7use eyre::Result;
8use openvm_build::{build_guest_package, get_package, guest_methods, GuestOptions};
9use openvm_transpiler::{elf::Elf, openvm_platform::memory::MEM_SIZE};
10use tempfile::tempdir;
11
12pub fn get_fixtures_dir() -> PathBuf {
13 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../fixtures")
14}
15
16pub fn get_programs_dir() -> PathBuf {
17 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../guest")
18}
19
20pub fn build_elf(manifest_dir: &PathBuf, profile: impl ToString) -> Result<Elf> {
21 let pkg = get_package(manifest_dir);
22 build_elf_with_path(&pkg, profile, None)
23}
24
25pub fn build_elf_with_path(
26 pkg: &Package,
27 profile: impl ToString,
28 elf_path: Option<&PathBuf>,
29) -> Result<Elf> {
30 let temp_dir = tempdir()?;
32 let target_dir = temp_dir.path();
33
34 let guest_opts = GuestOptions::default()
36 .with_target_dir(target_dir)
37 .with_profile(profile.to_string());
38
39 if let Err(Some(code)) = build_guest_package(pkg, &guest_opts, None, &None) {
40 std::process::exit(code);
41 }
42
43 let temp_elf_path = guest_methods(pkg, target_dir, &guest_opts.features, &guest_opts.profile)
45 .pop()
46 .unwrap();
47
48 if let Some(dest_path) = elf_path {
50 if let Some(parent) = dest_path.parent() {
52 if !parent.exists() {
53 std::fs::create_dir_all(parent)?;
54 }
55 }
56
57 std::fs::copy(&temp_elf_path, dest_path)?;
59 }
60
61 read_elf_file(&temp_elf_path)
62}
63
64pub fn get_elf_path(manifest_dir: &PathBuf) -> PathBuf {
65 let pkg = get_package(manifest_dir);
66 get_elf_path_with_pkg(manifest_dir, &pkg)
67}
68
69pub fn get_elf_path_with_pkg(manifest_dir: &Path, pkg: &Package) -> PathBuf {
70 let elf_file_name = format!("{}.elf", &pkg.name);
71 manifest_dir.join("elf").join(elf_file_name)
72}
73
74pub fn read_elf_file(elf_path: &PathBuf) -> Result<Elf> {
75 let data = read(elf_path)?;
76 Elf::decode(&data, MEM_SIZE as u32)
77}