-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathoptions.rs
247 lines (214 loc) · 6.51 KB
/
options.rs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
mod add;
mod build;
mod cmin;
mod fmt;
mod init;
mod list;
mod run;
mod tmin;
pub use self::{
add::Add, build::Build, cmin::Cmin, fmt::Fmt, init::Init, list::List, run::Run, tmin::Tmin,
};
use std::fmt as stdfmt;
use std::str::FromStr;
use structopt::StructOpt;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Sanitizer {
Address,
Leak,
Memory,
Thread,
None,
}
impl stdfmt::Display for Sanitizer {
fn fmt(&self, f: &mut stdfmt::Formatter) -> stdfmt::Result {
write!(
f,
"{}",
match self {
Sanitizer::Address => "address",
Sanitizer::Leak => "leak",
Sanitizer::Memory => "memory",
Sanitizer::Thread => "thread",
Sanitizer::None => "",
}
)
}
}
impl FromStr for Sanitizer {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"address" => Ok(Sanitizer::Address),
"leak" => Ok(Sanitizer::Leak),
"memory" => Ok(Sanitizer::Memory),
"thread" => Ok(Sanitizer::Thread),
"none" => Ok(Sanitizer::None),
_ => Err(format!("unknown sanitizer: {}", s)),
}
}
}
#[derive(Clone, Debug, StructOpt, PartialEq)]
pub struct BuildOptions {
#[structopt(short = "D", long = "dev", conflicts_with = "release")]
/// Build artifacts in development mode, without optimizations
pub dev: bool,
#[structopt(short = "O", long = "release", conflicts_with = "dev")]
/// Build artifacts in release mode, with optimizations
pub release: bool,
#[structopt(short = "a", long = "debug-assertions")]
/// Build artifacts with debug assertions and overflow checks enabled (default if not -O)
pub debug_assertions: bool,
/// Build target with verbose output from `cargo build`
#[structopt(short = "v", long = "verbose")]
pub verbose: bool,
#[structopt(long = "no-default-features")]
/// Build artifacts with default Cargo features disabled
pub no_default_features: bool,
#[structopt(
long = "all-features",
conflicts_with = "no-default-features",
conflicts_with = "features"
)]
/// Build artifacts with all Cargo features enabled
pub all_features: bool,
#[structopt(long = "features")]
/// Build artifacts with given Cargo feature enabled
pub features: Option<String>,
#[structopt(
short = "s",
long = "sanitizer",
possible_values(&["address", "leak", "memory", "thread", "none"]),
default_value = "address",
)]
/// Use a specific sanitizer
pub sanitizer: Sanitizer,
#[structopt(
name = "triple",
long = "target",
default_value(crate::utils::default_target())
)]
/// Target triple of the fuzz target
pub triple: String,
#[structopt(short = "Z", value_name = "FLAG")]
/// Unstable (nightly-only) flags to Cargo
pub unstable_flags: Vec<String>,
#[structopt(long = "target-dir")]
/// Target dir option to pass to cargo build.
pub target_dir: Option<String>,
}
impl stdfmt::Display for BuildOptions {
fn fmt(&self, f: &mut stdfmt::Formatter) -> stdfmt::Result {
if self.dev {
write!(f, " -D")?;
}
if self.release {
write!(f, " -O")?;
}
if self.debug_assertions {
write!(f, " -a")?;
}
if self.verbose {
write!(f, " -v")?;
}
if self.no_default_features {
write!(f, " --no-default-features")?;
}
if self.all_features {
write!(f, " --all-features")?;
}
if let Some(feature) = &self.features {
write!(f, " --features={}", feature)?;
}
match self.sanitizer {
Sanitizer::None => write!(f, " --sanitizer=none")?,
Sanitizer::Address => {}
_ => write!(f, " --sanitizer={}", self.sanitizer)?,
}
if self.triple != crate::utils::default_target() {
write!(f, " --target={}", self.triple)?;
}
for flag in &self.unstable_flags {
write!(f, " -Z{}", flag)?;
}
if let Some(target_dir) = &self.target_dir {
write!(f, " --target-dir={}", target_dir)?;
}
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn display_build_options() {
let default_opts = BuildOptions {
dev: false,
release: false,
debug_assertions: false,
verbose: false,
no_default_features: false,
all_features: false,
features: None,
sanitizer: Sanitizer::Address,
triple: String::from(crate::utils::default_target()),
unstable_flags: Vec::new(),
target_dir: None,
};
let opts = vec![
default_opts.clone(),
BuildOptions {
dev: true,
..default_opts.clone()
},
BuildOptions {
release: true,
..default_opts.clone()
},
BuildOptions {
debug_assertions: true,
..default_opts.clone()
},
BuildOptions {
verbose: true,
..default_opts.clone()
},
BuildOptions {
no_default_features: true,
..default_opts.clone()
},
BuildOptions {
all_features: true,
..default_opts.clone()
},
BuildOptions {
features: Some(String::from("features")),
..default_opts.clone()
},
BuildOptions {
sanitizer: Sanitizer::None,
..default_opts.clone()
},
BuildOptions {
triple: String::from("custom_triple"),
..default_opts.clone()
},
BuildOptions {
unstable_flags: vec![String::from("unstable"), String::from("flags")],
..default_opts.clone()
},
BuildOptions {
target_dir: Some(String::from("/tmp/test")),
..default_opts
},
];
for case in opts {
assert_eq!(
case,
BuildOptions::from_clap(
&BuildOptions::clap().get_matches_from(case.to_string().split(' '))
)
);
}
}
}