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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
mod build;
mod metadata;

use std::ffi::OsStr;
use std::path::PathBuf;

pub use build::{Builder, Config, Options, Sources};
pub use metadata::Metadata;

use crate::utils::make_rust_identifier;
use crate::vendor_dir::VendorDir;

#[derive(Debug, Clone)]
pub struct Library {
    name: String,
    config: Config,
    metadata: Metadata,
    vendor_dir: VendorDir,
}

impl Library {
    pub fn new(
        name: impl Into<String>,
        config: impl Into<Config>,
        metadata: impl Into<Metadata>,
        vendor_dir: VendorDir,
    ) -> Self {
        Self {
            name: name.into(),
            config: config.into(),
            metadata: metadata.into(),
            vendor_dir,
        }
    }

    pub fn id(&self) -> String {
        make_rust_identifier(self.name())
    }

    pub fn name(&self) -> String {
        self.name.clone()
    }

    pub fn path(&self) -> PathBuf {
        self.vendor_dir.path_for(&self.name)
    }

    pub fn config(&self) -> &Config {
        &self.config
    }

    pub fn metadata(&self) -> &Metadata {
        &self.metadata
    }

    pub fn include_dirs(&self) -> Vec<PathBuf> {
        vec![self.path().join("include")]
    }

    pub fn link_libraries(&self) -> Vec<PathBuf> {
        std::fs::read_dir(self.path().join("lib"))
            .map(|read_dir| {
                read_dir
                    .filter_map(|x| x.ok())
                    .map(|x| x.path())
                    .filter(|path| path.extension() == Some(OsStr::new("a")))
                    .collect()
            })
            .unwrap_or_default()
    }
}