#![deny(warnings)]
use alloc::{borrow::Cow, format, str::FromStr, sync::Arc, vec::Vec};
#[cfg(feature = "std")]
use alloc::{boxed::Box, string::ToString};
use core::fmt;
pub use miden_assembly_syntax::{
Library as CompiledLibrary, PathBuf as LibraryPath, PathComponent as LibraryPathComponent,
};
#[cfg(feature = "std")]
use miden_core::serde::Deserializable;
use miden_core_lib::CoreLibrary;
use midenc_hir_symbol::sync::LazyLock;
#[cfg(feature = "std")]
use crate::{Path, diagnostics::IntoDiagnostic};
use crate::{PathBuf, Session, TargetEnv, diagnostics::Report};
pub static STDLIB: LazyLock<Arc<CompiledLibrary>> =
LazyLock::new(|| Arc::new(CoreLibrary::default().into()));
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum LibraryKind {
#[default]
Mast,
Masm,
Masp,
}
impl fmt::Display for LibraryKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Mast => f.write_str("mast"),
Self::Masm => f.write_str("masm"),
Self::Masp => f.write_str("masp"),
}
}
}
impl FromStr for LibraryKind {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mast" | "masl" => Ok(Self::Mast),
"masm" => Ok(Self::Masm),
"masp" => Ok(Self::Masp),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkLibrary {
pub name: Cow<'static, str>,
pub path: Option<PathBuf>,
pub kind: LibraryKind,
}
impl LinkLibrary {
pub fn std() -> Self {
LinkLibrary {
name: "std".into(),
path: None,
kind: LibraryKind::Mast,
}
}
pub fn base() -> Self {
LinkLibrary {
name: "base".into(),
path: None,
kind: LibraryKind::Mast,
}
}
#[cfg(not(feature = "std"))]
pub fn load(&self, _session: &Session) -> Result<CompiledLibrary, Report> {
match self.name.as_ref() {
"std" => Ok((*STDLIB).as_ref().clone()),
"base" => Ok(miden_protocol::ProtocolLib::default().as_ref().clone()),
name => Err(Report::msg(format!(
"link library '{name}' cannot be loaded: compiler was built without standard \
library"
))),
}
}
#[cfg(feature = "std")]
pub fn load(&self, session: &Session) -> Result<CompiledLibrary, Report> {
if let Some(path) = self.path.as_deref() {
return self.load_from_path(path, session);
}
match self.name.as_ref() {
"std" => return Ok((*STDLIB).as_ref().clone()),
"base" => return Ok(miden_protocol::ProtocolLib::default().as_ref().clone()),
_ => (),
}
let path = self.find(session)?;
self.load_from_path(&path, session)
}
#[cfg(feature = "std")]
fn load_from_path(&self, path: &Path, session: &Session) -> Result<CompiledLibrary, Report> {
match self.kind {
LibraryKind::Masm => {
let ns = miden_assembly_syntax::Path::validate(&self.name).map_err(|err| {
Report::msg(format!(
"invalid library namespace '{}': {err}",
self.name.as_ref()
))
})?;
miden_assembly::Assembler::new(session.source_manager.clone())
.assemble_library_from_dir(path, ns)
}
LibraryKind::Mast => CompiledLibrary::deserialize_from_file(path).map_err(|err| {
Report::msg(format!(
"failed to deserialize library from '{}': {err}",
path.display()
))
}),
LibraryKind::Masp => {
let bytes = std::fs::read(path).into_diagnostic()?;
let package =
miden_mast_package::Package::read_from_bytes(&bytes).map_err(|e| {
Report::msg(format!(
"failed to load Miden package from {}: {e}",
path.display()
))
})?;
let lib = match package.mast {
miden_mast_package::MastArtifact::Executable(_) => {
return Err(Report::msg(format!(
"Expected Miden package to contain a Library, got Program: '{}'",
path.display()
)));
}
miden_mast_package::MastArtifact::Library(lib) => lib.clone(),
};
Ok((*lib).clone())
}
}
}
#[cfg(feature = "std")]
fn find(&self, session: &Session) -> Result<PathBuf, Report> {
use std::fs;
for search_path in session.options.search_paths.iter() {
let reader = fs::read_dir(search_path).map_err(|err| {
Report::msg(format!(
"invalid library search path '{}': {err}",
search_path.display()
))
})?;
for entry in reader {
let Ok(entry) = entry else {
continue;
};
let path = entry.path();
let Some(stem) = path.file_stem().and_then(|stem| stem.to_str()) else {
continue;
};
if stem != self.name.as_ref() {
continue;
}
match self.kind {
LibraryKind::Mast => {
if !path.is_file() {
return Err(Report::msg(format!(
"unable to load MAST library from '{}': not a file",
path.display()
)));
}
}
LibraryKind::Masm => {
if !path.is_dir() {
return Err(Report::msg(format!(
"unable to load Miden Assembly library from '{}': not a directory",
path.display()
)));
}
}
LibraryKind::Masp => {
if !path.is_file() {
return Err(Report::msg(format!(
"unable to load Miden Assembly package from '{}': not a file",
path.display()
)));
}
}
}
return Ok(path);
}
}
Err(Report::msg(format!(
"unable to locate library '{}' using any of the provided search paths",
&self.name
)))
}
}
#[cfg(feature = "std")]
impl clap::builder::ValueParserFactory for LinkLibrary {
type Parser = LinkLibraryParser;
fn value_parser() -> Self::Parser {
LinkLibraryParser
}
}
#[cfg(feature = "std")]
#[doc(hidden)]
#[derive(Clone)]
pub struct LinkLibraryParser;
#[cfg(feature = "std")]
impl clap::builder::TypedValueParser for LinkLibraryParser {
type Value = LinkLibrary;
fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
use clap::builder::PossibleValue;
Some(Box::new(
[
PossibleValue::new("masm").help("A Miden Assembly project directory"),
PossibleValue::new("masl").help("A compiled MAST library file"),
]
.into_iter(),
))
}
fn parse_ref(
&self,
_cmd: &clap::Command,
_arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::error::Error> {
use clap::error::{Error, ErrorKind};
let value = value.to_str().ok_or_else(|| Error::new(ErrorKind::InvalidUtf8))?;
let (kind, name) = value
.split_once('=')
.map(|(kind, name)| (Some(kind), name))
.unwrap_or((None, value));
if name.is_empty() {
return Err(Error::raw(
ErrorKind::ValueValidation,
"invalid link library: must specify a name or path",
));
}
let maybe_path = Path::new(name);
let extension = maybe_path.extension().map(|ext| ext.to_str().unwrap());
let kind = match kind {
Some(kind) if !kind.is_empty() => kind.parse::<LibraryKind>().map_err(|_| {
Error::raw(ErrorKind::InvalidValue, format!("'{kind}' is not a valid library kind"))
})?,
Some(_) | None => match extension {
Some(kind) => kind.parse::<LibraryKind>().map_err(|_| {
Error::raw(
ErrorKind::InvalidValue,
format!("'{kind}' is not a valid library kind"),
)
})?,
None => LibraryKind::default(),
},
};
if maybe_path.is_absolute() {
let meta = maybe_path.metadata().map_err(|err| {
Error::raw(
ErrorKind::ValueValidation,
format!(
"invalid link library: unable to load '{}': {err}",
maybe_path.display()
),
)
})?;
match kind {
LibraryKind::Mast if !meta.is_file() => {
return Err(Error::raw(
ErrorKind::ValueValidation,
format!("invalid link library: '{}' is not a file", maybe_path.display()),
));
}
LibraryKind::Masm if !meta.is_dir() => {
return Err(Error::raw(
ErrorKind::ValueValidation,
format!(
"invalid link library: kind 'masm' was specified, but '{}' is not a \
directory",
maybe_path.display()
),
));
}
_ => (),
}
let name = maybe_path.file_stem().unwrap().to_str().unwrap().to_string();
Ok(LinkLibrary {
name: name.into(),
path: Some(maybe_path.to_path_buf()),
kind,
})
} else if extension.is_some() {
let name = name.strip_suffix(unsafe { extension.unwrap_unchecked() }).unwrap();
let mut name = name.to_string();
name.pop();
Ok(LinkLibrary {
name: name.into(),
path: None,
kind,
})
} else {
Ok(LinkLibrary {
name: name.to_string().into(),
path: None,
kind,
})
}
}
}
pub fn add_target_link_libraries(
link_libraries_in: Vec<LinkLibrary>,
target: &TargetEnv,
) -> Vec<LinkLibrary> {
let mut link_libraries_out = link_libraries_in;
match target {
TargetEnv::Base | TargetEnv::Emu => {
if !link_libraries_out.iter().any(|ll| ll.name == "std") {
link_libraries_out.push(LinkLibrary::std());
}
}
TargetEnv::Rollup { .. } => {
if !link_libraries_out.iter().any(|ll| ll.name == "std") {
link_libraries_out.push(LinkLibrary::std());
}
if !link_libraries_out.iter().any(|ll| ll.name == "base") {
link_libraries_out.push(LinkLibrary::base());
}
}
}
link_libraries_out
}