puffin_build/library/
mod.rs

1mod build;
2mod metadata;
3
4use std::ffi::OsStr;
5use std::path::PathBuf;
6
7pub use build::{Builder, Config, Options, Sources};
8pub use metadata::Metadata;
9
10use crate::utils::make_rust_identifier;
11use crate::vendor_dir::VendorDir;
12
13#[derive(Debug, Clone)]
14pub struct Library {
15    name: String,
16    config: Config,
17    metadata: Metadata,
18    vendor_dir: VendorDir,
19}
20
21impl Library {
22    pub fn new(
23        name: impl Into<String>,
24        config: impl Into<Config>,
25        metadata: impl Into<Metadata>,
26        vendor_dir: VendorDir,
27    ) -> Self {
28        Self {
29            name: name.into(),
30            config: config.into(),
31            metadata: metadata.into(),
32            vendor_dir,
33        }
34    }
35
36    pub fn id(&self) -> String {
37        make_rust_identifier(self.name())
38    }
39
40    pub fn name(&self) -> String {
41        self.name.clone()
42    }
43
44    pub fn path(&self) -> PathBuf {
45        self.vendor_dir.path_for(&self.name)
46    }
47
48    pub fn config(&self) -> &Config {
49        &self.config
50    }
51
52    pub fn metadata(&self) -> &Metadata {
53        &self.metadata
54    }
55
56    pub fn include_dirs(&self) -> Vec<PathBuf> {
57        let mut dirs = vec![self.path().join("include")];
58        let vendor_src = self.path().join("src").join("vendor");
59        if vendor_src.exists() {
60            dirs.push(vendor_src);
61        }
62        dirs
63    }
64
65    pub fn link_libraries(&self) -> Vec<PathBuf> {
66        std::fs::read_dir(self.path().join("lib"))
67            .map(|read_dir| {
68                read_dir
69                    .filter_map(|x| x.ok())
70                    .map(|x| x.path())
71                    .filter(|path| path.extension() == Some(OsStr::new("a")))
72                    .collect()
73            })
74            .unwrap_or_default()
75    }
76}