Skip to content

Commit 411fded

Browse files
committed
refactor(scripts): share otool parsing between native package scripts
ensure-native-binary.nu and stage-native-package.nu both defined `binary_name` and both turned `otool -L` output into a list of linked dylibs, so the two copies of the parse had already drifted: the staging copy kept blank rows the checking copy filtered out. Extract both into a `native-binary.nu` module alongside them, following the `pricing-lock.nu` precedent for a shared non-executable module. `linked-dylibs` returns `{ok, stderr, dylibs}` rather than erroring, because the two callers want different failure handling — staging aborts with the captured stderr, the portability check treats an otool failure as not portable. Selecting dylib rows by their tab indent instead of `skip 1` also fixes a latent bug. `otool -L` repeats an unindented `<binary> (architecture <arch>):` header once per architecture, so `skip 1` dropped only the first and fed the remaining headers through as if they were linked libraries. Any fat binary was therefore reported as depending on itself, and since a binary path does not start with /usr/lib/ or /System/Library/ the portability check failed it. Cargo emits single-architecture binaries, so the shipped path never hit this, and old and new parsing agree exactly on single-architecture input. Verified against real Mach-O binaries: identical dylib lists to the previous implementations for single-architecture input, and /bin/ls now reports its 6 real dylibs and passes the portability check instead of failing it. Also ran a full darwin staging cycle over a nix-store binary that links nix-store libiconv, confirming the install_name_tool rewrite and the prepack verification still pass.
1 parent 8b6152e commit 411fded

3 files changed

Lines changed: 52 additions & 33 deletions

File tree

‎apps/ccusage/scripts/ensure-native-binary.nu‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/env nix
22
#! nix shell --inputs-from ../../.. nixpkgs#nushell --command nu
3+
4+
use ./native-binary.nu [binary-name, linked-dylibs]
5+
36
const system_dylib_prefixes = ['/usr/lib/', '/System/Library/']
47
def main [] {
58
let repo_root = (
69
$env.CURRENT_FILE | path dirname | path join ../../.. | path expand
710
)
811
let target_platform = (node_platform)
912
let target_arch = (node_arch)
10-
let binary_name = (binary_name $target_platform)
13+
let binary_name = (binary-name $target_platform)
1114
let native_package_root = (matching_native_package_root $repo_root $target_platform $target_arch)
1215
let native_binary = (match $native_package_root {
1316
null => null
@@ -41,12 +44,6 @@ def node_arch [] {
4144
$other => $other
4245
}
4346
}
44-
def binary_name [platform: string] {
45-
match $platform {
46-
'win32' => 'ccusage.exe'
47-
_ => 'ccusage'
48-
}
49-
}
5047
def matching_native_package_root [repo_root: path, target_platform: string, target_arch: string] {
5148
glob ($repo_root | path join packages 'ccusage-*' package.json)
5249
| where {|package_json_path|
@@ -88,19 +85,11 @@ def linux_binary_is_static [binary: path] {
8885
$"($result.stdout)($result.stderr)" =~ '(?i)not a dynamic executable|statically linked'
8986
}
9087
def darwin_binary_links_only_system_dylibs [binary: path] {
91-
let result = ^otool -L $binary | complete
92-
match $result.exit_code {
93-
0 => (
94-
$result.stdout
95-
| lines
96-
| skip 1
97-
| each {|line| $line | str trim }
98-
| where {|line| $line | is-not-empty }
99-
| each {|line| $line | split row --regex '\s+' | first }
100-
| all {|dylib| $system_dylib_prefixes | any {|prefix| $dylib | str starts-with $prefix } }
101-
)
102-
_ => false
103-
}
88+
let linked = (linked-dylibs $binary)
89+
$linked.ok and (
90+
$linked.dylibs
91+
| all {|dylib| $system_dylib_prefixes | any {|prefix| $dylib | str starts-with $prefix } }
92+
)
10493
}
10594
def has_expected_version [binary, version: string] {
10695
($binary != null) and ($binary | path exists) and ((reported_version $binary) == $version)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Shared helpers for the scripts that stage and validate the Rust binary
2+
# shipped inside packages/ccusage-<platform>-<arch>.
3+
4+
# The file name npm expects for a Node platform identifier.
5+
export def binary-name [platform: string]: nothing -> string {
6+
match $platform {
7+
'win32' => 'ccusage.exe'
8+
_ => 'ccusage'
9+
}
10+
}
11+
12+
# The dynamic libraries `otool -L` reports for a Mach-O binary.
13+
#
14+
# `ok` is false when otool itself failed, so a caller can choose between
15+
# aborting with the captured stderr and treating the binary as unusable.
16+
export def linked-dylibs [binary: path]: nothing -> record {
17+
let result = run-external otool '-L' $binary | complete
18+
{
19+
ok: ($result.exit_code == 0)
20+
stderr: $result.stderr
21+
dylibs: (match $result.exit_code {
22+
# Only the `<dylib> (compatibility version ...)` rows are tab
23+
# indented. Selecting on that indent rather than skipping a fixed
24+
# number of leading rows keeps the unindented `<binary>
25+
# (architecture <arch>):` header out of the result for fat binaries,
26+
# which repeat that header once per architecture.
27+
0 => (
28+
$result.stdout
29+
| lines
30+
| where {|line| $line | str starts-with (char tab) }
31+
| each {|line| $line | str trim | split row --regex '\s+' | first }
32+
)
33+
_ => []
34+
})
35+
}
36+
}

‎apps/ccusage/scripts/stage-native-package.nu‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env nix
22
#! nix shell --inputs-from ../../.. nixpkgs#nushell --command nu
3+
4+
use ./native-binary.nu [binary-name, linked-dylibs]
5+
36
const package_dirs = {
47
darwin-arm64: 'ccusage-darwin-arm64'
58
darwin-x64: 'ccusage-darwin-x64'
@@ -20,19 +23,13 @@ def main [--platform: string, --arch: string, --binary: string] {
2023
let target_dir = [$repo_root, 'packages', $package_dir, 'bin'] | path join
2124
let target = [
2225
$target_dir
23-
(binary_name $platform)
26+
(binary-name $platform)
2427
] | path join
2528
mkdir $target_dir
2629
cp -f $source $target
2730
finalize_target $platform $target
2831
print $target
2932
}
30-
def binary_name [platform: string] {
31-
match $platform {
32-
'win32' => 'ccusage.exe'
33-
_ => 'ccusage'
34-
}
35-
}
3633
def finalize_target [platform: string, target: path] {
3734
match $platform {
3835
'win32' => null
@@ -44,15 +41,12 @@ def finalize_target [platform: string, target: path] {
4441
}
4542
}
4643
def rewrite_darwin_system_libraries [binary_path: string] {
47-
let linked = run-external otool '-L' $binary_path | complete
48-
if $linked.exit_code != 0 {
44+
let linked = (linked-dylibs $binary_path)
45+
if not $linked.ok {
4946
error make {msg: $"otool failed for ($binary_path)\n($linked.stderr)"}
5047
}
5148
let failed_rewrite = (
52-
$linked.stdout
53-
| lines
54-
| skip 1
55-
| each {|line| $line | str trim | split row --regex '\s+' | first }
49+
$linked.dylibs
5650
| where {|library| $library =~ '^/nix/store/[^/]+-libiconv-[^/]+/lib/libiconv\.2\.dylib$' }
5751
| each {|library|
5852
{library: $library, rewrite: (run-external install_name_tool '-change' $library /usr/lib/libiconv.2.dylib $binary_path | complete)}

0 commit comments

Comments
 (0)