Skip to content

Commit

Permalink
probe_or_exit for cleaner Cargo error output
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Sep 18, 2024
1 parent a6717ef commit 8ff2627
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,15 @@ pub fn find_library(name: &str) -> Result<Library, String> {
probe_library(name).map_err(|e| e.to_string())
}

/// Simple shortcut for using all default options for finding a library,
/// and exiting the build script with a verbose error message on failure.
///
/// This is preferred over `probe_library().unwrap()`, because it can
/// print a more readable output.
pub fn probe_library_or_exit(name: &str) -> Library {
Config::new().probe_or_exit(name)
}

/// Simple shortcut for using all default options for finding a library.
pub fn probe_library(name: &str) -> Result<Library, Error> {
Config::new().probe(name)
Expand Down Expand Up @@ -543,6 +552,21 @@ impl Config {
}
}

/// Clone the Config, with output buffering enabled
fn with_metadata_buffer(&self) -> Config {
Config {
statik: self.statik,
min_version: self.min_version.clone(),
max_version: self.max_version.clone(),
extra_args: self.extra_args.clone(),
print_system_cflags: self.print_system_cflags,
print_system_libs: self.print_system_libs,
cargo_metadata: self.cargo_metadata,
env_metadata: self.env_metadata,
metadata_buffer: Some(Mutex::default()),
}
}

/// Emit buffered metadata, if any
fn print_bufferred(&self) {
if let Some(mut buf) = self.metadata_buffer.as_ref().and_then(|m| m.lock().ok()) {
Expand Down Expand Up @@ -640,6 +664,34 @@ impl Config {
self.probe(name).map_err(|e| e.to_string())
}

/// Run `pkg-config` to find the library `name`, or exit the
/// build script with a verbose error.
///
/// This is preferred over `probe().unwrap()`, because it can
/// print a more readable error.
///
/// This will use all configuration previously set to specify how
/// `pkg-config` is run.
pub fn probe_or_exit(&self, name: &str) -> Library {
let buffered = self.with_metadata_buffer();
match buffered.probe(name) {
Ok(lib) => {
buffered.print_bufferred();
lib
}
Err(err) => {
println!(
"cargo:warning={}",
err.error_message().replace("\n", "\ncargo:warning=")
);
eprintln!("{}", err);

// The same error code as a panic, but it won't print an irrelevant backtrace
std::process::exit(101);
}
}
}

/// Run `pkg-config` to find the library `name`.
///
/// This will use all configuration previously set to specify how
Expand Down

0 comments on commit 8ff2627

Please sign in to comment.