@@ -60,45 +60,47 @@ impl Build {
6060 self
6161 }
6262
63- fn cmd_make ( & self ) -> Command {
64- let host = & self . host . as_ref ( ) . expect ( "HOST dir not set" ) [ ..] ;
65- if host. contains ( "dragonfly" )
66- || host. contains ( "freebsd" )
67- || host. contains ( "openbsd" )
68- || host. contains ( "solaris" )
69- || host. contains ( "illumos" )
70- {
71- Command :: new ( "gmake" )
72- } else {
73- Command :: new ( "make" )
74- }
63+ fn cmd_make ( & self ) -> Result < Command , & ' static str > {
64+ let host = & self . host . as_ref ( ) . ok_or ( "HOST dir not set" ) ?[ ..] ;
65+ Ok (
66+ if host. contains ( "dragonfly" )
67+ || host. contains ( "freebsd" )
68+ || host. contains ( "openbsd" )
69+ || host. contains ( "solaris" )
70+ || host. contains ( "illumos" )
71+ {
72+ Command :: new ( "gmake" )
73+ } else {
74+ Command :: new ( "make" )
75+ } ,
76+ )
7577 }
7678
7779 #[ cfg( windows) ]
78- #[ track_caller]
7980 fn check_env_var ( & self , var_name : & str ) -> Option < bool > {
80- env:: var_os ( var_name) . map ( |s| {
81+ env:: var_os ( var_name) . and_then ( |s| {
8182 if s == "1" {
8283 // a message to stdout, let user know asm is force enabled
8384 println ! (
84- "{}: nasm.exe is force enabled by the \
85+ "cargo:warning= {}: nasm.exe is force enabled by the \
8586 'OPENSSL_RUST_USE_NASM' env var.",
8687 env!( "CARGO_PKG_NAME" )
8788 ) ;
88- true
89+ Some ( true )
8990 } else if s == "0" {
9091 // a message to stdout, let user know asm is force disabled
9192 println ! (
92- "{}: nasm.exe is force disabled by the \
93+ "cargo:warning= {}: nasm.exe is force disabled by the \
9394 'OPENSSL_RUST_USE_NASM' env var.",
9495 env!( "CARGO_PKG_NAME" )
9596 ) ;
96- false
97+ Some ( false )
9798 } else {
98- panic ! (
99- "The environment variable {} is set to an unacceptable value: {:?}" ,
99+ println ! (
100+ "cargo:warning= The environment variable {} is set to an unacceptable value: {:?}" ,
100101 var_name, s
101102 ) ;
103+ None
102104 }
103105 } )
104106 }
@@ -108,11 +110,11 @@ impl Build {
108110 self . check_env_var ( "OPENSSL_RUST_USE_NASM" )
109111 . unwrap_or_else ( || {
110112 // On Windows, use cmd `where` command to check if nasm is installed
111- let wherenasm = Command :: new ( "cmd" )
113+ Command :: new ( "cmd" )
112114 . args ( & [ "/C" , "where nasm" ] )
113115 . output ( )
114- . expect ( "Failed to execute `cmd`." ) ;
115- wherenasm . status . success ( )
116+ . map ( |w| w . status . success ( ) )
117+ . unwrap_or ( false )
116118 } )
117119 }
118120
@@ -135,22 +137,22 @@ impl Build {
135137 }
136138
137139 pub fn try_build ( & mut self ) -> Result < Artifacts , String > {
138- let target = & self . target . as_ref ( ) . expect ( "TARGET dir not set" ) [ ..] ;
139- let host = & self . host . as_ref ( ) . expect ( "HOST dir not set" ) [ ..] ;
140- let out_dir = self . out_dir . as_ref ( ) . expect ( "OUT_DIR not set" ) ;
140+ let target = & self . target . as_ref ( ) . ok_or ( "TARGET dir not set" ) ? [ ..] ;
141+ let host = & self . host . as_ref ( ) . ok_or ( "HOST dir not set" ) ? [ ..] ;
142+ let out_dir = self . out_dir . as_ref ( ) . ok_or ( "OUT_DIR not set" ) ? ;
141143 let build_dir = out_dir. join ( "build" ) ;
142144 let install_dir = out_dir. join ( "install" ) ;
143145
144146 if build_dir. exists ( ) {
145- fs:: remove_dir_all ( & build_dir) . unwrap ( ) ;
147+ fs:: remove_dir_all ( & build_dir) . map_err ( |e| format ! ( "build_dir: {e}" ) ) ? ;
146148 }
147149 if install_dir. exists ( ) {
148- fs:: remove_dir_all ( & install_dir) . unwrap ( ) ;
150+ fs:: remove_dir_all ( & install_dir) . map_err ( |e| format ! ( "install_dir: {e}" ) ) ? ;
149151 }
150152
151153 let inner_dir = build_dir. join ( "src" ) ;
152- fs:: create_dir_all ( & inner_dir) . unwrap ( ) ;
153- cp_r ( & source_dir ( ) , & inner_dir) ;
154+ fs:: create_dir_all ( & inner_dir) . map_err ( |e| format ! ( "{}: {e}" , inner_dir . display ( ) ) ) ? ;
155+ cp_r ( & source_dir ( ) , & inner_dir) ? ;
154156
155157 let perl_program =
156158 env:: var ( "OPENSSL_SRC_PERL" ) . unwrap_or ( env:: var ( "PERL" ) . unwrap_or ( "perl" . to_string ( ) ) ) ;
@@ -167,7 +169,10 @@ impl Build {
167169 // native and cross builds.
168170 configure. arg ( & format ! (
169171 "--prefix={}" ,
170- install_dir. to_str( ) . unwrap( ) . replace( "\\ " , "/" )
172+ install_dir
173+ . to_str( )
174+ . ok_or( "bad install_dir" ) ?
175+ . replace( "\\ " , "/" )
171176 ) ) ;
172177 } else {
173178 configure. arg ( & format ! ( "--prefix={}" , install_dir. display( ) ) ) ;
@@ -183,7 +188,7 @@ impl Build {
183188 let openssl_dir = self
184189 . openssl_dir
185190 . as_ref ( )
186- . expect ( "path to the openssl directory must be set" ) ;
191+ . ok_or ( "path to the openssl directory must be set" ) ? ;
187192 let mut dir_arg: OsString = "--openssldir=" . into ( ) ;
188193 dir_arg. push ( openssl_dir) ;
189194 configure. arg ( dir_arg) ;
@@ -404,7 +409,12 @@ impl Build {
404409 "aarch64-unknown-linux-ohos" => "linux-aarch64" ,
405410 "armv7-unknown-linux-ohos" => "linux-generic32" ,
406411 "x86_64-unknown-linux-ohos" => "linux-x86_64" ,
407- _ => panic ! ( "don't know how to configure OpenSSL for {}" , target) ,
412+ _ => {
413+ return Err ( format ! (
414+ "don't know how to configure OpenSSL for {}" ,
415+ target
416+ ) )
417+ }
408418 } ;
409419
410420 let mut ios_isysroot: std:: option:: Option < String > = None ;
@@ -423,7 +433,7 @@ impl Build {
423433 cc_env = compiler. path ( ) . to_path_buf ( ) . into_os_string ( ) ;
424434 }
425435 configure. env ( "CC" , cc_env) ;
426- let path = compiler. path ( ) . to_str ( ) . unwrap ( ) ;
436+ let path = compiler. path ( ) . to_str ( ) . ok_or ( "compiler path" ) ? ;
427437
428438 // Both `cc::Build` and `./Configure` take into account
429439 // `CROSS_COMPILE` environment variable. So to avoid double
@@ -477,7 +487,7 @@ impl Build {
477487
478488 if is_isysroot {
479489 is_isysroot = false ;
480- ios_isysroot = Some ( arg. to_str ( ) . unwrap ( ) . to_string ( ) ) ;
490+ ios_isysroot = Some ( arg. to_str ( ) . ok_or ( "isysroot arg" ) ? . to_string ( ) ) ;
481491 continue ;
482492 }
483493 }
@@ -593,20 +603,20 @@ impl Build {
593603 // have that take a different path than the standard `make` below.
594604 if target. contains ( "msvc" ) {
595605 let mut build =
596- cc:: windows_registry:: find ( target, "nmake.exe" ) . expect ( "failed to find nmake" ) ;
606+ cc:: windows_registry:: find ( target, "nmake.exe" ) . ok_or ( "failed to find nmake" ) ? ;
597607 build. arg ( "build_libs" ) . current_dir ( & inner_dir) ;
598608 self . run_command ( build, "building OpenSSL" ) ?;
599609
600610 let mut install =
601- cc:: windows_registry:: find ( target, "nmake.exe" ) . expect ( "failed to find nmake" ) ;
611+ cc:: windows_registry:: find ( target, "nmake.exe" ) . ok_or ( "failed to find nmake" ) ? ;
602612 install. arg ( "install_dev" ) . current_dir ( & inner_dir) ;
603613 self . run_command ( install, "installing OpenSSL" ) ?;
604614 } else {
605- let mut depend = self . cmd_make ( ) ;
615+ let mut depend = self . cmd_make ( ) ? ;
606616 depend. arg ( "depend" ) . current_dir ( & inner_dir) ;
607617 self . run_command ( depend, "building OpenSSL dependencies" ) ?;
608618
609- let mut build = self . cmd_make ( ) ;
619+ let mut build = self . cmd_make ( ) ? ;
610620 build. arg ( "build_libs" ) . current_dir ( & inner_dir) ;
611621 if !cfg ! ( windows) {
612622 if let Some ( s) = env:: var_os ( "CARGO_MAKEFLAGS" ) {
@@ -622,7 +632,7 @@ impl Build {
622632
623633 self . run_command ( build, "building OpenSSL" ) ?;
624634
625- let mut install = self . cmd_make ( ) ;
635+ let mut install = self . cmd_make ( ) ? ;
626636 install. arg ( "install_dev" ) . current_dir ( & inner_dir) ;
627637 self . run_command ( install, "installing OpenSSL" ) ?;
628638 }
@@ -633,7 +643,7 @@ impl Build {
633643 vec ! [ "ssl" . to_string( ) , "crypto" . to_string( ) ]
634644 } ;
635645
636- fs:: remove_dir_all ( & inner_dir) . unwrap ( ) ;
646+ fs:: remove_dir_all ( & inner_dir) . map_err ( |e| format ! ( "{}: {e}" , inner_dir . display ( ) ) ) ? ;
637647
638648 Ok ( Artifacts {
639649 lib_dir : install_dir. join ( "lib" ) ,
@@ -675,12 +685,16 @@ impl Build {
675685 }
676686}
677687
678- #[ track_caller]
679- fn cp_r ( src : & Path , dst : & Path ) {
680- for f in fs:: read_dir ( src) . unwrap ( ) {
681- let f = f. unwrap ( ) ;
688+ fn cp_r ( src : & Path , dst : & Path ) -> Result < ( ) , String > {
689+ for f in fs:: read_dir ( src) . map_err ( |e| format ! ( "{}: {e}" , src. display( ) ) ) ? {
690+ let f = match f {
691+ Ok ( f) => f,
692+ _ => continue ,
693+ } ;
682694 let path = f. path ( ) ;
683- let name = path. file_name ( ) . unwrap ( ) ;
695+ let name = path
696+ . file_name ( )
697+ . ok_or_else ( || format ! ( "bad dir {}" , src. display( ) ) ) ?;
684698
685699 // Skip git metadata as it's been known to cause issues (#26) and
686700 // otherwise shouldn't be required
@@ -689,27 +703,32 @@ fn cp_r(src: &Path, dst: &Path) {
689703 }
690704
691705 let dst = dst. join ( name) ;
692- let ty = f. file_type ( ) . unwrap ( ) ;
706+ let ty = f. file_type ( ) . map_err ( |e| e . to_string ( ) ) ? ;
693707 if ty. is_dir ( ) {
694- fs:: create_dir_all ( & dst) . unwrap ( ) ;
695- cp_r ( & path, & dst) ;
708+ fs:: create_dir_all ( & dst) . map_err ( |e| e . to_string ( ) ) ? ;
709+ cp_r ( & path, & dst) ? ;
696710 } else if ty. is_symlink ( ) && path. iter ( ) . any ( |p| p == "cloudflare-quiche" ) {
697711 // not needed to build
698712 continue ;
699713 } else {
700714 let _ = fs:: remove_file ( & dst) ;
701715 if let Err ( e) = fs:: copy ( & path, & dst) {
702- panic ! ( "failed to copy {path:?} to {dst:?}: {e}" ) ;
716+ return Err ( format ! (
717+ "failed to copy '{}' to '{}': {e}" ,
718+ path. display( ) ,
719+ dst. display( )
720+ ) ) ;
703721 }
704722 }
705723 }
724+ Ok ( ( ) )
706725}
707726
708727fn sanitize_sh ( path : & Path ) -> String {
709728 if !cfg ! ( windows) {
710- return path. to_str ( ) . unwrap ( ) . to_string ( ) ;
729+ return path. to_string_lossy ( ) . into_owned ( ) ;
711730 }
712- let path = path. to_str ( ) . unwrap ( ) . replace ( "\\ " , "/" ) ;
731+ let path = path. to_string_lossy ( ) . replace ( "\\ " , "/" ) ;
713732 return change_drive ( & path) . unwrap_or ( path) ;
714733
715734 fn change_drive ( s : & str ) -> Option < String > {
0 commit comments