Skip to main content

miden_project/
target.rs

1use miden_assembly_syntax::Path;
2
3use crate::*;
4
5/// Represents build target configuration
6#[derive(Debug, Clone)]
7pub struct Target {
8    pub ty: TargetType,
9    /// The effective name of this target
10    ///
11    /// If unspecified in the project file, the name is the same as `namespace`
12    ///
13    /// The name must be unique within a project
14    pub name: Span<Arc<str>>,
15    /// The namespace root for this target
16    pub namespace: Span<Arc<Path>>,
17    /// The path from the project manifest to the root source file for this target
18    ///
19    /// If not provided, it is expected that source modules will be provided to the assembler
20    /// through other means. For example, `midenc` will compile Rust code to MASM, and then provide
21    /// the MASM modules to an instantiated assembler when assembling this project.
22    pub path: Option<Span<Uri>>,
23}
24
25impl Target {
26    /// Construct a new virtual executable target named `name`
27    pub fn executable(name: impl Into<Arc<str>>) -> Self {
28        Self::r#virtual(TargetType::Executable, name.into(), Path::exec_path())
29    }
30
31    /// Construct a new virtual library target named `name` with namespace `namespace`
32    pub fn library(namespace: impl Into<Arc<Path>>) -> Self {
33        let namespace = namespace.into();
34        let name: Arc<str> = namespace.as_str().into();
35        Self::r#virtual(TargetType::Library, name, namespace)
36    }
37
38    /// Construct a new virtual target of type `ty`, with the given `name` and `namespace`
39    pub fn r#virtual(
40        ty: TargetType,
41        name: impl Into<Arc<str>>,
42        namespace: impl Into<Arc<Path>>,
43    ) -> Self {
44        Self {
45            ty,
46            name: Span::unknown(name.into()),
47            namespace: Span::unknown(namespace.into()),
48            path: None,
49        }
50    }
51
52    /// Construct this [Target] with the given root module [Uri].
53    pub fn with_path(mut self, path: impl Into<Uri>) -> Self {
54        self.path = Some(Span::unknown(path.into()));
55        self
56    }
57
58    /// Returns true if this target is an executable target
59    pub const fn is_executable(&self) -> bool {
60        matches!(self.ty, TargetType::Executable)
61    }
62
63    /// Returns true if this target is a non-executable target
64    pub const fn is_library(&self) -> bool {
65        !self.is_executable()
66    }
67
68    /// Returns true if this target is a kernel target
69    pub const fn is_kernel(&self) -> bool {
70        matches!(self.ty, TargetType::Kernel)
71    }
72}