aboutsummaryrefslogtreecommitdiffhomepage
path: root/detours-sys/build.rs
blob: 20bb342644a2a579d89699ba2095414a852845ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#[cfg(not(target_os = "windows"))]
fn main() {}

#[cfg(target_os = "windows")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    windows::main_impl()
}

#[cfg(target_os = "windows")]
mod windows {
    use std::error::Error;

    const CPP_FILES: [&'static str; 5] = [
        "../ext/detours/src/creatwth.cpp",
        "../ext/detours/src/detours.cpp",
        "../ext/detours/src/disasm.cpp",
        "../ext/detours/src/image.cpp",
        "../ext/detours/src/modules.cpp",
    ];

    pub fn main_impl() -> Result<(), Box<dyn Error>> {
        println!("cargo:rerun-if-changed=build/wrapper.h");
        for f in &CPP_FILES {
            println!("cargo:rerun-if-changed={}", f);
        }
        build_detours()
    }

    fn build_detours() -> Result<(), Box<dyn Error>> {
        add_target_options(
            cc::Build::new()
                .include("../ext/detours/src")
                .files(&CPP_FILES),
        )
        .try_compile("detours")?;
        Ok(())
    }
    fn add_target_options(build: &mut cc::Build) -> &mut cc::Build {
        if std::env::var("CARGO_CFG_TARGET_ENV").unwrap() != "msvc" {
            build
                .compiler("clang")
                .cpp(true)
                .flag("-fms-extensions")
                .flag("-Wno-everything")
        } else {
            build
        }
    }
}