|
| 1 | +// Configuration shared with both libm and libm-test |
| 2 | + |
| 3 | +use std::env; |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +#[allow(dead_code)] |
| 7 | +pub struct Config { |
| 8 | + pub manifest_dir: PathBuf, |
| 9 | + pub out_dir: PathBuf, |
| 10 | + pub opt_level: u8, |
| 11 | + pub target_arch: String, |
| 12 | + pub target_env: String, |
| 13 | + pub target_family: Option<String>, |
| 14 | + pub target_os: String, |
| 15 | + pub target_string: String, |
| 16 | + pub target_vendor: String, |
| 17 | + pub target_features: Vec<String>, |
| 18 | +} |
| 19 | + |
| 20 | +impl Config { |
| 21 | + pub fn from_env() -> Self { |
| 22 | + let target_features = env::var("CARGO_CFG_TARGET_FEATURE") |
| 23 | + .map(|feats| feats.split(',').map(ToOwned::to_owned).collect()) |
| 24 | + .unwrap_or_default(); |
| 25 | + |
| 26 | + Self { |
| 27 | + manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()), |
| 28 | + out_dir: PathBuf::from(env::var("OUT_DIR").unwrap()), |
| 29 | + opt_level: env::var("OPT_LEVEL").unwrap().parse().unwrap(), |
| 30 | + target_arch: env::var("CARGO_CFG_TARGET_ARCH").unwrap(), |
| 31 | + target_env: env::var("CARGO_CFG_TARGET_ENV").unwrap(), |
| 32 | + target_family: env::var("CARGO_CFG_TARGET_FAMILY").ok(), |
| 33 | + target_os: env::var("CARGO_CFG_TARGET_OS").unwrap(), |
| 34 | + target_string: env::var("TARGET").unwrap(), |
| 35 | + target_vendor: env::var("CARGO_CFG_TARGET_VENDOR").unwrap(), |
| 36 | + target_features, |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Libm gets most config options made available. |
| 42 | +#[allow(dead_code)] |
| 43 | +pub fn emit_libm_config(cfg: &Config) { |
| 44 | + emit_intrinsics_cfg(); |
| 45 | + emit_arch_cfg(); |
| 46 | + emit_optimization_cfg(cfg); |
| 47 | + emit_cfg_shorthands(cfg); |
| 48 | + emit_f16_f128_cfg(cfg); |
| 49 | +} |
| 50 | + |
| 51 | +/// Tests don't need most feature-related config. |
| 52 | +#[allow(dead_code)] |
| 53 | +pub fn emit_test_config(cfg: &Config) { |
| 54 | + emit_optimization_cfg(cfg); |
| 55 | + emit_cfg_shorthands(cfg); |
| 56 | + emit_f16_f128_cfg(cfg); |
| 57 | +} |
| 58 | + |
| 59 | +/// Simplify the feature logic for enabling intrinsics so code only needs to use |
| 60 | +/// `cfg(intrinsics_enabled)`. |
| 61 | +fn emit_intrinsics_cfg() { |
| 62 | + println!("cargo:rustc-check-cfg=cfg(intrinsics_enabled)"); |
| 63 | + |
| 64 | + // Disabled by default; `unstable-intrinsics` enables again; `force-soft-floats` overrides |
| 65 | + // to disable. |
| 66 | + if cfg!(feature = "unstable-intrinsics") && !cfg!(feature = "force-soft-floats") { |
| 67 | + println!("cargo:rustc-cfg=intrinsics_enabled"); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Simplify the feature logic for enabling arch-specific features so code only needs to use |
| 72 | +/// `cfg(arch_enabled)`. |
| 73 | +fn emit_arch_cfg() { |
| 74 | + println!("cargo:rustc-check-cfg=cfg(arch_enabled)"); |
| 75 | + |
| 76 | + // Enabled by default via the "arch" feature, `force-soft-floats` overrides to disable. |
| 77 | + if cfg!(feature = "arch") && !cfg!(feature = "force-soft-floats") { |
| 78 | + println!("cargo:rustc-cfg=arch_enabled"); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Some tests are extremely slow. Emit a config option based on optimization level. |
| 83 | +fn emit_optimization_cfg(cfg: &Config) { |
| 84 | + println!("cargo:rustc-check-cfg=cfg(optimizations_enabled)"); |
| 85 | + |
| 86 | + if cfg.opt_level >= 2 { |
| 87 | + println!("cargo:rustc-cfg=optimizations_enabled"); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// Provide an alias for common longer config combinations. |
| 92 | +fn emit_cfg_shorthands(cfg: &Config) { |
| 93 | + println!("cargo:rustc-check-cfg=cfg(x86_no_sse)"); |
| 94 | + if cfg.target_arch == "x86" && !cfg.target_features.iter().any(|f| f == "sse") { |
| 95 | + // Shorthand to detect i586 targets |
| 96 | + println!("cargo:rustc-cfg=x86_no_sse"); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Configure whether or not `f16` and `f128` support should be enabled. |
| 101 | +fn emit_f16_f128_cfg(cfg: &Config) { |
| 102 | + println!("cargo:rustc-check-cfg=cfg(f16_enabled)"); |
| 103 | + println!("cargo:rustc-check-cfg=cfg(f128_enabled)"); |
| 104 | + |
| 105 | + // `unstable-float` enables these features. |
| 106 | + if !cfg!(feature = "unstable-float") { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Set whether or not `f16` and `f128` are supported at a basic level by LLVM. This only means |
| 111 | + // that the backend will not crash when using these types and generates code that can be called |
| 112 | + // without crashing (no infinite recursion). This does not mean that the platform doesn't have |
| 113 | + // ABI or other bugs. |
| 114 | + // |
| 115 | + // We do this here rather than in `rust-lang/rust` because configuring via cargo features is |
| 116 | + // not straightforward. |
| 117 | + // |
| 118 | + // Original source of this list: |
| 119 | + // <https://github.com/rust-lang/compiler-builtins/pull/652#issuecomment-2266151350> |
| 120 | + let f16_enabled = match cfg.target_arch.as_str() { |
| 121 | + // Unsupported <https://github.com/llvm/llvm-project/issues/94434> |
| 122 | + "arm64ec" => false, |
| 123 | + // Selection failure <https://github.com/llvm/llvm-project/issues/50374> |
| 124 | + "s390x" => false, |
| 125 | + // Infinite recursion <https://github.com/llvm/llvm-project/issues/97981> |
| 126 | + // FIXME(llvm): loongarch fixed by <https://github.com/llvm/llvm-project/pull/107791> |
| 127 | + "csky" => false, |
| 128 | + "hexagon" => false, |
| 129 | + "loongarch64" => false, |
| 130 | + "mips" | "mips64" | "mips32r6" | "mips64r6" => false, |
| 131 | + "powerpc" | "powerpc64" => false, |
| 132 | + "sparc" | "sparc64" => false, |
| 133 | + "wasm32" | "wasm64" => false, |
| 134 | + // Most everything else works as of LLVM 19 |
| 135 | + _ => true, |
| 136 | + }; |
| 137 | + |
| 138 | + let f128_enabled = match cfg.target_arch.as_str() { |
| 139 | + // Unsupported (libcall is not supported) <https://github.com/llvm/llvm-project/issues/121122> |
| 140 | + "amdgpu" => false, |
| 141 | + // Unsupported <https://github.com/llvm/llvm-project/issues/94434> |
| 142 | + "arm64ec" => false, |
| 143 | + // Selection failure <https://github.com/llvm/llvm-project/issues/96432> |
| 144 | + "mips64" | "mips64r6" => false, |
| 145 | + // Selection failure <https://github.com/llvm/llvm-project/issues/95471> |
| 146 | + "nvptx64" => false, |
| 147 | + // Selection failure <https://github.com/llvm/llvm-project/issues/101545> |
| 148 | + "powerpc64" if &cfg.target_os == "aix" => false, |
| 149 | + // Selection failure <https://github.com/llvm/llvm-project/issues/41838> |
| 150 | + "sparc" => false, |
| 151 | + // Most everything else works as of LLVM 19 |
| 152 | + _ => true, |
| 153 | + }; |
| 154 | + |
| 155 | + // If the feature is set, disable these types. |
| 156 | + let disable_both = env::var_os("CARGO_FEATURE_NO_F16_F128").is_some(); |
| 157 | + |
| 158 | + println!("cargo:rustc-check-cfg=cfg(f16_enabled)"); |
| 159 | + println!("cargo:rustc-check-cfg=cfg(f128_enabled)"); |
| 160 | + |
| 161 | + if f16_enabled && !disable_both { |
| 162 | + println!("cargo:rustc-cfg=f16_enabled"); |
| 163 | + } |
| 164 | + |
| 165 | + if f128_enabled && !disable_both { |
| 166 | + println!("cargo:rustc-cfg=f128_enabled"); |
| 167 | + } |
| 168 | +} |
0 commit comments