Skip to content

Commit

Permalink
Try some things out
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 23, 2024
1 parent 1ff5673 commit 3b6bc91
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 9 deletions.
62 changes: 62 additions & 0 deletions rubicon/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions rubicon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
14 changes: 14 additions & 0 deletions rubicon/build.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
76 changes: 76 additions & 0 deletions rubicon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//==============================================================================
Expand Down Expand Up @@ -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)*) => {};
}
24 changes: 20 additions & 4 deletions test-crates/bin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions test-crates/mod_a/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3b6bc91

Please sign in to comment.