1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::debian_triple_from_rust_triple;
use crate::error::{CDResult, CargoDebError};
use std::path::Path;
use std::process::Command;

/// Resolves the dependencies based on the output of dpkg-shlibdeps on the binary.
pub fn resolve(path: &Path, target: &Option<String>) -> CDResult<Vec<String>> {
    let temp_folder = tempfile::tempdir()?;
    let debian_folder = temp_folder.path().join("debian");
    let control_file_path = debian_folder.join("control");
    std::fs::create_dir_all(&debian_folder)?;
    // dpkg-shlibdeps requires a (possibly empty) debian/control file to exist in its working
    // directory. The executable location doesn't matter.
    let _ = std::fs::File::create(control_file_path);

    // Print result to stdout instead of a file.
    let mut args = vec!["-O"];
    let libpath_arg;
    // determine library search path from target
    if let Some(target) = target {
        libpath_arg = format!("-l/usr/{}/lib", debian_triple_from_rust_triple(target));
        args.push(&libpath_arg);
    }
    const DPKG_SHLIBDEPS_COMMAND: &str = "dpkg-shlibdeps";
    let output = Command::new(DPKG_SHLIBDEPS_COMMAND)
        .args(args)
        .arg(path)
        .current_dir(temp_folder.path())
        .output()
        .map_err(|e| CargoDebError::CommandFailed(e, DPKG_SHLIBDEPS_COMMAND))?;
    if !output.status.success() {
        return Err(CargoDebError::CommandError(
            DPKG_SHLIBDEPS_COMMAND,
            path.display().to_string(),
            output.stderr,
        ));
    }

    log::debug!("dpkg-shlibdeps for {}: {}", path.display(), String::from_utf8_lossy(&output.stdout));

    let deps = output.stdout.as_slice().split(|&c| c == b'\n')
        .find_map(|line| line.strip_prefix(b"shlibs:Depends="))
        .ok_or(CargoDebError::Str("Failed to find dependency specification."))?
        .split(|&c| c == b',')
        .filter_map(|dep| std::str::from_utf8(dep).ok())
        .map(|dep| dep.trim_matches(|c: char| c.is_ascii_whitespace()))
        // libgcc guaranteed by LSB to always be present
        .filter(|dep| !dep.starts_with("libgcc-") && !dep.starts_with("libgcc1"))
        .map(|dep| dep.to_string())
        .collect();

    Ok(deps)
}

#[test]
#[cfg(target_os = "linux")]
fn resolve_test() {
    let exe = std::env::current_exe().unwrap();
    let deps = resolve(&exe, &None).unwrap();
    assert!(deps.iter().any(|d| d.starts_with("libc")));
    assert!(!deps.iter().any(|d| d.starts_with("libgcc")), "{deps:?}");
}