Skip to content

Commit

Permalink
test: introduce SKIP_RUST env var to skip rust compilation in
Browse files Browse the repository at this point in the history
integration tests
  • Loading branch information
greenhat committed Jun 26, 2024
1 parent d8ea27a commit bf08ddb
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 60 deletions.
8 changes: 8 additions & 0 deletions tests/integration/src/cargo_proj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn panic_error(what: &str, err: impl Into<anyhow::Error>) -> ! {

pub mod paths;
use self::paths::CargoPathExt;
use crate::compiler_test::skip_rust_compilation;

/*
*
Expand Down Expand Up @@ -204,6 +205,13 @@ impl ProjectBuilder {

/// Creates the project.
pub fn build(mut self) -> Project {
let last_path_component =
self.root.root().file_name().unwrap().to_string_lossy().to_string();
if skip_rust_compilation(&self.root(), &last_path_component) {
// Return the root directory without re-creating any files
return self.root;
}

// First, clean the directory if it already exists
self.rm_root();

Expand Down
150 changes: 92 additions & 58 deletions tests/integration/src/compiler_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,75 +167,88 @@ impl CompilerTest {
/// Set the Rust source code to compile a library Cargo project to Wasm module
pub fn rust_source_cargo_lib(
cargo_project_folder: PathBuf,
artifact_name: &str,
is_build_std: bool,
entry_func_name: Option<String>,
) -> Self {
let manifest_path = cargo_project_folder.join("Cargo.toml");
let mut cargo_build_cmd = Command::new("cargo");
let compiler_workspace_dir = get_workspace_dir();
// Enable Wasm bulk-memory proposal (uses Wasm `memory.copy` op instead of `memcpy` import)
// Remap the compiler workspace directory to `~` to have a reproducible build that does not
// have the absolute local path baked into the Wasm binary
cargo_build_cmd.env(
"RUSTFLAGS",
format!(
"-C target-feature=+bulk-memory --remap-path-prefix {compiler_workspace_dir}=~"
),
);
cargo_build_cmd
.arg("build")
.arg("--manifest-path")
.arg(manifest_path)
.arg("--release")
.arg("--target=wasm32-wasi");
if is_build_std {
// compile std as part of crate graph compilation
// https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
cargo_build_cmd.arg("-Z")
let expected_wasm_artifact_path = wasm_artifact_path(&cargo_project_folder, artifact_name);
// dbg!(&wasm_artifact_path);
let wasm_artifact_path = if !skip_rust_compilation(&cargo_project_folder, artifact_name)
|| !expected_wasm_artifact_path.exists()
{
let manifest_path = cargo_project_folder.join("Cargo.toml");
let mut cargo_build_cmd = Command::new("cargo");
let compiler_workspace_dir = get_workspace_dir();
// Enable Wasm bulk-memory proposal (uses Wasm `memory.copy` op instead of `memcpy`
// import) Remap the compiler workspace directory to `~` to have a
// reproducible build that does not have the absolute local path baked into
// the Wasm binary
cargo_build_cmd.env(
"RUSTFLAGS",
format!(
"-C target-feature=+bulk-memory --remap-path-prefix {compiler_workspace_dir}=~"
),
);
cargo_build_cmd
.arg("build")
.arg("--manifest-path")
.arg(manifest_path)
.arg("--release")
.arg("--target=wasm32-wasi");
if is_build_std {
// compile std as part of crate graph compilation
// https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
cargo_build_cmd.arg("-Z")
.arg("build-std=std,core,alloc,panic_abort")
.arg("-Z")
// abort on panic without message formatting (core::fmt uses call_indirect)
.arg("build-std-features=panic_immediate_abort");
}
let mut child = cargo_build_cmd
.arg("--message-format=json-render-diagnostics")
.stdout(Stdio::piped())
.spawn()
.unwrap_or_else(|_| {
panic!(
"Failed to execute cargo build {}.",
cargo_build_cmd
.get_args()
.map(|arg| format!("'{}'", arg.to_str().unwrap()))
.collect::<Vec<_>>()
.join(" ")
)
}
let mut child = cargo_build_cmd
.arg("--message-format=json-render-diagnostics")
.stdout(Stdio::piped())
.spawn()
.unwrap_or_else(|_| {
panic!(
"Failed to execute cargo build {}.",
cargo_build_cmd
.get_args()
.map(|arg| format!("'{}'", arg.to_str().unwrap()))
.collect::<Vec<_>>()
.join(" ")
)
});

// Find the Wasm artifacts from the cargo build output for debugging purposes
let mut wasm_artifacts = find_wasm_artifacts(&mut child);
let output = child.wait().expect("Couldn't get cargo's exit status");
if !output.success() {
report_cargo_error(child);
}
assert!(output.success());
// filter out dependencies
wasm_artifacts.retain(|path| {
let path_str = path.to_str().unwrap();
!path_str.contains("release/deps")
});
let mut wasm_artifacts = find_wasm_artifacts(&mut child);
let output = child.wait().expect("Couldn't get cargo's exit status");
if !output.success() {
report_cargo_error(child);
}
assert!(output.success());
// filter out dependencies
wasm_artifacts.retain(|path| {
let path_str = path.to_str().unwrap();
!path_str.contains("release/deps")
});
// dbg!(&wasm_artifacts);
assert_eq!(wasm_artifacts.len(), 1, "Expected one Wasm artifact");
let wasm_comp_path = &wasm_artifacts.first().unwrap();
let artifact_name = wasm_comp_path.file_stem().unwrap().to_str().unwrap().to_string();
dbg!(&artifact_name);
dbg!(&wasm_artifacts);
assert_eq!(wasm_artifacts.len(), 1, "Expected one Wasm artifact");
wasm_artifacts.first().unwrap().to_path_buf()
} else {
expected_wasm_artifact_path
};

let entrypoint = entry_func_name.map(|func_name| FunctionIdent {
module: Ident::new(Symbol::intern(artifact_name.clone()), SourceSpan::default()),
module: Ident::new(Symbol::intern(artifact_name), SourceSpan::default()),
function: Ident::new(Symbol::intern(func_name.to_string()), SourceSpan::default()),
});
let input_file = InputFile::from_path(wasm_artifacts.first().unwrap()).unwrap();
let input_file = InputFile::from_path(wasm_artifact_path).unwrap();
Self {
config: WasmTranslationConfig::default(),
session: default_session(input_file),
source: CompilerTestSource::RustCargoLib { artifact_name },
source: CompilerTestSource::RustCargoLib {
artifact_name: artifact_name.to_string(),
},
entrypoint,
hir: None,
masm_program: None,
Expand Down Expand Up @@ -416,7 +429,7 @@ impl CompilerTest {
.as_str(),
)
.build();
Self::rust_source_cargo_lib(proj.root(), is_build_std, Some("entrypoint".to_string()))
Self::rust_source_cargo_lib(proj.root(), name, is_build_std, Some("entrypoint".to_string()))
}

/// Set the Rust source code to compile with `miden-stdlib-sys` (stdlib + intrinsics)
Expand Down Expand Up @@ -481,7 +494,7 @@ impl CompilerTest {
)
.build();

Self::rust_source_cargo_lib(proj.root(), is_build_std, Some("entrypoint".to_string()))
Self::rust_source_cargo_lib(proj.root(), name, is_build_std, Some("entrypoint".to_string()))
}

/// Compare the compiled Wasm against the expected output
Expand Down Expand Up @@ -564,7 +577,8 @@ impl CompilerTest {
/// The compiled Wasm component/module
fn wasm_bytes(&self) -> Vec<u8> {
match &self.session.input.file {
InputType::Real(file_path) => fs::read(file_path).unwrap(),
InputType::Real(file_path) => fs::read(file_path)
.unwrap_or_else(|_| panic!("Failed to read Wasm file: {}", file_path.display())),
InputType::Stdin { name: _, input } => input.clone(),
}
}
Expand All @@ -585,6 +599,26 @@ impl CompilerTest {
}
}

fn wasm_artifact_path(cargo_project_folder: &Path, artifact_name: &str) -> PathBuf {
cargo_project_folder
.to_path_buf()
.join("target")
.join("wasm32-wasi")
.join("release")
.join(artifact_name)
.with_extension("wasm")
}

/// Directs if we should do the Rust compilation step or not
pub fn skip_rust_compilation(cargo_project_folder: &Path, artifact_name: &str) -> bool {
let expected_wasm_artifact_path = wasm_artifact_path(cargo_project_folder, artifact_name);
let skip_rust = std::env::var("SKIP_RUST").is_ok() && expected_wasm_artifact_path.exists();
if skip_rust {
eprintln!("Skipping Rust compilation");
};
skip_rust
}

// Assemble the VM MASM program from the compiled IR MASM modules
fn masm_prog_from_modules(
user_ns_name: &str,
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/src/rust_masm_tests/rust_sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use crate::{cargo_proj::project, compiler_test::sdk_crate_path, CompilerTest};

#[test]
fn account() {
let artifact_name = "miden_sdk_account_test";
let mut test = CompilerTest::rust_source_cargo_lib(
PathBuf::from("../rust-apps-wasm/rust-sdk/account-test"),
artifact_name,
true,
None,
);
let artifact_name = test.source.artifact_name();
test.expect_wasm(expect_file![format!(
"../../expected/rust_sdk_account_test/{artifact_name}.wat"
)]);
Expand Down Expand Up @@ -85,7 +86,7 @@ fn basic_wallet() {
)
.build();

let mut test = CompilerTest::rust_source_cargo_lib(proj.root(), true, None);
let mut test = CompilerTest::rust_source_cargo_lib(proj.root(), project_name, true, None);
let artifact_name = test.source.artifact_name();
test.expect_wasm(expect_file![format!("../../expected/{project_name}/{artifact_name}.wat")]);
test.expect_ir(expect_file![format!("../../expected/{project_name}/{artifact_name}.hir")]);
Expand Down

0 comments on commit bf08ddb

Please sign in to comment.