Skip to content

Static NIFs #688

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions rustler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ rust-version = "1.70"
big_integer = ["dep:num-bigint"]
default = ["nif_version_2_15"]
derive = []
alternative_nif_init_name = []
allocator = []
nif_version_2_14 = []
nif_version_2_15 = ["nif_version_2_14"]
Expand All @@ -23,7 +22,7 @@ serde = ["dep:serde"]

[dependencies]
inventory = "0.3"
rustler_codegen = { path = "../rustler_codegen", version = "0.36.1"}
rustler_codegen = { path = "../rustler_codegen", version = "0.36.1" }
num-bigint = { version = "0.4", optional = true }
serde = { version = "1", optional = true }

Expand Down
43 changes: 38 additions & 5 deletions rustler_codegen/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,58 @@ impl From<InitMacroInput> for proc_macro2::TokenStream {
}
};

let nif_init_name = {
let lib_name = std::env::var("CARGO_CRATE_NAME").unwrap();
format!("{lib_name}_nif_init")
};

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

let should_generate_primary_nif = std::env::var("RUSTLER_PRIMARY_NIF_INIT").is_ok()
|| std::env::var("CARGO_PRIMARY_PACKAGE").is_ok();

let maybe_primary_nif_init = if should_generate_primary_nif {
quote! {
#[cfg(not(windows))]
#[no_mangle]
fn nif_init() -> *const ::rustler::codegen_runtime::DEF_NIF_ENTRY {
#nif_init_name()
}

#[cfg(windows)]
#[no_mangle]
fn nif_init(callbacks: *mut ::rustler::codegen_runtime::DynNifCallbacks) -> *const ::rustler::codegen_runtime::DEF_NIF_ENTRY {
#nif_init_name(callbacks)
}
}
} else {
quote!()
};

quote! {
#maybe_warning

#[cfg(unix)]
#[cfg(not(windows))]
#[no_mangle]
extern "C" fn nif_init() -> *const rustler::codegen_runtime::DEF_NIF_ENTRY {
unsafe { rustler::codegen_runtime::internal_write_symbols() };
extern "C" fn #nif_init_name() -> *const ::rustler::codegen_runtime::DEF_NIF_ENTRY {
unsafe {
::rustler::codegen_runtime::internal_write_symbols()
}

#inner
}

#[cfg(windows)]
#[no_mangle]
extern "C" fn nif_init(callbacks: *mut rustler::codegen_runtime::DynNifCallbacks) -> *const rustler::codegen_runtime::DEF_NIF_ENTRY {
extern "C" fn #nif_init_name(callbacks: *mut ::rustler::codegen_runtime::DynNifCallbacks) -> *const ::rustler::codegen_runtime::DEF_NIF_ENTRY {
unsafe {
rustler::codegen_runtime::internal_set_symbols(*callbacks);
::rustler::codegen_runtime::internal_set_symbols(*callbacks);
}

#inner
}

#maybe_primary_nif_init
}
}
}
Loading