From 2c0b5a4b980e125b428034f5e3356574e7f7eab7 Mon Sep 17 00:00:00 2001 From: Marco Stronati Date: Mon, 9 Dec 2024 16:05:39 +0100 Subject: [PATCH] cli: bundle add kernel option --- miden/masm-examples/bundle/kernel_main.masm | 9 ++++ miden/src/cli/bundle.rs | 48 ++++++++++++++------- miden/tests/integration/cli/cli_test.rs | 21 +++++++++ 3 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 miden/masm-examples/bundle/kernel_main.masm diff --git a/miden/masm-examples/bundle/kernel_main.masm b/miden/masm-examples/bundle/kernel_main.masm new file mode 100644 index 000000000..62d062b28 --- /dev/null +++ b/miden/masm-examples/bundle/kernel_main.masm @@ -0,0 +1,9 @@ +proc.internal_proc + caller + drop + exec.::kernel::lib::lib_proc +end + +export.kernel_proc + exec.internal_proc +end diff --git a/miden/src/cli/bundle.rs b/miden/src/cli/bundle.rs index 7ee67708c..1552fff26 100644 --- a/miden/src/cli/bundle.rs +++ b/miden/src/cli/bundle.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use assembly::{ diagnostics::{IntoDiagnostic, Report}, - Assembler, Library, LibraryNamespace, + Assembler, KernelLibrary, Library, LibraryNamespace, }; use clap::Parser; @@ -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, } impl BundleCmd { @@ -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::().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::().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(()) } diff --git a/miden/tests/integration/cli/cli_test.rs b/miden/tests/integration/cli/cli_test.rs index f550c7c9d..c7094a207 100644 --- a/miden/tests/integration/cli/cli_test.rs +++ b/miden/tests/integration/cli/cli_test.rs @@ -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(); +}