Skip to content

Commit 41dfc1d

Browse files
committed
Expose fallible try_build
1 parent 7a49e98 commit 41dfc1d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/lib.rs

Lines changed: 20 additions & 12 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!("{e}"),
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,30 +631,30 @@ 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,
649+
Ok(status) if status.success() => return Ok(()),
642650
Ok(status) => format!("'{exe}' reported failure with {status}", exe = command.get_program().to_string_lossy()),
643651
Err(failed) => match failed.kind() {
644652
std::io::ErrorKind::NotFound => format!("Command '{exe}' not found. Is {exe} installed?", exe = command.get_program().to_string_lossy()),
645653
_ => format!("Could not run '{exe}', because {failed}", exe = command.get_program().to_string_lossy()),
646654
}
647655
};
648656
println!("cargo:warning={desc}: {verbose_error}");
649-
panic!(
657+
Err(format!(
650658
"
651659
652660
@@ -655,7 +663,7 @@ Error {desc}:
655663
Command failed: {command:?}
656664
657665
658-
");
666+
"))
659667
}
660668
}
661669

0 commit comments

Comments
 (0)