Skip to content

Commit eaac2d6

Browse files
committed
sequester build and link into a separate function
1 parent 47620bc commit eaac2d6

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ once_cell = "1"
1616
cc = { version = "1.0", features = ["parallel"] }
1717

1818
[features]
19-
default = ["link"]
20-
link = []
19+
default = ["link_libfuzzer"]
20+
link_libfuzzer = []
2121
arbitrary-derive = ["arbitrary/derive"]
2222

2323
[workspace]

build.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
1-
fn main() {
2-
if cfg!(feature = "link") {
3-
println!("cargo:rerun-if-env-changed=CUSTOM_LIBFUZZER_PATH");
4-
if let Ok(custom) = ::std::env::var("CUSTOM_LIBFUZZER_PATH") {
5-
println!("cargo:rerun-if-changed={custom}");
1+
fn build_and_link_libfuzzer() {
2+
println!("cargo:rerun-if-env-changed=CUSTOM_LIBFUZZER_PATH");
3+
if let Ok(custom) = ::std::env::var("CUSTOM_LIBFUZZER_PATH") {
4+
println!("cargo:rerun-if-changed={custom}");
65

7-
let custom_lib_path = ::std::path::PathBuf::from(&custom);
8-
let custom_lib_dir = custom_lib_path.parent().unwrap().to_string_lossy();
6+
let custom_lib_path = ::std::path::PathBuf::from(&custom);
7+
let custom_lib_dir = custom_lib_path.parent().unwrap().to_string_lossy();
98

10-
let custom_lib_name = custom_lib_path.file_stem().unwrap().to_string_lossy();
11-
let custom_lib_name = custom_lib_name
12-
.strip_prefix("lib")
13-
.unwrap_or(custom_lib_name.as_ref());
9+
let custom_lib_name = custom_lib_path.file_stem().unwrap().to_string_lossy();
10+
let custom_lib_name = custom_lib_name
11+
.strip_prefix("lib")
12+
.unwrap_or(custom_lib_name.as_ref());
1413

15-
println!("cargo:rustc-link-search=native={}", custom_lib_dir);
16-
println!("cargo:rustc-link-lib=static={}", custom_lib_name);
14+
println!("cargo:rustc-link-search=native={}", custom_lib_dir);
15+
println!("cargo:rustc-link-lib=static={}", custom_lib_name);
1716

18-
match std::env::var("CUSTOM_LIBFUZZER_STD_CXX") {
19-
// Default behavior for backwards compat.
20-
Err(_) => println!("cargo:rustc-link-lib=stdc++"),
21-
Ok(s) if s == "none" => (),
22-
Ok(s) => println!("cargo:rustc-link-lib={}", s),
23-
}
24-
} else {
25-
let mut build = cc::Build::new();
26-
let sources = ::std::fs::read_dir("libfuzzer")
27-
.expect("listable source directory")
28-
.map(|de| de.expect("file in directory").path())
29-
.filter(|p| p.extension().map(|ext| ext == "cpp") == Some(true))
30-
.collect::<Vec<_>>();
31-
for source in sources.iter() {
32-
println!("cargo:rerun-if-changed={}", source.display());
33-
build.file(source.to_str().unwrap());
34-
}
35-
build.flag("-std=c++17");
36-
build.flag("-fno-omit-frame-pointer");
37-
build.flag("-w");
38-
build.cpp(true);
39-
build.compile("libfuzzer.a");
17+
match std::env::var("CUSTOM_LIBFUZZER_STD_CXX") {
18+
// Default behavior for backwards compat.
19+
Err(_) => println!("cargo:rustc-link-lib=stdc++"),
20+
Ok(s) if s == "none" => (),
21+
Ok(s) => println!("cargo:rustc-link-lib={}", s),
22+
}
23+
} else {
24+
let mut build = cc::Build::new();
25+
let sources = ::std::fs::read_dir("libfuzzer")
26+
.expect("listable source directory")
27+
.map(|de| de.expect("file in directory").path())
28+
.filter(|p| p.extension().map(|ext| ext == "cpp") == Some(true))
29+
.collect::<Vec<_>>();
30+
for source in sources.iter() {
31+
println!("cargo:rerun-if-changed={}", source.display());
32+
build.file(source.to_str().unwrap());
4033
}
34+
build.flag("-std=c++17");
35+
build.flag("-fno-omit-frame-pointer");
36+
build.flag("-w");
37+
build.cpp(true);
38+
build.compile("libfuzzer.a");
39+
}
40+
}
41+
42+
fn main() {
43+
if cfg!(feature = "link_libfuzzer") {
44+
build_and_link_libfuzzer();
4145
}
4246
}

0 commit comments

Comments
 (0)