build.rs 4.1 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;
9 10
#[cfg(target_arch = "x86_64")]
extern crate nasm_rs;
Thomas Daede's avatar
Thomas Daede committed
11

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

Thomas Daede's avatar
Thomas Daede committed
16
fn main() {
17
    #[cfg(all(target_arch = "x86_64", not(windows)))] {
Luca Barbato's avatar
Luca Barbato committed
18 19
        use std::fs::File;
        use std::io::Write;
20 21 22 23
        let out_dir = env::var("OUT_DIR").unwrap();
        {
            let dest_path = Path::new(&out_dir).join("config.asm");
            let mut config_file = File::create(dest_path).unwrap();
24
            config_file.write(b"	%define private_prefix rav1e\n").unwrap();
25 26 27 28
            config_file.write(b"	%define ARCH_X86_32 0\n").unwrap();
            config_file.write(b" %define ARCH_X86_64 1\n").unwrap();
            config_file.write(b"	%define PIC 1\n").unwrap();
            config_file.write(b" %define STACK_ALIGNMENT 32\n").unwrap();
29 30 31
            if cfg!(target_os="macos") {
              config_file.write(b" %define PREFIX 1\n").unwrap();
            }
32 33 34 35
        }
        let mut config_include_arg = String::from("-I");
        config_include_arg.push_str(&out_dir);
        config_include_arg.push('/');
36 37
        nasm_rs::compile_library_args("rav1easm", &["src/x86/ipred.asm"], &[&config_include_arg, "-Isrc/"]);
        println!("cargo:rustc-link-lib=static=rav1easm");
38 39
        rerun_dir("src/x86");
        rerun_dir("src/ext/x86");
40 41
    }

42 43 44 45
    if cfg!(windows) && cfg!(feature = "decode_test") {
        panic!("Unsupported feature on this platform!");
    }

46 47
    let cargo_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let build_path = Path::new(&cargo_dir).join("aom_build/aom");
48 49
    let debug = if let Some(v) = env::var("PROFILE").ok() {
        match v.as_str() {
50 51
            "bench" | "release" => false,
            _ => true,
52 53
        }
    } else {
54
        false
55
    };
56

57
    let dst = cmake::Config::new(build_path)
58
        .define("CONFIG_DEBUG", (debug as u8).to_string())
Luca Barbato's avatar
Luca Barbato committed
59
        .define("CONFIG_ANALYZER", "0")
60
        .define("ENABLE_DOCS", "0")
61
        .define("ENABLE_NASM", "1")
62 63
        .define("ENABLE_TESTS", "0")
        .no_build_target(cfg!(windows))
64 65
        .build();

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

69
    #[cfg(windows)] {
70 71 72 73
        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");
74
    }
75

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

80 81
        #[cfg(feature = "decode_test")] {
            use std::io::Write;
82

83
            let out_dir = env::var("OUT_DIR").unwrap();
84

85
            let headers = _libs.include_paths.clone();
86

87 88 89 90
            let mut builder = bindgen::builder()
                .blacklist_type("max_align_t")
                .rustfmt_bindings(false)
                .header("data/aom.h");
91

92 93 94
            for header in headers {
                builder = builder.clang_arg("-I").clang_arg(header.to_str().unwrap());
            }
Luca Barbato's avatar
Luca Barbato committed
95

96 97 98 99 100 101 102
            // Manually fix the comment so rustdoc won't try to pick them
            let s = builder
                .generate()
                .unwrap()
                .to_string()
                .replace("/**", "/*")
                .replace("/*!", "/*");
103

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

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

108 109
            let _ = file.write(s.as_bytes());
        }
Luca Barbato's avatar
Luca Barbato committed
110
    }
111

112 113 114 115 116 117 118 119
    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);
120 121 122
            }
        }
    }
123 124

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