| 1 |
//! Target detection |
| 2 |
//! |
| 3 |
//! This build script translates the target for which `radium` is being compiled |
| 4 |
//! into a set of directives that the crate can use to control which atomic |
| 5 |
//! symbols it attempts to name. |
| 6 |
//! |
| 7 |
//! The compiler maintains its store of target information here: |
| 8 |
//! <https://github.com/rust-lang/rust/tree/be28b6235e64e0f662b96b710bf3af9de169215c/compiler/rustc_target/src/spec> |
| 9 |
//! |
| 10 |
//! That module is not easily extracted into something that can be loaded here, |
| 11 |
//! so we are replicating it through string matching on the target name until |
| 12 |
//! we are able to uniquely identify targets through `cfg` checks. |
| 13 |
//! |
| 14 |
//! Use `rustc --print target-list` to enumerate the full list of targets |
| 15 |
//! available, and `rustc --print cfg` (optionally with `-Z unstable-options`) |
| 16 |
//! to see what `cfg` values are produced for a target. |
| 17 |
//! |
| 18 |
//! The missing `cfg` checks required for conditional compilation, rather than a |
| 19 |
//! build script, are: |
| 20 |
//! |
| 21 |
//! - [`accessible`](https://github.com/rust-lang/rust/issues/64797) |
| 22 |
//! - [`target_feature`](https://github.com/rust-lang/rust/issues/69098) |
| 23 |
//! - [`target_has_atomic`](https://github.com/rust-lang/rust/issues/32976) |
| 24 |
//! |
| 25 |
//! Once any of these becomes usable on the stable series, we can switch to a |
| 26 |
//! set of `cfg` checks instead of a build script. |
| 27 |
|
| 28 |
use std::{env, error::Error}; |
| 29 |
|
| 30 |
/// Collection of flags indicating whether the target processor supports atomic |
| 31 |
/// instructions for a certain width. |
| 32 |
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 33 |
struct Atomics { |
| 34 |
/// Target supports 8-bit atomics |
| 35 |
has_8: bool, |
| 36 |
/// Target supports 16-bit atomics |
| 37 |
has_16: bool, |
| 38 |
/// Target supports 32-bit atomics |
| 39 |
has_32: bool, |
| 40 |
/// Target supports 64-bit atomics |
| 41 |
has_64: bool, |
| 42 |
/// Target supports word-width atomics |
| 43 |
has_ptr: bool, |
| 44 |
} |
| 45 |
|
| 46 |
impl Atomics { |
| 47 |
const ALL: Self = Self { |
| 48 |
has_8: true, |
| 49 |
has_16: true, |
| 50 |
has_32: true, |
| 51 |
has_64: true, |
| 52 |
has_ptr: true, |
| 53 |
}; |
| 54 |
const NONE: Self = Self { |
| 55 |
has_8: false, |
| 56 |
has_16: false, |
| 57 |
has_32: false, |
| 58 |
has_64: false, |
| 59 |
has_ptr: false, |
| 60 |
}; |
| 61 |
} |
| 62 |
|
| 63 |
fn main() -> Result<(), Box<dyn Error>> { |
| 64 |
let mut atomics = Atomics::ALL; |
| 65 |
|
| 66 |
let target = env::var("TARGET")?; |
| 67 |
// Add new target strings here with their atomic availability. |
| 68 |
#[allow(clippy::match_single_binding, clippy::single_match)] |
| 69 |
match &*target { |
| 70 |
"arm-linux-androideabi" => atomics.has_64 = false, |
| 71 |
_ => {} |
| 72 |
} |
| 73 |
|
| 74 |
// If for some reason splitting fails, use the whole target string. |
| 75 |
let tgt_arch = target.split("-").next().unwrap_or(&target); |
| 76 |
// Additionally, the `cfg!(target_arch)` value may be of use for some |
| 77 |
// targets. Note that it does **not** carry distinguishing information in |
| 78 |
// all cases! `armv5te` and `armv7` targets are both |
| 79 |
// `cfg!(target_arch = "arm")`. |
| 80 |
let env_arch = env::var("CARGO_CFG_TARGET_ARCH")?; |
| 81 |
// Add new architecture sections here with their atomic availability. |
| 82 |
#[allow(clippy::match_single_binding, clippy::single_match)] |
| 83 |
match tgt_arch { |
| 84 |
"armv5te" | "mips" | "mipsel" | "powerpc" | "riscv32imac" | "thumbv7em" | "thumbv7m" |
| 85 |
| "thumbv8m.base" | "thumbv8m.main" | "armebv7r" | "armv7r" => atomics.has_64 = false, |
| 86 |
// These ARMv7 targets have 32-bit pointers and 64-bit atomics. |
| 87 |
"armv7" | "armv7a" | "armv7s" => atomics.has_64 = true, |
| 88 |
// "riscv32imc-unknown-none-elf" and "riscv32imac-unknown-none-elf" are |
| 89 |
// both `target_arch = "riscv32", and have no stable `cfg`-discoverable |
| 90 |
// distinction. As such, the non-atomic RISC-V targets must be |
| 91 |
// discovered here. |
| 92 |
"riscv32i" | "riscv32imc" | "thumbv6m" => atomics = Atomics::NONE, |
| 93 |
_ => {} |
| 94 |
} |
| 95 |
#[allow(clippy::match_single_binding, clippy::single_match)] |
| 96 |
match &*env_arch { |
| 97 |
"avr" => atomics = Atomics::NONE, |
| 98 |
_ => {} |
| 99 |
} |
| 100 |
|
| 101 |
if atomics.has_8 { |
| 102 |
println!("cargo:rustc-cfg=radium_atomic_8"); |
| 103 |
} |
| 104 |
if atomics.has_16 { |
| 105 |
println!("cargo:rustc-cfg=radium_atomic_16"); |
| 106 |
} |
| 107 |
if atomics.has_32 { |
| 108 |
println!("cargo:rustc-cfg=radium_atomic_32"); |
| 109 |
} |
| 110 |
if atomics.has_64 { |
| 111 |
println!("cargo:rustc-cfg=radium_atomic_64"); |
| 112 |
} |
| 113 |
if atomics.has_ptr { |
| 114 |
println!("cargo:rustc-cfg=radium_atomic_ptr"); |
| 115 |
} |
| 116 |
|
| 117 |
Ok(()) |
| 118 |
} |