]> git.proxmox.com Git - cargo.git/blob - vendor/memchr/build.rs
New upstream version 0.47.0
[cargo.git] / vendor / memchr / build.rs
1 use std::env;
2
3 fn main() {
4 enable_simd_optimizations();
5 enable_libc();
6 }
7
8 // This adds various simd cfgs if this compiler supports it.
9 //
10 // This can be disabled with RUSTFLAGS="--cfg memchr_disable_auto_simd", but
11 // this is generally only intended for testing.
12 fn enable_simd_optimizations() {
13 if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD") {
14 return;
15 }
16 println!("cargo:rustc-cfg=memchr_runtime_simd");
17 println!("cargo:rustc-cfg=memchr_runtime_sse2");
18 println!("cargo:rustc-cfg=memchr_runtime_sse42");
19 println!("cargo:rustc-cfg=memchr_runtime_avx");
20 }
21
22 // This adds a `memchr_libc` cfg if and only if libc can be used, if no other
23 // better option is available.
24 //
25 // This could be performed in the source code, but it's simpler to do it once
26 // here and consolidate it into one cfg knob.
27 //
28 // Basically, we use libc only if its enabled and if we aren't targeting a
29 // known bad platform. For example, wasm32 doesn't have a libc and the
30 // performance of memchr on Windows is seemingly worse than the fallback
31 // implementation.
32 fn enable_libc() {
33 const NO_ARCH: &'static [&'static str] = &["wasm32", "windows"];
34 const NO_ENV: &'static [&'static str] = &["sgx"];
35
36 if !is_feature_set("LIBC") {
37 return;
38 }
39
40 let arch = match env::var("CARGO_CFG_TARGET_ARCH") {
41 Err(_) => return,
42 Ok(arch) => arch,
43 };
44 let env = match env::var("CARGO_CFG_TARGET_ENV") {
45 Err(_) => return,
46 Ok(env) => env,
47 };
48 if NO_ARCH.contains(&&*arch) || NO_ENV.contains(&&*env) {
49 return;
50 }
51
52 println!("cargo:rustc-cfg=memchr_libc");
53 }
54
55 fn is_feature_set(name: &str) -> bool {
56 is_env_set(&format!("CARGO_FEATURE_{}", name))
57 }
58
59 fn is_env_set(name: &str) -> bool {
60 env::var_os(name).is_some()
61 }