cargo-dist 0.32.0

Shippable application packaging for Rust
Documentation
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! The Linkage Checker, which lets us detect what a binary dynamically links to (and why)

use std::{
    fs::{self, File},
    io::{Cursor, Read},
};

use axoasset::SourceFile;
use axoprocess::Cmd;
use camino::Utf8PathBuf;
use cargo_dist_schema::{
    AssetInfo, BuildEnvironment, DistManifest, GlibcVersion, Library, Linkage, PackageManager,
    TripleNameRef,
};
use comfy_table::{presets::UTF8_FULL, Table};
use goblin::Object;
use mach_object::{LoadCommand, OFile};
use tracing::warn;

use crate::{config::Config, errors::*, gather_work, Artifact, DistGraph};

/// Arguments for `dist linkage` ([`do_linkage][])
#[derive(Debug)]
pub struct LinkageArgs {
    /// Print human-readable output
    pub print_output: bool,
    /// Print output as JSON
    pub print_json: bool,
    /// Read linkage data from JSON rather than performing a live check
    pub from_json: Option<String>,
}

/// Determinage dynamic linkage of built artifacts (impl of `dist linkage`)
pub fn do_linkage(cfg: &Config, args: &LinkageArgs) -> DistResult<()> {
    let manifest = if let Some(target) = args.from_json.clone() {
        let file = SourceFile::load_local(target)?;
        file.deserialize_json()?
    } else {
        let (dist, mut manifest) = gather_work(cfg)?;
        compute_linkage_assuming_local_build(&dist, &mut manifest, cfg)?;
        manifest
    };

    if args.print_output {
        eprintln!("{}", LinkageDisplay(&manifest));
    }
    if args.print_json {
        let string = serde_json::to_string_pretty(&manifest).unwrap();
        println!("{string}");
    }
    Ok(())
}

/// Assuming someone just ran `dist build` on the current machine,
/// compute the linkage by checking binaries in the temp to-be-zipped dirs.
fn compute_linkage_assuming_local_build(
    dist: &DistGraph,
    manifest: &mut DistManifest,
    cfg: &Config,
) -> DistResult<()> {
    let targets = &cfg.targets;
    let artifacts = &dist.artifacts;
    let dist_dir = &dist.dist_dir;

    for target in targets {
        let artifacts: Vec<Artifact> = artifacts
            .clone()
            .into_iter()
            .filter(|r| r.target_triples.contains(target))
            .collect();

        if artifacts.is_empty() {
            eprintln!("No matching artifact for target {target}");
            continue;
        }

        for artifact in artifacts {
            let path = Utf8PathBuf::from(&dist_dir).join(format!("{}-{target}", artifact.id));

            for (bin_idx, binary_relpath) in artifact.required_binaries {
                let bin = dist.binary(bin_idx);
                let bin_path = path.join(binary_relpath);
                if !bin_path.exists() {
                    eprintln!("Binary {bin_path} missing; skipping check");
                } else {
                    let linkage = determine_linkage(&bin_path, target);
                    manifest.assets.insert(
                        bin.id.clone(),
                        AssetInfo {
                            id: bin.id.clone(),
                            name: bin.name.clone(),
                            system: dist.system_id.clone(),
                            linkage: Some(linkage),
                            target_triples: vec![target.clone()],
                        },
                    );
                }
            }
        }
    }

    Ok(())
}

/// Formatter for a DistManifest that prints the linkage human-readably
pub struct LinkageDisplay<'a>(pub &'a DistManifest);

impl std::fmt::Display for LinkageDisplay<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for asset in self.0.assets.values() {
            let Some(linkage) = &asset.linkage else {
                continue;
            };
            let name = &asset.name;
            let targets = asset.target_triples.join(", ");
            write!(f, "{name}")?;
            if !targets.is_empty() {
                write!(f, " ({targets})")?;
            }
            writeln!(f, "\n")?;
            format_linkage_table(f, linkage)?;
        }
        Ok(())
    }
}

