1 | use std::env; |
2 | //use std::ffi::OsString; |
3 | //use std::process::Command; |
4 | |
5 | fn main() { |
6 | // We don't currently need to check the Version anymore... |
7 | // But leaving this in place in case we need to in the future. |
8 | /* |
9 | let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); |
10 | let output = Command::new(&rustc) |
11 | .arg("--version") |
12 | .output() |
13 | .expect("failed to check 'rustc --version'") |
14 | .stdout; |
15 | |
16 | let version = String::from_utf8(output) |
17 | .expect("rustc version output should be utf-8"); |
18 | */ |
19 | |
20 | enable_new_features(/*&version*/); |
21 | } |
22 | |
23 | fn enable_new_features(/*raw_version: &str*/) { |
24 | /* |
25 | let version = match Version::parse(raw_version) { |
26 | Ok(version) => version, |
27 | Err(err) => { |
28 | println!("cargo:warning=failed to parse `rustc --version`: {}", err); |
29 | return; |
30 | } |
31 | }; |
32 | */ |
33 | |
34 | enable_simd(/*version*/); |
35 | } |
36 | |
37 | fn enable_simd(/*version: Version*/) { |
38 | if env::var_os("CARGO_FEATURE_STD" ).is_none() { |
39 | println!("cargo:warning=building for no_std disables httparse SIMD" ); |
40 | return; |
41 | } |
42 | if env::var_os("CARGO_CFG_MIRI" ).is_some() { |
43 | println!("cargo:warning=building for Miri disables httparse SIMD" ); |
44 | return; |
45 | } |
46 | |
47 | let env_disable = "CARGO_CFG_HTTPARSE_DISABLE_SIMD" ; |
48 | if var_is(env_disable, "1" ) { |
49 | println!("cargo:warning=detected {} environment variable, disabling SIMD" , env_disable); |
50 | return; |
51 | } |
52 | |
53 | println!("cargo:rustc-cfg=httparse_simd" ); |
54 | |
55 | // cfg(target_feature) isn't stable yet, but CARGO_CFG_TARGET_FEATURE has |
56 | // a list... We aren't doing anything unsafe, since the is_x86_feature_detected |
57 | // macro still checks in the actual lib, BUT! |
58 | // |
59 | // By peeking at the list here, we can change up slightly how we do feature |
60 | // detection in the lib. If our features aren't in the feature list, we |
61 | // stick with a cached runtime detection strategy. |
62 | // |
63 | // But if the features *are* in the list, we benefit from removing our cache, |
64 | // since the compiler will eliminate several branches with its internal |
65 | // cfg(target_feature) usage. |
66 | |
67 | |
68 | let env_runtime_only = "CARGO_CFG_HTTPARSE_DISABLE_SIMD_COMPILETIME" ; |
69 | if var_is(env_runtime_only, "1" ) { |
70 | println!("cargo:warning=detected {} environment variable, using runtime SIMD detection only" , env_runtime_only); |
71 | return; |
72 | } |
73 | let feature_list = match env::var_os("CARGO_CFG_TARGET_FEATURE" ) { |
74 | Some(var) => match var.into_string() { |
75 | Ok(s) => s, |
76 | Err(_) => { |
77 | println!("cargo:warning=CARGO_CFG_TARGET_FEATURE was not valid utf-8" ); |
78 | return; |
79 | }, |
80 | }, |
81 | None => { |
82 | println!("cargo:warning=CARGO_CFG_TARGET_FEATURE was not set" ); |
83 | return |
84 | }, |
85 | }; |
86 | |
87 | let mut saw_sse42 = false; |
88 | let mut saw_avx2 = false; |
89 | |
90 | for feature in feature_list.split(',' ) { |
91 | let feature = feature.trim(); |
92 | if !saw_sse42 && feature == "sse4.2" { |
93 | saw_sse42 = true; |
94 | println!("cargo:rustc-cfg=httparse_simd_target_feature_sse42" ); |
95 | } |
96 | |
97 | if !saw_avx2 && feature == "avx2" { |
98 | saw_avx2 = true; |
99 | println!("cargo:rustc-cfg=httparse_simd_target_feature_avx2" ); |
100 | } |
101 | } |
102 | } |
103 | |
104 | /* |
105 | #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] |
106 | struct Version { |
107 | major: u32, |
108 | minor: u32, |
109 | patch: u32, |
110 | } |
111 | |
112 | impl Version { |
113 | fn parse(mut s: &str) -> Result<Version, String> { |
114 | if !s.starts_with("rustc ") { |
115 | return Err(format!("unrecognized version string: {}", s)); |
116 | } |
117 | s = &s["rustc ".len()..]; |
118 | |
119 | let parts: Vec<&str> = s.split(".").collect(); |
120 | if parts.len() < 3 { |
121 | return Err(format!("not enough version parts: {:?}", parts)); |
122 | } |
123 | |
124 | let mut num = String::new(); |
125 | for c in parts[0].chars() { |
126 | if !c.is_digit(10) { |
127 | break; |
128 | } |
129 | num.push(c); |
130 | } |
131 | let major = num.parse::<u32>().map_err(|e| e.to_string())?; |
132 | |
133 | num.clear(); |
134 | for c in parts[1].chars() { |
135 | if !c.is_digit(10) { |
136 | break; |
137 | } |
138 | num.push(c); |
139 | } |
140 | let minor = num.parse::<u32>().map_err(|e| e.to_string())?; |
141 | |
142 | num.clear(); |
143 | for c in parts[2].chars() { |
144 | if !c.is_digit(10) { |
145 | break; |
146 | } |
147 | num.push(c); |
148 | } |
149 | let patch = num.parse::<u32>().map_err(|e| e.to_string())?; |
150 | |
151 | Ok(Version { |
152 | major: major, |
153 | minor: minor, |
154 | patch: patch, |
155 | }) |
156 | } |
157 | } |
158 | */ |
159 | |
160 | fn var_is(key: &str, val: &str) -> bool { |
161 | match env::var(key) { |
162 | Ok(v) => v == val, |
163 | Err(_) => false, |
164 | } |
165 | } |
166 | |