@@ -60,45 +60,47 @@ impl Build {
60
60
self
61
61
}
62
62
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
+ )
75
77
}
76
78
77
79
#[ cfg( windows) ]
78
- #[ track_caller]
79
80
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| {
81
82
if s == "1" {
82
83
// a message to stdout, let user know asm is force enabled
83
84
println ! (
84
- "{}: nasm.exe is force enabled by the \
85
+ "cargo:warning= {}: nasm.exe is force enabled by the \
85
86
'OPENSSL_RUST_USE_NASM' env var.",
86
87
env!( "CARGO_PKG_NAME" )
87
88
) ;
88
- true
89
+ Some ( true )
89
90
} else if s == "0" {
90
91
// a message to stdout, let user know asm is force disabled
91
92
println ! (
92
- "{}: nasm.exe is force disabled by the \
93
+ "cargo:warning= {}: nasm.exe is force disabled by the \
93
94
'OPENSSL_RUST_USE_NASM' env var.",
94
95
env!( "CARGO_PKG_NAME" )
95
96
) ;
96
- false
97
+ Some ( false )
97
98
} 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: {:?}" ,
100
101
var_name, s
101
102
) ;
103
+ None
102
104
}
103
105
} )
104
106
}
@@ -108,11 +110,11 @@ impl Build {
108
110
self . check_env_var ( "OPENSSL_RUST_USE_NASM" )
109
111
. unwrap_or_else ( || {
110
112
// On Windows, use cmd `where` command to check if nasm is installed
111
- let wherenasm = Command :: new ( "cmd" )
113
+ Command :: new ( "cmd" )
112
114
. args ( & [ "/C" , "where nasm" ] )
113
115
. output ( )
114
- . expect ( "Failed to execute `cmd`." ) ;
115
- wherenasm . status . success ( )
116
+ . map ( |w| w . status . success ( ) )
117
+ . unwrap_or ( false )
116
118
} )
117
119
}
118
120
@@ -135,22 +137,22 @@ impl Build {
135
137
}
136
138
137
139
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" ) ? ;
141
143
let build_dir = out_dir. join ( "build" ) ;
142
144
let install_dir = out_dir. join ( "install" ) ;
143
145
144
146
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}" ) ) ? ;
146
148
}
147
149
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}" ) ) ? ;
149
151
}
150
152
151
153
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) ? ;
154
156
155
157
let perl_program =
156
158
env:: var ( "OPENSSL_SRC_PERL" ) . unwrap_or ( env:: var ( "PERL" ) . unwrap_or ( "perl" . to_string ( ) ) ) ;
@@ -167,7 +169,10 @@ impl Build {
167
169
// native and cross builds.
168
170
configure. arg ( & format ! (
169
171
"--prefix={}" ,
170
- install_dir. to_str( ) . unwrap( ) . replace( "\\ " , "/" )
172
+ install_dir
173
+ . to_str( )
174
+ . ok_or( "bad install_dir" ) ?
175
+ . replace( "\\ " , "/" )
171
176
) ) ;
172
177
} else {
173
178
configure. arg ( & format ! ( "--prefix={}" , install_dir. display( ) ) ) ;
@@ -183,7 +188,7 @@ impl Build {
183
188
let openssl_dir = self
184
189
. openssl_dir
185
190
. as_ref ( )
186
- . expect ( "path to the openssl directory must be set" ) ;
191
+ . ok_or ( "path to the openssl directory must be set" ) ? ;
187
192
let mut dir_arg: OsString = "--openssldir=" . into ( ) ;
188
193
dir_arg. push ( openssl_dir) ;
189
194
configure. arg ( dir_arg) ;
@@ -404,7 +409,7 @@ impl Build {
404
409
"aarch64-unknown-linux-ohos" => "linux-aarch64" ,
405
410
"armv7-unknown-linux-ohos" => "linux-generic32" ,
406
411
"x86_64-unknown-linux-ohos" => "linux-x86_64" ,
407
- _ => panic ! ( "don't know how to configure OpenSSL for {}" , target) ,
412
+ _ => return Err ( format ! ( "don't know how to configure OpenSSL for {}" , target) ) ,
408
413
} ;
409
414
410
415
let mut ios_isysroot: std:: option:: Option < String > = None ;
@@ -423,7 +428,7 @@ impl Build {
423
428
cc_env = compiler. path ( ) . to_path_buf ( ) . into_os_string ( ) ;
424
429
}
425
430
configure. env ( "CC" , cc_env) ;
426
- let path = compiler. path ( ) . to_str ( ) . unwrap ( ) ;
431
+ let path = compiler. path ( ) . to_str ( ) . ok_or ( "compiler path" ) ? ;
427
432
428
433
// Both `cc::Build` and `./Configure` take into account
429
434
// `CROSS_COMPILE` environment variable. So to avoid double
@@ -477,7 +482,7 @@ impl Build {
477
482
478
483
if is_isysroot {
479
484
is_isysroot = false ;
480
- ios_isysroot = Some ( arg. to_str ( ) . unwrap ( ) . to_string ( ) ) ;
485
+ ios_isysroot = Some ( arg. to_str ( ) . ok_or ( "isysroot arg" ) ? . to_string ( ) ) ;
481
486
continue ;
482
487
}
483
488
}
@@ -593,20 +598,20 @@ impl Build {
593
598
// have that take a different path than the standard `make` below.
594
599
if target. contains ( "msvc" ) {
595
600
let mut build =
596
- cc:: windows_registry:: find ( target, "nmake.exe" ) . expect ( "failed to find nmake" ) ;
601
+ cc:: windows_registry:: find ( target, "nmake.exe" ) . ok_or ( "failed to find nmake" ) ? ;
597
602
build. arg ( "build_libs" ) . current_dir ( & inner_dir) ;
598
603
self . run_command ( build, "building OpenSSL" ) ?;
599
604
600
605
let mut install =
601
- 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" ) ? ;
602
607
install. arg ( "install_dev" ) . current_dir ( & inner_dir) ;
603
608
self . run_command ( install, "installing OpenSSL" ) ?;
604
609
} else {
605
- let mut depend = self . cmd_make ( ) ;
610
+ let mut depend = self . cmd_make ( ) ? ;
606
611
depend. arg ( "depend" ) . current_dir ( & inner_dir) ;
607
612
self . run_command ( depend, "building OpenSSL dependencies" ) ?;
608
613
609
- let mut build = self . cmd_make ( ) ;
614
+ let mut build = self . cmd_make ( ) ? ;
610
615
build. arg ( "build_libs" ) . current_dir ( & inner_dir) ;
611
616
if !cfg ! ( windows) {
612
617
if let Some ( s) = env:: var_os ( "CARGO_MAKEFLAGS" ) {
@@ -622,7 +627,7 @@ impl Build {
622
627
623
628
self . run_command ( build, "building OpenSSL" ) ?;
624
629
625
- let mut install = self . cmd_make ( ) ;
630
+ let mut install = self . cmd_make ( ) ? ;
626
631
install. arg ( "install_dev" ) . current_dir ( & inner_dir) ;
627
632
self . run_command ( install, "installing OpenSSL" ) ?;
628
633
}
@@ -633,7 +638,7 @@ impl Build {
633
638
vec ! [ "ssl" . to_string( ) , "crypto" . to_string( ) ]
634
639
} ;
635
640
636
- fs:: remove_dir_all ( & inner_dir) . unwrap ( ) ;
641
+ fs:: remove_dir_all ( & inner_dir) . map_err ( |e| format ! ( "{}: {e}" , inner_dir . display ( ) ) ) ? ;
637
642
638
643
Ok ( Artifacts {
639
644
lib_dir : install_dir. join ( "lib" ) ,
@@ -675,12 +680,16 @@ impl Build {
675
680
}
676
681
}
677
682
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 ( ) ;
683
+ fn cp_r ( src : & Path , dst : & Path ) -> Result < ( ) , String > {
684
+ for f in fs:: read_dir ( src) . map_err ( |e| format ! ( "{}: {e}" , src. display( ) ) ) ? {
685
+ let f = match f {
686
+ Ok ( f) => f,
687
+ _ => continue ,
688
+ } ;
682
689
let path = f. path ( ) ;
683
- let name = path. file_name ( ) . unwrap ( ) ;
690
+ let name = path
691
+ . file_name ( )
692
+ . ok_or_else ( || format ! ( "bad dir {}" , src. display( ) ) ) ?;
684
693
685
694
// Skip git metadata as it's been known to cause issues (#26) and
686
695
// otherwise shouldn't be required
@@ -689,27 +698,28 @@ fn cp_r(src: &Path, dst: &Path) {
689
698
}
690
699
691
700
let dst = dst. join ( name) ;
692
- let ty = f. file_type ( ) . unwrap ( ) ;
701
+ let ty = f. file_type ( ) . map_err ( |e| e . to_string ( ) ) ? ;
693
702
if ty. is_dir ( ) {
694
- fs:: create_dir_all ( & dst) . unwrap ( ) ;
695
- cp_r ( & path, & dst) ;
703
+ fs:: create_dir_all ( & dst) . map_err ( |e| e . to_string ( ) ) ? ;
704
+ cp_r ( & path, & dst) ? ;
696
705
} else if ty. is_symlink ( ) && path. iter ( ) . any ( |p| p == "cloudflare-quiche" ) {
697
706
// not needed to build
698
707
continue ;
699
708
} else {
700
709
let _ = fs:: remove_file ( & dst) ;
701
710
if let Err ( e) = fs:: copy ( & path, & dst) {
702
- panic ! ( "failed to copy {path:?} to {dst:?} : {e}" ) ;
711
+ return Err ( format ! ( "failed to copy '{}' to '{}' : {e}" , path . display ( ) , dst . display ( ) ) ) ;
703
712
}
704
713
}
705
714
}
715
+ Ok ( ( ) )
706
716
}
707
717
708
718
fn sanitize_sh ( path : & Path ) -> String {
709
719
if !cfg ! ( windows) {
710
- return path. to_str ( ) . unwrap ( ) . to_string ( ) ;
720
+ return path. to_string_lossy ( ) . into_owned ( ) ;
711
721
}
712
- let path = path. to_str ( ) . unwrap ( ) . replace ( "\\ " , "/" ) ;
722
+ let path = path. to_string_lossy ( ) . replace ( "\\ " , "/" ) ;
713
723
return change_drive ( & path) . unwrap_or ( path) ;
714
724
715
725
fn change_drive ( s : & str ) -> Option < String > {
0 commit comments