From 5e37bb7cf9ed25f14c878d6973a0d519a37b1c64 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Sun, 28 May 2023 13:42:24 +1000 Subject: [PATCH] Revert "Remove build.rs file" This reverts commit 7f09d72e5fd5387c672b9803ead6548b45920f55. --- .github/workflows/main.yml | 4 ++-- Cargo.toml | 2 +- build.rs | 46 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 21 +++++++++-------- 4 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 build.rs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fb038d280..783b5052d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -127,8 +127,8 @@ jobs: - uses: actions/checkout@master - name: Install Rust run: | - rustup update 1.60.0 --no-self-update - rustup default 1.60.0 + rustup update 1.31.0 --no-self-update + rustup default 1.31.0 - run: cargo build --verbose - run: cargo build --verbose --features serde - run: cargo build --verbose --features std diff --git a/Cargo.toml b/Cargo.toml index 2f4e3e36f..c9fddf999 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ A lightweight logging facade for Rust categories = ["development-tools::debugging"] keywords = ["logging"] exclude = ["rfcs/**/*"] -rust-version = "1.60.0" +build = "build.rs" [package.metadata.docs.rs] features = ["std", "serde", "kv_unstable_std", "kv_unstable_sval", "kv_unstable_serde"] diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..11c26a333 --- /dev/null +++ b/build.rs @@ -0,0 +1,46 @@ +//! This build script detects target platforms that lack proper support for +//! atomics and sets `cfg` flags accordingly. + +use std::env; +use std::str; + +fn main() { + let target = match rustc_target() { + Some(target) => target, + None => return, + }; + + if target_has_atomic_cas(&target) { + println!("cargo:rustc-cfg=atomic_cas"); + } + + if target_has_atomics(&target) { + println!("cargo:rustc-cfg=has_atomics"); + } + + println!("cargo:rerun-if-changed=build.rs"); +} + +fn target_has_atomic_cas(target: &str) -> bool { + match target { + "thumbv6m-none-eabi" + | "msp430-none-elf" + | "riscv32i-unknown-none-elf" + | "riscv32imc-unknown-none-elf" => false, + _ => true, + } +} + +fn target_has_atomics(target: &str) -> bool { + match target { + "thumbv4t-none-eabi" + | "msp430-none-elf" + | "riscv32i-unknown-none-elf" + | "riscv32imc-unknown-none-elf" => false, + _ => true, + } +} + +fn rustc_target() -> Option { + env::var("TARGET").ok() +} diff --git a/src/lib.rs b/src/lib.rs index c809db36c..82c648a4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -344,20 +344,20 @@ mod serde; #[cfg(feature = "kv_unstable")] pub mod kv; -#[cfg(target_has_atomic = "ptr")] +#[cfg(has_atomics)] use std::sync::atomic::{AtomicUsize, Ordering}; -#[cfg(not(target_has_atomic = "ptr"))] +#[cfg(not(has_atomics))] use std::cell::Cell; -#[cfg(not(target_has_atomic = "ptr"))] +#[cfg(not(has_atomics))] use std::sync::atomic::Ordering; -#[cfg(not(target_has_atomic = "ptr"))] +#[cfg(not(has_atomics))] struct AtomicUsize { v: Cell, } -#[cfg(not(target_has_atomic = "ptr"))] +#[cfg(not(has_atomics))] impl AtomicUsize { const fn new(v: usize) -> AtomicUsize { AtomicUsize { v: Cell::new(v) } @@ -371,7 +371,7 @@ impl AtomicUsize { self.v.set(val) } - #[cfg(target_has_atomic = "ptr")] + #[cfg(atomic_cas)] fn compare_exchange( &self, current: usize, @@ -389,7 +389,7 @@ impl AtomicUsize { // Any platform without atomics is unlikely to have multiple cores, so // writing via Cell will not be a race condition. -#[cfg(not(target_has_atomic = "ptr"))] +#[cfg(not(has_atomics))] unsafe impl Sync for AtomicUsize {} // The LOGGER static holds a pointer to the global logger. It is protected by @@ -1217,7 +1217,6 @@ where /// /// Note that `Trace` is the maximum level, because it provides the maximum amount of detail in the emitted logs. #[inline] -#[cfg(target_has_atomic = "ptr")] pub fn set_max_level(level: LevelFilter) { MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed); } @@ -1286,7 +1285,7 @@ pub fn max_level() -> LevelFilter { /// An error is returned if a logger has already been set. /// /// [`set_logger`]: fn.set_logger.html -#[cfg(all(feature = "std", target_has_atomic = "ptr"))] +#[cfg(all(feature = "std", atomic_cas))] pub fn set_boxed_logger(logger: Box) -> Result<(), SetLoggerError> { set_logger_inner(|| Box::leak(logger)) } @@ -1344,12 +1343,12 @@ pub fn set_boxed_logger(logger: Box) -> Result<(), SetLoggerError> { /// ``` /// /// [`set_logger_racy`]: fn.set_logger_racy.html -#[cfg(target_has_atomic = "ptr")] +#[cfg(atomic_cas)] pub fn set_logger(logger: &'static dyn Log) -> Result<(), SetLoggerError> { set_logger_inner(|| logger) } -#[cfg(target_has_atomic = "ptr")] +#[cfg(atomic_cas)] fn set_logger_inner(make_logger: F) -> Result<(), SetLoggerError> where F: FnOnce() -> &'static dyn Log,