Skip to content

The agent that grows with you

fix(desktop): native helpers link against the SDK paired with the active toolchain

Konrad Czarski-Bonanatyc940027adef9
diff --git a/apps/desktop/electron/hud-modifier-native.test.ts b/apps/desktop/electron/hud-modifier-native.test.ts
index 1d2b1d92583..9defdb47a54 100644
--- a/apps/desktop/electron/hud-modifier-native.test.ts
+++ b/apps/desktop/electron/hud-modifier-native.test.ts
@@ -6,6 +6,8 @@ import { resolve } from 'node:path'
import { test } from 'vitest'
+import { macosSysroot, xcrunClangArgv } from '../scripts/macos-sysroot.mjs'
+
// hud-modifier-gesture.h is the clean-tap state machine the macOS (.m) and
// Linux XI2 (-x11.c) helpers both include. Compile its C contract and run it
// wherever a C toolchain ships with the runner: Linux (the JS CI lane) and
@@ -21,7 +23,7 @@ test.skipIf(process.platform === 'win32')(
const flags = ['-std=c11', '-Wall', '-Wextra', '-Werror', source, '-o', binary]
if (process.platform === 'darwin') {
- execFileSync('xcrun', ['--sdk', 'macosx', 'clang', ...flags])
+ execFileSync('xcrun', [...xcrunClangArgv(macosSysroot()), ...flags])
} else {
execFileSync('cc', flags)
}
diff --git a/apps/desktop/scripts/build-command-screenshot-monitor.mjs b/apps/desktop/scripts/build-command-screenshot-monitor.mjs
index a77a35a2c0d..3af2b017e53 100644
--- a/apps/desktop/scripts/build-command-screenshot-monitor.mjs
+++ b/apps/desktop/scripts/build-command-screenshot-monitor.mjs
@@ -4,28 +4,27 @@ import { execFileSync } from 'node:child_process'
import { chmodSync, mkdirSync, renameSync, rmSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
+import { macosSysroot, xcrunClangArgv } from './macos-sysroot.mjs'
const script = fileURLToPath(import.meta.url)
const root = resolve(dirname(script), '..')
-// `platform` is injectable so tests can exercise both branches without
-// redefining process.platform.
+// `platform` and `sysroot` are injectable so tests can exercise the branches
+// without redefining process.platform or shelling out to xcode-select.
export function buildCommandScreenshotMonitor({
distDir = resolve(root, 'dist'),
platform = process.platform,
+ sysroot,
} = {}) {
if (platform !== 'darwin') return null
const output = resolve(distDir, 'native/command-screenshot-monitor')
const staging = `${output}.${process.pid}.tmp`
mkdirSync(dirname(output), { recursive: true })
+ const sdk = sysroot === undefined ? macosSysroot() : sysroot
try {
- // Pin the SDK explicitly: a bare `xcrun clang` inherits the host default
- // SDK, which may be newer than the active linker (unknown-arch .tbd stubs
- // at link time, #113708). `--sdk macosx` names the same default SDK while
- // forcing the driver and linker to agree on it.
execFileSync('xcrun', [
- '--sdk', 'macosx',
- 'clang', '-arch', 'arm64', '-arch', 'x86_64', '-mmacosx-version-min=11.0',
+ ...xcrunClangArgv(sdk),
+ '-arch', 'arm64', '-arch', 'x86_64', '-mmacosx-version-min=11.0',
'-fobjc-arc', '-fblocks', '-O2', '-Wall', '-Wextra',
'-framework', 'Cocoa', '-framework', 'CoreGraphics',
resolve(root, 'electron/native/command-screenshot-monitor.m'), '-o', staging,
diff --git a/apps/desktop/scripts/build-command-screenshot-monitor.test.mjs b/apps/desktop/scripts/build-command-screenshot-monitor.test.mjs
index b83b608cfae..a72e12fbf0c 100644
--- a/apps/desktop/scripts/build-command-screenshot-monitor.test.mjs
+++ b/apps/desktop/scripts/build-command-screenshot-monitor.test.mjs
@@ -14,24 +14,43 @@ afterEach(() => {
vi.mocked(execFileSync).mockReset()
})
+function stageDistDir(prefix) {
+ const distDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix))
+ const staging = path.resolve(distDir, `native/command-screenshot-monitor.${process.pid}.tmp`)
+ fs.mkdirSync(path.dirname(staging), { recursive: true })
+ fs.writeFileSync(staging, 'staged')
+ return distDir
+}
+
// The helper shells out to xcrun; pin the argv contract (not the toolchain),
// so a non-macOS CI host still proves what the macOS build will run.
describe('buildCommandScreenshotMonitor argv', () => {
- it('names the macOS SDK explicitly so driver and linker agree', () => {
- const distDir = fs.mkdtempSync(path.join(os.tmpdir(), 'csm-argv-'))
- const staging = path.resolve(distDir, `native/command-screenshot-monitor.${process.pid}.tmp`)
- fs.mkdirSync(path.dirname(staging), { recursive: true })
- fs.writeFileSync(staging, 'staged')
+ it('links against the sysroot paired with the active toolchain', () => {
+ const distDir = stageDistDir('csm-argv-')
- const out = buildCommandScreenshotMonitor({ distDir, platform: 'darwin' })
+ const out = buildCommandScreenshotMonitor({
+ distDir,
+ platform: 'darwin',
+ sysroot: '/Developer/SDKs/MacOSX.sdk',
+ })
expect(execFileSync).toHaveBeenCalledOnce()
const [cmd, argv] = vi.mocked(execFileSync).mock.calls[0]
expect(cmd).toBe('xcrun')
+ expect(argv.slice(0, 3)).toEqual(['clang', '-isysroot', '/Developer/SDKs/MacOSX.sdk'])
+ expect(out).toBe(path.resolve(distDir, 'native/command-screenshot-monitor'))
+ fs.rmSync(distDir, { recursive: true, force: true })
+ })
+
+ it('falls back to the default SDK when no paired sysroot exists', () => {
+ const distDir = stageDistDir('csm-fallback-')
+
+ buildCommandScreenshotMonitor({ distDir, platform: 'darwin', sysroot: null })
+
+ const [, argv] = vi.mocked(execFileSync).mock.calls[0]
// `--sdk macosx` must precede the tool name: xcrun options after `clang`
// would be handed to clang instead of resolving the SDK.
expect(argv.slice(0, 3)).toEqual(['--sdk', 'macosx', 'clang'])
- expect(out).toBe(path.resolve(distDir, 'native/command-screenshot-monitor'))
fs.rmSync(distDir, { recursive: true, force: true })
})
diff --git a/apps/desktop/scripts/build-hud-modifier-monitor.mjs b/apps/desktop/scripts/build-hud-modifier-monitor.mjs
index c4386ff4fbd..ee0d22e37ce 100644
--- a/apps/desktop/scripts/build-hud-modifier-monitor.mjs
+++ b/apps/desktop/scripts/build-hud-modifier-monitor.mjs
@@ -4,6 +4,7 @@ import { execFileSync } from 'node:child_process'
import { chmodSync, existsSync, mkdirSync, renameSync, rmSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
+import { macosSysroot, xcrunClangArgv } from './macos-sysroot.mjs'
const script = fileURLToPath(import.meta.url)
const root = resolve(dirname(script), '..')
@@ -24,7 +25,8 @@ export function hudModifierBinaryRelativePath(platform = process.platform, arch
export function buildHudModifierMonitor({
distDir = resolve(root, 'dist'),
platform = process.platform,
- arch = process.arch
+ arch = process.arch,
+ sysroot
} = {}) {
// Cross-packaging must not ship a host binary under the target's name. The
// capability stays unavailable unless that target was built on its own host.
@@ -43,9 +45,7 @@ export function buildHudModifierMonitor({
execFileSync(
'xcrun',
[
- '--sdk',
- 'macosx',
- 'clang',
+ ...xcrunClangArgv(sysroot === undefined ? macosSysroot() : sysroot),
'-arch',
'arm64',
'-arch',
diff --git a/apps/desktop/scripts/macos-sysroot.mjs b/apps/desktop/scripts/macos-sysroot.mjs
new file mode 100644
index 00000000000..0c509b20e0e
--- /dev/null
+++ b/apps/desktop/scripts/macos-sysroot.mjs
@@ -0,0 +1,25 @@
+// Build-time only: resolves the macOS SDK the native helpers compile against.
+import { execFileSync } from 'node:child_process'
+import { existsSync } from 'node:fs'
+import { resolve } from 'node:path'
+
+// `xcrun --sdk macosx` resolves to the highest-versioned SDK installed, not the
+// one the active toolchain ships with. A host carrying an SDK newer than its
+// Command Line Tools then links against .tbd stubs whose architectures the
+// linker cannot parse (#113708). The MacOSX.sdk symlink under the active
+// developer dir names the paired SDK, so target that instead.
+export function macosSysroot(env = process.env) {
+ if (env.SDKROOT) return env.SDKROOT
+ const developerDir = execFileSync('xcode-select', ['-p'], { encoding: 'utf8' }).trim()
+ return [
+ resolve(developerDir, 'SDKs/MacOSX.sdk'),
+ resolve(developerDir, 'Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'),
+ ].find(existsSync) ?? null
+}
+
+// clang needs the SDK named before the linker runs; when no paired SDK exists
+// fall back to xcrun's own default, which at least keeps driver and linker in
+// agreement. `--sdk macosx` must precede the tool name or xcrun hands it to clang.
+export function xcrunClangArgv(sysroot) {
+ return sysroot ? ['clang', '-isysroot', sysroot] : ['--sdk', 'macosx', 'clang']
+}
diff --git a/apps/desktop/scripts/macos-sysroot.test.mjs b/apps/desktop/scripts/macos-sysroot.test.mjs
new file mode 100644
index 00000000000..55ece0ab278
--- /dev/null
+++ b/apps/desktop/scripts/macos-sysroot.test.mjs
@@ -0,0 +1,58 @@
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+vi.mock('node:child_process', () => ({
+ execFileSync: vi.fn(),
+}))
+
+const { execFileSync } = await import('node:child_process')
+const { macosSysroot, xcrunClangArgv } = await import('./macos-sysroot.mjs')
+
+afterEach(() => {
+ vi.mocked(execFileSync).mockReset()
+})
+
+describe('macosSysroot', () => {
+ it('prefers an explicit SDKROOT over the developer dir', () => {
+ expect(macosSysroot({ SDKROOT: '/pinned/MacOSX.sdk' })).toBe('/pinned/MacOSX.sdk')
+ expect(execFileSync).not.toHaveBeenCalled()
+ })
+
+ it('names the Command Line Tools SDK paired with the toolchain', () => {
+ const developerDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sysroot-clt-'))
+ fs.mkdirSync(path.join(developerDir, 'SDKs/MacOSX.sdk'), { recursive: true })
+ vi.mocked(execFileSync).mockReturnValue(`${developerDir}\n`)
+
+ expect(macosSysroot({})).toBe(path.join(developerDir, 'SDKs/MacOSX.sdk'))
+ fs.rmSync(developerDir, { recursive: true, force: true })
+ })
+
+ it('falls back to the Xcode platform SDK layout', () => {
+ const developerDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sysroot-xcode-'))
+ const sdk = 'Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'
+ fs.mkdirSync(path.join(developerDir, sdk), { recursive: true })
+ vi.mocked(execFileSync).mockReturnValue(`${developerDir}\n`)
+
+ expect(macosSysroot({})).toBe(path.join(developerDir, sdk))
+ fs.rmSync(developerDir, { recursive: true, force: true })
+ })
+
+ it('returns null when the developer dir ships no MacOSX.sdk', () => {
+ vi.mocked(execFileSync).mockReturnValue(`${os.tmpdir()}/sysroot-no-such-dir\n`)
+
+ expect(macosSysroot({})).toBeNull()
+ })
+})
+
+describe('xcrunClangArgv', () => {
+ it('pins the sysroot when one was resolved', () => {
+ expect(xcrunClangArgv('/sdk/MacOSX.sdk')).toEqual(['clang', '-isysroot', '/sdk/MacOSX.sdk'])
+ })
+
+ // xcrun options after `clang` would be handed to clang instead of resolving the SDK.
+ it('names the SDK before the tool when falling back', () => {
+ expect(xcrunClangArgv(null)).toEqual(['--sdk', 'macosx', 'clang'])
+ })
+})