aboutsummaryrefslogtreecommitdiffhomepage
path: root/zluda_inject/build.rs
blob: 743e7089f308ade387ec06ef03fd46ae71afd932 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{
    env::{self, VarError},
    fs::{self, DirEntry},
    io,
    path::{self, PathBuf},
    process::Command,
};

fn main() -> Result<(), VarError> {
    println!("cargo:rerun-if-changed=build.rs");
    if env::var("PROFILE")? != "debug" {
        return Ok(());
    }
    let rustc_exe = env::var("RUSTC")?;
    let out_dir = env::var("OUT_DIR")?;
    let target = env::var("TARGET")?;
    let is_msvc = env::var("CARGO_CFG_TARGET_ENV")? == "msvc";
    let opt_level = env::var("OPT_LEVEL")?;
    let debug = str::parse::<bool>(env::var("DEBUG")?.as_str()).unwrap();
    let mut helpers_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
    helpers_dir.push("tests");
    helpers_dir.push("helpers");
    let helpers_dir_as_string = helpers_dir.to_string_lossy();
    println!("cargo:rerun-if-changed={}", helpers_dir_as_string);
    for rust_file in fs::read_dir(&helpers_dir).unwrap().filter_map(rust_file) {
        let full_file_path = format!(
            "{}{}{}",
            helpers_dir_as_string,
            path::MAIN_SEPARATOR,
            rust_file
        );
        let mut rustc_cmd = Command::new(&*rustc_exe);
        if debug {
            rustc_cmd.arg("-g");
        }
        rustc_cmd.arg(format!("-Lnative={}", helpers_dir_as_string));
        if !is_msvc {
            // HACK ALERT
            // I have no idea why the extra library below have to be linked
            rustc_cmd.arg(r"-lucrt");
        }
        rustc_cmd
            .arg("-ldylib=nvcuda")
            .arg("-C")
            .arg(format!("opt-level={}", opt_level))
            .arg("--out-dir")
            .arg(format!("{}", out_dir))
            .arg("--target")
            .arg(format!("{}", target))
            .arg(full_file_path);
        assert!(rustc_cmd.status().unwrap().success());
    }
    println!("cargo:rustc-env=HELPERS_OUT_DIR={}", &out_dir);
    Ok(())
}

fn rust_file(entry: io::Result<DirEntry>) -> Option<String> {
    entry.ok().and_then(|e| {
        let os_file_name = e.file_name();
        let file_name = os_file_name.to_string_lossy();
        let is_file = e.file_type().ok().map(|t| t.is_file()).unwrap_or(false);
        if is_file && file_name.ends_with(".rs") {
            Some(file_name.to_string())
        } else {
            None
        }
    })
}