| 1 |
// Configuration shared with both libm and libm-test |
| 2 |
|
| 3 |
use std::env; |
| 4 |
use std::path::PathBuf; |
| 5 |
|
| 6 |
#[derive(Debug)] |
| 7 |
#[allow(dead_code)] |
| 8 |
pub struct Config { |
| 9 |
pub manifest_dir: PathBuf, |
| 10 |
pub out_dir: PathBuf, |
| 11 |
pub opt_level: String, |
| 12 |
pub cargo_features: Vec<String>, |
| 13 |
pub target_triple: String, |
| 14 |
pub target_arch: String, |
| 15 |
pub target_env: String, |
| 16 |
pub target_families: Vec<String>, |
| 17 |
pub target_os: String, |
| 18 |
pub target_string: String, |
| 19 |
pub target_vendor: String, |
| 20 |
pub target_features: Vec<String>, |
| 21 |
pub reliable_f128: bool, |
| 22 |
pub reliable_f16: bool, |
| 23 |
} |
| 24 |
|
| 25 |
impl Config { |
| 26 |
pub fn from_env() -> Self { |
| 27 |
let target_triple = env::var("TARGET").unwrap(); |
| 28 |
let target_families = env::var("CARGO_CFG_TARGET_FAMILY") |
| 29 |
.map(|feats| feats.split(',').map(ToOwned::to_owned).collect()) |
| 30 |
.unwrap_or_default(); |
| 31 |
let target_features = env::var("CARGO_CFG_TARGET_FEATURE") |
| 32 |
.map(|feats| feats.split(',').map(ToOwned::to_owned).collect()) |
| 33 |
.unwrap_or_default(); |
| 34 |
let cargo_features = env::vars() |
| 35 |
.filter_map(|(name, _value)| name.strip_prefix("CARGO_FEATURE_").map(ToOwned::to_owned)) |
| 36 |
.map(|s| s.to_lowercase().replace("_", "-")) |
| 37 |
.collect(); |
| 38 |
|
| 39 |
Self { |
| 40 |
target_triple, |
| 41 |
manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()), |
| 42 |
out_dir: PathBuf::from(env::var("OUT_DIR").unwrap()), |
| 43 |
opt_level: env::var("OPT_LEVEL").unwrap(), |
| 44 |
cargo_features, |
| 45 |
target_arch: env::var("CARGO_CFG_TARGET_ARCH").unwrap(), |
| 46 |
target_env: env::var("CARGO_CFG_TARGET_ENV").unwrap(), |
| 47 |
target_families, |
| 48 |
target_os: env::var("CARGO_CFG_TARGET_OS").unwrap(), |
| 49 |
target_string: env::var("TARGET").unwrap(), |
| 50 |
target_vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(), |
| 51 |
target_features, |
| 52 |
// Note that these are unstable options, so only show up with the nightly compiler or |
| 53 |
// with `RUSTC_BOOTSTRAP=1` (which is required to use the types anyway). |
| 54 |
reliable_f128: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F128").is_some(), |
| 55 |
reliable_f16: env::var_os("CARGO_CFG_TARGET_HAS_RELIABLE_F16").is_some(), |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/// Libm gets most config options made available. |
| 61 |
#[allow(dead_code)] |
| 62 |
pub fn emit_libm_config(cfg: &Config) { |
| 63 |
emit_intrinsics_cfg(); |
| 64 |
emit_arch_cfg(); |
| 65 |
emit_optimization_cfg(cfg); |
| 66 |
emit_cfg_shorthands(cfg); |
| 67 |
emit_cfg_env(cfg); |
| 68 |
emit_f16_f128_cfg(cfg); |
| 69 |
} |
| 70 |
|
| 71 |
/// Tests don't need most feature-related config. |
| 72 |
#[allow(dead_code)] |
| 73 |
pub fn emit_test_config(cfg: &Config) { |
| 74 |
emit_optimization_cfg(cfg); |
| 75 |
emit_cfg_shorthands(cfg); |
| 76 |
emit_cfg_env(cfg); |
| 77 |
emit_f16_f128_cfg(cfg); |
| 78 |
} |
| 79 |
|
| 80 |
/// Simplify the feature logic for enabling intrinsics so code only needs to use |
| 81 |
/// `cfg(intrinsics_enabled)`. |
| 82 |
fn emit_intrinsics_cfg() { |
| 83 |
println!("cargo:rustc-check-cfg=cfg(intrinsics_enabled)"); |
| 84 |
|
| 85 |
// Disabled by default; `unstable-intrinsics` enables again; `force-soft-floats` overrides |
| 86 |
// to disable. |
| 87 |
if cfg!(feature = "unstable-intrinsics") && !cfg!(feature = "force-soft-floats") { |
| 88 |
println!("cargo:rustc-cfg=intrinsics_enabled"); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/// Simplify the feature logic for enabling arch-specific features so code only needs to use |
| 93 |
/// `cfg(arch_enabled)`. |
| 94 |
fn emit_arch_cfg() { |
| 95 |
println!("cargo:rustc-check-cfg=cfg(arch_enabled)"); |
| 96 |
|
| 97 |
// Enabled by default via the "arch" feature, `force-soft-floats` overrides to disable. |
| 98 |
if cfg!(feature = "arch") && !cfg!(feature = "force-soft-floats") { |
| 99 |
println!("cargo:rustc-cfg=arch_enabled"); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/// Some tests are extremely slow. Emit a config option based on optimization level. |
| 104 |
fn emit_optimization_cfg(cfg: &Config) { |
| 105 |
println!("cargo:rustc-check-cfg=cfg(optimizations_enabled)"); |
| 106 |
|
| 107 |
if !matches!(cfg.opt_level.as_str(), "0" | "1") { |
| 108 |
println!("cargo:rustc-cfg=optimizations_enabled"); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/// Provide an alias for common longer config combinations. |
| 113 |
fn emit_cfg_shorthands(cfg: &Config) { |
| 114 |
println!("cargo:rustc-check-cfg=cfg(x86_no_sse)"); |
| 115 |
if cfg.target_arch == "x86" && !cfg.target_features.iter().any(|f| f == "sse") { |
| 116 |
// Shorthand to detect i586 targets |
| 117 |
println!("cargo:rustc-cfg=x86_no_sse"); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/// Reemit config that we make use of for test logging. |
| 122 |
fn emit_cfg_env(cfg: &Config) { |
| 123 |
println!( |
| 124 |
"cargo:rustc-env=CFG_CARGO_FEATURES={:?}", |
| 125 |
cfg.cargo_features |
| 126 |
); |
| 127 |
println!("cargo:rustc-env=CFG_OPT_LEVEL={}", cfg.opt_level); |
| 128 |
println!( |
| 129 |
"cargo:rustc-env=CFG_TARGET_FEATURES={:?}", |
| 130 |
cfg.target_features |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/// Configure whether or not `f16` and `f128` support should be enabled. |
| 135 |
fn emit_f16_f128_cfg(cfg: &Config) { |
| 136 |
println!("cargo:rustc-check-cfg=cfg(f16_enabled)"); |
| 137 |
println!("cargo:rustc-check-cfg=cfg(f128_enabled)"); |
| 138 |
|
| 139 |
// `unstable-float` enables these features. |
| 140 |
if !cfg!(feature = "unstable-float") { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
/* See the compiler-builtins configure file for info about the meaning of these options */ |
| 145 |
|
| 146 |
// If the feature is set, disable both of these types. |
| 147 |
let no_f16_f128 = cfg.cargo_features.iter().any(|s| s == "no-f16-f128"); |
| 148 |
|
| 149 |
println!("cargo:rustc-check-cfg=cfg(f16_enabled)"); |
| 150 |
if cfg.reliable_f16 && !no_f16_f128 { |
| 151 |
println!("cargo:rustc-cfg=f16_enabled"); |
| 152 |
} |
| 153 |
|
| 154 |
println!("cargo:rustc-check-cfg=cfg(f128_enabled)"); |
| 155 |
if cfg.reliable_f128 && !no_f16_f128 { |
| 156 |
println!("cargo:rustc-cfg=f128_enabled"); |
| 157 |
} |
| 158 |
} |