use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=wrapper.cpp");
println!("cargo:rerun-if-changed=backends/imgui_impl_sdl3.cpp");
println!("cargo:rerun-if-changed=backends/imgui_impl_sdl3.h");
println!("cargo:rerun-if-changed=backends/imgui_impl_opengl3.cpp");
println!("cargo:rerun-if-changed=backends/imgui_impl_opengl3.h");
if env::var("CARGO_CFG_TARGET_OS")
.map(|os| os == "windows")
.unwrap_or(false)
{
println!("cargo:rustc-link-lib=user32");
}
let imgui_root = env::var("DEP_DEAR_IMGUI_THIRD_PARTY")
.or_else(|_| env::var("DEP_DEAR_IMGUI_IMGUI_INCLUDE_PATH"))
.expect(
"DEP_DEAR_IMGUI_THIRD_PARTY or DEP_DEAR_IMGUI_IMGUI_INCLUDE_PATH not set. \
Make sure dear-imgui-sys is built before dear-imgui-sdl3.",
);
let imgui_root = PathBuf::from(imgui_root);
let crate_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let local_backends = crate_root.join("backends");
let mut build = cc::Build::new();
build.cpp(true).std("c++17");
build.include(&imgui_root);
build.include(&crate_root);
let mut have_sdl_headers = false;
if let Ok(dir) = env::var("SDL3_INCLUDE_DIR") {
build.include(&dir);
have_sdl_headers = true;
println!("cargo:warning=dear-imgui-sdl3: using SDL3_INCLUDE_DIR={dir}");
} else {
if let Ok(lib) = pkg_config::Config::new()
.print_system_libs(false)
.probe("sdl3")
{
for p in lib.include_paths {
build.include(&p);
}
have_sdl_headers = true;
println!("cargo:warning=dear-imgui-sdl3: using SDL3 headers from pkg-config (sdl3)");
} else {
let candidates = [
"/opt/homebrew/include",
"/usr/local/include",
"/opt/local/include",
];
for c in candidates {
let hdr = PathBuf::from(c).join("SDL3/SDL.h");
if hdr.exists() {
build.include(c);
have_sdl_headers = true;
println!(
"cargo:warning=dear-imgui-sdl3: using SDL3 headers from {}",
c
);
break;
}
}
}
}
if !have_sdl_headers && let Ok(out_dir) = env::var("DEP_SDL3_OUT_DIR") {
let out = PathBuf::from(out_dir);
let include_root = out.join("include");
let hdr = include_root.join("SDL3/SDL.h");
if hdr.exists() {
build.include(&include_root);
have_sdl_headers = true;
println!(
"cargo:warning=dear-imgui-sdl3: using SDL3 headers from sdl3-sys OUT_DIR={}",
include_root.display()
);
}
}
if !have_sdl_headers {
panic!(
"dear-imgui-sdl3: could not find SDL3 headers. \
Install SDL3 development files (e.g. `brew install sdl3`), \
set SDL3_INCLUDE_DIR to the SDL3 include path, \
or enable `build-from-source` on the `sdl3` crate so sdl3-sys can \
build SDL3 and expose headers via DEP_SDL3_OUT_DIR."
);
}
build.file(local_backends.join("imgui_impl_sdl3.cpp"));
if cfg!(feature = "opengl3-renderer") {
build.define("DEAR_IMGUI_SDL3_OPENGL3_RENDERER", None);
build.file(local_backends.join("imgui_impl_opengl3.cpp"));
}
build.file("wrapper.cpp");
build.compile("dear-imgui-sdl3-backend");
}