Skip to content

Commit

Permalink
Use rust-native macho library
Browse files Browse the repository at this point in the history
  • Loading branch information
mistydemeo committed Sep 18, 2023
1 parent 20ef8b3 commit f6086f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cargo-dist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ include_dir = "0.7.3"
itertools = "0.11.0"
cargo-wix = "0.3.6"
uuid = { version = "1", features = ["v4"] }
mach_object = "0.1"

[dev-dependencies]
insta = { version = "1.26.0", features = ["filters"] }
Expand Down
55 changes: 33 additions & 22 deletions cargo-dist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use std::{
collections::{BTreeMap, HashMap},
fs::File,
io::{Cursor, Read},
process::Command,
};

Expand All @@ -26,6 +28,7 @@ use cargo_dist_schema::{Asset, AssetKind, DistManifest, ExecutableAsset};
use config::{
ArtifactMode, ChecksumStyle, CompressionImpl, Config, DirtyMode, GenerateMode, ZipStyle,
};
use mach_object::{LoadCommand, OFile};
use semver::Version;
use serde::Serialize;
use tracing::{info, warn};
Expand Down Expand Up @@ -809,29 +812,37 @@ Other: {}"#,
}

fn do_otool(path: &Utf8PathBuf) -> DistResult<Vec<String>> {
let output = Command::new("otool")
.arg("-XL")
.arg(path)
.output()
.expect("otool failed to run");

let mut result = String::from_utf8_lossy(&output.stdout).to_string();
if let Some(stripped) = result.strip_suffix('\n') {
result = stripped.to_owned();
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 OFile::MachFile {
header: _,
commands,
} = OFile::parse(&mut cur).unwrap()
{
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());
}
_ => {}
}
}
}
let libraries: Vec<String> = result
.split('\n')
// Lines are formatted like:
// "/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)"
.map(|line| {
line.trim_start()
.split('(')
.next()
.unwrap()
.trim_end()
.to_string()
})
.collect();

Ok(libraries)
}
Expand Down

0 comments on commit f6086f4

Please sign in to comment.