-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbuild.rs
152 lines (123 loc) · 4.51 KB
/
build.rs
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
142
143
144
145
146
147
148
149
150
151
152
use std::env;
use std::fs::{self, File};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
fn main() {
cfg();
macros();
simd_macros();
println!("cargo:rerun-if-changed=build.rs");
}
fn cfg() {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let target_feature = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
let ok_arch = matches!(&*target_arch, "x86" | "x86_64");
let sse4_2_guaranteed = target_feature.split(',').any(|f| f == "sse4.2");
if sse4_2_guaranteed {
println!(r#"cargo:rustc-cfg=jetscii_sse4_2="yes""#);
} else if ok_arch {
println!(r#"cargo:rustc-cfg=jetscii_sse4_2="maybe""#);
} else {
println!(r#"cargo:rustc-cfg=jetscii_sse4_2="no""#);
}
}
fn macros() {
let mut base: PathBuf = env::var_os("OUT_DIR").unwrap().into();
base.push("src");
fs::create_dir_all(&base)
.unwrap_or_else(|e| panic!("Could not create directory {}: {}", base.display(), e));
base.push("macros.rs");
let mut f = File::create(&base)
.unwrap_or_else(|e| panic!("Could not create {}: {}", base.display(), e));
macros_bytes(&mut f, &base);
macros_ascii_chars(&mut f, &base);
}
fn macros_bytes(f: &mut File, base: &Path) {
let arms: String = (1..=16)
.map(|max| {
let args: Vec<_> = (0..max).map(|i| format!("$b{:02}:expr", i)).collect();
let args = args.join(", ");
let arg_values: Vec<_> = (0..max).map(|i| format!("$b{:02} as u8", i)).collect();
let mut array = arg_values.clone();
array.extend((max..16).map(|_| String::from("0")));
let array = array.join(", ");
let closure_body: Vec<_> = arg_values.iter().map(|n| format!("{} == c", n)).collect();
let closure = format!("|c| {}", closure_body.join(" || "));
format!(
"({}) => ($crate::Bytes::new([{}], {}, {}));\n",
args, array, max, closure
)
})
.collect();
write!(
f,
r#"
/// A convenience constructor for a [`Bytes`] that automatically
/// implements a fallback. Provide 1 to 16 characters.
#[macro_export]
macro_rules! bytes {{
{}}}
"#,
arms
).unwrap_or_else(|e| panic!("Could not write {}: {}", base.display(), e));
}
fn macros_ascii_chars(f: &mut File, base: &Path) {
let arms: String = (1..=16)
.map(|max| {
let args: Vec<_> = (0..max).map(|i| format!("$b{:02}:expr", i)).collect();
let args = args.join(", ");
let arg_values: Vec<_> = (0..max).map(|i| format!("$b{:02} as u8", i)).collect();
let mut array = arg_values.clone();
array.extend((max..16).map(|_| String::from("0")));
let array = array.join(", ");
let closure_body: Vec<_> = arg_values.iter().map(|n| format!("{} == c", n)).collect();
let closure = format!("|c| {}", closure_body.join(" || "));
format!(
"({}) => ($crate::AsciiChars::new([{}], {}, {}));\n",
args, array, max, closure
)
})
.collect();
write!(
f,
r#"
/// A convenience constructor for an [`AsciiChars`] that automatically
/// implements a fallback. Provide 1 to 16 characters.
#[macro_export]
macro_rules! ascii_chars {{
{}}}
"#,
arms
).unwrap_or_else(|e| panic!("Could not write {}: {}", base.display(), e));
}
fn simd_macros() {
let mut base: PathBuf = env::var_os("OUT_DIR").unwrap().into();
base.push("src");
fs::create_dir_all(&base)
.unwrap_or_else(|e| panic!("Could not create directory {}: {}", base.display(), e));
base.push("simd_macros.rs");
let mut f = File::create(&base)
.unwrap_or_else(|e| panic!("Could not create {}: {}", base.display(), e));
let arms: String = (1..=16)
.map(|max| {
let args: Vec<_> = (0..max).map(|i| format!("$b{:02}:expr", i)).collect();
let args = args.join(", ");
let mut array: Vec<_> = (0..max).map(|i| format!("$b{:02}", i)).collect();
array.extend((max..16).map(|_| String::from("0")));
let array = array.join(", ");
format!(
"({}) => ($crate::simd::Bytes::new([{}], {}));\n",
args, array, max
)
})
.collect();
write!(
&mut f,
r#"
#[allow(unused_macros)]
macro_rules! simd_bytes {{
{}}}
"#,
arms
).unwrap_or_else(|e| panic!("Could not write {}: {}", base.display(), e));
}