Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created November 2, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcrichton/108f3c3bb70d8e5d250a63f0ca986d58 to your computer and use it in GitHub Desktop.
Save alexcrichton/108f3c3bb70d8e5d250a63f0ca986d58 to your computer and use it in GitHub Desktop.
diff --git a/src/rust/tor_allocate/build.rs b/src/rust/tor_allocate/build.rs
new file mode 100644
index 000000000..c97fd55ce
--- /dev/null
+++ b/src/rust/tor_allocate/build.rs
@@ -0,0 +1,34 @@
+use std::env;
+use std::process::Command;
+use std::str;
+
+fn main() {
+ if let Some(version) = rustc_minor_version() {
+ if version >= 28 {
+ println!("cargo:rustc-cfg=global_allocator");
+ }
+ }
+}
+
+fn rustc_minor_version() -> Option<u32> {
+ macro_rules! otry {
+ ($e:expr) => {
+ match $e {
+ Some(e) => e,
+ None => return None,
+ }
+ };
+ }
+
+ let rustc = otry!(env::var_os("RUSTC"));
+ let output = otry!(Command::new(rustc).arg("--version").output().ok());
+ let version = otry!(str::from_utf8(&output.stdout).ok());
+ let mut pieces = version.split('.');
+
+ if pieces.next() != Some("rustc 1") {
+ return None;
+ }
+
+ otry!(pieces.next()).parse().ok()
+}
+
diff --git a/src/rust/tor_allocate/lib.rs b/src/rust/tor_allocate/lib.rs
index 1cfa0b517..0f3f00dd3 100644
--- a/src/rust/tor_allocate/lib.rs
+++ b/src/rust/tor_allocate/lib.rs
@@ -11,10 +11,12 @@
extern crate libc;
+#[cfg(global_allocator)]
use std::alloc::System;
mod tor_allocate;
pub use tor_allocate::*;
#[global_allocator]
+#[cfg(global_allocator)]
static A: System = System;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment