From 63028c7d74ea34f378b6c139a0f117358979f277 Mon Sep 17 00:00:00 2001 From: Tomas Fabrizio Orsi Date: Mon, 5 May 2025 16:51:51 -0300 Subject: [PATCH 1/4] chore: Update documentation link Signed-off-by: Tomas Fabrizio Orsi --- docs/src/usage/cargo-miden.md | 2 +- tools/cargo-miden/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/usage/cargo-miden.md b/docs/src/usage/cargo-miden.md index 7460316d5..dbc44405d 100644 --- a/docs/src/usage/cargo-miden.md +++ b/docs/src/usage/cargo-miden.md @@ -88,4 +88,4 @@ See `midenc run --help` for the inputs file format. ## Examples -Check out the [examples](https://github.com/0xPolygonMiden/compiler/tree/next/examples) for some `cargo-miden` project examples. +Check out the [examples](https://github.com/0xMiden/compiler/tree/next/examples) for some `cargo-miden` project examples. diff --git a/tools/cargo-miden/README.md b/tools/cargo-miden/README.md index 7dd839385..8f91c49b6 100644 --- a/tools/cargo-miden/README.md +++ b/tools/cargo-miden/README.md @@ -2,4 +2,4 @@ This crate provides a cargo extension that allows you to compile Rust code to Miden VM MASM. -See the [documentation](/docs/usage/cargo-miden.md) for more details. \ No newline at end of file +See the [documentation](/docs/src/usage/cargo-miden.md) for more details. From 8ba4a80c5704664d6576d355b1ee5616dd9080f6 Mon Sep 17 00:00:00 2001 From: Tomas Fabrizio Orsi Date: Wed, 7 May 2025 13:38:37 -0300 Subject: [PATCH 2/4] First draft of adding flags to cargo miden. It adds them, but fails to build Compiler struct with --stdout Signed-off-by: Tomas Fabrizio Orsi --- tools/cargo-miden/src/compile_masm.rs | 7 ++++++ tools/cargo-miden/src/lib.rs | 31 +++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/tools/cargo-miden/src/compile_masm.rs b/tools/cargo-miden/src/compile_masm.rs index ce8f315ec..e9e94de6a 100644 --- a/tools/cargo-miden/src/compile_masm.rs +++ b/tools/cargo-miden/src/compile_masm.rs @@ -13,6 +13,7 @@ pub fn wasm_to_masm( wasm_file_path: &Path, output_folder: &Path, is_bin: bool, + flags: &Option>, ) -> Result { if !output_folder.exists() { return Err(Report::msg(format!( @@ -52,6 +53,12 @@ pub fn wasm_to_masm( args.push(entrypoint_opt.as_ref()); } + if let Some(flags) = flags { + for flag in flags { + args.push(flag.as_ref()) + } + } + let session = Rc::new(Compiler::new_session([input], None, args)); let context = Rc::new(Context::new(session)); midenc_compile::compile(context.clone())?; diff --git a/tools/cargo-miden/src/lib.rs b/tools/cargo-miden/src/lib.rs index bc4c0877c..1342e78cf 100644 --- a/tools/cargo-miden/src/lib.rs +++ b/tools/cargo-miden/src/lib.rs @@ -62,6 +62,31 @@ enum Command { New(NewCommand), } +fn detect_flags(args: I) -> Option> +where + I: IntoIterator, + T: Into + Clone, +{ + let iter = args.into_iter().map(Into::into).skip_while(|arg| arg != "--"); + let mut flags = Vec::new(); + for item in iter { + if item == "--" { + continue; + } else if !item.starts_with("-") { + break; + } else if !item.starts_with("--") { + break; + }; + flags.push(item); + } + + if !flags.is_empty() { + Some(flags) + } else { + None + } +} + fn detect_subcommand(args: I) -> Option where I: IntoIterator, @@ -106,6 +131,7 @@ where // The first argument is the cargo-miden binary path let args = args.skip_while(|arg| arg != "miden").collect::>(); let subcommand = detect_subcommand(args.clone()); + let flags = detect_flags(args.clone()); let outputs = match subcommand.as_deref() { // Check for built-in command or no command (shows help) @@ -261,8 +287,9 @@ where // so far, we only support the Miden VM programs, unless `--lib` is // specified (in our integration tests) let is_bin = !args.contains(&"--lib".to_string()); - let output = wasm_to_masm(&wasm, miden_out_dir.as_std_path(), is_bin) - .map_err(|e| anyhow::anyhow!("{e}"))?; + let output = + wasm_to_masm(&wasm, miden_out_dir.as_std_path(), is_bin, &flags) + .map_err(|e| anyhow::anyhow!("{e}"))?; outputs.push(output); } outputs From d7ab56834d5934de1ec7c9dab82103b39e41e489 Mon Sep 17 00:00:00 2001 From: Tomas Fabrizio Orsi Date: Wed, 7 May 2025 17:50:14 -0300 Subject: [PATCH 3/4] WIP: Enable double output Signed-off-by: Tomas Fabrizio Orsi --- midenc-compile/src/compiler.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/midenc-compile/src/compiler.rs b/midenc-compile/src/compiler.rs index 1a6f27ecd..c668ede91 100644 --- a/midenc-compile/src/compiler.rs +++ b/midenc-compile/src/compiler.rs @@ -461,19 +461,34 @@ impl Compiler { log::trace!(target: "driver", "current working directory = {}", cwd.display()); // Determine if a specific output file has been requested - let output_file = match self.output_file { - Some(path) => Some(OutputFile::Real(path)), - None if self.stdout => Some(OutputFile::Stdout), - None => None, + std::dbg!(&self.stdout); + std::dbg!(&self.output_file); + let output_files = match (self.output_file, self.stdout) { + (Some(path), false) => Some(vec![OutputFile::Real(path)]), + (None, true) => Some(vec![OutputFile::Stdout]), + (Some(path), true) => Some(vec![OutputFile::Real(path), OutputFile::Stdout]), + _ => None, }; // Initialize output types let mut output_types = OutputTypes::new(self.output_types).unwrap_or_else(|err| err.exit()); if output_types.is_empty() { - output_types.insert(OutputType::Masp, output_file.clone()); - } else if output_file.is_some() && output_types.get(&OutputType::Masp).is_some() { + if let Some(ref output_file) = output_files { + for file in output_file { + output_types.insert(OutputType::Masp, Some(file.clone())); + } + } else { + output_types.insert(OutputType::Masp, None); + } + } else if output_files.is_some() && output_types.get(&OutputType::Masp).is_some() { // The -o flag overrides --emit - output_types.insert(OutputType::Masp, output_file.clone()); + if let Some(ref output_file) = output_files { + for file in output_file { + output_types.insert(OutputType::Masp, Some(file.clone())); + } + } else { + output_types.insert(OutputType::Masp, None); + } } // Convert --exe or --lib to project type @@ -518,7 +533,7 @@ impl Compiler { Session::new( inputs, self.output_dir, - output_file, + output_files, target_dir, options, emitter, From 1dc14d9b9a80dcfba9c43f4f08a06acb826c14fa Mon Sep 17 00:00:00 2001 From: Tomas Fabrizio Orsi Date: Thu, 8 May 2025 11:26:59 -0300 Subject: [PATCH 4/4] Make output_files a vector inside Session::new() Signed-off-by: Tomas Fabrizio Orsi --- midenc-session/src/lib.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/midenc-session/src/lib.rs b/midenc-session/src/lib.rs index 07fbc933b..e79c0bb52 100644 --- a/midenc-session/src/lib.rs +++ b/midenc-session/src/lib.rs @@ -119,7 +119,7 @@ impl Session { pub fn new( inputs: I, output_dir: Option, - output_file: Option, + output_files: Option>, target_dir: PathBuf, options: Options, emitter: Option>, @@ -130,13 +130,13 @@ impl Session { { let inputs = inputs.into_iter().collect::>(); - Self::make(inputs, output_dir, output_file, target_dir, options, emitter, source_manager) + Self::make(inputs, output_dir, output_files, target_dir, options, emitter, source_manager) } fn make( inputs: Vec, output_dir: Option, - output_file: Option, + output_files: Option>, target_dir: PathBuf, options: Options, emitter: Option>, @@ -156,9 +156,21 @@ impl Session { .unwrap_or("".to_string()) ); log::debug!( - target: "driver", - " | output_file = {}", - output_file.as_ref().map(|of| of.to_string()).unwrap_or("".to_string()) + target: "driver", + " | output_files = {}", + output_files.as_ref() + .map(|output_files| { + output_files + .iter() + .map(|of| of.to_string()) + .reduce(|mut acc, e| { + acc.push_str(", "); + acc.push_str(e.as_str()); + acc + }) + .unwrap_or("".to_string()) + }) + .unwrap_or("".to_string()) ); log::debug!(target: "driver", " | target_dir = {}", target_dir.display()); } @@ -168,6 +180,11 @@ impl Session { emitter.unwrap_or_else(|| options.default_emitter()), )); + let output_file: Option = if let Some(output_files) = output_files { + output_files.into_iter().filter(|of| of.parent().is_some()).reduce(|acc, _| acc) + } else { + None + }; let output_dir = output_dir .as_deref() .or_else(|| output_file.as_ref().and_then(|of| of.parent()))