Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add used_linker feature #281

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
);
}
}