/// Formatted human-readable output
fn format_linkage_table(f: &mut std::fmt::Formatter<'_>, linkage: &Linkage) -> std::fmt::Result {
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .set_header(vec!["Category", "Libraries"])
        .add_row(vec![
            "System",
            linkage
                .system
                .clone()
                .into_iter()
                .map(|l| l.to_string())
                .collect::<Vec<String>>()
                .join("\n")
                .as_str(),
        ])
        .add_row(vec![
            "Homebrew",
            linkage
                .homebrew
                .clone()
                .into_iter()
                .map(|l| l.to_string())
                .collect::<Vec<String>>()
                .join("\n")
                .as_str(),
        ])
        .add_row(vec![
            "Public (unmanaged)",
            linkage
                .public_unmanaged
                .clone()
                .into_iter()
                .map(|l| l.path)
                .collect::<Vec<String>>()
                .join("\n")
                .as_str(),
        ])
        .add_row(vec![
            "Frameworks",
            linkage
                .frameworks
                .clone()
                .into_iter()
                .map(|l| l.path)
                .collect::<Vec<String>>()
                .join("\n")
                .as_str(),
        ])
        .add_row(vec![
            "Other",
            linkage
                .other
                .clone()
                .into_iter()
                .map(|l| l.to_string())
                .collect::<Vec<String>>()
                .join("\n")
                .as_str(),
        ]);
    write!(f, "{table}")
}

/// Create a homebrew library for the given path
pub fn library_from_homebrew(library: String) -> Library {
    // Doesn't currently support Homebrew installations in
    // non-default locations
    let brew_prefix = if library.starts_with("/opt/homebrew/opt/") {
        Some("/opt/homebrew/opt/")
    } else if library.starts_with("/usr/local/opt/") {
        Some("/usr/local/opt/")
    } else {
        None
    };

    if let Some(prefix) = brew_prefix {
        let cloned = library.clone();
        let stripped = cloned.strip_prefix(prefix).unwrap();
        let mut package = stripped.split('/').next().unwrap().to_owned();

        // The path alone isn't enough to determine the tap the formula
        // came from. If the install receipt exists, we can use it to
        // get the name of the source tap.
        let receipt = Utf8PathBuf::from(&prefix)
            .join(&package)
            .join("INSTALL_RECEIPT.json");

        // If the receipt doesn't exist or can't be loaded, that's not an
        // error; we can fall back to the package basename we parsed out
        // of the path.
        if receipt.exists() {
            let _ = SourceFile::load_local(&receipt)
                .and_then(|file| file.deserialize_json())
                .map(|parsed: serde_json::Value| {
                    if let Some(tap) = parsed["source"]["tap"].as_str() {
                        if tap != "homebrew/core" {
                            package = format!("{tap}/{package}");
                        }
                    }
                });
        }

        Library {
            path: library,
            source: Some(package.to_owned()),
            package_manager: Some(PackageManager::Homebrew),
        }
    } else {
        Library {
            path: library,
            source: None,
            package_manager: None,
        }
    }
}

/// Create an apt library for the given path
pub fn library_from_apt(library: String) -> DistResult<Library> {
    // We can't get this information on other OSs
    if std::env::consts::OS != "linux" {
        return Ok(Library {
            path: library,
            source: None,
            package_manager: None,
        });
    }

    let process = Cmd::new("dpkg", "get linkage info from dpkg")
        .arg("--search")
        .arg(&library)
        .output();
    match process {
        Ok(output) => {
            let output = String::from_utf8(output.stdout)?;

            let package = output.split(':').next().unwrap();
            let source = if package.is_empty() {
                None
            } else {
                Some(package.to_owned())
            };
            let package_manager = if source.is_some() {
                Some(PackageManager::Apt)
            } else {
                None
            };

            Ok(Library {
                path: library,
                source,
                package_manager,
            })
        }
        // Couldn't find a package for this file
        Err(_) => Ok(Library {
            path: library,
            source: None,
            package_manager: None,
        }),
    }
}

