1 use std::env;
2 
main()3 fn main() {
4     enable_simd_optimizations();
5     enable_libc();
6 }
7 
8 // This adds various simd cfgs if this compiler and target support it.
9 //
10 // This can be disabled with RUSTFLAGS="--cfg memchr_disable_auto_simd", but
11 // this is generally only intended for testing.
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.
enable_simd_optimizations()17 fn enable_simd_optimizations() {
18     if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD")
19         || !target_has_feature("sse2")
20     {
21         return;
22     }
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 
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.
enable_libc()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 
is_feature_set(name: &str) -> bool62 fn is_feature_set(name: &str) -> bool {
63     is_env_set(&format!("CARGO_FEATURE_{}", name))
64 }
65 
is_env_set(name: &str) -> bool66 fn is_env_set(name: &str) -> bool {
67     env::var_os(name).is_some()
68 }
69 
target_has_feature(feature: &str) -> bool70 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 }
75