@@ -488,6 +488,15 @@ pub fn find_library(name: &str) -> Result<Library, String> {
488
488
probe_library ( name) . map_err ( |e| e. to_string ( ) )
489
489
}
490
490
491
+ /// Simple shortcut for using all default options for finding a library,
492
+ /// and exiting the build script with a verbose error message on failure.
493
+ ///
494
+ /// This is preferred over `probe_library().unwrap()`, because it can
495
+ /// print a more readable output.
496
+ pub fn probe_library_or_exit ( name : & str ) -> Library {
497
+ Config :: new ( ) . probe_or_exit ( name)
498
+ }
499
+
491
500
/// Simple shortcut for using all default options for finding a library.
492
501
pub fn probe_library ( name : & str ) -> Result < Library , Error > {
493
502
Config :: new ( ) . probe ( name)
@@ -530,6 +539,21 @@ impl Config {
530
539
}
531
540
}
532
541
542
+ /// Clone the Config, with output buffering enabled
543
+ fn with_metadata_buffer ( & self ) -> Config {
544
+ Config {
545
+ statik : self . statik ,
546
+ min_version : self . min_version . clone ( ) ,
547
+ max_version : self . max_version . clone ( ) ,
548
+ extra_args : self . extra_args . clone ( ) ,
549
+ print_system_cflags : self . print_system_cflags ,
550
+ print_system_libs : self . print_system_libs ,
551
+ cargo_metadata : self . cargo_metadata ,
552
+ env_metadata : self . env_metadata ,
553
+ metadata_buffer : Some ( Mutex :: default ( ) ) ,
554
+ }
555
+ }
556
+
533
557
/// Emit buffered metadata, if any
534
558
fn print_bufferred ( & self ) {
535
559
if let Some ( mut buf) = self . metadata_buffer . as_ref ( ) . and_then ( |m| m. lock ( ) . ok ( ) ) {
@@ -627,6 +651,34 @@ impl Config {
627
651
self . probe ( name) . map_err ( |e| e. to_string ( ) )
628
652
}
629
653
654
+ /// Run `pkg-config` to find the library `name`, or exit the
655
+ /// build script with a verbose error.
656
+ ///
657
+ /// This is preferred over `probe().unwrap()`, because it can
658
+ /// print a more readable error.
659
+ ///
660
+ /// This will use all configuration previously set to specify how
661
+ /// `pkg-config` is run.
662
+ pub fn probe_or_exit ( & self , name : & str ) -> Library {
663
+ let buffered = self . with_metadata_buffer ( ) ;
664
+ match buffered. probe ( name) {
665
+ Ok ( lib) => {
666
+ buffered. print_bufferred ( ) ;
667
+ lib
668
+ }
669
+ Err ( err) => {
670
+ println ! (
671
+ "cargo:warning={}" ,
672
+ err. error_message( ) . replace( "\n " , "\n cargo:warning=" )
673
+ ) ;
674
+ eprintln ! ( "{}" , err) ;
675
+
676
+ // The same error code as a panic, but it won't print an irrelevant backtrace
677
+ std:: process:: exit ( 101 ) ;
678
+ }
679
+ }
680
+ }
681
+
630
682
/// Run `pkg-config` to find the library `name`.
631
683
///
632
684
/// This will use all configuration previously set to specify how
0 commit comments