use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=CUDA_HOME");
if env::var_os("CARGO_FEATURE_REQUIRE_CUDA").is_none() {
return;
}
if env::var_os("CARGO_FEATURE_REMOTE_ACCESS").is_none() {
panic!(
"The `require-cuda` feature is enabled, but the `remote-access` feature is not enabled.\n\
Enable the `remote-access` feature or disable the `require-cuda` feature.\n\
Learn more: https://docs.rs/foxglove/latest/foxglove/#nvenc-hardware-acceleration"
);
}
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let cuda_supported_arch = target_os == "linux"
&& (matches!(target_arch.as_str(), "x86_64" | "x86" | "aarch64")
|| target_arch.contains("arm"));
if !cuda_supported_arch {
panic!(
"The `require-cuda` feature is enabled, but NVENC is only built by webrtc-sys on Linux \n\
for x86_64, x86, aarch64, and arm targets, not `{target_os}-{target_arch}`.\n\
Disable the `require-cuda` feature for this target.\n\
Learn more: https://docs.rs/foxglove/latest/foxglove/#nvenc-hardware-acceleration"
);
}
let cuda_home = env::var_os("CUDA_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("/usr/local/cuda"));
let cuda_header = cuda_home.join("include").join("cuda.h");
if cuda_header.exists() {
return;
}
let header_display = cuda_header.display();
panic!(
"The `require-cuda` feature is enabled but cuda.h was not found at {header_display}.\n\
Install the CUDA toolkit (e.g. `apt install nvidia-cuda-dev` on Ubuntu, \
then export CUDA_HOME=/usr) or disable the `require-cuda` feature.\n\
Learn more: https://docs.rs/foxglove/latest/foxglove/#nvenc-hardware-acceleration"
);
}