From 3b6bc91b8d16048cb21d2921e090830902bcb202 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Tue, 23 Jul 2024 12:26:38 +0200 Subject: [PATCH] Try some things out --- rubicon/Cargo.lock | 62 +++++++++++++++++++++++++++++ rubicon/Cargo.toml | 9 ++++- rubicon/build.rs | 14 +++++++ rubicon/src/lib.rs | 76 ++++++++++++++++++++++++++++++++++++ test-crates/bin/Cargo.lock | 24 ++++++++++-- test-crates/mod_a/Cargo.lock | 62 +++++++++++++++++++++++++++++ test-crates/mod_b/Cargo.lock | 62 +++++++++++++++++++++++++++++ test-crates/mokio/Cargo.lock | 64 +++++++++++++++++++++++++++++- test-crates/mokio/Cargo.toml | 2 + test-crates/mokio/src/lib.rs | 27 ++++++++++++- 10 files changed, 393 insertions(+), 9 deletions(-) create mode 100644 rubicon/build.rs diff --git a/rubicon/Cargo.lock b/rubicon/Cargo.lock index 40127dd..fbdfc31 100644 --- a/rubicon/Cargo.lock +++ b/rubicon/Cargo.lock @@ -2,15 +2,77 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ctor" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "paste" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rubicon" version = "3.0.1" dependencies = [ + "ctor", "paste", + "rustc_version", ] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/rubicon/Cargo.toml b/rubicon/Cargo.toml index 69573ad..18e0518 100644 --- a/rubicon/Cargo.toml +++ b/rubicon/Cargo.toml @@ -14,9 +14,14 @@ keywords = ["ffi", "thread-local"] crate-type = ["dylib"] [dependencies] +ctor = { version = "0.2.8", optional = true } paste = { version = "1.0.15", optional = true } +[build-dependencies] +rustc_version = { version = "0.4.0", optional = true } + [features] default = [] -export-globals = ["dep:paste"] -import-globals = ["dep:paste"] +export-globals = ["dep:paste", "dep:rustc_version"] +import-globals = ["dep:paste", "dep:rustc_version", "dep:ctor"] +ctor = ["dep:ctor"] diff --git a/rubicon/build.rs b/rubicon/build.rs new file mode 100644 index 0000000..0b6b312 --- /dev/null +++ b/rubicon/build.rs @@ -0,0 +1,14 @@ +fn main() { + #[cfg(any(feature = "export-globals", feature = "import-globals"))] + { + use std::env; + + // Get the Rust compiler version and set it as an environment variable. + let rustc_version = rustc_version::version().unwrap(); + println!("cargo:rustc-env=RUBICON_RUSTC_VERSION={}", rustc_version); + + // Pass the target triple. + let target = env::var("TARGET").unwrap(); + println!("cargo:rustc-env=RUBICON_TARGET_TRIPLE={}", target); + } +} diff --git a/rubicon/src/lib.rs b/rubicon/src/lib.rs index 1bd3652..8687921 100644 --- a/rubicon/src/lib.rs +++ b/rubicon/src/lib.rs @@ -36,6 +36,15 @@ compile_error!("The features `export-globals` and `import-globals` are mutually #[cfg(any(feature = "export-globals", feature = "import-globals"))] pub use paste::paste; +#[cfg(feature = "import-globals")] +pub use ctor; + +#[cfg(any(feature = "export-globals", feature = "import-globals"))] +pub const RUBICON_RUSTC_VERSION: &str = env!("RUBICON_RUSTC_VERSION"); + +#[cfg(any(feature = "export-globals", feature = "import-globals"))] +pub const RUBICON_TARGET_TRIPLE: &str = env!("RUBICON_TARGET_TRIPLE"); + //============================================================================== // Wrappers //============================================================================== @@ -288,3 +297,70 @@ macro_rules! process_local_inner_mut { } }; } + +//============================================================================== +// Compatibility check +//============================================================================== + +#[cfg(feature = "export-globals")] +#[macro_export] +macro_rules! compatibility_check { + ($($feature:tt)*) => { + use std::env; + + $crate::paste! { + #[no_mangle] + #[export_name = concat!(env!("CARGO_PKG_NAME"), "_compatibility_info")] + static __RUBICON_COMPATIBILITY_INFO_: &'static [(&'static str, &'static str)] = &[ + ("rustc-version", $crate::RUBICON_RUSTC_VERSION), + ("target-triple", $crate::RUBICON_TARGET_TRIPLE), + $($feature)* + ]; + } + }; +} + +#[cfg(feature = "import-globals")] +#[macro_export] +macro_rules! compatibility_check { + ($($feature:tt)*) => { + use std::env; + use $crate::ctor::ctor; + + extern "C" { + #[link_name = concat!(env!("CARGO_PKG_NAME"), "_compatibility_info")] + static COMPATIBILITY_INFO: &'static [(&'static str, &'static str)]; + } + + #[ctor] + fn check_compatibility() { + let ref_compatibility_info: &[(&str, &str)] = &[ + ("rustc-version", $crate::RUBICON_RUSTC_VERSION), + ("target-triple", $crate::RUBICON_TARGET_TRIPLE), + $($feature)* + ]; + + let exported = unsafe { COMPATIBILITY_INFO }; + + let missing: Vec<_> = ref_compatibility_info.iter().filter(|&item| !exported.contains(item)).collect(); + let extra: Vec<_> = exported.iter().filter(|&item| !ref_compatibility_info.contains(item)).collect(); + + if !missing.is_empty() || !extra.is_empty() { + eprintln!("Compatibility mismatch detected!"); + if !missing.is_empty() { + eprintln!("Missing features: {:?}", missing); + } + if !extra.is_empty() { + eprintln!("Extra features: {:?}", extra); + } + std::process::exit(1); + } + } + }; +} + +#[cfg(not(any(feature = "export-globals", feature = "import-globals")))] +#[macro_export] +macro_rules! compatibility_check { + ($($feature:tt)*) => {}; +} diff --git a/test-crates/bin/Cargo.lock b/test-crates/bin/Cargo.lock index 4447dcb..0588abd 100644 --- a/test-crates/bin/Cargo.lock +++ b/test-crates/bin/Cargo.lock @@ -28,9 +28,9 @@ dependencies = [ [[package]] name = "libloading" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" +checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" dependencies = [ "cfg-if", "windows-targets", @@ -54,13 +54,29 @@ name = "rubicon" version = "3.0.1" dependencies = [ "paste", + "rustc_version", ] +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + [[package]] name = "soprintln" -version = "3.2.0" +version = "3.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc96941d6cac2e2654f62398ae8319ff55a730d6ee2edc78d112f37e5507613" +checksum = "7561c6c12cc21c549193bb82f86800ee0d5d69e85a4393ee1a2766917c2a35cb" [[package]] name = "windows-targets" diff --git a/test-crates/mod_a/Cargo.lock b/test-crates/mod_a/Cargo.lock index 80a3aab..90427c2 100644 --- a/test-crates/mod_a/Cargo.lock +++ b/test-crates/mod_a/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ctor" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "mod_a" version = "0.1.0" @@ -24,15 +34,67 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rubicon" version = "3.0.1" dependencies = [ + "ctor", "paste", + "rustc_version", ] +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + [[package]] name = "soprintln" version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cc96941d6cac2e2654f62398ae8319ff55a730d6ee2edc78d112f37e5507613" + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/test-crates/mod_b/Cargo.lock b/test-crates/mod_b/Cargo.lock index 0ac7beb..c434309 100644 --- a/test-crates/mod_b/Cargo.lock +++ b/test-crates/mod_b/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ctor" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "mod_b" version = "0.1.0" @@ -24,15 +34,67 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rubicon" version = "3.0.1" dependencies = [ + "ctor", "paste", + "rustc_version", ] +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + [[package]] name = "soprintln" version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cc96941d6cac2e2654f62398ae8319ff55a730d6ee2edc78d112f37e5507613" + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/test-crates/mokio/Cargo.lock b/test-crates/mokio/Cargo.lock index 299478c..2b05b96 100644 --- a/test-crates/mokio/Cargo.lock +++ b/test-crates/mokio/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ctor" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "mokio" version = "0.1.0" @@ -15,9 +25,61 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rubicon" -version = "2.0.0" +version = "3.0.1" dependencies = [ + "ctor", "paste", + "rustc_version", ] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/test-crates/mokio/Cargo.toml b/test-crates/mokio/Cargo.toml index 88cad95..4b77f5d 100644 --- a/test-crates/mokio/Cargo.toml +++ b/test-crates/mokio/Cargo.toml @@ -7,5 +7,7 @@ edition = "2021" rubicon = { path = "../../rubicon" } [features] +default = [] +timer = [] import-globals = ["rubicon/import-globals"] export-globals = ["rubicon/export-globals"] diff --git a/test-crates/mokio/src/lib.rs b/test-crates/mokio/src/lib.rs index 349c9ab..d852406 100644 --- a/test-crates/mokio/src/lib.rs +++ b/test-crates/mokio/src/lib.rs @@ -1,4 +1,27 @@ -use std::sync::atomic::AtomicU64; +use std::sync::{atomic::AtomicU64, Arc, Mutex}; + +rubicon::compatibility_check! { + #[cfg(feature = "timer")] + ("timer", "enabled"), +} + +#[derive(Default)] +#[cfg(feature = "timer")] +struct TimerInternals { + #[allow(dead_code)] + random_stuff: [u64; 4], +} + +#[derive(Default)] +pub struct Runtime { + #[cfg(feature = "timer")] + #[allow(dead_code)] + timer: TimerInternals, + + // this field is second on purpose so that it'll be offset + // if the feature is enabled/disabled + pub counter: u64, +} rubicon::process_local! { pub static MOKIO_PL1: AtomicU64 = AtomicU64::new(0); @@ -10,7 +33,7 @@ rubicon::process_local! { rubicon::thread_local! { pub static MOKIO_TL1: AtomicU64 = AtomicU64::new(0); - pub static MOKIO_TL2: AtomicU64 = AtomicU64::new(0); + pub static MOKIO_TL2: Arc> = Arc::new(Mutex::new(Runtime::default())); } pub fn inc_dangerous() -> u64 {