use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
let doslike_path = Path::new("dos-like");
init_submodule(&doslike_path);
println!("cargo:rerun-if-changed=dos-like/source/dos.c");
let compiled_lib_path = compile(doslike_path);
println!("cargo:rustc-link-search={}", compiled_lib_path.display());
link();
}
fn init_submodule(doslike_path: &Path) {
if !doslike_path.join("source").exists() {
Command::new("git")
.args(&["submodule", "update", "--init"])
.current_dir(doslike_path.clone())
.status()
.expect("Git is needed to retrieve the dos-like source files");
}
}
fn compile(source_path: &Path) -> PathBuf {
let include_paths = compute_include_paths("/usr/include/SDL2");
let mut build = cc::Build::new();
build
.file(source_path.join("source/dos.c"))
.warnings(false)
.extra_warnings(false)
.includes(include_paths)
.define("NO_MAIN_DEF", "1");
if cfg!(feature = "disable-screen-frame") {
build.define("DISABLE_SCREEN_FRAME", "1");
}
build.compile("dos-like");
PathBuf::from("dos-like")
}
fn compute_include_paths(fallback_path: impl AsRef<Path>) -> Vec<PathBuf> {
let mut include_paths: Vec<_> = vec![];
if let Ok(include_path) = env::var("SDL2_INCLUDE_PATH") {
include_paths.push(PathBuf::from(include_path));
};
#[cfg(feature = "pkg-config")]
{
let pkg_config_library = pkg_config::Config::new()
.print_system_libs(false)
.probe("sdl2")
.unwrap();
for path in pkg_config_library.include_paths {
include_paths.push(path);
}
}
#[cfg(feature = "vcpkg")]
{
let vcpkg_library = vcpkg::Config::new()
.cargo_metadata(false)
.probe("sdl2")
.unwrap();
for path in vcpkg_library.include_paths {
include_paths.push(path);
}
}
if include_paths.is_empty() {
include_paths.push(fallback_path.as_ref().to_owned());
}
include_paths
}
fn link() {
#[cfg(feature = "use-pkgconfig")]
{
get_pkg_config();
}
#[cfg(feature = "use-vcpkg")]
{
get_vcpkg_config();
}
if cfg!(feature = "use-pkgconfig") == false && cfg!(feature = "use-vcpkg") == false {
println!("cargo:rustc-flags=-l SDL2main");
println!("cargo:rustc-flags=-l SDL2");
}
println!("cargo:rustc-flags=-l GLEW");
println!("cargo:rustc-flags=-l GL");
}
#[cfg(feature = "use-pkgconfig")]
fn get_pkg_config() {
let statik: bool = if cfg!(feature = "static-link") {
true
} else {
false
};
}
#[cfg(feature = "use-pkgconfig")]
fn pkg_config_print(statik: bool, lib_name: &str) {
pkg_config::Config::new()
.statik(statik)
.probe(lib_name)
.unwrap();
}