1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#[cfg(feature = "use-compiled-tools")]
pub mod compiled;

#[cfg(feature = "use-installed-tools")]
pub mod tool;

#[derive(Copy, Clone, Default)]
pub struct AssemblerOptions {
    /// Numeric IDs in the binary will have the same values as in the source.
    /// Non-numeric IDs are allocated by filling in the gaps, starting with 1
    /// and going up.
    pub preserve_numeric_ids: bool,
}

impl Into<u32> for AssemblerOptions {
    fn into(self) -> u32 {
        // This is weird, the "none" is 1, so I'm not sure if that means having
        // it disables all other options or...?
        let mut res = 0; //assembler::BinaryOptions::None as u32;

        if self.preserve_numeric_ids {
            res |= spirv_tools_sys::assembler::BinaryOptions::PreserveNumberIds as u32;
        }

        res
    }
}

pub trait Assembler: Default {
    fn with_env(target_env: crate::TargetEnv) -> Self;
    fn assemble(
        &self,
        text: &str,
        options: AssemblerOptions,
    ) -> Result<crate::binary::Binary, crate::error::Error>;
}

pub fn create(te: Option<crate::TargetEnv>) -> impl Assembler {
    let target_env = te.unwrap_or_default();

    #[cfg(feature = "use-compiled-tools")]
    {
        compiled::CompiledAssembler::with_env(target_env)
    }

    #[cfg(all(feature = "use-installed-tools", not(feature = "use-compiled-tools")))]
    {
        tool::ToolAssembler::with_env(target_env)
    }
}