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 the official recommended naming pattern for cffi module file #2406

Merged
merged 1 commit into from
Dec 23, 2024
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
1 change: 1 addition & 0 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ impl BuildContext {

write_cffi_module(
&mut writer,
&self.target,
&self.project_layout,
self.manifest_path.parent().unwrap(),
&self.target_dir,
Expand Down
32 changes: 16 additions & 16 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,14 @@ fn entry_points_txt(
}

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

import os
from .ffi import ffi

lib = ffi.dlopen(os.path.join(os.path.dirname(__file__), '{extension_name}.so'))
lib = ffi.dlopen(os.path.join(os.path.dirname(__file__), '{cffi_module_file_name}'))
del os
"#
)
Expand Down Expand Up @@ -882,6 +882,7 @@ if hasattr({ext_name}, "__all__"):
#[allow(clippy::too_many_arguments)]
pub fn write_cffi_module(
writer: &mut impl ModuleWriter,
target: &Target,
project_layout: &ProjectLayout,
crate_dir: &Path,
target_dir: &Path,
Expand All @@ -898,22 +899,28 @@ pub fn write_cffi_module(
.context("Failed to add the python module to the package")?;
}

let cffi_module_file_name = {
let extension_name = &project_layout.extension_name;
// https://cffi.readthedocs.io/en/stable/embedding.html#issues-about-using-the-so
match target.target_os() {
Os::Macos => format!("lib{extension_name}.dylib"),
Os::Windows => format!("{extension_name}.dll"),
_ => format!("lib{extension_name}.so"),
}
};
let module;
if let Some(python_module) = &project_layout.python_module {
if editable {
let base_path = python_module.join(&project_layout.extension_name);
fs::create_dir_all(&base_path)?;
let target = base_path.join(format!(
"{extension_name}.so",
extension_name = &project_layout.extension_name
));
let target = base_path.join(&cffi_module_file_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(&project_layout.extension_name).as_bytes())?;
.write_all(cffi_init_file(&cffi_module_file_name).as_bytes())?;
File::create(base_path.join("ffi.py"))?.write_all(cffi_declarations.as_bytes())?;
}

Expand Down Expand Up @@ -942,17 +949,10 @@ pub fn write_cffi_module(
writer.add_bytes(
module.join("__init__.py"),
None,
cffi_init_file(&project_layout.extension_name).as_bytes(),
cffi_init_file(&cffi_module_file_name).as_bytes(),
)?;
writer.add_bytes(module.join("ffi.py"), None, cffi_declarations.as_bytes())?;
writer.add_file_with_permissions(
module.join(format!(
"{extension_name}.so",
extension_name = &project_layout.extension_name
)),
artifact,
0o755,
)?;
writer.add_file_with_permissions(module.join(&cffi_module_file_name), artifact, 0o755)?;
}

Ok(())
Expand Down
Loading