-
-
Notifications
You must be signed in to change notification settings - Fork 420
Create a binding generator trait #2872
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,22 +1,190 @@ | ||||||||||||||||||||||||||||||||||||||||||
| use std::collections::HashMap; | ||||||||||||||||||||||||||||||||||||||||||
| use std::io::Write as _; | ||||||||||||||||||||||||||||||||||||||||||
| use std::path::Path; | ||||||||||||||||||||||||||||||||||||||||||
| use std::path::PathBuf; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| use anyhow::Context as _; | ||||||||||||||||||||||||||||||||||||||||||
| use anyhow::Result; | ||||||||||||||||||||||||||||||||||||||||||
| use fs_err as fs; | ||||||||||||||||||||||||||||||||||||||||||
| use fs_err::File; | ||||||||||||||||||||||||||||||||||||||||||
| use tempfile::TempDir; | ||||||||||||||||||||||||||||||||||||||||||
| use tempfile::tempdir; | ||||||||||||||||||||||||||||||||||||||||||
| use tracing::debug; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| use crate::BuildArtifact; | ||||||||||||||||||||||||||||||||||||||||||
| use crate::BuildContext; | ||||||||||||||||||||||||||||||||||||||||||
| use crate::Metadata24; | ||||||||||||||||||||||||||||||||||||||||||
| use crate::ModuleWriter; | ||||||||||||||||||||||||||||||||||||||||||
| use crate::PythonInterpreter; | ||||||||||||||||||||||||||||||||||||||||||
| use crate::module_writer::ModuleWriterExt; | ||||||||||||||||||||||||||||||||||||||||||
| use crate::module_writer::write_python_part; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| mod cffi_binding; | ||||||||||||||||||||||||||||||||||||||||||
| mod pyo3_binding; | ||||||||||||||||||||||||||||||||||||||||||
| mod uniffi_binding; | ||||||||||||||||||||||||||||||||||||||||||
| mod wasm_binding; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| pub use cffi_binding::write_cffi_module; | ||||||||||||||||||||||||||||||||||||||||||
| pub use pyo3_binding::write_bindings_module; | ||||||||||||||||||||||||||||||||||||||||||
| pub use pyo3_binding::Pyo3BindingGenerator; | ||||||||||||||||||||||||||||||||||||||||||
| pub use uniffi_binding::write_uniffi_module; | ||||||||||||||||||||||||||||||||||||||||||
| pub use wasm_binding::write_wasm_launcher; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ///A trait to generate the binding files to be included in the built module | ||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||
| /// This trait is used to generate the support files necessary to build a python | ||||||||||||||||||||||||||||||||||||||||||
| /// module for any [crate::BridgeModel] | ||||||||||||||||||||||||||||||||||||||||||
| pub(crate) trait BindingGenerator { | ||||||||||||||||||||||||||||||||||||||||||
| fn generate_bindings( | ||||||||||||||||||||||||||||||||||||||||||
| &self, | ||||||||||||||||||||||||||||||||||||||||||
| context: &BuildContext, | ||||||||||||||||||||||||||||||||||||||||||
| interpreter: Option<&PythonInterpreter>, | ||||||||||||||||||||||||||||||||||||||||||
| artifact: &BuildArtifact, | ||||||||||||||||||||||||||||||||||||||||||
| module: &Path, | ||||||||||||||||||||||||||||||||||||||||||
| temp_dir: &TempDir, | ||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<GeneratorOutput>; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug)] | ||||||||||||||||||||||||||||||||||||||||||
| pub(crate) struct GeneratorOutput { | ||||||||||||||||||||||||||||||||||||||||||
| /// The path, relative to the archive root, where the built artifact/module | ||||||||||||||||||||||||||||||||||||||||||
| /// should be installed | ||||||||||||||||||||||||||||||||||||||||||
| artifact_target: PathBuf, | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// In some cases, the source path of the artifact is altered | ||||||||||||||||||||||||||||||||||||||||||
| /// (e.g. when the build output is an archive which needs to be unpacked) | ||||||||||||||||||||||||||||||||||||||||||
| artifact_source_override: Option<PathBuf>, | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Additional files to be installed (e.g. __init__.py) | ||||||||||||||||||||||||||||||||||||||||||
| /// The provided PathBuf should be relative to the archive root | ||||||||||||||||||||||||||||||||||||||||||
| additional_files: Option<HashMap<PathBuf, Vec<u8>>>, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /// Every binding generator ultimately has to install the following: | ||||||||||||||||||||||||||||||||||||||||||
| /// 1. The python files (if any) | ||||||||||||||||||||||||||||||||||||||||||
| /// 2. The artifact | ||||||||||||||||||||||||||||||||||||||||||
| /// 3. Additional files | ||||||||||||||||||||||||||||||||||||||||||
| /// 4. Type stubs (if any/pure rust only) | ||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||
| /// Additionally, the above are installed to 2 potential locations: | ||||||||||||||||||||||||||||||||||||||||||
| /// 1. The archive | ||||||||||||||||||||||||||||||||||||||||||
| /// 2. The filesystem | ||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||
| /// For editable installs: | ||||||||||||||||||||||||||||||||||||||||||
| /// If the project is pure rust, the wheel is built as normal and installed | ||||||||||||||||||||||||||||||||||||||||||
| /// If the project has python, the artifact is installed into the project and a pth is written to the archive | ||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||
| /// So the full matrix comes down to: | ||||||||||||||||||||||||||||||||||||||||||
| /// 1. editable, has python => install to fs, write pth to archive | ||||||||||||||||||||||||||||||||||||||||||
| /// 2. everything else => install to archive/build as normal | ||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||
| /// Note: Writing the pth to the archive is handled by [BuildContext], not here | ||||||||||||||||||||||||||||||||||||||||||
| pub fn generate_binding( | ||||||||||||||||||||||||||||||||||||||||||
| writer: &mut impl ModuleWriter, | ||||||||||||||||||||||||||||||||||||||||||
| generator: &impl BindingGenerator, | ||||||||||||||||||||||||||||||||||||||||||
| context: &BuildContext, | ||||||||||||||||||||||||||||||||||||||||||
| interpreter: Option<&PythonInterpreter>, | ||||||||||||||||||||||||||||||||||||||||||
| artifact: &BuildArtifact, | ||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||
| // 1. Install the python files | ||||||||||||||||||||||||||||||||||||||||||
| if !context.editable { | ||||||||||||||||||||||||||||||||||||||||||
| write_python_part( | ||||||||||||||||||||||||||||||||||||||||||
| writer, | ||||||||||||||||||||||||||||||||||||||||||
| &context.project_layout, | ||||||||||||||||||||||||||||||||||||||||||
| context.pyproject_toml.as_ref(), | ||||||||||||||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let base_path = context | ||||||||||||||||||||||||||||||||||||||||||
| .project_layout | ||||||||||||||||||||||||||||||||||||||||||
| .python_module | ||||||||||||||||||||||||||||||||||||||||||
| .as_ref() | ||||||||||||||||||||||||||||||||||||||||||
| .map(|python_module| python_module.parent().unwrap().to_path_buf()); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let module = match &base_path { | ||||||||||||||||||||||||||||||||||||||||||
| Some(base_path) => context | ||||||||||||||||||||||||||||||||||||||||||
| .project_layout | ||||||||||||||||||||||||||||||||||||||||||
| .rust_module | ||||||||||||||||||||||||||||||||||||||||||
| .strip_prefix(base_path) | ||||||||||||||||||||||||||||||||||||||||||
| .unwrap() | ||||||||||||||||||||||||||||||||||||||||||
| .to_path_buf(), | ||||||||||||||||||||||||||||||||||||||||||
| None => PathBuf::from(&context.project_layout.extension_name), | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let temp_dir = tempdir()?; | ||||||||||||||||||||||||||||||||||||||||||
| let GeneratorOutput { | ||||||||||||||||||||||||||||||||||||||||||
| artifact_target, | ||||||||||||||||||||||||||||||||||||||||||
| artifact_source_override, | ||||||||||||||||||||||||||||||||||||||||||
| additional_files, | ||||||||||||||||||||||||||||||||||||||||||
| } = generator.generate_bindings(context, interpreter, artifact, &module, &temp_dir)?; | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+113
to
+118
|
||||||||||||||||||||||||||||||||||||||||||
| let temp_dir = tempdir()?; | |
| let GeneratorOutput { | |
| artifact_target, | |
| artifact_source_override, | |
| additional_files, | |
| } = generator.generate_bindings(context, interpreter, artifact, &module, &temp_dir)?; | |
| // Only create a temporary directory for AIX builds, as required. | |
| #[allow(unused_mut)] | |
| let mut temp_dir = None; | |
| let temp_dir_path = if cfg!(target_os = "aix") { | |
| temp_dir = Some(tempdir()?); | |
| temp_dir.as_ref().unwrap().path() | |
| } else { | |
| Path::new("") | |
| }; | |
| let GeneratorOutput { | |
| artifact_target, | |
| artifact_source_override, | |
| additional_files, | |
| } = generator.generate_bindings(context, interpreter, artifact, &module, temp_dir_path)?; |
e-nomem marked this conversation as resolved.
e-nomem marked this conversation as resolved.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.