Skip to content

Commit

Permalink
Revert "Remove build.rs file"
Browse files Browse the repository at this point in the history
This reverts commit 7f09d72.
  • Loading branch information
KodrAus committed May 28, 2023
1 parent 3ea1c66 commit 5e37bb7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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"]
Expand Down
46 changes: 46 additions & 0 deletions 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<String> {
env::var("TARGET").ok()
}
21 changes: 10 additions & 11 deletions src/lib.rs
Expand Up @@ -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<usize>,
}

#[cfg(not(target_has_atomic = "ptr"))]
#[cfg(not(has_atomics))]
impl AtomicUsize {
const fn new(v: usize) -> AtomicUsize {
AtomicUsize { v: Cell::new(v) }
Expand All @@ -371,7 +371,7 @@ impl AtomicUsize {
self.v.set(val)
}

#[cfg(target_has_atomic = "ptr")]
#[cfg(atomic_cas)]
fn compare_exchange(
&self,
current: usize,
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<dyn Log>) -> Result<(), SetLoggerError> {
set_logger_inner(|| Box::leak(logger))
}
Expand Down Expand Up @@ -1344,12 +1343,12 @@ pub fn set_boxed_logger(logger: Box<dyn Log>) -> 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<F>(make_logger: F) -> Result<(), SetLoggerError>
where
F: FnOnce() -> &'static dyn Log,
Expand Down

0 comments on commit 5e37bb7

Please sign in to comment.