Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions scripts/build-macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,42 @@ get_expected_artifacts() {
printf "%s\n" "$PROJECT_DIR/release/mac*/*.app"
}

# Copy each bundled binary's Homebrew dylibs next to it and rewrite its load
# commands to @executable_path/ (see the call site for why).
#
# Homebrew's fluidsynth links its own library as @rpath/libfluidsynth.3.dylib.
# dylibbundler can't resolve @rpath without a search path, so it asks for a
# directory on stdin. With no terminal in CI it read EOF forever and the Mac
# job spun on "does not exist. Try again" until it timed out (v0.4.17, v0.4.18).
# So: give it Homebrew's lib dirs, and answer any further prompt with "quit"
# so an unresolvable library fails the build at once.
bundle_brew_dylibs() {
local bin_dir="$1"
command -v dylibbundler &>/dev/null || return 0
local brew_prefix search=()
brew_prefix="$(brew --prefix 2>/dev/null || echo /opt/homebrew)"
search+=(-s "$brew_prefix/lib")
local formula
for formula in fluid-synth; do
local keg
keg="$(brew --prefix "$formula" 2>/dev/null || true)"
[[ -n "$keg" && -d "$keg/lib" ]] && search+=(-s "$keg/lib")
done
local bin
for bin in fluidsynth ffmpeg ffprobe vgmstream-cli; do
local target="$bin_dir/$bin"
[[ -f "$target" ]] || continue
echo -e "${BLUE}Bundling ${bin} dependencies...${NC}"
if ! dylibbundler -cd -b -of -x "$target" \
-d "$bin_dir" \
-p '@executable_path/' \
"${search[@]}" <<<"quit"; then
echo "Error: dylibbundler couldn't find every library $bin needs (searched: ${search[*]})." >&2
exit 1
fi
done
}

# Platform-specific: Bundle system binaries
bundle_binaries_impl() {
# macOS: copy existing binaries and bundle dependencies
Expand Down Expand Up @@ -354,16 +390,7 @@ bundle_binaries_impl() {
# against its own brew deps. dylibbundler is idempotent and skips
# paths it has already rewritten, so the per-binary loop is safe
# even when binaries share dylibs.
if command -v dylibbundler &>/dev/null; then
for bin in fluidsynth ffmpeg ffprobe vgmstream-cli; do
local target="$PROJECT_DIR/resources/bin/$bin"
[[ -f "$target" ]] || continue
echo -e "${BLUE}Bundling ${bin} dependencies...${NC}"
dylibbundler -cd -b -of -x "$target" \
-d "$PROJECT_DIR/resources/bin" \
-p '@executable_path/'
done
fi
bundle_brew_dylibs "$PROJECT_DIR/resources/bin"

# Sign all bundled native binaries with the Developer ID Application
# cert before verify_bundled_binaries runs them. Signing also clears
Expand Down
72 changes: 72 additions & 0 deletions tests/build-macos-dylibs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// The Mac build's dylibbundler step (bundle_brew_dylibs in scripts/build-macos.sh).
//
// v0.4.17 and v0.4.18 Mac builds hung for hours: Homebrew's fluidsynth links
// @rpath/libfluidsynth.3.dylib, dylibbundler had no search path for it and
// prompted for a directory on stdin, which in CI is EOF forever.
//
// Runs the real function in bash with `dylibbundler` and `brew` replaced by
// shell functions. Pins that every call gets Homebrew's lib dirs as search
// paths, that a prompt is answered with "quit" rather than EOF, and that an
// unresolvable library fails the build instead of hanging.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { spawnSync } = require('node:child_process');

const SRC = fs.readFileSync(path.join(__dirname, '..', 'scripts', 'build-macos.sh'), 'utf8').replace(/\r\n/g, '\n');
const FN = SRC.match(/^bundle_brew_dylibs\(\) \{[\s\S]*?^\}$/m)[0];

function run({ failFor = '' } = {}) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dylibs-'));
const bin = path.join(dir, 'bin').replace(/\\/g, '/');
fs.mkdirSync(bin);
for (const b of ['fluidsynth', 'ffmpeg', 'vgmstream-cli']) fs.writeFileSync(path.join(bin, b), '');
const log = path.join(dir, 'calls.log').replace(/\\/g, '/');
const script = `
set -euo pipefail
BLUE=''; NC=''
brew() { if [[ $# -eq 1 ]]; then echo /opt/homebrew; else echo "/opt/homebrew/opt/$2"; fi; }
dylibbundler() {
local target="" ; local args="$*"
while [[ $# -gt 0 ]]; do [[ "$1" == "-x" ]] && target="$2"; shift; done
local answer=""; read -r answer || answer="<EOF>"
echo "$(basename "$target")|$args|stdin=$answer" >> "${log}"
[[ -n "${failFor}" && "$(basename "$target")" == "${failFor}" ]] && return 1
return 0
}
${FN}
bundle_brew_dylibs "${bin}"
echo DONE
`;
const r = spawnSync('bash', ['-c', script], { encoding: 'utf8' });
const calls = fs.existsSync(log) ? fs.readFileSync(log, 'utf8').trim().split('\n') : [];
fs.rmSync(dir, { recursive: true, force: true });
return { r, calls };
}

test('every bundled binary gets Homebrew lib search paths and a "quit" answer, not EOF', () => {
const { r, calls } = run();
assert.equal(r.status, 0, r.stderr);
assert.match(r.stdout, /DONE/);
assert.deepEqual(calls.map((c) => c.split('|')[0]), ['fluidsynth', 'ffmpeg', 'vgmstream-cli'], 'missing binaries (ffprobe) are skipped');
for (const c of calls) {
assert.match(c, /-s \/opt\/homebrew\/lib/, `brew lib dir searched: ${c}`);
assert.match(c, /-p @executable_path\//);
assert.match(c, /stdin=quit$/, `a prompt is answered with quit: ${c}`);
}
});

test('a library dylibbundler cannot find fails the build instead of hanging', () => {
const { r, calls } = run({ failFor: 'fluidsynth' });
assert.notEqual(r.status, 0);
assert.doesNotMatch(r.stdout, /DONE/);
assert.match(r.stderr, /dylibbundler couldn't find every library fluidsynth needs/);
assert.equal(calls.length, 1, 'stops at the first failure');
});

test('the build calls the shared function, with no other dylibbundler call site', () => {
assert.match(SRC, /^\s+bundle_brew_dylibs "\$PROJECT_DIR\/resources\/bin"$/m);
assert.equal((SRC.match(/^\s*(if ! )?dylibbundler /gm) || []).length, 1);
});
2 changes: 1 addition & 1 deletion tests/test-inventory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ENGINE = [
// Every node:test file at the untangler baseline plus the Phase 2 safety nets.
const ALL = [
'audio-bridge-fallbacks', 'audio-chain-persistence', 'audio-effects-executor', 'boundaries',
'build-plugin-clone', 'chain-mutation-storm', 'config-bootstrap',
'build-plugin-clone', 'build-macos-dylibs', 'chain-mutation-storm', 'config-bootstrap',
'config-migrations', 'config-paths', 'config-reset', 'contract-check',
'desktop-config', 'device-setup-lifecycle', 'engine-no-default-mic',
'guitar-input-channel-default', 'heard-clock-wiring', 'input-source-selection-sync',
Expand Down
Loading