libasm/
generate.rs

1use std::fs::File;
2use std::path::PathBuf;
3use std::io::Write;
4use std::env;
5use cc;
6
7use super::Asm;
8
9impl Asm {
10    #[cfg(not(windows))]
11    pub fn generate(&self) {
12        let out_dir = env::var("OUT_DIR").unwrap();
13        let path = PathBuf::from(out_dir).join(self.name.clone() + ".S");
14        let mut output = File::create(&path).unwrap();
15        writeln!(
16            output,
17            ".intel_syntax\n.text\n.globl {}\n{}:",
18            self.name, self.name
19        ).unwrap();
20        for line in &self.body {
21            writeln!(output, "  {}", line).unwrap();
22        }
23
24        writeln!(output, "  ret").unwrap();
25
26        cc::Build::new().file(&path).compile(&self.name.clone());
27    }
28
29    #[cfg(windows)]
30    pub fn generate(&self) {
31        let out_dir = env::var("OUT_DIR").unwrap();
32        let path = PathBuf::from(out_dir).join(self.name.clone() + ".asm");
33        let mut output = File::create(&path).unwrap();
34        writeln!(output, ".code\nPUBLIC {}\n{} PROC", self.name, self.name).unwrap();
35        for line in &self.body {
36            writeln!(output, "{}", line).unwrap();
37        }
38
39        writeln!(output, "ret\n{} ENDP\nEND", self.name).unwrap();
40
41        cc::Build::new().file(&path).compile(&self.name.clone());
42    }
43}