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

Set executable bit on shared library #421

Merged
merged 1 commit into from
Feb 17, 2021
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 Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

* Escape version in wheel metadata by messense in [#420](https://github.com/PyO3/maturin/pull/420)
* Set executable bit on shared library by messense in [#421](https://github.com/PyO3/maturin/pull/421)

## 0.9.1 - 2021-01-13

Expand Down
32 changes: 25 additions & 7 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ pub trait ModuleWriter {
.context(format!("Failed to write to {}", target.as_ref().display()))?;
Ok(())
}

/// Copies the source file the the target path relative to the module base path while setting
/// the given unix permissions
fn add_file_with_permissions(
&mut self,
target: impl AsRef<Path>,
source: impl AsRef<Path>,
permissions: u32,
) -> Result<()> {
let read_failed_context = format!("Failed to read {}", source.as_ref().display());
let mut file = File::open(source.as_ref()).context(read_failed_context.clone())?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).context(read_failed_context)?;
self.add_bytes_with_permissions(target.as_ref(), &buffer, permissions)
.context(format!("Failed to write to {}", target.as_ref().display()))?;
Ok(())
}
}

/// A [ModuleWriter] that adds the module somewhere in the filesystem, e.g. in a virtualenv
Expand Down Expand Up @@ -588,10 +605,14 @@ pub fn write_bindings_module(
))?;
}

writer.add_file(Path::new(&module_name).join(&so_filename), &artifact)?;
writer.add_file_with_permissions(
Path::new(&module_name).join(&so_filename),
&artifact,
0o755,
)?;
}
ProjectLayout::PureRust => {
writer.add_file(so_filename, &artifact)?;
writer.add_file_with_permissions(so_filename, &artifact, 0o755)?;
}
}

Expand Down Expand Up @@ -639,7 +660,7 @@ pub fn write_cffi_module(
writer.add_directory(&module)?;
writer.add_bytes(&module.join("__init__.py"), cffi_init_file().as_bytes())?;
writer.add_bytes(&module.join("ffi.py"), cffi_declarations.as_bytes())?;
writer.add_file(&module.join("native.so"), &artifact)?;
writer.add_file_with_permissions(&module.join("native.so"), &artifact, 0o755)?;

Ok(())
}
Expand All @@ -661,10 +682,7 @@ pub fn write_bin(
writer.add_directory(&data_dir)?;

// We can't use add_file since we need to mark the file as executable
let mut file = File::open(artifact)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
writer.add_bytes_with_permissions(&data_dir.join(bin_name), &buffer, 0o755)?;
writer.add_file_with_permissions(&data_dir.join(bin_name), artifact, 0o755)?;
Ok(())
}

Expand Down