Skip to content

Commit 47620bc

Browse files
committed
Make it possible to link to custom libfuzzer in another dep
1 parent df3ac96 commit 47620bc

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

Cargo.toml

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

1818
[features]
19+
default = ["link"]
20+
link = []
1921
arbitrary-derive = ["arbitrary/derive"]
2022

2123
[workspace]

build.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
fn main() {
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}");
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}");
56

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

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());
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());
1314

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

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),
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");
2240
}
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());
33-
}
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");
3941
}
4042
}

0 commit comments

Comments
 (0)