Skip to content

Commit

Permalink
Merge pull request #997 from 0xPolygonMiden/hacka-cli-compile-library
Browse files Browse the repository at this point in the history
cli: add command to compile libraries
  • Loading branch information
bobbinth authored Jul 18, 2023
2 parents dd1fa01 + a7c35cc commit 9155e63
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion assembly/src/assembler/span_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl SpanBuilder {
///
/// This indicates that the provided instruction should be tracked and the cycle count for
/// this instruction will be computed when the call to set_instruction_cycle_count() is made.
pub fn track_instruction(&mut self, instruction: &Instruction, ctx: &mut AssemblyContext) {
pub fn track_instruction(&mut self, instruction: &Instruction, ctx: &AssemblyContext) {
let context_name = ctx.current_context_name().to_string();
let num_cycles = 0;
let op = instruction.to_string();
Expand Down
2 changes: 1 addition & 1 deletion assembly/src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Module {

impl PartialOrd for Module {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.path.partial_cmp(&other.path)
Some(self.cmp(other))
}
}

Expand Down
57 changes: 57 additions & 0 deletions miden/src/cli/bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use assembly::{LibraryNamespace, MaslLibrary, Version};
use std::path::PathBuf;
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(
name = "Compile Library",
about = "Bundles .masm files into a single .masl library"
)]
pub struct BundleCmd {
/// Path to a directory containing the `.masm` files which are part of the library.
#[structopt(parse(from_os_str))]
dir: PathBuf,
/// Defines the top-level namespace, e.g. `mylib`, otherwise the directory name is used.
#[structopt(short, long)]
namespace: Option<String>,
/// Version of the library, defaults to `0.1.0`.
#[structopt(short, long, default_value = "0.1.0")]
version: String,
}

impl BundleCmd {
pub fn execute(&self) -> Result<(), String> {
println!("============================================================");
println!("Build library");
println!("============================================================");

let namespace = match &self.namespace {
Some(namespace) => namespace.to_string(),
None => self
.dir
.file_name()
.expect("dir must be a folder")
.to_string_lossy()
.into_owned(),
};

let library_namespace =
LibraryNamespace::try_from(namespace.clone()).expect("invalid base namespace");
let version = Version::try_from(self.version.as_ref()).expect("invalid cargo version");
let with_source_locations = true;
let stdlib = MaslLibrary::read_from_dir(
self.dir.clone(),
library_namespace,
with_source_locations,
version,
)
.map_err(|e| e.to_string())?;

// write the masl output
stdlib.write_to_dir(self.dir.clone()).map_err(|e| e.to_string())?;

println!("Built library {}", namespace);

Ok(())
}
}
2 changes: 2 additions & 0 deletions miden/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bundle;
mod compile;
mod data;
mod debug;
Expand All @@ -6,6 +7,7 @@ mod repl;
mod run;
mod verify;

pub use bundle::BundleCmd;
pub use compile::CompileCmd;
pub use data::InputFile;
pub use debug::DebugCmd;
Expand Down
2 changes: 2 additions & 0 deletions miden/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Cli {
pub enum Actions {
Analyze(tools::Analyze),
Compile(cli::CompileCmd),
Bundle(cli::BundleCmd),
Debug(cli::DebugCmd),
Example(examples::ExampleOptions),
Prove(cli::ProveCmd),
Expand All @@ -35,6 +36,7 @@ impl Cli {
match &self.action {
Actions::Analyze(analyze) => analyze.execute(),
Actions::Compile(compile) => compile.execute(),
Actions::Bundle(compile) => compile.execute(),
Actions::Debug(debug) => debug.execute(),
Actions::Example(example) => example.execute(),
Actions::Prove(prove) => prove.execute(),
Expand Down

0 comments on commit 9155e63

Please sign in to comment.