Skip to content

Commit fd39574

Browse files
committed
Remove panics
1 parent 2d9057e commit fd39574

File tree

1 file changed

+62
-52
lines changed

1 file changed

+62
-52
lines changed

src/lib.rs

+62-52
Original file line numberDiff line numberDiff line change
@@ -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,7 @@ 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+
_ => return Err(format!("don't know how to configure OpenSSL for {}", target)),
408413
};
409414

410415
let mut ios_isysroot: std::option::Option<String> = None;
@@ -423,7 +428,7 @@ impl Build {
423428
cc_env = compiler.path().to_path_buf().into_os_string();
424429
}
425430
configure.env("CC", cc_env);
426-
let path = compiler.path().to_str().unwrap();
431+
let path = compiler.path().to_str().ok_or("compiler path")?;
427432

428433
// Both `cc::Build` and `./Configure` take into account
429434
// `CROSS_COMPILE` environment variable. So to avoid double
@@ -477,7 +482,7 @@ impl Build {
477482

478483
if is_isysroot {
479484
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());
481486
continue;
482487
}
483488
}
@@ -593,20 +598,20 @@ impl Build {
593598
// have that take a different path than the standard `make` below.
594599
if target.contains("msvc") {
595600
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")?;
597602
build.arg("build_libs").current_dir(&inner_dir);
598603
self.run_command(build, "building OpenSSL")?;
599604

600605
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")?;
602607
install.arg("install_dev").current_dir(&inner_dir);
603608
self.run_command(install, "installing OpenSSL")?;
604609
} else {
605-
let mut depend = self.cmd_make();
610+
let mut depend = self.cmd_make()?;
606611
depend.arg("depend").current_dir(&inner_dir);
607612
self.run_command(depend, "building OpenSSL dependencies")?;
608613

609-
let mut build = self.cmd_make();
614+
let mut build = self.cmd_make()?;
610615
build.arg("build_libs").current_dir(&inner_dir);
611616
if !cfg!(windows) {
612617
if let Some(s) = env::var_os("CARGO_MAKEFLAGS") {
@@ -622,7 +627,7 @@ impl Build {
622627

623628
self.run_command(build, "building OpenSSL")?;
624629

625-
let mut install = self.cmd_make();
630+
let mut install = self.cmd_make()?;
626631
install.arg("install_dev").current_dir(&inner_dir);
627632
self.run_command(install, "installing OpenSSL")?;
628633
}
@@ -633,7 +638,7 @@ impl Build {
633638
vec!["ssl".to_string(), "crypto".to_string()]
634639
};
635640

636-
fs::remove_dir_all(&inner_dir).unwrap();
641+
fs::remove_dir_all(&inner_dir).map_err(|e| format!("{}: {e}", inner_dir.display()))?;
637642

638643
Ok(Artifacts {
639644
lib_dir: install_dir.join("lib"),
@@ -675,12 +680,16 @@ impl Build {
675680
}
676681
}
677682

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+
};
682689
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()))?;
684693

685694
// Skip git metadata as it's been known to cause issues (#26) and
686695
// otherwise shouldn't be required
@@ -689,27 +698,28 @@ fn cp_r(src: &Path, dst: &Path) {
689698
}
690699

691700
let dst = dst.join(name);
692-
let ty = f.file_type().unwrap();
701+
let ty = f.file_type().map_err(|e| e.to_string())?;
693702
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)?;
696705
} else if ty.is_symlink() && path.iter().any(|p| p == "cloudflare-quiche") {
697706
// not needed to build
698707
continue;
699708
} else {
700709
let _ = fs::remove_file(&dst);
701710
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()));
703712
}
704713
}
705714
}
715+
Ok(())
706716
}
707717

708718
fn sanitize_sh(path: &Path) -> String {
709719
if !cfg!(windows) {
710-
return path.to_str().unwrap().to_string();
720+
return path.to_string_lossy().into_owned();
711721
}
712-
let path = path.to_str().unwrap().replace("\\", "/");
722+
let path = path.to_string_lossy().replace("\\", "/");
713723
return change_drive(&path).unwrap_or(path);
714724

715725
fn change_drive(s: &str) -> Option<String> {

0 commit comments

Comments
 (0)