Skip to content

Commit dcb8919

Browse files
authored
fix(webrtc-sys-build): add error context to debug issues (#664)
* add context * oops
1 parent aa945dd commit dcb8919

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webrtc-sys/build/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ regex = "1.0"
1313
scratch = "1.0"
1414
fs2 = "0.4"
1515
semver = "1.0"
16+
anyhow = "1.0"

webrtc-sys/build/src/lib.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
use std::path::PathBuf;
1616
use std::{
1717
env,
18-
error::Error,
1918
fs::{self, File},
2019
io::{self, BufRead, Write},
2120
path,
2221
process::Command,
2322
};
2423

24+
use anyhow::{anyhow, Context, Result};
2525
use fs2::FileExt;
2626
use regex::Regex;
2727
use reqwest::StatusCode;
@@ -136,10 +136,10 @@ pub fn webrtc_defines() -> Vec<(String, Option<String>)> {
136136
vec
137137
}
138138

139-
pub fn configure_jni_symbols() -> Result<(), Box<dyn Error>> {
140-
download_webrtc()?;
139+
pub fn configure_jni_symbols() -> Result<()> {
140+
download_webrtc().context("Failed to download WebRTC binaries for JNI configuration")?;
141141

142-
let toolchain = android_ndk_toolchain()?;
142+
let toolchain = android_ndk_toolchain().context("Failed to locate Android NDK toolchain")?;
143143
let toolchain_bin = toolchain.join("bin");
144144

145145
let webrtc_dir = webrtc_dir();
@@ -160,7 +160,7 @@ pub fn configure_jni_symbols() -> Result<(), Box<dyn Error>> {
160160
jni_regex.captures_iter(&content).map(|cap| cap.get(1).unwrap().as_str()).collect();
161161

162162
if jni_symbols.is_empty() {
163-
return Err("No JNI symbols found".into()); // Shouldn't happen
163+
return Err(anyhow!("No JNI symbols found")); // Shouldn't happen
164164
}
165165

166166
// Keep JNI symbols
@@ -170,45 +170,53 @@ pub fn configure_jni_symbols() -> Result<(), Box<dyn Error>> {
170170

171171
// Version script
172172
let vs_path = out_dir.join("webrtc_jni.map");
173-
let mut vs_file = fs::File::create(&vs_path).unwrap();
173+
let mut vs_file = fs::File::create(&vs_path).context("Failed to create version script file")?;
174174

175175
let jni_symbols = jni_symbols.join("; ");
176-
write!(vs_file, "JNI_WEBRTC {{\n\tglobal: {}; \n}};", jni_symbols).unwrap();
176+
write!(vs_file, "JNI_WEBRTC {{\n\tglobal: {}; \n}};", jni_symbols)
177+
.context("Failed to write version script")?;
177178

178179
println!("cargo:rustc-link-arg=-Wl,--version-script={}", vs_path.display());
179180

180181
Ok(())
181182
}
182183

183-
pub fn download_webrtc() -> Result<(), Box<dyn Error>> {
184+
pub fn download_webrtc() -> Result<()> {
184185
let dir = scratch::path(SCRATH_PATH);
185-
let flock = File::create(dir.join(".lock"))?;
186-
flock.lock_exclusive()?;
186+
let flock = File::create(dir.join(".lock"))
187+
.context("Failed to create lock file for WebRTC download")?;
188+
flock.lock_exclusive().context("Failed to acquire exclusive lock for WebRTC download")?;
187189

188190
let webrtc_dir = webrtc_dir();
189191
if webrtc_dir.exists() {
190192
return Ok(());
191193
}
192194

193-
let mut resp = reqwest::blocking::get(download_url())?;
195+
let mut resp = reqwest::blocking::get(download_url())
196+
.context("Failed to send HTTP request to download WebRTC")?;
194197
if resp.status() != StatusCode::OK {
195-
return Err(format!("failed to download webrtc: {}", resp.status()).into());
198+
return Err(anyhow!("failed to download webrtc: {}", resp.status()));
196199
}
197200

198201
let out_dir = env::var("OUT_DIR").unwrap();
199202
let tmp_path = PathBuf::from(out_dir).join("webrtc.zip");
200-
let mut file = fs::File::options().write(true).read(true).create(true).open(&tmp_path)?;
201-
resp.copy_to(&mut file)?;
202-
203-
let mut archive = zip::ZipArchive::new(file)?;
204-
archive.extract(webrtc_dir.parent().unwrap())?;
203+
let mut file = fs::File::options()
204+
.write(true)
205+
.read(true)
206+
.create(true)
207+
.open(&tmp_path)
208+
.context("Failed to create temporary file for WebRTC download")?;
209+
resp.copy_to(&mut file).context("Failed to write WebRTC download to temporary file")?;
210+
211+
let mut archive = zip::ZipArchive::new(file).context("Failed to open WebRTC zip archive")?;
212+
archive.extract(webrtc_dir.parent().unwrap()).context("Failed to extract WebRTC archive")?;
205213
drop(archive);
206214

207-
fs::remove_file(tmp_path)?;
215+
fs::remove_file(&tmp_path).context("Failed to remove temporary WebRTC zip file")?;
208216
Ok(())
209217
}
210218

211-
pub fn android_ndk_toolchain() -> Result<path::PathBuf, &'static str> {
219+
pub fn android_ndk_toolchain() -> Result<path::PathBuf> {
212220
let host_os = host_os();
213221

214222
let home = env::var("HOME");
@@ -221,7 +229,7 @@ pub fn android_ndk_toolchain() -> Result<path::PathBuf, &'static str> {
221229
} else if host_os == Some("windows") {
222230
path::PathBuf::from(local.unwrap())
223231
} else {
224-
return Err("Unsupported host OS");
232+
return Err(anyhow!("Unsupported host OS"));
225233
};
226234

227235
let ndk_dir = || -> Option<path::PathBuf> {
@@ -262,12 +270,12 @@ pub fn android_ndk_toolchain() -> Result<path::PathBuf, &'static str> {
262270
} else if host_os == Some("windows") {
263271
"windows-x86_64"
264272
} else {
265-
return Err("Unsupported host OS");
273+
return Err(anyhow!("Unsupported host OS"));
266274
};
267275

268276
Ok(ndk_dir.join(format!("toolchains/llvm/prebuilt/{}", llvm_dir)))
269277
} else {
270-
Err("Android NDK not found, please set ANDROID_NDK_HOME to your NDK path")
278+
Err(anyhow!("Android NDK not found, please set ANDROID_NDK_HOME to your NDK path"))
271279
}
272280
}
273281

0 commit comments

Comments
 (0)