build.rs 2.87 KB
Newer Older
Thomas Daede's avatar
Thomas Daede committed
1
2
// build.rs

3
extern crate cmake;
4
#[cfg(unix)]
Luca Barbato's avatar
Luca Barbato committed
5
extern crate pkg_config;
6
#[cfg(unix)]
Luca Barbato's avatar
Luca Barbato committed
7
8
#[cfg(feature = "decode_test")]
extern crate bindgen;
Thomas Daede's avatar
Thomas Daede committed
9

10
use std::env;
Luca Barbato's avatar
Luca Barbato committed
11
use std::fs;
12
use std::path::Path;
13

Thomas Daede's avatar
Thomas Daede committed
14
fn main() {
15
16
17
18
    if cfg!(windows) && cfg!(feature = "decode_test") {
        panic!("Unsupported feature on this platform!");
    }

19
20
    let cargo_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let build_path = Path::new(&cargo_dir).join("aom_build/aom");
Luca Barbato's avatar
Luca Barbato committed
21
22
    let debug = if let Some(v) = env::var("PROFILE").ok() {
        match v.as_str() {
23
24
            "bench" | "release" => false,
            _ => true,
Luca Barbato's avatar
Luca Barbato committed
25
26
        }
    } else {
27
        false
Luca Barbato's avatar
Luca Barbato committed
28
    };
29

30
    let dst = cmake::Config::new(build_path)
31
        .define("CONFIG_DEBUG", (debug as u8).to_string())
Luca Barbato's avatar
Luca Barbato committed
32
        .define("CONFIG_ANALYZER", "0")
33
        .define("ENABLE_DOCS", "0")
34
35
        .define("ENABLE_TESTS", "0")
        .no_build_target(cfg!(windows))
36
37
        .build();

38
    // Dirty hack to force a rebuild whenever the defaults are changed upstream
39
    let _ = fs::remove_file(dst.join("build/CMakeCache.txt"));
40

41
    #[cfg(windows)] {
42
43
44
45
        println!("cargo:rustc-link-search=native={}", dst.join("build").to_str().unwrap());
        println!("cargo:rustc-link-search=native={}", dst.join("build/Debug").to_str().unwrap());
        println!("cargo:rustc-link-search=native={}", dst.join("build/Release").to_str().unwrap());
        println!("cargo:rustc-link-lib=static=aom");
46
    }
47

48
49
50
    #[cfg(unix)] {
        env::set_var("PKG_CONFIG_PATH", dst.join("lib/pkgconfig"));
        let _libs = pkg_config::Config::new().statik(true).probe("aom").unwrap();
51

52
53
        #[cfg(feature = "decode_test")] {
            use std::io::Write;
54

55
            let out_dir = env::var("OUT_DIR").unwrap();
56

57
            let headers = _libs.include_paths.clone();
58

59
60
61
62
            let mut builder = bindgen::builder()
                .blacklist_type("max_align_t")
                .rustfmt_bindings(false)
                .header("data/aom.h");
63

64
65
66
            for header in headers {
                builder = builder.clang_arg("-I").clang_arg(header.to_str().unwrap());
            }
Luca Barbato's avatar
Luca Barbato committed
67

68
69
70
71
72
73
74
            // Manually fix the comment so rustdoc won't try to pick them
            let s = builder
                .generate()
                .unwrap()
                .to_string()
                .replace("/**", "/*")
                .replace("/*!", "/*");
75

76
            let dest_path = Path::new(&out_dir).join("aom.rs");
77

78
            let mut file = fs::File::create(dest_path).unwrap();
Luca Barbato's avatar
Luca Barbato committed
79

80
81
            let _ = file.write(s.as_bytes());
        }
Luca Barbato's avatar
Luca Barbato committed
82
    }
83

84
85
86
87
88
89
90
91
    fn rerun_dir<P: AsRef<Path>>(dir: P) {
        for entry in fs::read_dir(dir).unwrap() {
            let entry = entry.unwrap();
            let path = entry.path();
            println!("cargo:rerun-if-changed={}", path.to_string_lossy());

            if path.is_dir() {
                rerun_dir(path);
92
93
94
            }
        }
    }
95
96

    rerun_dir("aom_build");
Thomas Daede's avatar
Thomas Daede committed
97
}