#![allow(clippy::if_same_then_else)]
#![allow(clippy::len_zero)]
#![allow(clippy::redundant_pattern_matching)]
use std::env;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
if cfg!(feature = "cargo_check") {
println!("Run with `cargo_check` enabled, skipping the build.");
return;
}
#[cfg(feature = "static_bundled_build")]
{
do_static_bundled();
}
#[cfg(not(feature = "static_bundled_build"))]
{
do_dynamic_system();
}
}
#[cfg(feature = "static_bundled_build")]
fn do_static_bundled() {
let out_dir = env::var("OUT_DIR").unwrap();
println!("out_dir:{}", out_dir);
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo_manifest_dir:{}", cargo_manifest_dir);
let target = env::var("TARGET").expect("Could not read `TARGET`!");
println!("target:{}", target);
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
println!("target_os:{}", target_os);
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
println!("target_arch:{}", target_arch);
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
println!("target_vendor:{}", target_vendor);
let mut cm = cmake::Config::new(
std::path::Path::new(&cargo_manifest_dir).join("SDL2-2.24.1"),
);
cm.profile("Release");
cm.static_crt(true);
cm.target(&target);
cm.define("SDL_SHARED", "ON");
cm.define("SDL_STATIC", "ON");
cm.define("HIDAPI", "ON");
if target_vendor == "apple" {
match target_arch.as_str() {
"aarch64" => {
cm.define("CMAKE_OSX_ARCHITECTURES", "arm64");
}
"x86_64" => {
cm.define("CMAKE_OSX_ARCHITECTURES", "x86_64");
}
arch => {
println!("Unrecognized architecture for Apple platform \"{}\", not setting CMAKE_OSX_ARCHITECTURES", arch);
}
}
}
let build_output_path = cm.build();
println!("build_output_path: {}", build_output_path.display());
println!(
"cargo:rustc-link-search={}",
build_output_path.join("lib").display()
);
if target.contains("windows") {
println!("cargo:rustc-link-lib=static=SDL2-static");
println!("cargo:rustc-link-lib=user32");
println!("cargo:rustc-link-lib=gdi32");
println!("cargo:rustc-link-lib=winmm");
println!("cargo:rustc-link-lib=imm32");
println!("cargo:rustc-link-lib=ole32");
println!("cargo:rustc-link-lib=oleaut32");
println!("cargo:rustc-link-lib=version");
println!("cargo:rustc-link-lib=uuid");
println!("cargo:rustc-link-lib=advapi32");
println!("cargo:rustc-link-lib=setupapi");
println!("cargo:rustc-link-lib=shell32");
} else {
let sdl2_cfg_cmd = format!(
"{}",
build_output_path.join("bin").join("sdl2-config").display()
);
let link_style_arg: &str =
if cfg!(feature = "dynamic_link") { "--libs" } else { "--static-libs" };
let sd2_config_linking = std::process::Command::new(sdl2_cfg_cmd)
.arg(link_style_arg)
.output()
.unwrap_or_else(|_| {
panic!("Couldn't run `sdl2-config {}`.", link_style_arg)
});
assert!(sd2_config_linking.status.success());
let sd2_config_linking_stdout: String =
String::from_utf8_lossy(&sd2_config_linking.stdout).into_owned();
println!("sd2_config_linking_stdout: {}", sd2_config_linking_stdout);
assert!(sd2_config_linking_stdout.len() > 0);
process_sdl2_config_output(sd2_config_linking_stdout);
println!("cargo:rustc-link-lib=static=SDL2");
}
}
#[cfg(not(feature = "static_bundled_build"))]
fn do_dynamic_system() {
println!("Performing dynamically-linked build against the system SDL2.");
let out_dir = env::var("OUT_DIR").unwrap();
println!("out_dir:{}", out_dir);
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo_manifest_dir:{}", cargo_manifest_dir);
let target = env::var("TARGET").expect("Could not read `TARGET`!");
println!("target:{}", target);
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
println!("target_os:{}", target_os);
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
println!("target_arch:{}", target_arch);
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
println!("target_vendor:{}", target_vendor);
if target_os.contains("windows") {
if target == "x86_64-pc-windows-msvc" {
let devel_path =
std::path::Path::new(&cargo_manifest_dir).join("SDL2-2.24.1-devel");
let dll_from = devel_path.join("SDL2.dll");
println!("dll_from:{}", dll_from.display());
let dll_to = std::path::Path::new(&out_dir).join("SDL2.dll");
println!("dll_to:{}", dll_to.display());
std::fs::copy(dll_from, dll_to).unwrap();
println!("cargo:rustc-link-search={}", devel_path.display());
println!("cargo:rustc-link-lib=SDL2");
} else {
panic!("On windows, dynamic_system builds are only supported for `x86_64-pc-windows-msvc`. Consider the `static_bundled_build` feature instead.");
}
} else {
let sd2_config_linking = std::process::Command::new("sdl2-config")
.arg("--libs")
.output()
.unwrap_or_else(|_| {
panic!("Couldn't run `sdl2-config --libs`, is SDL2 properly installed?")
});
assert!(sd2_config_linking.status.success());
let sd2_config_linking_stdout: String =
String::from_utf8_lossy(&sd2_config_linking.stdout).into_owned();
println!("sd2_config_linking_stdout: {}", sd2_config_linking_stdout);
assert!(sd2_config_linking_stdout.len() > 0);
process_sdl2_config_output(sd2_config_linking_stdout);
println!("cargo:rustc-link-lib=SDL2");
}
}
fn process_sdl2_config_output(sd2_config_linking_stdout: String) {
for term in sd2_config_linking_stdout.split_whitespace() {
if let Some(link_search_path) = term.strip_prefix("-L") {
println!("cargo:rustc-link-search=native={}", link_search_path);
} else if term.starts_with("-lSDL2") {
} else if let Some(link_target) = term.strip_prefix("-l") {
if link_target.ends_with(".framework") {
let name_framework = link_target.rsplit('/').next().unwrap();
let name = name_framework.split('.').next().unwrap();
println!("cargo:rustc-link-lib=framework={name}");
} else {
println!("cargo:rustc-link-lib={link_target}");
}
} else if let Some(framework_link) = term.strip_prefix("-Wl,-framework,") {
println!("cargo:rustc-link-lib=framework={framework_link}");
} else if let Some(framework_link) =
term.strip_prefix("-Wl,-weak_framework,")
{
println!("cargo:rustc-link-lib=framework={framework_link}");
} else if let Some(rpath) = term.strip_prefix("-Wl,-rpath,") {
println!("cargo:rustc-env=DYLD_LIBRARY_PATH={rpath}");
} else if let Some(_) = term.strip_prefix("-Wl,--enable-new-dtags") {
} else if let Some(_) = term.strip_prefix("-Wl,--no-undefined") {
} else if let Some(_) = term.strip_prefix("-pthread") {
} else if let Some(_) = term.strip_prefix("-Wl,-current_version,") {
} else if let Some(_) = term.strip_prefix("-Wl,-compatibility_version,") {
} else if let Some(_) = term.strip_prefix("-Wl,-undefined,error") {
} else {
}
}
}