hermes-agent/apps/desktop/scripts/build-hud-modifier-monitor.mjs
ethernet bb86a74b67 Merge origin/main into ethie/pm-clean
Conflicts:
- hermes_bootstrap.py: main calls install_never_free_environ() in the
  apply-on-import block right after the console fixes, where pm-clean runs its
  PM block (activation, relaunch, environ writes) instead of
  activate_durable_lazy_target(). It now runs ahead of the whole PM block,
  main's order, so the glibc < 2.41 guard is in before anything writes
  os.environ.
- tests/test_hermes_bootstrap.py: pm-clean dissolved the entry-point class and
  dropped its source-reading test; main's new
  test_library_imports_of_dual_use_entry_modules_stay_side_effect_free lands
  as a module-level function.
2026-09-23 22:39:57 -04:00

123 lines
4.8 KiB
JavaScript

#!/usr/bin/env node
// Host-built helper. Windows uses its in-box .NET Framework compiler; no SDK download.
import { execFileSync } from 'node:child_process'
import { chmodSync, existsSync, mkdirSync, renameSync, rmdirSync, 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), '..')
export function resolveWindowsFrameworkCompiler() {
const framework = resolve(process.env.SystemRoot || 'C:\\Windows', 'Microsoft.NET')
const compiler = ['Framework64', 'Framework']
.map(dir => resolve(framework, dir, 'v4.0.30319', 'csc.exe'))
.find(existsSync)
if (!compiler) throw new Error('The Windows HUD helper needs the .NET Framework included with Windows.')
return compiler
}
export function hudModifierBinaryRelativePath(platform = process.platform, arch = process.arch) {
return `native/${platform}-${platform === 'darwin' ? 'universal' : arch}/hud-modifier-monitor${platform === 'win32' ? '.exe' : ''}`
}
export function buildHudModifierMonitor({
source = resolve(root, '../..'),
distDir = resolve(source, 'apps/desktop/dist'),
platform = process.platform,
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.
if (platform !== process.platform || (platform === 'linux' && arch !== process.arch)) {
if (platform === 'win32') throw new Error('Build Windows packages on Windows so the HUD helper is included.')
console.warn(`[hud-modifier] ${platform}-${arch} needs a native build; modifier tap unavailable for this target`)
return null
}
if (!['darwin', 'linux', 'win32'].includes(platform)) return null
const output = resolve(distDir, hudModifierBinaryRelativePath(platform, arch))
const staging = `${output}.${process.pid}.tmp${platform === 'win32' ? '.exe' : ''}`
const nativeSource = name => resolve(source, 'apps/desktop/electron/native', name)
mkdirSync(dirname(output), { recursive: true })
try {
if (platform === 'darwin') {
execFileSync(
'xcrun',
[
...xcrunClangArgv(sysroot === undefined ? macosSysroot() : sysroot),
'-arch',
'arm64',
'-arch',
'x86_64',
'-mmacosx-version-min=11.0',
'-fobjc-arc',
'-fblocks',
'-O2',
'-Wall',
'-Wextra',
'-framework',
'Cocoa',
'-framework',
'CoreGraphics',
nativeSource('hud-modifier-monitor.m'),
'-o',
staging
],
{ stdio: 'pipe', timeout: 120_000 }
)
} else if (platform === 'win32') {
execFileSync(resolveWindowsFrameworkCompiler(), [
'/nologo', '/target:exe', '/platform:anycpu', '/optimize+', '/warnaserror+',
'/reference:System.Windows.Forms.dll', `/out:${staging}`,
nativeSource('hud-modifier-monitor-win.cs'), nativeSource('hud-modifier-gesture.cs')
], { stdio: 'pipe', timeout: 120_000 })
} else {
execFileSync(
process.env.CC || 'cc',
[
'-std=gnu11',
'-O2',
'-Wall',
'-Wextra',
nativeSource('hud-modifier-monitor-x11.c'),
'-o',
staging,
'-lX11', '-lXi'
],
{ stdio: 'pipe', timeout: 120_000 }
)
}
chmodSync(staging, 0o755)
renameSync(staging, output)
console.log(`built ${output}`)
return output
} catch (error) {
rmSync(output, { force: true }) // Never keep a stale helper after a failed rebuild.
// Packaged ASAR output omits empty directories. Leaving native/linux-* behind
// makes the packaged renderer differ from its compiler receipt, so source
// installs report a healthy desktop bundle as stale when X11 headers are
// unavailable. Remove only empty ancestors; stop at the product root.
for (let directory = dirname(output); directory !== resolve(distDir); directory = dirname(directory)) {
try {
rmdirSync(directory)
} catch {
break
}
}
if (platform !== 'linux') throw error
console.warn('[hud-modifier] unavailable: native build needs a C compiler, libx11-dev and libxi-dev; desktop packaging continues')
console.warn(String(error.stderr || error.message))
return null
} finally {
rmSync(staging, { force: true })
}
}
if (process.argv[1] && resolve(process.argv[1]) === script) {
const args = process.argv.slice(2)
if (args.length && (args.length !== 2 || args[0] !== '--out-dir'))
throw new Error('Usage: build-hud-modifier-monitor.mjs [--out-dir PATH]')
buildHudModifierMonitor(args.length ? { distDir: resolve(args[1]) } : {})
}