Skip to content

Commit efa24d1

Browse files
committed
Deduce whehter to export nif_init
1 parent a9e37f7 commit efa24d1

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

rustler/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ nif_version_2_15 = ["nif_version_2_14"]
1919
nif_version_2_16 = ["nif_version_2_15"]
2020
nif_version_2_17 = ["nif_version_2_16"]
2121
serde = ["dep:serde"]
22-
staticlib = ["rustler_codegen/staticlib"]
2322

2423
[dependencies]
2524
inventory = "0.3"

rustler_codegen/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ edition = "2021"
1212
name = "rustler_codegen"
1313
proc-macro = true
1414

15-
[features]
16-
staticlib = []
17-
1815
[dependencies]
1916
syn = { version = "2.0", features = ["full", "extra-traits"] }
2017
quote = "1.0"

rustler_codegen/src/init.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,34 @@ impl From<InitMacroInput> for proc_macro2::TokenStream {
136136
}
137137
};
138138

139-
let nif_init_name = if cfg!(feature = "staticlib") {
139+
let nif_init_name = {
140140
let lib_name = std::env::var("CARGO_CRATE_NAME").unwrap();
141141
format!("{lib_name}_nif_init")
142-
} else {
143-
"nif_init".to_string()
144142
};
145143

146144
let nif_init_name = Ident::new(&nif_init_name, Span::call_site());
147145

146+
let should_generate_primary_nif = std::env::var("RUSTLER_PRIMARY_NIF_INIT").is_ok()
147+
|| std::env::var("CARGO_PRIMARY_PACKAGE").is_ok();
148+
149+
let maybe_primary_nif_init = if should_generate_primary_nif {
150+
quote! {
151+
#[cfg(not(windows))]
152+
#[no_mangle]
153+
fn nif_init() -> *const ::rustler::codegen_runtime::DEF_NIF_ENTRY {
154+
#nif_init_name()
155+
}
156+
157+
#[cfg(windows)]
158+
#[no_mangle]
159+
fn nif_init(callbacks: *mut ::rustler::codegen_runtime::DynNifCallbacks) -> *const ::rustler::codegen_runtime::DEF_NIF_ENTRY {
160+
#nif_init_name(callbacks)
161+
}
162+
}
163+
} else {
164+
quote!()
165+
};
166+
148167
quote! {
149168
#maybe_warning
150169

@@ -167,6 +186,8 @@ impl From<InitMacroInput> for proc_macro2::TokenStream {
167186

168187
#inner
169188
}
189+
190+
#maybe_primary_nif_init
170191
}
171192
}
172193
}

0 commit comments

Comments
 (0)