Skip to content

Commit 02b199f

Browse files
authored
Merge pull request #485 from 0xMiden/greenhat/i368-path-dependency
feature(cargo-miden): parse local `path` Miden dependencies from `Cargo.toml`
2 parents 4837727 + 9aa5cac commit 02b199f

File tree

10 files changed

+412
-100
lines changed

10 files changed

+412
-100
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/src/compiler_test.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub struct CompilerTestBuilder {
182182
/// The extra MASM modules to link to the compiled MASM program
183183
link_masm_modules: LinkMasmModules,
184184
/// Extra flags to pass to the midenc driver
185-
midenc_flags: Vec<Cow<'static, str>>,
185+
midenc_flags: Vec<String>,
186186
/// Extra RUSTFLAGS to set when compiling Rust code
187187
rustflags: Vec<Cow<'static, str>>,
188188
/// The cargo workspace directory of the compiler
@@ -225,8 +225,7 @@ impl CompilerTestBuilder {
225225
]);
226226
let mut midenc_flags = vec!["--debug".into(), "--verbose".into()];
227227
if let Some(entrypoint) = entrypoint {
228-
midenc_flags
229-
.extend(["--entrypoint".into(), format!("{}", entrypoint.display()).into()]);
228+
midenc_flags.extend(["--entrypoint".into(), format!("{}", entrypoint.display())]);
230229
}
231230
Self {
232231
config: Default::default(),
@@ -269,7 +268,7 @@ impl CompilerTestBuilder {
269268
None => (),
270269
}
271270
self.midenc_flags
272-
.extend(["--entrypoint".into(), format!("{}", entrypoint.display()).into()]);
271+
.extend(["--entrypoint".into(), format!("{}", entrypoint.display())]);
273272
self
274273
}
275274

@@ -278,7 +277,7 @@ impl CompilerTestBuilder {
278277
&mut self,
279278
flags: impl IntoIterator<Item = Cow<'static, str>>,
280279
) -> &mut Self {
281-
self.midenc_flags.extend(flags);
280+
self.midenc_flags.extend(flags.into_iter().map(|s| s.to_string()));
282281
self
283282
}
284283

