diff --git a/ctor/Cargo.toml b/ctor/Cargo.toml index 75b934d..0d44428 100644 --- a/ctor/Cargo.toml +++ b/ctor/Cargo.toml @@ -11,6 +11,10 @@ readme = "../README.md" [badges] travis-ci = { repository = "mmastrac/rust-ctor", branch = "master" } +[features] +# For nightly users, used(linker) may be a better choice +used_linker = [] + [dependencies] quote = "1.0.20" diff --git a/ctor/src/example.rs b/ctor/src/example.rs index 73babb1..ea6fb15 100644 --- a/ctor/src/example.rs +++ b/ctor/src/example.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "used_linker", feature(used_with_arg))] extern crate ctor; extern crate libc_print; diff --git a/ctor/src/lib.rs b/ctor/src/lib.rs index b5be692..83d3827 100644 --- a/ctor/src/lib.rs +++ b/ctor/src/lib.rs @@ -32,7 +32,7 @@ extern crate syn; #[macro_use] extern crate quote; -use proc_macro::{TokenStream}; +use proc_macro::TokenStream; /// Attributes required to mark a function as a constructor. This may be exposed in the future if we determine /// it to be stable. @@ -65,6 +65,7 @@ macro_rules! ctor_attributes { /// Print a startup message (using `libc_print` for safety): /// /// ```rust +/// # #![cfg_attr(feature="used_linker", feature(used_with_arg))] /// # extern crate ctor; /// # use ctor::*; /// use libc_print::std_name::println; @@ -82,6 +83,7 @@ macro_rules! ctor_attributes { /// Make changes to `static` variables: /// /// ```rust +/// # #![cfg_attr(feature="used_linker", feature(used_with_arg))] /// # extern crate ctor; /// # use ctor::*; /// # use std::sync::atomic::{AtomicBool, Ordering}; @@ -176,7 +178,9 @@ pub fn ctor(_attribute: TokenStream, function: TokenStream) -> TokenStream { #(#attrs)* #vis #unsafety extern #abi #constness fn #ident() #block - #[used] + #[cfg_attr(not(feature = "used_linker"), used)] + #[cfg_attr(feature = "used_linker", used(linker))] + #[no_mangle] #[allow(non_upper_case_globals)] #[doc(hidden)] #tokens diff --git a/tests/Cargo.toml b/tests/Cargo.toml index e69cd1f..8fa8de9 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -5,6 +5,10 @@ authors = ["Matt Mastracci "] edition = "2018" publish = false +[features] +# For nightly users, used(linker) may be a better choice +used_linker = ["ctor/used_linker"] + [dependencies] ctor = { path = "../ctor" } dlopen = "0.1.8" diff --git a/tests/src/dylib.rs b/tests/src/dylib.rs index e5c4751..032c587 100644 --- a/tests/src/dylib.rs +++ b/tests/src/dylib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "used_linker", feature(used_with_arg))] #![allow(dead_code, unused_imports)] use ctor::*; diff --git a/tests/src/dylib_load.rs b/tests/src/dylib_load.rs index ef58cc6..d44806a 100644 --- a/tests/src/dylib_load.rs +++ b/tests/src/dylib_load.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "used_linker", feature(used_with_arg))] #![allow(unused_imports)] use ctor::*; diff --git a/tests/src/lib.rs b/tests/src/lib.rs index a281230..e70b126 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "used_linker", feature(used_with_arg))] // Prevent a spurious 'unused_imports' warning #[allow(unused_imports)] #[macro_use] @@ -88,6 +89,10 @@ mod test { // There are four possible outcomes for stderr, depending on the order // that functions are called - assert!(a == s || b == s || c == s || d == s, "s was unexpected:\n{}", s.replace("\n", "\\n")); + assert!( + a == s || b == s || c == s || d == s, + "s was unexpected:\n{}", + s.replace("\n", "\\n") + ); } }