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

Use extension name as library name, instead of hardcoded 'native.so' #1918

Merged
merged 1 commit into from
Jan 26, 2024
Merged
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
30 changes: 23 additions & 7 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,17 @@ fn entry_points_txt(
}

/// Glue code that exposes `lib`.
fn cffi_init_file() -> &'static str {
r#"__all__ = ["lib", "ffi"]
fn cffi_init_file(extension_name: &str) -> String {
format!(
r#"__all__ = ["lib", "ffi"]

import os
from .ffi import ffi

lib = ffi.dlopen(os.path.join(os.path.dirname(__file__), 'native.so'))
lib = ffi.dlopen(os.path.join(os.path.dirname(__file__), '{extension_name}.so'))
del os
"#
)
}

/// Wraps some boilerplate around error handling when calling python
Expand Down Expand Up @@ -804,13 +806,17 @@ pub fn write_cffi_module(
if editable {
let base_path = python_module.join(&project_layout.extension_name);
fs::create_dir_all(&base_path)?;
let target = base_path.join("native.so");
let target = base_path.join(format!(
"{extension_name}.so",
extension_name = &project_layout.extension_name
));
fs::copy(artifact, &target).context(format!(
"Failed to copy {} to {}",
artifact.display(),
target.display()
))?;
File::create(base_path.join("__init__.py"))?.write_all(cffi_init_file().as_bytes())?;
File::create(base_path.join("__init__.py"))?
.write_all(cffi_init_file(&project_layout.extension_name).as_bytes())?;
File::create(base_path.join("ffi.py"))?.write_all(cffi_declarations.as_bytes())?;
}

Expand All @@ -836,9 +842,19 @@ pub fn write_cffi_module(
};

if !editable || project_layout.python_module.is_none() {
writer.add_bytes(&module.join("__init__.py"), cffi_init_file().as_bytes())?;
writer.add_bytes(
&module.join("__init__.py"),
cffi_init_file(&project_layout.extension_name).as_bytes(),
)?;
writer.add_bytes(&module.join("ffi.py"), cffi_declarations.as_bytes())?;
writer.add_file_with_permissions(&module.join("native.so"), artifact, 0o755)?;
writer.add_file_with_permissions(
&module.join(format!(
"{extension_name}.so",
extension_name = &project_layout.extension_name
)),
artifact,
0o755,
)?;
}

Ok(())
Expand Down
Loading