]>
Commit | Line | Data |
---|---|---|
0731742a | 1 | use std::env; |
0731742a XL |
2 | |
3 | fn main() { | |
74b04a01 | 4 | enable_simd_optimizations(); |
48663c56 | 5 | enable_libc(); |
0731742a XL |
6 | } |
7 | ||
5869c6ff | 8 | // This adds various simd cfgs if this compiler and target support it. |
48663c56 XL |
9 | // |
10 | // This can be disabled with RUSTFLAGS="--cfg memchr_disable_auto_simd", but | |
11 | // this is generally only intended for testing. | |
5869c6ff XL |
12 | // |
13 | // On targets which don't feature SSE2, this is disabled, as LLVM wouln't know | |
14 | // how to work with SSE2 operands. Enabling SSE4.2 and AVX on SSE2-only targets | |
15 | // is not a problem. In that case, the fastest option will be chosen at | |
16 | // runtime. | |
74b04a01 | 17 | fn enable_simd_optimizations() { |
5869c6ff XL |
18 | if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD") |
19 | || !target_has_feature("sse2") | |
20 | { | |
0731742a XL |
21 | return; |
22 | } | |
0731742a XL |
23 | println!("cargo:rustc-cfg=memchr_runtime_simd"); |
24 | println!("cargo:rustc-cfg=memchr_runtime_sse2"); | |
25 | println!("cargo:rustc-cfg=memchr_runtime_sse42"); | |
26 | println!("cargo:rustc-cfg=memchr_runtime_avx"); | |
27 | } | |
28 | ||
48663c56 XL |
29 | // This adds a `memchr_libc` cfg if and only if libc can be used, if no other |
30 | // better option is available. | |
31 | // | |
32 | // This could be performed in the source code, but it's simpler to do it once | |
33 | // here and consolidate it into one cfg knob. | |
34 | // | |
35 | // Basically, we use libc only if its enabled and if we aren't targeting a | |
36 | // known bad platform. For example, wasm32 doesn't have a libc and the | |
37 | // performance of memchr on Windows is seemingly worse than the fallback | |
38 | // implementation. | |
39 | fn enable_libc() { | |
40 | const NO_ARCH: &'static [&'static str] = &["wasm32", "windows"]; | |
41 | const NO_ENV: &'static [&'static str] = &["sgx"]; | |
42 | ||
43 | if !is_feature_set("LIBC") { | |
44 | return; | |
45 | } | |
46 | ||
47 | let arch = match env::var("CARGO_CFG_TARGET_ARCH") { | |
48 | Err(_) => return, | |
49 | Ok(arch) => arch, | |
50 | }; | |
51 | let env = match env::var("CARGO_CFG_TARGET_ENV") { | |
52 | Err(_) => return, | |
53 | Ok(env) => env, | |
54 | }; | |
55 | if NO_ARCH.contains(&&*arch) || NO_ENV.contains(&&*env) { | |
56 | return; | |
57 | } | |
58 | ||
59 | println!("cargo:rustc-cfg=memchr_libc"); | |
60 | } | |
61 | ||
62 | fn is_feature_set(name: &str) -> bool { | |
74b04a01 | 63 | is_env_set(&format!("CARGO_FEATURE_{}", name)) |
48663c56 XL |
64 | } |
65 | ||
0731742a | 66 | fn is_env_set(name: &str) -> bool { |
48663c56 XL |
67 | env::var_os(name).is_some() |
68 | } | |
5869c6ff XL |
69 | |
70 | fn target_has_feature(feature: &str) -> bool { | |
71 | env::var("CARGO_CFG_TARGET_FEATURE") | |
72 | .map(|features| features.contains(feature)) | |
73 | .unwrap_or(false) | |
74 | } |