add sqrt_fast_f16/f32/f64
[vector-math.git] / build.rs
1 use std::{
2 env, fs,
3 io::{self, Read, Write},
4 path::Path,
5 process::{Child, Command, Stdio},
6 thread,
7 };
8 use vector_math_build_helpers::make_context_types;
9
10 fn format_source(source: String) -> String {
11 match try_format_source(&source) {
12 Ok(v) => v,
13 Err(e) => {
14 eprintln!("rustfmt failed: {}", e);
15 source
16 }
17 }
18 }
19
20 fn try_format_source(source: &str) -> io::Result<String> {
21 let mut command = Command::new("rustfmt")
22 .stdin(Stdio::piped())
23 .stdout(Stdio::piped())
24 .spawn()?;
25 let stdin = command.stdin.take().unwrap();
26 let reader_thread = thread::spawn(move || -> io::Result<(String, Child)> {
27 let mut output = String::new();
28 command.stdout.take().unwrap().read_to_string(&mut output)?;
29 Ok((output, command))
30 });
31 let write_result = { stdin }.write_all(source.as_bytes());
32 let join_result = reader_thread.join().unwrap();
33 write_result?;
34 let (output, mut command) = join_result?;
35 let exit_status = command.wait()?;
36 if exit_status.success() {
37 Ok(output)
38 } else {
39 Err(io::Error::new(
40 io::ErrorKind::Other,
41 format!("exited with status {}", exit_status),
42 ))
43 }
44 }
45
46 fn main() {
47 fs::write(
48 Path::new(&env::var_os("OUT_DIR").unwrap()).join("context_trait.rs"),
49 format_source(make_context_types().to_string()),
50 )
51 .unwrap();
52 }