]> git.proxmox.com Git - cargo.git/blob - vendor/memchr/build.rs
New upstream version 0.63.1
[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 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.
17 fn enable_simd_optimizations() {
18 if is_env_set("CARGO_CFG_MEMCHR_DISABLE_AUTO_SIMD") {
19 return;
20 }
21 let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
22 match &arch[..] {
23 "x86_64" => {
24 if !target_has_feature("sse2") {
25 return;
26 }
27 println!("cargo:rustc-cfg=memchr_runtime_simd");
28 println!("cargo:rustc-cfg=memchr_runtime_sse2");
29 println!("cargo:rustc-cfg=memchr_runtime_sse42");
30 println!("cargo:rustc-cfg=memchr_runtime_avx");
31 }
32 "wasm32" | "wasm64" => {
33 if !target_has_feature("simd128") {
34 return;
35 }
36 println!("cargo:rustc-cfg=memchr_runtime_simd");
37 println!("cargo:rustc-cfg=memchr_runtime_wasm128");
38 }
39 _ => {}
40 }
41 }
42
43 // This adds a `memchr_libc` cfg if and only if libc can be used, if no other
44 // better option is available.
45 //
46 // This could be performed in the source code, but it's simpler to do it once
47 // here and consolidate it into one cfg knob.
48 //
49 // Basically, we use libc only if its enabled and if we aren't targeting a
50 // known bad platform. For example, wasm32 doesn't have a libc and the
51 // performance of memchr on Windows is seemingly worse than the fallback
52 // implementation.
53 fn enable_libc() {
54 const NO_ARCH: &'static [&'static str] = &["wasm32", "windows"];
55 const NO_ENV: &'static [&'static str] = &["sgx"];
56
57 if !is_feature_set("LIBC") {
58 return;
59 }
60
61 let arch = match env::var("CARGO_CFG_TARGET_ARCH") {
62 Err(_) => return,
63 Ok(arch) => arch,
64 };
65 let env = match env::var("CARGO_CFG_TARGET_ENV") {
66 Err(_) => return,
67 Ok(env) => env,
68 };
69 if NO_ARCH.contains(&&*arch) || NO_ENV.contains(&&*env) {
70 return;
71 }
72
73 println!("cargo:rustc-cfg=memchr_libc");
74 }
75
76 fn is_feature_set(name: &str) -> bool {
77 is_env_set(&format!("CARGO_FEATURE_{}", name))
78 }
79
80 fn is_env_set(name: &str) -> bool {
81 env::var_os(name).is_some()
82 }
83
84 fn target_has_feature(feature: &str) -> bool {
85 env::var("CARGO_CFG_TARGET_FEATURE")
86 .map(|features| features.contains(feature))
87 .unwrap_or(false)
88 }