Skip to content

Commit

Permalink
Add libary search paths from cargo build scripts to auditwheel
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 9, 2022
1 parent bdfe7e4 commit 82e15c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix abi3 wheel build when no Python interpreters found in [#1072](https://github.com/PyO3/maturin/pull/1072)
* Add `zig ar` support in [#1073](https://github.com/PyO3/maturin/pull/1073)
* Fix sdist build for optional path dependencies in [#1084](https://github.com/PyO3/maturin/pull/1084)
* auditwheel: find dylibs in Cargo target directory in [#1092](https://github.com/PyO3/maturin/pull/1092)

## [0.13.2] - 2022-08-14

Expand Down
16 changes: 11 additions & 5 deletions src/auditwheel/audit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::musllinux::{find_musl_libc, get_musl_version};
use super::policy::{Policy, MANYLINUX_POLICIES, MUSLLINUX_POLICIES};
use crate::auditwheel::{find_external_libs, PlatformTag};
use crate::compile::BuildArtifact;
use crate::target::Target;
use anyhow::{bail, Context, Result};
use fs_err::File;
Expand Down Expand Up @@ -253,13 +254,14 @@ fn get_default_platform_policies() -> Vec<Policy> {
///
/// Does nothing for `platform_tag` set to `Off`/`Linux` or non-linux platforms.
pub fn auditwheel_rs(
path: &Path,
artifact: &BuildArtifact,
target: &Target,
platform_tag: Option<PlatformTag>,
) -> Result<(Policy, bool), AuditWheelError> {
if !target.is_linux() || platform_tag == Some(PlatformTag::Linux) {
return Ok((Policy::default(), false));
}
let path = &artifact.path;
let arch = target.target_arch().to_string();
let mut file = File::open(path).map_err(AuditWheelError::IoError)?;
let mut buffer = Vec::new();
Expand Down Expand Up @@ -413,7 +415,7 @@ pub fn get_sysroot_path(target: &Target) -> Result<PathBuf> {
/// For the given compilation result, return the manylinux platform and the external libs
/// we need to add to repair it
pub fn get_policy_and_libs(
artifact: &Path,
artifact: &BuildArtifact,
platform_tag: Option<PlatformTag>,
target: &Target,
) -> Result<(Policy, Vec<Library>)> {
Expand All @@ -427,9 +429,13 @@ pub fn get_policy_and_libs(
})?;
let external_libs = if should_repair {
let sysroot = get_sysroot_path(target).unwrap_or_else(|_| PathBuf::from("/"));
// FIXME: gather ld_paths from `cargo rustc`
let ld_paths = Vec::new();
find_external_libs(&artifact, &policy, sysroot, ld_paths).with_context(|| {
find_external_libs(
&artifact.path,
&policy,
sysroot,
artifact.linked_paths.clone(),
)
.with_context(|| {
if let Some(platform_tag) = platform_tag {
format!("Error repairing wheel for {} compliance", platform_tag)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ impl BuildContext {
others.sort();

if self.bridge.is_bin() && !musllinux.is_empty() {
return get_policy_and_libs(&artifact.path, Some(musllinux[0]), &self.target);
return get_policy_and_libs(artifact, Some(musllinux[0]), &self.target);
}

let tag = others.get(0).or_else(|| musllinux.get(0)).copied();
get_policy_and_libs(&artifact.path, tag, &self.target)
get_policy_and_libs(artifact, tag, &self.target)
}

fn add_external_libs(
Expand Down
22 changes: 22 additions & 0 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const LIB_CRATE_TYPES: [&str; 4] = ["lib", "dylib", "rlib", "staticlib"];
pub struct BuildArtifact {
/// Path to the build artifact
pub path: PathBuf,
/// Array of paths to include in the library search path, as indicated by
/// the `cargo:rustc-link-search` instruction.
pub linked_paths: Vec<PathBuf>,
}

/// Builds the rust crate into a native module (i.e. an .so or .dll) for a
Expand Down Expand Up @@ -135,6 +138,7 @@ fn compile_universal2(
let mut result = HashMap::new();
let universal_artifact = BuildArtifact {
path: PathBuf::from(output_path),
..x86_64_artifact
};
result.insert(build_type.to_string(), universal_artifact);
universal_artifacts.push(result);
Expand Down Expand Up @@ -387,6 +391,7 @@ fn compile_target(
.context("Failed to run `cargo rustc`")?;

let mut artifacts = HashMap::new();
let mut linked_paths = Vec::new();

let stream = cargo_build
.stdout
Expand Down Expand Up @@ -429,18 +434,35 @@ fn compile_target(
for (crate_type, filename) in tuples {
let artifact = BuildArtifact {
path: filename.into(),
linked_paths: Vec::new(),
};
artifacts.insert(crate_type, artifact);
}
}
}
// See https://doc.rust-lang.org/cargo/reference/external-tools.html#build-script-output
cargo_metadata::Message::BuildScriptExecuted(msg) => {
for path in msg.linked_paths.iter().map(|p| p.as_str()) {
// `linked_paths` may include a "KIND=" prefix in the string where KIND is the library kind
if let Some(index) = path.find('=') {
linked_paths.push(PathBuf::from(&path[index + 1..]));
} else {
linked_paths.push(PathBuf::from(path));
}
}
}
cargo_metadata::Message::CompilerMessage(msg) => {
println!("{}", msg.message);
}
_ => (),
}
}

// Add linked_paths to build artifacts
for artifact in artifacts.values_mut() {
artifact.linked_paths = linked_paths.clone();
}

let status = cargo_build
.wait()
.expect("Failed to wait on cargo child process");
Expand Down

0 comments on commit 82e15c0

Please sign in to comment.