aboutsummaryrefslogtreecommitdiff
path: root/tests/ci/src/main.rs
blob: df5803738d4bfff0d616744dfd8369f82e3363f2 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use anyhow::bail;
use std::path::Path;
use std::process::Command;

struct TestCommand {
    command: Command,
    repr: String,
}

impl TestCommand {
    fn new(args: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
        Self::new_inner(args, None)
    }

    fn new_with_dir(args: impl IntoIterator<Item = impl AsRef<str>>, dir: &str) -> Self {
        Self::new_inner(args, Some(dir))
    }

    fn new_inner(args: impl IntoIterator<Item = impl AsRef<str>>, dir: Option<&str>) -> Self {
        let mut args = args.into_iter();
        let prog = args.next().unwrap().as_ref().to_string();
        let mut command = Command::new(&prog);
        let mut repr = prog;
        if let Some(dir) = dir {
            command.current_dir(dir);
            repr = format!("cd {} && {}", dir, repr);
        }
        for arg in args {
            let arg_str: &str = arg.as_ref();
            command.arg(arg_str);
            repr.push_str(" ");
            repr.push_str(arg_str);
        }
        Self { command, repr }
    }

    fn run(&mut self) -> anyhow::Result<()> {
        println!("====================================================");
        println!("Command: {}", &self.repr);
        println!("====================================================");
        let status = self.command.status()?;
        if !status.success() {
            bail!("command failed: {}", &self.repr);
        }
        Ok(())
    }
}

fn main() -> anyhow::Result<()> {
    let show = matches!(std::env::args().nth(1), Some(s) if s == "--show");

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        let mark = |b| if b { "YES ✔" } else { "no" };
        println!("x86 CPU features detected:");
        println!("  SSE4.1: {}", mark(is_x86_feature_detected!("sse4.1")));
        println!("    AVX2: {}", mark(is_x86_feature_detected!("avx2")));
        println!(" AVX512F: {}", mark(is_x86_feature_detected!("avx512f")));
        println!("AVX512VL: {}", mark(is_x86_feature_detected!("avx512vl")));
        println!("");
    }

    let project_root_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap();
    std::env::set_current_dir(project_root_dir)?;

    let mut commands = Vec::new();
    commands.push(TestCommand::new(&["cargo", "test"]));
    commands.push(TestCommand::new(&[
        "cargo",
        "test",
        "--no-default-features",
    ]));

    for purity in &[&[][..], &["prefer_intrinsics"][..], &["pure"][..]] {
        for support in &[
            &[][..],
            &["no_avx512"][..],
            &["no_avx512", "no_avx2"][..],
            &["no_avx512", "no_avx2", "no_sse41"][..],
        ] {
            for &release in &[false, true] {
                let mut command = vec!["cargo", "test"];
                let mut features_vec: Vec<&str> = purity.to_vec();
                features_vec.extend_from_slice(support);
                let features = format!("--features={}", features_vec.join(","));
                command.push(&features);
                if release {
                    command.push("--release");
                }
                commands.push(TestCommand::new(command));
            }
        }
    }

    commands.push(TestCommand::new_with_dir(&["cargo", "test"], "b3sum"));
    commands.push(TestCommand::new_with_dir(
        &["cargo", "test", "--no-default-features"],
        "b3sum",
    ));

    commands.push(TestCommand::new_with_dir(
        &["cargo", "test"],
        "test_vectors",
    ));
    commands.push(TestCommand::new_with_dir(
        &["cargo", "test", "--features=prefer_intrinsics"],
        "test_vectors",
    ));
    commands.push(TestCommand::new_with_dir(
        &["cargo", "test", "--features=pure"],
        "test_vectors",
    ));

    commands.push(TestCommand::new_with_dir(
        &["cargo", "test"],
        "c/blake3_c_rust_bindings",
    ));
    commands.push(TestCommand::new_with_dir(
        &["cargo", "test", "--features=prefer_intrinsics"],
        "c/blake3_c_rust_bindings",
    ));

    commands.push(TestCommand::new_with_dir(
        &["cargo", "test"],
        "reference_impl",
    ));

    for command in &mut commands {
        if show {
            println!("{}", &command.repr);
        } else {
            command.run()?;
        }
    }

    Ok(())
}