Skip to content

Commit

Permalink
Add used_linker feature (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed May 30, 2023
1 parent fdaf51e commit 5be16f5
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ctor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions ctor/src/example.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
extern crate ctor;
extern crate libc_print;

Expand Down
8 changes: 6 additions & 2 deletions ctor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ authors = ["Matt Mastracci <matthew@mastracci.com>"]
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"
Expand Down
1 change: 1 addition & 0 deletions tests/src/dylib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
#![allow(dead_code, unused_imports)]

use ctor::*;
Expand Down
1 change: 1 addition & 0 deletions tests/src/dylib_load.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
#![allow(unused_imports)]

use ctor::*;
Expand Down
7 changes: 6 additions & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
// Prevent a spurious 'unused_imports' warning
#[allow(unused_imports)]
#[macro_use]
Expand Down Expand Up @@ -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")
);
}
}

0 comments on commit 5be16f5

Please sign in to comment.