//! Compiles the 64-bit-patched 1996 C sources (cport/src, generated by cport/tools/build_csrc.sh)
//! plus the hand-written portable replacements (cport/port). Iteration-1 scaffolding: these C
//! files are what c2rust will translate next; the platform/runtime layer is already Rust.
use std::fs;

fn main() {
    let mut b = cc::Build::new();
    for dir in ["cport/src", "cport/port"] {
        let mut files: Vec<_> = fs::read_dir(dir).unwrap().filter_map(|e| e.ok()).map(|e| e.path())
            .filter(|p| p.extension().map_or(false, |x| x == "c")).collect();
        files.sort();
        for f in files { println!("cargo:rerun-if-changed={}", f.display()); b.file(f); }
    }
    println!("cargo:rerun-if-changed=cport/include");
    b.include("cport/include").include("cport/src")
        .flag("-include").flag("cport/include/compat.h")
        .flag("-std=gnu89")
        .flag("-funsigned-char")       // Watcom's plain char was unsigned
        .flag("-fwrapv")               // the code relies on 32-bit two's-complement wraparound
        .flag("-fno-strict-aliasing")  // lots of type punning through casts
        .flag("-w")
        .flag_if_supported("-Wno-return-mismatch")
        .flag_if_supported("-Wno-incompatible-function-pointer-types")
        .flag_if_supported("-Wno-incompatible-pointer-types")
        .flag_if_supported("-Wno-int-conversion")
        .define("main", "duke_main")
        .opt_level(2)
        .debug(true)
        .compile("duke3d_c");
}
