Skip to content

Commit

Permalink
feat: revamp cargo-miden to pass the unrecognized options to cargo,
Browse files Browse the repository at this point in the history
and pick up the artifacts from the cargo build to compile the MASM.
Similar to the way cargo-component does it.

Keep in mind that this cargo extension is planned to be the cargo-component
replacement when it comes to building/publishing/etc the Wasm/Miden components.
This should explain some implementation details. The overaching idea is
to introduce specific to Miden subcommands (e.g. `new`) and pass the
rest of the subcommands to cargo where its (e.g. `build`).
  • Loading branch information
greenhat committed Dec 7, 2023
1 parent 253d491 commit 6e563ef
Show file tree
Hide file tree
Showing 18 changed files with 3,943 additions and 464 deletions.
2,820 changes: 2,647 additions & 173 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions tools/cargo-miden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ path = "tests/mod.rs"
midenc-compile.workspace = true
midenc-session.workspace = true
miden-diagnostics.workspace = true
env_logger.workspace = true
log.workspace = true
clap.workspace = true
anyhow.workspace = true
cargo-component = "0.5"
cargo-component-core = "0.5"
cargo_metadata = "0.18"
cargo-generate = "0.18"
semver = "1.0.20"
parse_arg = "0.1.4"
path-absolutize = "3.1.1"

[dev-dependencies]
12 changes: 2 additions & 10 deletions tools/cargo-miden/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ To install the extension, run:
cargo install cargo-miden
```

## Requirements

Since Rust is first compiled to Wasm, you'll need to have the `wasm32-unknown-unknown` target installed:

```bash
rustup target add wasm32-unknown-unknown
```

## Usage

### Getting help
Expand All @@ -44,7 +36,7 @@ cargo miden new <project-name>
To compile a Rust crate to Miden VM MASM, run:

```bash
cargo miden compile -o <output-file>
cargo miden build
```

Without any additional arguments, this will compile the library target of the crate in the current directory.
Without any additional arguments, this will compile the library target in the target directory in the `miden` folder.
64 changes: 64 additions & 0 deletions tools/cargo-miden/src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::{
path::{Path, PathBuf},
sync::Arc,
};

use anyhow::{bail, Context};
use miden_diagnostics::Verbosity;
use midenc_session::{
InputFile, OutputFile, OutputType, OutputTypeSpec, OutputTypes, ProjectType, Session, TargetEnv,
};

pub fn build_masm(
wasm_file_path: &Path,
output_folder: &Path,
is_bin: bool,
) -> anyhow::Result<PathBuf> {
let project_type = if is_bin {
ProjectType::Program
} else {
ProjectType::Library
};

if !output_folder.exists() {
bail!(
"MASM output folder '{}' does not exist.",
output_folder.to_str().unwrap()
);
}
log::debug!(
"Compiling '{}' Wasm to '{}' directory with midenc ...",
wasm_file_path.to_str().unwrap(),
&output_folder.to_str().unwrap()
);
let input = InputFile::from_path(wasm_file_path).context("Invalid input file")?;
let output_file_folder = OutputFile::Real(output_folder.to_path_buf());
let output_type = OutputType::Masm;
let output_types = OutputTypes::new(vec![OutputTypeSpec {
output_type,
path: Some(output_file_folder.clone()),
}]);
let cwd = std::env::current_dir().context("Failed to get current working directory")?;
let options = midenc_session::Options::new(cwd)
// .with_color(color)
.with_verbosity(Verbosity::Debug)
// .with_warnings(self.warn)
.with_output_types(output_types);
let target = TargetEnv::default();
let session = Arc::new(
Session::new(
target,
input,
Some(output_folder.to_path_buf()),
None,
None,
options,
None,
)
.with_project_type(project_type),
);
midenc_compile::compile(session.clone()).context("Wasm to MASM compilation failed!")?;
let mut output_path = output_folder.join(wasm_file_path.file_stem().unwrap());
output_path.set_extension(output_type.extension());
Ok(output_path)
}
48 changes: 0 additions & 48 deletions tools/cargo-miden/src/cli_commands.rs

This file was deleted.

129 changes: 0 additions & 129 deletions tools/cargo-miden/src/compile.rs

This file was deleted.

Loading

0 comments on commit 6e563ef

Please sign in to comment.