use std::env;
use std::process::Command;
fn main() {
if std::env::var("DOCS_RS").is_ok() {
return;
}
let build_type = if cfg!(all(debug_assertions, not(windows))) {
"Debug"
} else {
"Release"
};
let out_dir = env::var("OUT_DIR").unwrap();
let mediasoup_out_dir = format!("{}/out", out_dir.replace('\\', "/"));
#[cfg(target_os = "linux")]
{
let path = Command::new(env::var("CXX").unwrap_or_else(|_| "c++".to_string()))
.arg("--print-file-name=libstdc++.a")
.output()
.expect("Failed to start")
.stdout;
println!(
"cargo:rustc-link-search=native={}",
String::from_utf8_lossy(&path)
.trim()
.strip_suffix("libstdc++.a")
.expect("Failed to strip suffix"),
);
println!("cargo:rustc-link-lib=static=stdc++");
}
#[cfg(any(
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
{
let path = Command::new(env::var("CXX").unwrap_or_else(|_| "c++".to_string()))
.arg("--print-file-name=libc++.a")
.output()
.expect("Failed to start")
.stdout;
println!(
"cargo:rustc-link-search=native={}",
String::from_utf8_lossy(&path)
.trim()
.strip_suffix("libc++.a")
.expect("Failed to strip suffix"),
);
println!("cargo:rustc-link-lib=static=c++");
}
#[cfg(target_os = "macos")]
{
let path = Command::new("xcrun")
.args(&["--show-sdk-path"])
.output()
.expect("Failed to start")
.stdout;
let libpath = format!(
"{}/usr/lib",
String::from_utf8(path)
.expect("Failed to decode path")
.trim()
);
println!("cargo:rustc-link-search={}", libpath);
println!("cargo:rustc-link-lib=dylib=c++");
println!("cargo:rustc-link-lib=dylib=c++abi");
}
#[cfg(target_os = "windows")]
{
}
if !Command::new("make")
.arg("libmediasoup-worker")
.env("MEDIASOUP_OUT_DIR", &mediasoup_out_dir)
.env("MEDIASOUP_BUILDTYPE", &build_type)
.env("INSTALL_DIR", &out_dir.replace('\\', "/"))
.spawn()
.expect("Failed to start")
.wait()
.expect("Wasn't running")
.success()
{
panic!("Failed to build libmediasoup-worker")
}
#[cfg(target_os = "windows")]
{
let dot_a = format!("{}/libmediasoup-worker.a", out_dir);
let dot_lib = format!("{}/mediasoup-worker.lib", out_dir);
if std::path::Path::new(&dot_a).exists() {
std::fs::copy(&dot_a, &dot_lib).unwrap_or_else(|error| {
panic!(
"Failed to copy static library from {} to {}: {}",
dot_a, dot_lib, error
)
});
}
println!("cargo:rustc-link-lib=psapi");
println!("cargo:rustc-link-lib=user32");
println!("cargo:rustc-link-lib=advapi32");
println!("cargo:rustc-link-lib=iphlpapi");
println!("cargo:rustc-link-lib=userenv");
println!("cargo:rustc-link-lib=ws2_32");
println!("cargo:rustc-link-lib=ws2_32");
println!("cargo:rustc-link-lib=gdi32");
println!("cargo:rustc-link-lib=advapi32");
println!("cargo:rustc-link-lib=crypt32");
println!("cargo:rustc-link-lib=user32");
}
if env::var("KEEP_BUILD_ARTIFACTS") != Ok("1".to_string()) {
if !Command::new("make")
.arg("clean-all")
.env("MEDIASOUP_OUT_DIR", &mediasoup_out_dir)
.spawn()
.expect("Failed to start")
.wait()
.expect("Wasn't running")
.success()
{
panic!("Failed to clean libmediasoup-worker")
}
}
println!("cargo:rustc-link-lib=static=mediasoup-worker");
println!("cargo:rustc-link-search=native={}", out_dir);
}