@@ -306,7 +305,7 @@ impl CompilerTestBuilder {
306305

307306
/// Consume the builder, invoke any tools required to obtain the inputs for the test, and if
308307
/// successful, return a [CompilerTest], ready for evaluation.
309-
pub fn build(self) -> CompilerTest {
308+
pub fn build(mut self) -> CompilerTest {
310309
// Set up the command used to compile the test inputs (typically Rust -> Wasm)
311310
let mut command = match self.source {
312311
CompilerTestInputType::CargoMiden(_) => {
@@ -398,31 +397,26 @@ impl CompilerTestBuilder {
398397

399398
// Build test
400399
match self.source {
401-
CompilerTestInputType::CargoMiden(config) => {
402-
let expected_wasm_artifact_path = config.wasm_artifact_path();
403-
let skip_rust_compilation =
404-
std::env::var("SKIP_RUST").is_ok() && expected_wasm_artifact_path.exists();
405-
let wasm_artifact_path = if !skip_rust_compilation {
406-
let mut args = vec![command.get_program().to_str().unwrap().to_string()];
407-
let cmd_args: Vec<String> = command
408-
.get_args()
409-
.collect::<Vec<&OsStr>>()
410-
.iter()
411-
.map(|s| s.to_str().unwrap().to_string())
412-
.collect();
413-
args.extend(cmd_args);
414-
let wasm_artifacts =
415-
cargo_miden::run(args.into_iter(), cargo_miden::OutputType::Wasm).unwrap();
416-
assert_eq!(
417-
wasm_artifacts.len(),
418-
1,
419-
"expected one Wasm artifact, got {:?}",
420-
wasm_artifacts
421-
);
422-
wasm_artifacts.first().unwrap().clone()
423-
} else {
424-
drop(command);
425-
expected_wasm_artifact_path
400+
CompilerTestInputType::CargoMiden(..) => {
401+
let mut args = vec![command.get_program().to_str().unwrap().to_string()];
402+
let cmd_args: Vec<String> = command
403+
.get_args()
404+
.collect::<Vec<&OsStr>>()
405+
.iter()
406+
.map(|s| s.to_str().unwrap().to_string())
407+
.collect();
408+
args.extend(cmd_args);
409+
let build_output =
410+
cargo_miden::run(args.into_iter(), cargo_miden::OutputType::Wasm)
411+
.unwrap()
412+
.expect("'cargo miden build' should return Some(CommandOutput)")
413+
.unwrap_build_output(); // Use the new method
414+
let (wasm_artifact_path, dependencies) = match build_output {
415+
cargo_miden::BuildOutput::Wasm {
416+
artifact_path,
417+
dependencies,
418+
} => (artifact_path, dependencies),
419+
other => panic!("Expected Wasm output, got {:?}", other),
426420
};
427421
let artifact_name =
428422
wasm_artifact_path.file_stem().unwrap().to_str().unwrap().to_string();
@@ -440,6 +434,10 @@ impl CompilerTestBuilder {
440434
}));
441435
// dbg!(&inputs);
442436

437+
for dep in &dependencies {
438+
self.midenc_flags.push("--link-library".to_string());
439+
self.midenc_flags.push(dep.to_str().unwrap().to_string());
440+
}
443441
let context = setup::default_context(inputs, &self.midenc_flags);
444442
let session = context.session_rc();
445443
CompilerTest {
@@ -448,6 +446,7 @@ impl CompilerTestBuilder {
448446
context,
449447
artifact_name: artifact_name.into(),
450448
entrypoint: self.entrypoint,
449+
dependencies,
451450
..Default::default()
452451
}
453452
}
@@ -874,6 +873,8 @@ pub struct CompilerTest {
874873
ir_masm_program: Option<Result<Arc<midenc_codegen_masm::MasmComponent>, String>>,
875874
/// The compiled package containing a program executable by the VM
876875
package: Option<Result<Arc<miden_mast_package::Package>, String>>,
876+
/// Miden packages for dependencies
877+
pub dependencies: Vec<PathBuf>,
877878
}
878879

879880
impl fmt::Debug for CompilerTest {
@@ -907,6 +908,7 @@ impl Default for CompilerTest {
907908
masm_src: None,
908909
ir_masm_program: None,
909910
package: None,
911+
dependencies: Vec::new(),
910912
}
911913
}
912914
}

tests/integration/src/rust_masm_tests/rust_sdk.rs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,14 @@ fn rust_sdk_p2id_note_script() {
9090
.map(|s| s.to_string())
9191
.collect();
9292
dbg!(env::current_dir().unwrap().display());
93-
let outputs = cargo_miden::run(args.into_iter(), cargo_miden::OutputType::Masm)
94-
.expect("Failed to compile the basic-wallet package");
95-
let masp_path: PathBuf = outputs.first().unwrap().clone();
93+
let build_output = cargo_miden::run(args.into_iter(), cargo_miden::OutputType::Masm)
94+
.expect("Failed to compile the basic-wallet package")
95+
.expect("'cargo miden build' for basic-wallet should return Some(CommandOutput)")
96+
.unwrap_build_output(); // Use the new method
97+
let masp_path = match build_output {
98+
cargo_miden::BuildOutput::Masm { artifact_path } => artifact_path,
99+
other => panic!("Expected Masm output for basic-wallet, got {:?}", other),
100+
};
96101
dbg!(&masp_path);
97102

98103
//
@@ -163,46 +168,14 @@ fn rust_sdk_cross_ctx_account() {
163168

164169
#[test]
165170
fn rust_sdk_cross_ctx_note() {
166-
// Build cross-ctx-account package
167-
let args: Vec<String> = [
168-
"cargo",
169-
"miden",
170-
"build",
171-
"--manifest-path",
172-
"../rust-apps-wasm/rust-sdk/cross-ctx-account/Cargo.toml",
173-
"--lib",
174-
"--release",
175-
// Use the target dir of this test's cargo project to avoid issues running tests in parallel
176-
// i.e. avoid using the same target dir as the basic-wallet test (see above)
177-
"--target-dir",
178-
"../rust-apps-wasm/rust-sdk/cross-ctx-note/target",
179-
]
180-
.iter()
181-
.map(|s| s.to_string())
182-
.collect();
183-
dbg!(env::current_dir().unwrap().display());
184-
185-
let outputs = cargo_miden::run(args.into_iter(), cargo_miden::OutputType::Masm)
186-
.expect("Failed to compile the cross-ctx-account package for cross-ctx-note");
187-
let masp_path: PathBuf = outputs.first().unwrap().clone();
188-
189-
dbg!(&masp_path);
190-
191171
let _ = env_logger::builder().is_test(true).try_init();
192172

193173
let config = WasmTranslationConfig::default();
194174

195175
let mut builder = CompilerTestBuilder::rust_source_cargo_miden(
196176
"../rust-apps-wasm/rust-sdk/cross-ctx-note",
197177
config,
198-
[
199-
"-l".into(),
200-
"std".into(),
201-
"-l".into(),
202-
"base".into(),
203-
"--link-library".into(),
204-
masp_path.clone().into_os_string().into_string().unwrap().into(),
205-
],
178+
["-l".into(), "std".into(), "-l".into(), "base".into()],
206179
);
207180
builder.with_entrypoint(FunctionIdent {
208181
module: Ident::new(Symbol::intern("miden:base/[email protected]"), SourceSpan::default()),
@@ -215,10 +188,12 @@ fn rust_sdk_cross_ctx_note() {
215188
test.expect_masm(expect_file![format!("../../expected/rust_sdk/{artifact_name}.masm")]);
216189
let package = test.compiled_package();
217190
let mut exec = Executor::new(vec![]);
218-
let account_package =
219-
Arc::new(Package::read_from_bytes(&std::fs::read(masp_path).unwrap()).unwrap());
220-
exec.dependency_resolver_mut()
221-
.add(account_package.digest(), account_package.into());
191+
for dep_path in test.dependencies {
192+
let account_package =
193+
Arc::new(Package::read_from_bytes(&std::fs::read(dep_path).unwrap()).unwrap());
194+
exec.dependency_resolver_mut()
195+
.add(account_package.digest(), account_package.into());
196+
}
222197
exec.with_dependencies(&package.manifest.dependencies).unwrap();
223198
let trace = exec.execute(&package.unwrap_program(), &test.session);
224199
}

tests/rust-apps-wasm/rust-sdk/cross-ctx-note/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ overflow-checks = false
3939
debug = true
4040
trim-paths = ["diagnostics", "object"]
4141

42+
# Miden dependencies for cargo-miden build/linking
43+
[package.metadata.miden.dependencies]
44+
"miden:cross-ctx-account" = { path = "../cross-ctx-account" }
45+
4246
# TODO: switch to miden table
4347
[package.metadata.component]
4448
package = "miden:cross-ctx-note"

tools/cargo-miden/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ cargo-generate = "0.23"
3535
path-absolutize = "3.1.1"
3636
tokio.workspace = true
3737
cargo-config2 = "0.1.24"
38+
serde.workspace = true
39+
toml.workspace = true
40+
serde_json = "1.0"
3841

3942
[dev-dependencies]
4043
miden-mast-package.workspace = true

tools/cargo-miden/src/compile_masm.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn wasm_to_masm(
1313
wasm_file_path: &Path,
1414
output_folder: &Path,
1515
is_bin: bool,
16+
dependency_paths: &[PathBuf], // New parameter
1617
) -> Result<PathBuf, Report> {
1718
if !output_folder.exists() {
1819
return Err(Report::msg(format!(
@@ -52,6 +53,13 @@ pub fn wasm_to_masm(
5253
args.push(entrypoint_opt.as_ref());
5354
}
5455

56+
// Add dependency linker arguments (Step 3.3)
57+
for dep_path in dependency_paths {
58+
args.push("--link-library".as_ref());
59+
// Ensure the path is valid OsStr
60+
args.push(dep_path.as_os_str());
61+
}
62+
5563
let session = Rc::new(Compiler::new_session([input], None, args));
5664
let context = Rc::new(Context::new(session));
5765
midenc_compile::compile(context.clone())?;

0 commit comments

Comments
 (0)