fn do_otool(path: &Utf8PathBuf) -> DistResult<Vec<String>> {
    let mut libraries = vec![];

    let mut f = File::open(path)?;
    let mut buf = vec![];
    let size = f.read_to_end(&mut buf).unwrap();
    let mut cur = Cursor::new(&buf[..size]);
    if let Ok(OFile::MachFile {
        header: _,
        commands,
    }) = OFile::parse(&mut cur)
    {
        let commands = commands
            .iter()
            .map(|load| load.command())
            .cloned()
            .collect::<Vec<LoadCommand>>();

        for command in commands {
            match command {
                LoadCommand::IdDyLib(ref dylib)
                | LoadCommand::LoadDyLib(ref dylib)
                | LoadCommand::LoadWeakDyLib(ref dylib)
                | LoadCommand::ReexportDyLib(ref dylib)
                | LoadCommand::LoadUpwardDylib(ref dylib)
                | LoadCommand::LazyLoadDylib(ref dylib) => {
                    libraries.push(dylib.name.to_string());
                }
                _ => {}
            }
        }
    }

    Ok(libraries)
}

fn do_ldd(path: &Utf8PathBuf) -> DistResult<Vec<String>> {
    let mut libraries = vec![];

    // We ignore the status here because for whatever reason arm64 glibc ldd can decide
    // to return non-zero status on binaries with no dynamic linkage (e.g. musl-static).
    // This was observed both in arm64 ubuntu and asahi (both glibc ldd).
    // x64 glibc ldd is perfectly fine with this and returns 0, so... *shrug* compilers!
    let output = Cmd::new("ldd", "get linkage info from ldd")
        .arg(path)
        .check(false)
        .output()?;

    let result = String::from_utf8_lossy(&output.stdout).to_string();
    let lines = result.trim_end().split('\n');

    for line in lines {
        let line = line.trim();

        // There's no dynamic linkage at all; we can safely break,
        // there will be nothing useful to us here.
        if line.starts_with("not a dynamic executable") || line.starts_with("statically linked") {
            break;
        }

        // Not a library that actually concerns us
        if line.starts_with("linux-vdso") {
            continue;
        }

        // Format: libname.so.1 => /path/to/libname.so.1 (address)
        if let Some(path) = line.split(" => ").nth(1) {
            // This may be a symlink rather than the actual underlying library;
            // we resolve the symlink here so that we return the real paths,
            // making it easier to map them to their packages later.
            let lib = (path.split(' ').next().unwrap()).to_owned();
            let realpath = fs::canonicalize(&lib)?;
            libraries.push(realpath.to_string_lossy().to_string());
        } else {
            continue;
        }
    }

    Ok(libraries)
}

fn do_pe(path: &Utf8PathBuf) -> DistResult<Vec<String>> {
    let buf = std::fs::read(path)?;
    match Object::parse(&buf)? {
        Object::PE(pe) => Ok(pe.libraries.into_iter().map(|s| s.to_owned()).collect()),
        // Static libraries link against nothing
        Object::Archive(_) => Ok(vec![]),
        _ => Err(DistError::LinkageCheckUnsupportedBinary),
    }
}

/// Get the linkage for a single binary
///
/// If linkage fails for any reason we warn and return the default empty linkage
pub fn determine_linkage(path: &Utf8PathBuf, target: &TripleNameRef) -> Linkage {
    match try_determine_linkage(path, target) {
        Ok(linkage) => linkage,
        Err(e) => {
            warn!("Skipping linkage for {path}:\n{:?}", miette::Report::new(e));
            Linkage::default()
        }
    }
}

