Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to link to custom runtime in another dep #106

Merged
merged 4 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ once_cell = "1"
cc = { version = "1.0", features = ["parallel"] }

[features]
default = ["link"]
link = []
arbitrary-derive = ["arbitrary/derive"]

[workspace]
Expand Down
66 changes: 34 additions & 32 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
fn main() {
println!("cargo:rerun-if-env-changed=CUSTOM_LIBFUZZER_PATH");
if let Ok(custom) = ::std::env::var("CUSTOM_LIBFUZZER_PATH") {
println!("cargo:rerun-if-changed={custom}");
if cfg!(feature = "link") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move all the linking code out to a new function that is also cfg(feature = "link") so that it is easier to read this giant if with no else?

println!("cargo:rerun-if-env-changed=CUSTOM_LIBFUZZER_PATH");
if let Ok(custom) = ::std::env::var("CUSTOM_LIBFUZZER_PATH") {
println!("cargo:rerun-if-changed={custom}");

let custom_lib_path = ::std::path::PathBuf::from(&custom);
let custom_lib_dir = custom_lib_path.parent().unwrap().to_string_lossy();
let custom_lib_path = ::std::path::PathBuf::from(&custom);
let custom_lib_dir = custom_lib_path.parent().unwrap().to_string_lossy();

let custom_lib_name = custom_lib_path.file_stem().unwrap().to_string_lossy();
let custom_lib_name = custom_lib_name
.strip_prefix("lib")
.unwrap_or(custom_lib_name.as_ref());
let custom_lib_name = custom_lib_path.file_stem().unwrap().to_string_lossy();
let custom_lib_name = custom_lib_name
.strip_prefix("lib")
.unwrap_or(custom_lib_name.as_ref());

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

match std::env::var("CUSTOM_LIBFUZZER_STD_CXX") {
// Default behavior for backwards compat.
Err(_) => println!("cargo:rustc-link-lib=stdc++"),
Ok(s) if s == "none" => (),
Ok(s) => println!("cargo:rustc-link-lib={}", s),
match std::env::var("CUSTOM_LIBFUZZER_STD_CXX") {
// Default behavior for backwards compat.
Err(_) => println!("cargo:rustc-link-lib=stdc++"),
Ok(s) if s == "none" => (),
Ok(s) => println!("cargo:rustc-link-lib={}", s),
}
} else {
let mut build = cc::Build::new();
let sources = ::std::fs::read_dir("libfuzzer")
.expect("listable source directory")
.map(|de| de.expect("file in directory").path())
.filter(|p| p.extension().map(|ext| ext == "cpp") == Some(true))
.collect::<Vec<_>>();
for source in sources.iter() {
println!("cargo:rerun-if-changed={}", source.display());
build.file(source.to_str().unwrap());
}
build.flag("-std=c++17");
build.flag("-fno-omit-frame-pointer");
build.flag("-w");
build.cpp(true);
build.compile("libfuzzer.a");
}
} else {
let mut build = cc::Build::new();
let sources = ::std::fs::read_dir("libfuzzer")
.expect("listable source directory")
.map(|de| de.expect("file in directory").path())
.filter(|p| p.extension().map(|ext| ext == "cpp") == Some(true))
.collect::<Vec<_>>();
for source in sources.iter() {
println!("cargo:rerun-if-changed={}", source.display());
build.file(source.to_str().unwrap());
}
build.flag("-std=c++17");
build.flag("-fno-omit-frame-pointer");
build.flag("-w");
build.cpp(true);
build.compile("libfuzzer.a");
}
}