| 1 |
use anyhow::{bail, Context, Result}; |
| 2 |
use std::ffi::OsStr; |
| 3 |
use std::path::Path; |
| 4 |
use std::process::Command; |
| 5 |
|
| 6 |
static MISSING_PATCHELF_ERROR: &str = "Failed to execute 'patchelf', did you install it? Hint: Try `pip install maturin[patchelf]` (or just `pip install patchelf`)"; |
| 7 |
|
| 8 |
/// Verify patchelf version |
| 9 |
pub fn verify_patchelf() -> Result<()> { |
| 10 |
let output = Command::new("patchelf") |
| 11 |
.arg("--version") |
| 12 |
.output() |
| 13 |
.context(MISSING_PATCHELF_ERROR)?; |
| 14 |
let version = String::from_utf8(output.stdout) |
| 15 |
.context("Failed to parse patchelf version")? |
| 16 |
.trim() |
| 17 |
.to_string(); |
| 18 |
let version = version.strip_prefix("patchelf").unwrap_or(&version).trim(); |
| 19 |
let semver = version.parse::<semver::Version>().context( |
| 20 |
"Failed to parse patchelf version, auditwheel repair requires patchelf >= 0.14.0.", |
| 21 |
)?; |
| 22 |
if semver < semver::Version::new(0, 14, 0) { |
| 23 |
bail!( |
| 24 |
"patchelf {} found. auditwheel repair requires patchelf >= 0.14.0.", |
| 25 |
version |
| 26 |
); |
| 27 |
} |
| 28 |
Ok(()) |
| 29 |
} |
| 30 |
|
| 31 |
/// Replace a declared dependency on a dynamic library with another one (`DT_NEEDED`) |
| 32 |
pub fn replace_needed<O: AsRef<OsStr>, N: AsRef<OsStr>>( |
| 33 |
file: impl AsRef<Path>, |
| 34 |
old_new_pairs: &[(O, N)], |
| 35 |
) -> Result<()> { |
| 36 |
let mut cmd = Command::new("patchelf"); |
| 37 |
for (old, new) in old_new_pairs { |
| 38 |
cmd.arg("--replace-needed").arg(old).arg(new); |
| 39 |
} |
| 40 |
cmd.arg(file.as_ref()); |
| 41 |
let output = cmd.output().context(MISSING_PATCHELF_ERROR)?; |
| 42 |
if !output.status.success() { |
| 43 |
bail!( |
| 44 |
"patchelf --replace-needed failed: {}", |
| 45 |
String::from_utf8_lossy(&output.stderr) |
| 46 |
); |
| 47 |
} |
| 48 |
Ok(()) |
| 49 |
} |
| 50 |
|
| 51 |
/// Change `SONAME` of a dynamic library |
| 52 |
pub fn set_soname<S: AsRef<OsStr>>(file: impl AsRef<Path>, soname: &S) -> Result<()> { |
| 53 |
let mut cmd = Command::new("patchelf"); |
| 54 |
cmd.arg("--set-soname").arg(soname).arg(file.as_ref()); |
| 55 |
let output = cmd.output().context(MISSING_PATCHELF_ERROR)?; |
| 56 |
if !output.status.success() { |
| 57 |
bail!( |
| 58 |
"patchelf --set-soname failed: {}", |
| 59 |
String::from_utf8_lossy(&output.stderr) |
| 60 |
); |
| 61 |
} |
| 62 |
Ok(()) |
| 63 |
} |
| 64 |
|
| 65 |
/// Remove a `RPATH` from executables and libraries |
| 66 |
pub fn remove_rpath(file: impl AsRef<Path>) -> Result<()> { |
| 67 |
let mut cmd = Command::new("patchelf"); |
| 68 |
cmd.arg("--remove-rpath").arg(file.as_ref()); |
| 69 |
let output = cmd.output().context(MISSING_PATCHELF_ERROR)?; |
| 70 |
if !output.status.success() { |
| 71 |
bail!( |
| 72 |
"patchelf --remove-rpath failed: {}", |
| 73 |
String::from_utf8_lossy(&output.stderr) |
| 74 |
); |
| 75 |
} |
| 76 |
Ok(()) |
| 77 |
} |
| 78 |
|
| 79 |
/// Change the `RPATH` of executables and libraries |
| 80 |
pub fn set_rpath<S: AsRef<OsStr>>(file: impl AsRef<Path>, rpath: &S) -> Result<()> { |
| 81 |
remove_rpath(&file)?; |
| 82 |
let mut cmd = Command::new("patchelf"); |
| 83 |
cmd.arg("--force-rpath") |
| 84 |
.arg("--set-rpath") |
| 85 |
.arg(rpath) |
| 86 |
.arg(file.as_ref()); |
| 87 |
let output = cmd.output().context(MISSING_PATCHELF_ERROR)?; |
| 88 |
if !output.status.success() { |
| 89 |
bail!( |
| 90 |
"patchelf --set-rpath failed: {}", |
| 91 |
String::from_utf8_lossy(&output.stderr) |
| 92 |
); |
| 93 |
} |
| 94 |
Ok(()) |
| 95 |
} |
| 96 |
|
| 97 |
/// Get the `RPATH` of executables and libraries |
| 98 |
pub fn get_rpath(file: impl AsRef<Path>) -> Result<Vec<String>> { |
| 99 |
let file = file.as_ref(); |
| 100 |
let contents = fs_err::read(file)?; |
| 101 |
match goblin::Object::parse(&contents) { |
| 102 |
Ok(goblin::Object::Elf(elf)) => { |
| 103 |
let rpaths = if !elf.runpaths.is_empty() { |
| 104 |
elf.runpaths |
| 105 |
} else { |
| 106 |
elf.rpaths |
| 107 |
}; |
| 108 |
Ok(rpaths.iter().map(|r| r.to_string()).collect()) |
| 109 |
} |
| 110 |
Ok(_) => bail!("'{}' is not an ELF file", file.display()), |
| 111 |
Err(e) => bail!("Failed to parse ELF file at '{}': {}", file.display(), e), |
| 112 |
} |
| 113 |
} |