puffin_build/harness/
mod.rs

1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3
4use itertools::Itertools;
5
6use crate::error::{Error, Result};
7use crate::library::Library;
8use crate::puffin;
9use crate::utils::cmake;
10
11mod bundle;
12mod put;
13
14pub use bundle::{Bundle, BundleBuilder};
15pub use put::Put;
16
17pub fn bundle(puts: Vec<Put>) -> BundleBuilder {
18    BundleBuilder::new(puts)
19}
20
21#[derive(Debug, Clone)]
22pub struct Harness {
23    library: Library,
24    path: PathBuf,
25}
26
27impl Harness {
28    pub fn harness_for(protocol: impl AsRef<str>, library: impl Into<Library>) -> Option<Self> {
29        let library = library.into();
30        let harness_dir = puffin::project_dir().join(format!(
31            "{protocol}puffin/harness/{vendor}",
32            protocol = protocol.as_ref(),
33            vendor = library.metadata().vendor
34        ));
35
36        harness_dir.exists().then_some(Harness {
37            library,
38            path: harness_dir,
39        })
40    }
41
42    pub fn wrap(&self, out_dir: impl AsRef<Path>) -> Result<Put> {
43        let mut cmake_conf = cmake::command("harness", &out_dir);
44        let mut cflags: Vec<String> = vec![
45            format!("-DPUT_ID={id}", id = self.library.id()),
46            format!("-DREGISTER={id}", id = self.library.id()),
47        ];
48
49        if self
50            .library
51            .metadata()
52            .instrumentation
53            .contains(&String::from("claimer"))
54        {
55            cflags.push("-DHAS_CLAIMS".into());
56        }
57
58        if self
59            .library
60            .metadata()
61            .instrumentation
62            .contains(&String::from("sancov"))
63        {
64            cflags.push("-fsanitize-coverage=trace-pc-guard".into());
65        }
66
67        cmake_conf
68            .cfg_args
69            .push(format!("-DHARNESS={}", self.path.display()));
70
71        cmake_conf.cfg_args.push(format!(
72            "-DINCLUDE_DIRS={}",
73            self.library
74                .include_dirs()
75                .into_iter()
76                .map(|p| p.to_string_lossy().into_owned())
77                .join(",")
78        ));
79
80        cmake_conf.cfg_args.push(format!(
81            "-DLINK_LIBRARIES={}",
82            self.library
83                .link_libraries()
84                .into_iter()
85                .map(|p| p.to_string_lossy().into_owned())
86                .join(",")
87        ));
88
89        cmake_conf
90            .cfg_args
91            .push(format!("-DPUT_ID={id}", id = self.library.id()));
92
93        cmake_conf.cfg_args.push(format!(
94            "-DCMAKE_C_FLAGS='{cflags}'",
95            cflags = cflags.join(" ")
96        ));
97
98        cmake_conf.build().map_err(|e| Error::HarnessFailed {
99            harness: Box::new(self.clone()),
100            reason: e,
101        })?;
102
103        let objects: Vec<PathBuf> = std::fs::read_dir(&out_dir)
104            .map(|read_dir| {
105                read_dir
106                    .filter_map(|x| x.ok())
107                    .map(|x| x.path())
108                    .filter(|path| path.extension() == Some(OsStr::new("o")))
109                    .collect()
110            })
111            .unwrap_or_default();
112
113        Ok(Put::new(self.library.clone(), self.clone(), objects))
114    }
115}