cargo_deb/
dependencies.rs

1use crate::error::{CDResult, CargoDebError};
2use std::path::Path;
3use std::process::Command;
4
5const DPKG_SHLIBDEPS_COMMAND: &str = "dpkg-shlibdeps";
6
7/// Resolves the dependencies based on the output of dpkg-shlibdeps on the binary.
8pub(crate) fn resolve_with_dpkg(path: &Path, debian_arch: &str, lib_dir_search_paths: &[&Path]) -> CDResult<Vec<String>> {
9    let temp_folder = tempfile::tempdir().map_err(CargoDebError::Io)?;
10    let debian_folder = temp_folder.path().join("debian");
11    let control_file_path = debian_folder.join("control");
12    let _ = std::fs::create_dir_all(&debian_folder);
13    // dpkg-shlibdeps requires a (possibly empty) debian/control file to exist in its working
14    // directory. The executable location doesn't matter.
15    let _ = std::fs::File::create(&control_file_path)
16        .map_err(|e| CargoDebError::IoFile("Can't make temp file", e, control_file_path))?;
17
18    let mut cmd = Command::new(DPKG_SHLIBDEPS_COMMAND);
19    cmd.env("DEB_HOST_ARCH", debian_arch);
20    cmd.arg("-xlibgcc");
21    // determine library search path from target
22    for dir in lib_dir_search_paths {
23        debug_assert!(dir.exists());
24        cmd.arg(format!("-l{}", dir.display()));
25    }
26    // Print result to stdout instead of a file.
27    cmd.arg("-O");
28    let output = cmd
29        .arg(path)
30        .current_dir(temp_folder.path())
31        .output()
32        .map_err(|e| CargoDebError::CommandFailed(e, DPKG_SHLIBDEPS_COMMAND.into()))?;
33    if !output.status.success() {
34        return Err(CargoDebError::CommandError(
35            DPKG_SHLIBDEPS_COMMAND,
36            format!("{cmd:?}"),
37            output.stderr,
38        ));
39    }
40
41    log::debug!("dpkg-shlibdeps for {}: {}", path.display(), String::from_utf8_lossy(&output.stdout));
42
43    let deps = output.stdout.as_slice().split(|&c| c == b'\n')
44        .find_map(|line| line.strip_prefix(b"shlibs:Depends="))
45        .ok_or(CargoDebError::Str("Failed to find dependency specification."))?
46        .split(|&c| c == b',')
47        .filter_map(|dep| std::str::from_utf8(dep).ok())
48        .map(|dep| dep.trim_matches(|c: char| c.is_ascii_whitespace()))
49        // libgcc guaranteed by LSB to always be present
50        .filter(|dep| !dep.starts_with("libgcc-") && !dep.starts_with("libgcc1"))
51        .map(|dep| dep.to_string())
52        .collect();
53
54    Ok(deps)
55}
56
57#[test]
58#[cfg(target_os = "linux")]
59fn resolve_test() {
60    use crate::{debian_architecture_from_rust_triple, DEFAULT_TARGET};
61
62    let exe = std::env::current_exe().unwrap();
63    let deps = resolve_with_dpkg(&exe, debian_architecture_from_rust_triple(DEFAULT_TARGET), &[]).unwrap();
64    assert!(deps.iter().any(|d| d.starts_with("libc")));
65    assert!(!deps.iter().any(|d| d.starts_with("libgcc")), "{deps:?}");
66}