Skip to content

Commit

Permalink
cli: bundle add kernel option
Browse files Browse the repository at this point in the history
  • Loading branch information
paracetamolo committed Dec 10, 2024
1 parent cabb601 commit 2c0b5a4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
9 changes: 9 additions & 0 deletions miden/masm-examples/bundle/kernel_main.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
proc.internal_proc
caller
drop
exec.::kernel::lib::lib_proc
end

export.kernel_proc
exec.internal_proc
end
48 changes: 33 additions & 15 deletions miden/src/cli/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use assembly::{
diagnostics::{IntoDiagnostic, Report},
Assembler, Library, LibraryNamespace,
Assembler, KernelLibrary, Library, LibraryNamespace,
};
use clap::Parser;

Expand All @@ -24,6 +24,10 @@ pub struct BundleCmd {
/// Version of the library, defaults to `0.1.0`.
#[clap(short, long, default_value = "0.1.0")]
version: String,
/// Build a kernel library from module `kernel` and using the library `dir` as kernel
/// namespace.
#[clap(short, long)]
kernel: Option<PathBuf>,
}

impl BundleCmd {
Expand All @@ -32,28 +36,42 @@ impl BundleCmd {
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 assembler = Assembler::default().with_debug_mode(self.debug);
let library_namespace =
namespace.parse::<LibraryNamespace>().expect("invalid base namespace");
let library = Library::from_dir(&self.dir, library_namespace, assembler)?;

// write the masl output
let output_file = self
.dir
.join(self.namespace.as_deref().unwrap_or("out"))
.with_extension(Library::LIBRARY_EXTENSION);
library.write_to_file(output_file).into_diagnostic()?;

println!("Built library {}", namespace);
match &self.kernel {
Some(kernel) => {
assert!(kernel.is_file(), "kernel must be a file");
let library = KernelLibrary::from_dir(kernel, Some(&self.dir), assembler)?;
library.write_to_file(output_file).into_diagnostic()?;
println!(
"Built kernel module {} with library {}",
kernel.display(),
&self.dir.display()
);
},
None => {
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 =
namespace.parse::<LibraryNamespace>().expect("invalid base namespace");
let library = Library::from_dir(&self.dir, library_namespace, assembler)?;
library.write_to_file(output_file).into_diagnostic()?;
println!("Built library {}", namespace);
},
}

Ok(())
}
Expand Down
21 changes: 21 additions & 0 deletions miden/tests/integration/cli/cli_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,24 @@ fn cli_bundle_no_exports() {
.failure()
.stderr(predicate::str::contains("library must contain at least one exported procedure"));
}

#[test]
fn cli_bundle_kernel() {
let mut cmd = bin_under_test().command();
cmd.arg("bundle")
.arg("./masm-examples/bundle/lib")
.arg("--kernel")
.arg("./masm-examples/bundle/kernel_main.masm");
cmd.assert().success();
}

/// A kernel can bundle with a library w/o exports.
#[test]
fn cli_bundle_kernel_noexports() {
let mut cmd = bin_under_test().command();
cmd.arg("bundle")
.arg("./masm-examples/bundle/lib_noexports")
.arg("--kernel")
.arg("./masm-examples/bundle/kernel_main.masm");
cmd.assert().success();
}

0 comments on commit 2c0b5a4

Please sign in to comment.