Skip to content
Open
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
20 changes: 18 additions & 2 deletions scripts/build-native.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { execSync } from 'child_process';
import { mkdirSync } from 'fs';
import { mkdirSync, rmSync } from 'fs';
import { createRequire } from 'module';

const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -48,8 +48,24 @@ const swift = [
'-framework AVFoundation -framework Foundation'],
];

// macOS 11 (Big Sur): the arm64 minimum and a broad floor for Intel Macs. The Swift sources are
// unchanged — only the minimum-OS and the second arch slice differ from a stock single-arch build.
const MACOS_DEPLOYMENT_TARGET = 'macos11';

for (const [out, src, frameworks] of swift) {
run(`swiftc -O -o ${out} ${src} ${frameworks}`);
// Swift's driver rejects `-arch` ("unknown argument"), so compile each arch as its own slice and
// merge them into a universal Mach-O with `lipo -create`. Without this, each helper is single-arch
// and Intel Macs fail with "bad CPU type in executable" (#634).
const arm = `${out}.arm64`;
const x86 = `${out}.x86_64`;
try {
run(`swiftc -O -target arm64-apple-${MACOS_DEPLOYMENT_TARGET} -o ${arm} ${src} ${frameworks}`);
run(`swiftc -O -target x86_64-apple-${MACOS_DEPLOYMENT_TARGET} -o ${x86} ${src} ${frameworks}`);
run(`lipo -create ${arm} ${x86} -output ${out}`);
} finally {
rmSync(arm, { force: true });
rmSync(x86, { force: true });
}
}

// Build native Node addon (native_helpers.node)
Expand Down
66 changes: 66 additions & 0 deletions scripts/test-native-universal-arch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env node

import test from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const nativeDir = path.join(root, 'dist', 'native');

// The 15 Swift helper binaries produced by the `swift` array in scripts/build-native.mjs.
// Each must be a universal Mach-O (arm64 + x86_64) so Intel Macs stop seeing
// "bad CPU type in executable" (SuperCmd issue #634).
//
// `native_helpers.node` (node-gyp addon, already arch-aware via `--arch=${arch}`) is deliberately
// EXCLUDED — it is arch-specific by design. whisper/parakeet/soulver binaries come
// from separate build pipelines and are also excluded.
const UNIVERSAL_SWIFT_BINARIES = [
'get-selected-text',
'color-picker',
'keyboard-lock',
'screen-ocr',
'snippet-expander',
'menu-item-search',
'emoji-trigger-monitor',
'hotkey-hold-monitor',
'speech-recognizer',
'microphone-access',
'input-monitoring-request',
'window-adjust',
'calendar-events',
'settings-coordinator',
'audio-capturer',
];

// CI runs `npm test` on Linux (.github/workflows/test.yml uses ubuntu-latest) where dist/native/
// is never built and `lipo` does not exist. Universal-arch is a macOS-only property, so skip (not
// fail) off macOS — mirrors the { skip } pattern in scripts/test-renderer-crash-recovery-live.mjs.
const shouldSkip = process.platform !== 'darwin';
const skipReason = 'macOS-only: native helpers are built by scripts/build-native.mjs and need lipo';

// One test per binary so node --test reports each failure individually (15 failures on stock,
// not just the first). A missing file FAILS (it means build:native did not run) — never skipped.
for (const name of UNIVERSAL_SWIFT_BINARIES) {
test(`${name} is a universal Mach-O (x86_64 + arm64)`, { skip: shouldSkip ? skipReason : false }, () => {
const binaryPath = path.join(nativeDir, name);
assert.ok(
existsSync(binaryPath),
`${name}: expected binary at ${binaryPath} — run \`npm run build:native\` first`
);
const stdout = execFileSync('lipo', ['-archs', binaryPath], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
});
const arches = stdout.trim().split(/\s+/);
assert.ok(
arches.includes('x86_64') && arches.includes('arm64'),
`${name} (${binaryPath}) must contain BOTH x86_64 and arm64; lipo -archs reports only [${arches.join(', ')}] — missing ${[
'x86_64',
'arm64',
].filter((a) => !arches.includes(a)).join(' and ')}`
);
});
}
34 changes: 26 additions & 8 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6743,14 +6743,32 @@ function ensureHyperKeyMonitorBinary(): string | null {
return null;
}
fs.mkdirSync(path.dirname(binaryPath), { recursive: true });
execFileSync('swiftc', [
'-O',
'-o', binaryPath,
sourcePath,
'-framework', 'CoreGraphics',
'-framework', 'AppKit',
'-framework', 'Carbon',
]);
// Swift's driver rejects `-arch` ("unknown argument"), so compile each arch slice with an
// explicit -target and merge into a universal Mach-O with lipo — mirroring
// scripts/build-native.mjs (keep MACOS_DEPLOYMENT_TARGET in sync with it). The host slice is
// mandatory (this binary runs on the host that builds it); the other slice is best-effort so a
// toolchain that can't cross-compile (e.g. an Intel Mac with only Command Line Tools) still
// yields a working host-arch binary instead of disabling HyperKey (#634).
const MACOS_DEPLOYMENT_TARGET = 'macos11';
const hostArch = process.arch === 'x64' ? 'x86_64' : 'arm64';
const otherArch = hostArch === 'arm64' ? 'x86_64' : 'arm64';
const frameworks = ['-framework', 'CoreGraphics', '-framework', 'AppKit', '-framework', 'Carbon'];
const hostSlice = `${binaryPath}.${hostArch}`;
const otherSlice = `${binaryPath}.${otherArch}`;
execFileSync('swiftc', ['-O', '-target', `${hostArch}-apple-${MACOS_DEPLOYMENT_TARGET}`, '-o', hostSlice, sourcePath, ...frameworks]);
try {
execFileSync('swiftc', ['-O', '-target', `${otherArch}-apple-${MACOS_DEPLOYMENT_TARGET}`, '-o', otherSlice, sourcePath, ...frameworks]);
execFileSync('lipo', ['-create', hostSlice, otherSlice, '-output', binaryPath]);
} catch {
// Cross-compile/merge unavailable: ship the host-arch slice (it runs on this host).
fs.renameSync(hostSlice, binaryPath);
} finally {
// Best-effort cleanup only — a transient rm error (EBUSY/EACCES) must never mask a successful
// build (which would wrongly disable HyperKey). fs.rmSync({force:true}) only swallows ENOENT.
for (const slice of [hostSlice, otherSlice]) {
try { fs.rmSync(slice, { force: true }); } catch { /* cleanup is best-effort */ }
}
}
return binaryPath;
} catch (error) {
console.warn('[HyperKey] Failed to compile hyper key monitor:', error);
Expand Down