Skip to content

Commit 8668aef

Browse files
committed
Expose fallible try_build
1 parent 7a49e98 commit 8668aef

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

src/lib.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,15 @@ impl Build {
122122
false
123123
}
124124

125+
#[track_caller]
125126
pub fn build(&mut self) -> Artifacts {
127+
match self.try_build() {
128+
Ok(a) => a,
129+
Err(e) => panic!("\n\n\n{e}\n\n\n"),
130+
}
131+
}
132+
133+
pub fn try_build(&mut self) -> Result<Artifacts, String> {
126134
let target = &self.target.as_ref().expect("TARGET dir not set")[..];
127135
let host = &self.host.as_ref().expect("HOST dir not set")[..];
128136
let out_dir = self.out_dir.as_ref().expect("OUT_DIR not set");
@@ -575,24 +583,24 @@ impl Build {
575583

576584
// And finally, run the perl configure script!
577585
configure.current_dir(&inner_dir);
578-
self.run_command(configure, "configuring OpenSSL build");
586+
self.run_command(configure, "configuring OpenSSL build")?;
579587

580588
// On MSVC we use `nmake.exe` with a slightly different invocation, so
581589
// have that take a different path than the standard `make` below.
582590
if target.contains("msvc") {
583591
let mut build =
584592
cc::windows_registry::find(target, "nmake.exe").expect("failed to find nmake");
585593
build.arg("build_libs").current_dir(&inner_dir);
586-
self.run_command(build, "building OpenSSL");
594+
self.run_command(build, "building OpenSSL")?;
587595

588596
let mut install =
589597
cc::windows_registry::find(target, "nmake.exe").expect("failed to find nmake");
590598
install.arg("install_dev").current_dir(&inner_dir);
591-
self.run_command(install, "installing OpenSSL");
599+
self.run_command(install, "installing OpenSSL")?;
592600
} else {
593601
let mut depend = self.cmd_make();
594602
depend.arg("depend").current_dir(&inner_dir);
595-
self.run_command(depend, "building OpenSSL dependencies");
603+
self.run_command(depend, "building OpenSSL dependencies")?;
596604

597605
let mut build = self.cmd_make();
598606
build.arg("build_libs").current_dir(&inner_dir);
@@ -608,11 +616,11 @@ impl Build {
608616
build.env("CROSS_SDK", components[1]);
609617
}
610618

611-
self.run_command(build, "building OpenSSL");
619+
self.run_command(build, "building OpenSSL")?;
612620

613621
let mut install = self.cmd_make();
614622
install.arg("install_dev").current_dir(&inner_dir);
615-
self.run_command(install, "installing OpenSSL");
623+
self.run_command(install, "installing OpenSSL")?;
616624
}
617625

618626
let libs = if target.contains("msvc") {
@@ -623,39 +631,43 @@ impl Build {
623631

624632
fs::remove_dir_all(&inner_dir).unwrap();
625633

626-
Artifacts {
634+
Ok(Artifacts {
627635
lib_dir: install_dir.join("lib"),
628636
bin_dir: install_dir.join("bin"),
629637
include_dir: install_dir.join("include"),
630638
libs: libs,
631639
target: target.to_string(),
632-
}
640+
})
633641
}
634642

635643
#[track_caller]
636-
fn run_command(&self, mut command: Command, desc: &str) {
644+
fn run_command(&self, mut command: Command, desc: &str) -> Result<(), String> {
637645
println!("running {:?}", command);
638646
let status = command.status();
639647

640648
let verbose_error = match status {
641-
Ok(status) if status.success() => return,
642-
Ok(status) => format!("'{exe}' reported failure with {status}", exe = command.get_program().to_string_lossy()),
649+
Ok(status) if status.success() => return Ok(()),
650+
Ok(status) => format!(
651+
"'{exe}' reported failure with {status}",
652+
exe = command.get_program().to_string_lossy()
653+
),
643654
Err(failed) => match failed.kind() {
644-
std::io::ErrorKind::NotFound => format!("Command '{exe}' not found. Is {exe} installed?", exe = command.get_program().to_string_lossy()),
645-
_ => format!("Could not run '{exe}', because {failed}", exe = command.get_program().to_string_lossy()),
646-
}
655+
std::io::ErrorKind::NotFound => format!(
656+
"Command '{exe}' not found. Is {exe} installed?",
657+
exe = command.get_program().to_string_lossy()
658+
),
659+
_ => format!(
660+
"Could not run '{exe}', because {failed}",
661+
exe = command.get_program().to_string_lossy()
662+
),
663+
},
647664
};
648665
println!("cargo:warning={desc}: {verbose_error}");
649-
panic!(
650-
"
651-
652-
653-
Error {desc}:
666+
Err(format!(
667+
"Error {desc}:
654668
{verbose_error}
655-
Command failed: {command:?}
656-
657-
658-
");
669+
Command failed: {command:?}"
670+
))
659671
}
660672
}
661673

0 commit comments

Comments
 (0)