devbox_build/
build.rs

1use std::env;
2
3pub use super::cmd::Cmd;
4pub use super::fs::{File, Dir, Unit};
5pub use super::res::{Resource, Set};
6
7//-- Build -----------------------------------------------------------------------------------------
8
9/// Represents the build itself encaspsulating Cargo environment variables
10///
11/// You start the srcript by creating new `Build` instance which provides further access to
12/// project directories and standard Cargo environment variables.
13///
14/// For more info on Cargo environement variables check [Cargo env variables]
15/// [Cargo env variables]: https://doc.rust-lang.org/cargo/reference/environment-variables.html
16///
17#[derive(Debug)]
18pub struct Build {
19}
20
21impl Build {
22
23    /// Create new Build instance
24    pub fn new() -> Self {
25        Build {}
26    }
27
28    /// Current directory where the build has been run from
29    //TODO: how os it different to manifest dir?
30    pub fn current_dir(&self) -> Dir {
31        Dir::new(env::current_dir().unwrap())
32    }
33}
34
35/// Accessors for envionment variables set by Cargo when running the script
36///
37impl Build {
38
39    /// Cargo executable command
40    pub fn cargo_cmd(&self) -> Cmd { Cmd::new(env::var("CARGO").unwrap()) }
41
42    /// Rust compler executable command
43    pub fn rustc_cmd(&self) -> Cmd { Cmd::new(env::var("RUSTC").unwrap()) }
44
45    /// Rust linker executable command
46    pub fn rustc_linker_cmd(&self) -> Cmd { Cmd::new(env::var("RUSTC_LINKER").unwrap()) }
47
48    /// Rust doc executable command
49    pub fn rustdoc_cmd(&self) -> Cmd { Cmd::new(env::var("RUSTDOC").unwrap()) }
50
51    /// Directory containing the project manifest
52    pub fn manifest_dir(&self) -> Dir { Dir::new(env::var("CARGO_MANIFEST_DIR").unwrap()) }
53
54    /// Project manifest `links` value
55    pub fn manifest_links(&self) -> String { env::var("CARGO_MANIFEST_LINKS").unwrap() }
56
57    /// Directory in which all output should be placed
58    pub fn out_dir(&self) -> Dir { Dir::new(env::var("OUT_DIR").unwrap()) }
59
60    /// True if cargo profile is `release` (run with --release)
61    pub fn is_release_build(&self) -> bool { env::var("PROFILE").unwrap() == "release" }
62
63    /// True if care is being build with `feature` enabled
64    pub fn has_feature<P:AsRef<str>>(&self, feature: P) -> bool {
65        Self::prefixed_env_var("CARGO_FEATURE_", feature).is_ok()
66    }
67
68    /// Achitecure triple of the machine running the build
69    pub fn host_triple(&self) -> String { env::var("HOST").unwrap() }
70
71    /// Achitecure triple of the build binaries
72    pub fn target_triple(&self) -> String { env::var("TARGET").unwrap() }
73
74    /// Number of threads to be used by the build
75    pub fn num_jobs(&self) -> u16 { u16::from_str_radix(&env::var("NUM_JOBS").unwrap(), 10).unwrap() }
76
77    /// Return configuration (check Cargo documentation above for more info)
78    pub fn cfg<P:AsRef<str>>(&self, cfg: P) -> Option<String> {
79        Self::prefixed_env_var("CARGO_CFG_", cfg).ok()
80    }
81
82    fn prefixed_env_var<P:AsRef<str>>(prefix: &str, name: P) -> Result<String, std::env::VarError> {
83        let name = name.as_ref().to_owned().to_uppercase().replace("-", "_");
84        env::var(format!("{}_{}", prefix, name))
85    }
86}