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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::{convert::Into, path::Path, process::Command, string::String, vec::Vec};
pub mod clang;
pub use clang::{ClangWrapper, LLVMPasses};
#[derive(Debug)]
pub enum Error {
InvalidArguments(String),
Io(std::io::Error),
Unknown(String),
}
#[cfg(windows)]
pub const LIB_EXT: &str = "lib";
#[cfg(not(windows))]
pub const LIB_EXT: &str = "a";
#[cfg(windows)]
pub const LIB_PREFIX: &str = "";
#[cfg(not(windows))]
pub const LIB_PREFIX: &str = "lib";
pub trait CompilerWrapper {
fn from_args<S>(&mut self, args: &[S]) -> Result<&'_ mut Self, Error>
where
S: AsRef<str>;
fn add_arg<S>(&mut self, arg: S) -> &'_ mut Self
where
S: AsRef<str>;
fn add_cc_arg<S>(&mut self, arg: S) -> &'_ mut Self
where
S: AsRef<str>;
fn add_link_arg<S>(&mut self, arg: S) -> &'_ mut Self
where
S: AsRef<str>;
fn add_args<S>(&mut self, args: &[S]) -> &'_ mut Self
where
S: AsRef<str>,
{
for arg in args {
self.add_arg(arg);
}
self
}
fn add_cc_args<S>(&mut self, args: &[S]) -> &'_ mut Self
where
S: AsRef<str>,
{
for arg in args {
self.add_cc_arg(arg);
}
self
}
fn add_link_args<S>(&mut self, args: &[S]) -> &'_ mut Self
where
S: AsRef<str>,
{
for arg in args {
self.add_link_arg(arg);
}
self
}
fn link_staticlib<S>(&mut self, dir: &Path, name: S) -> &'_ mut Self
where
S: AsRef<str>;
fn command(&mut self) -> Result<Vec<String>, Error>;
fn is_linking(&self) -> bool;
fn silence(&mut self, value: bool) -> &'_ mut Self;
fn is_silent(&self) -> bool;
fn run(&mut self) -> Result<Option<i32>, Error> {
let args = self.command()?;
if !self.is_silent() {
dbg!(&args);
}
if args.is_empty() {
return Err(Error::InvalidArguments(
"The number of arguments cannot be 0".into(),
));
}
let status = match Command::new(&args[0]).args(&args[1..]).status() {
Ok(s) => s,
Err(e) => return Err(Error::Io(e)),
};
if !self.is_silent() {
dbg!(status);
}
Ok(status.code())
}
}