Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo extension for compiling Rust to MASM #61

Merged
merged 22 commits into from
Dec 12, 2023
Merged

Conversation

greenhat
Copy link
Contributor

@greenhat greenhat commented Nov 8, 2023

Close #60

This PR introduces a cargo extension in the cargo-miden crate, which supports the following commands:

  • miden build - compiles the selected target to MASM
  • miden new - creates a new project from a template. The template is downloaded from a git repository.

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 overarching idea is
to introduce specific to Miden subcommands (e.g., new) and pass the
rest of the subcommands to cargo (e.g., build) and process the produced artifacts.

If the wasm32-was target is not present, it will be installed.

cargo-ext/src/compile.rs Outdated Show resolved Hide resolved
@greenhat greenhat marked this pull request as ready for review November 15, 2023 12:19
@greenhat
Copy link
Contributor Author

@bobbinth Could you please create a new repo miden-project-template (or another name), for the template of the Miden project to be used by the cargo extension when creating a new project with cargo miden new command? I temporarily made https://github.com/greenhat/miden-project-template, but I cannot transfer it because github, for some reason, requires that I have a right to create a repo in the target organization. I'll copy the repo content ASAP. Thank you!

@greenhat greenhat force-pushed the greenhat/ssa-missing-def-i49 branch 2 times, most recently from 46d0dbc to 0727932 Compare November 16, 2023 09:37
@bobbinth
Copy link
Contributor

@bobbinth Could you please create a new repo miden-project-template (or another name), for the template of the Miden project to be used by the cargo extension when creating a new project with cargo miden new command?

What kind of projects would this be for? For example, if this is to create smart contracts (i.e., account code), it should probably be called miden-account-template, and then in the future we'd have also miden-note-template (or something like this).

One other question: do we need to create a separate repo per command? Or could we create one repo (e.g., miden-rust-templates) and then put all templates into that repo?

@bitwalker
Copy link
Contributor

What kind of projects would this be for? For example, if this is to create smart contracts (i.e., account code), it should probably be called miden-account-template, and then in the future we'd have also miden-note-template (or something like this).

I think this is probably just for a simple Rust project template initially.

One other question: do we need to create a separate repo per command? Or could we create one repo (e.g., miden-rust-templates) and then put all templates into that repo?

We need a repo per template I believe, so if we have three project templates, that's three separate repos. I'm not sure if we can put all templates in a single repo or not, @greenhat would be able to say for sure.

@greenhat
Copy link
Contributor Author

What kind of projects would this be for? For example, if this is to create smart contracts (i.e., account code), it should probably be called miden-account-template, and then in the future we'd have also miden-note-template (or something like this).

I think this is probably just for a simple Rust project template initially.

Yes, so far, it's just compiling a lib target to the MASM module. No smart contract stuff yet. I assume we want to jump on account code right away and get back to dealing with generic Miden VM programs/libs later. So, I either evolve this template or create a new one for the account code.

One other question: do we need to create a separate repo per command? Or could we create one repo (e.g., miden-rust-templates) and then put all templates into that repo?

We need a repo per template I believe, so if we have three project templates, that's three separate repos. I'm not sure if we can put all templates in a single repo or not, @greenhat would be able to say for sure.

Good question! cargo-generate supports multiple templates in a single repo. And although initially, I wanted to have a single repo per template, I cannot find a good reason for it. It's not public facing, meaning no one will clone it, and the code cannot be built by cargo anyway. I think it makes sense to have a single repo for all templates. miden-rust-templates sounds good to me.

@greenhat greenhat marked this pull request as draft November 17, 2023 14:33
@bobbinth
Copy link
Contributor

@greenhat - I've created rust-templates and you should have write access to it. Let me know if anything else is needed.

@greenhat greenhat linked an issue Nov 20, 2023 that may be closed by this pull request
@greenhat greenhat changed the base branch from greenhat/ssa-missing-def-i49 to main November 20, 2023 10:52
…put folder in cargo extension

Add WasmTranslationConfig::module_name_fallback to use if Wasm module
doesn't specify a name in the custom section.
In `Session::emit` if the path is a directory, emit to a file in that directory
named after a compiled item(module) name.
@greenhat greenhat marked this pull request as ready for review November 21, 2023 15:16
@greenhat
Copy link
Contributor Author

@bitwalker ready for review

Copy link
Contributor

@bitwalker bitwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

There's a few things I'd like to change about the way the extension works, and a few cosmetic things, but overall this looks good to me. I think we can probably have this merged tomorrow, but let me know if some of the changes I've suggested will complicate that and we can discuss - but I can either pick up where you leave off tomorrow, or we can just wait an extra day or whatever.

Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
cargo-ext/src/cli_commands.rs Outdated Show resolved Hide resolved
#[command(next_display_order(10), name = "compile")]
Compile {
/// The target environment to compile for
#[arg(long = "target", value_name = "TARGET", default_value_t = TargetEnv::Base, display_order(2))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can change long = "target" to just target, and remove the display_order flag (unless there is a specific need for it that isn't apparent from the context).

cargo-ext/src/cli_commands.rs Outdated Show resolved Hide resolved
}
let wasm_file_path = wasm_artifacts[0].clone();
match project_type {
ProjectType::Program => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will have enough project metadata with the other changes I've mentioned that we can just filter the CompilerArtifact messages. We either have a matching artifact or we don't have any once the build finishes.

Copy link
Contributor Author

@greenhat greenhat Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we'll go the route I outlined in #61 (comment), I think we might get away without specifying ProjectType to midenc or add an AutoDetect variant. If not, we can peek inside the Wasm component binary to determine what we're building. We may need this in other parts of our ecosystem anyway.

cargo-ext/src/cli_commands.rs Outdated Show resolved Hide resolved
cargo-ext/src/new_project.rs Outdated Show resolved Hide resolved
midenc-compile/src/stages/parse.rs Show resolved Hide resolved
@@ -285,13 +285,17 @@ impl Session {
if self.should_emit(output_type) {
match self.output_files.path(output_type) {
OutputFile::Real(path) => {
let path = if let Some(name) = item.name() {
path.with_file_name(name.as_str())
let file_path = if path.is_dir() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output file path is guaranteed to always be a file path, not a directory, so this check is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my case, it's a folder since I'm passing a target folder as output_dir in Session.

Copy link
Contributor

@bitwalker bitwalker Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the OutputFile API is designed to always return a file path, so if it's returning a directory there's a bug somewhere. I'll take a closer look

@greenhat greenhat marked this pull request as draft December 5, 2023 13:58
@greenhat
Copy link
Contributor Author

greenhat commented Dec 5, 2023

@bitwalker Thank you for the review! I addressed all your comments. I converted this PR to draft and I'm working on it. I outlined for myself what is left to do in the PR description.

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`).
@greenhat greenhat marked this pull request as ready for review December 7, 2023 16:08
@greenhat
Copy link
Contributor Author

greenhat commented Dec 7, 2023

@bitwalker I finished implementing all the review notes and it's ready for review. Please check the updated PR description for a summary.

Copy link
Contributor

@bitwalker bitwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'll need to look into the OutputFile thing I mentioned, but I'm not going to hold this PR up for that

@bitwalker bitwalker merged commit 1732c01 into main Dec 12, 2023
1 check passed
@bitwalker bitwalker deleted the greenhat/cargo-ext-i60 branch December 12, 2023 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cargo extension for compiling Rust code to MASM
3 participants