/// Get the linkage for a single binary
fn try_determine_linkage(path: &Utf8PathBuf, target: &TripleNameRef) -> DistResult<Linkage> {
    let libraries = if target.is_darwin() {
        do_otool(path)?
    } else if target.is_linux() {
        // Currently can only be run on Linux
        if std::env::consts::OS != "linux" {
            return Err(DistError::LinkageCheckInvalidOS {
                host: std::env::consts::OS.to_owned(),
                target: target.to_owned(),
            });
        }
        do_ldd(path)?
    } else if target.is_windows() {
        do_pe(path)?
    } else {
        return Err(DistError::LinkageCheckUnsupportedBinary);
    };

    let mut linkage = Linkage {
        system: Default::default(),
        homebrew: Default::default(),
        public_unmanaged: Default::default(),
        frameworks: Default::default(),
        other: Default::default(),
    };
    for library in libraries {
        if library.starts_with("/opt/homebrew") {
            linkage
                .homebrew
                .insert(library_from_homebrew(library.clone()));
        } else if library.starts_with("/usr/lib") || library.starts_with("/lib") {
            linkage.system.insert(library_from_apt(library.clone())?);
        } else if library.starts_with("/System/Library/Frameworks")
            || library.starts_with("/Library/Frameworks")
        {
            linkage.frameworks.insert(Library::new(library.clone()));
        } else if library.starts_with("/usr/local") {
            if std::fs::canonicalize(&library)?.starts_with("/usr/local/Cellar") {
                linkage
                    .homebrew
                    .insert(library_from_homebrew(library.clone()));
            } else {
                linkage
                    .public_unmanaged
                    .insert(Library::new(library.clone()));
            }
        } else {
            linkage.other.insert(library_from_apt(library.clone())?);
        }
    }

    Ok(linkage)
}

/// Determine the build environment on the current host
/// This should be done local to the builder!
pub fn determine_build_environment(target: &TripleNameRef) -> BuildEnvironment {
    if target.is_darwin() {
        determine_macos_build_environment().unwrap_or(BuildEnvironment::Indeterminate)
    } else if target.is_linux() {
        determine_linux_build_environment().unwrap_or(BuildEnvironment::Indeterminate)
    } else if target.is_windows() {
        BuildEnvironment::Windows
    } else {
        BuildEnvironment::Indeterminate
    }
}

fn determine_linux_build_environment() -> DistResult<BuildEnvironment> {
    // If we're running this cross-host somehow, we should return an
    // indeterminate result here
    if std::env::consts::OS != "linux" {
        return Ok(BuildEnvironment::Indeterminate);
    }

    let mut cmd = Cmd::new("ldd", "determine glibc version");
    cmd.arg("--version");
    let output = cmd.output()?;
    let output_str = String::from_utf8(output.stdout)?;
    let first_line = output_str.lines().next().unwrap_or(&output_str).trim_end();
    // Running on a system without glibc at all
    let glibc_version = if !first_line.contains("GNU libc") && !first_line.contains("GLIBC") {
        None
    } else {
        // Formats observed in the wild:
        // ldd (Ubuntu GLIBC 2.35-0ubuntu3.8) 2.35 (Ubuntu 22.04)
        // ldd (Debian GLIBC 2.36-9+deb12u7) 2.36 (Debian)
        // ldd (GNU libc) 2.39 (Fedora)
        first_line
            .split(' ')
            .next_back()
            .and_then(|s| s.split_once('.').map(glibc_from_tuple))
            .transpose()?
    };

    Ok(BuildEnvironment::Linux { glibc_version })
}

fn glibc_from_tuple(versions: (&str, &str)) -> Result<GlibcVersion, DistError> {
    let major = versions.0.parse::<u64>()?;
    let series = versions.1.parse::<u64>()?;

    Ok(GlibcVersion { major, series })
}

fn determine_macos_build_environment() -> DistResult<BuildEnvironment> {
    // If we're running this cross-host somehow, we should return an
    // indeterminate result here
    if std::env::consts::OS != "macos" {
        return Ok(BuildEnvironment::Indeterminate);
    }

    let mut cmd = Cmd::new("sw_vers", "determine OS version");
    cmd.arg("-productVersion");
    let output = cmd.output()?;
    let os_version = String::from_utf8(output.stdout)?.trim_end().to_owned();

    Ok(BuildEnvironment::MacOS { os_version })
}