| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| import { execFileSync } from 'node:child_process'; | |
| import { constants, existsSync } from 'node:fs'; | |
| import { release as osRelease } from 'node:os'; | |
| import { parseArgs } from 'node:util'; | |
| import { fileURLToPath, pathToFileURL } from 'node:url'; | |
| import { basename, dirname, join, resolve } from 'node:path'; | |
| import { | |
| chmod, | |
| copyFile, | |
| lstat, | |
| mkdir, | |
| mkdtemp, | |
| readFile, | |
| rename, | |
| rm, | |
| writeFile, | |
| } from 'node:fs/promises'; | |
| import { cargoBuild } from './cargo.mjs'; | |
| const root = fileURLToPath(new URL('../../', import.meta.url)); | |
| export function rustTarget(platform = process.platform, arch = process.arch) { | |
| const cpu = { x64: 'x86_64', arm64: 'aarch64' }[arch]; | |
| const os = { darwin: 'apple-darwin', linux: 'unknown-linux-gnu', win32: 'pc-windows-msvc' }[ | |
| platform | |
| ]; | |
| if (!cpu || !os) throw new Error(`Unsupported Maka target: ${platform}/${arch}`); | |
| return `${cpu}-${os}`; | |
| } | |
| function developmentFeatures(env, platform, release) { | |
| const mode = env.MAKA_CUA_BUILD_MODE ?? 'auto'; | |
| if (!['auto', 'native', 'bridge'].includes(mode)) | |
| throw new Error('MAKA_CUA_BUILD_MODE must be auto, native, or bridge'); | |
| const bridge = | |
| mode === 'bridge' || | |
| (mode === 'auto' && platform === 'linux' && release.toLowerCase().includes('microsoft')); | |
| return bridge ? ['--no-default-features'] : []; | |
| } | |
| const linuxMultiarchDirs = { | |
| 'x86_64-unknown-linux-gnu': '/usr/lib/x86_64-linux-gnu', | |
| 'aarch64-unknown-linux-gnu': '/usr/lib/aarch64-linux-gnu', | |
| }; | |
| // Zig treats glibc-versioned targets as cross-compilation and excludes host system | |
| // library directories; pass native dirs so GTK/X11/Vulkan link while Zig keeps its | |
| // bundled glibc 2.28 stubs for libc. Host .so files (e.g. Ubuntu's libvulkan.so) | |
| // internally reference newer host glibc symbols, so allow undefined symbols inside | |
| // external shared libraries while still requiring our own binary to satisfy 2.28. | |
| export function linuxSystemLibraryRustflags(triple, env = {}, exists = existsSync) { | |
| const multiarch = linuxMultiarchDirs[triple]; | |
| const candidates = | |
| multiarch && exists(multiarch) ? [multiarch, '/usr/lib'] : ['/usr/lib64', '/usr/lib']; | |
| const dirs = candidates.filter((dir) => exists(dir)); | |
| if (!dirs.length) return {}; | |
| const key = `CARGO_TARGET_${triple.toUpperCase().replace(/-/g, '_')}_RUSTFLAGS`; | |
| const existing = env[key]?.trim() ?? ''; | |
| const hasFlag = (flag) => | |
| new RegExp(`(?:^|\\s)${flag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:\\s|$)`).test(existing); | |
| const desired = [ | |
| ...dirs.map((dir) => `-L native=${dir}`), | |
| '-C link-arg=-Wl,--allow-shlib-undefined', | |
| ]; | |
| const added = desired.filter((flag) => !hasFlag(flag)); | |
| if (!added.length) return {}; | |
| return { [key]: existing ? `${existing} ${added.join(' ')}` : added.join(' ') }; | |
| } | |
| export async function compileCli({ | |
| release = false, | |
| profile, | |
| target, | |
| timings = false, | |
| desktop = true, | |
| repositoryRoot = root, | |
| env = process.env, | |
| platform = process.platform, | |
| arch = process.arch, | |
| osRelease: releaseName = osRelease(), | |
| } = {}) { | |
| const host = rustTarget(platform, arch); | |
| const triple = target ?? host; | |
| const isRelease = release || (profile !== undefined && profile !== 'dev'); | |
| const linuxRelease = isRelease && target !== undefined && triple.includes('-linux-'); | |
| if (linuxRelease && !/^(x86_64|aarch64)-unknown-linux-gnu$/u.test(triple)) { | |
| throw new Error('Linux releases require an x86_64 or aarch64 GNU target'); | |
| } | |
| // Explicit Linux release targets use the distribution's glibc floor. | |
| // Cargo writes the output under the Rust triple, without the glibc suffix. | |
| const outputTarget = linuxRelease ? triple : target; | |
| const args = [ | |
| linuxRelease ? 'zigbuild' : 'build', | |
| '--locked', | |
| '-p', | |
| 'maka-cli', | |
| ...(desktop ? ['-p', 'maka-desktop-shell'] : []), | |
| ...(desktop && triple.includes('-apple-darwin') ? ['-p', 'maka-host-shell'] : []), | |
| '-p', | |
| 'maka-python-runtime', | |
| ]; | |
| if (!isRelease && triple === host) args.push(...developmentFeatures(env, platform, releaseName)); | |
| if (profile) args.push('--profile', profile); | |
| else if (release) args.push('--release'); | |
| if (timings) args.push('--timings'); | |
| if (outputTarget) args.push('--target', linuxRelease ? `${triple}.2.28` : outputTarget); | |
| // cargo-config2 in zigbuild misreads Cargo's empty wrapper configuration as a program. | |
| const buildEnv = linuxRelease | |
| ? { | |
| ...env, | |
| RUSTC_WRAPPER: env.RUSTC_WRAPPER ?? '', | |
| RUSTC_WORKSPACE_WRAPPER: env.RUSTC_WORKSPACE_WRAPPER ?? '', | |
| ...(triple === host ? linuxSystemLibraryRustflags(triple, env) : {}), | |
| } | |
| : env; | |
| const result = await cargoBuild(args, { cwd: repositoryRoot, env: buildEnv }); | |
| const artifact = result.executables.get('maka'); | |
| const binary = artifact?.path; | |
| if (result.code === 0 && !result.signal && !binary) | |
| throw new Error('Cargo did not report the Maka executable'); | |
| if (result.code === 0 && !result.signal && !artifact.outDir) | |
| throw new Error('Cargo did not report the Maka build output directory'); | |
| if (result.code === 0 && !result.signal && desktop && triple.includes('-apple-darwin')) { | |
| const directory = dirname(binary); | |
| await stageDesktopBundle( | |
| repositoryRoot, | |
| join(directory, 'maka-desktop-shell'), | |
| join(directory, 'Maka.app'), | |
| ); | |
| } | |
| return { | |
| code: result.code, | |
| signal: result.signal, | |
| binary, | |
| bundleInputs: artifact?.outDir && join(artifact.outDir, 'product', 'bundle-inputs.json'), | |
| }; | |
| } | |
| export async function buildCli(options = {}) { | |
| const { code, signal, binary, bundleInputs } = await compileCli(options); | |
| if (code !== 0 || signal) throw new Error(`Maka build failed: ${signal ?? code}`); | |
| return { binary, bundleInputs }; | |
| } | |
| /** | |
| * The desktop shell's path from the directory holding `maka`. macOS shows a | |
| * process with the name and icon of the bundle its executable is in, so there | |
| * the shell runs from inside `Maka.app`. | |
| */ | |
| export function desktopShell(platform) { | |
| if (platform === 'darwin') return join('Maka.app', 'Contents', 'MacOS', 'maka-desktop-shell'); | |
| return platform === 'win32' ? 'maka-desktop-shell.exe' : 'maka-desktop-shell'; | |
| } | |
| /** The desktop's identity; the shell gives its window the same application id. */ | |
| const desktopApp = { | |
| id: 'org.apache.maka.desktop', | |
| name: 'Maka', | |
| extra: [ | |
| [ | |
| 'NSMicrophoneUsageDescription', | |
| 'Maka uses the microphone to transcribe speech into your message draft.', | |
| ], | |
| ], | |
| }; | |
| /** | |
| * The identity the Host runs under on macOS, so privacy grants belong to it | |
| * (products/maka/cli/src/deployment/host_identity.rs). macOS ties a grant to | |
| * the bytes of its executable, and an installed helper is replaced only by a | |
| * higher version: bump `version` only when the helper must change. Every | |
| * bump makes users allow "Maka Host" again. | |
| */ | |
| const hostApp = { id: 'org.apache.maka.host', name: 'Maka Host', version: '2' }; | |
| /** `[workspace.package] version`, which every Maka crate takes. */ | |
| export function workspaceVersion(manifest) { | |
| const section = /^\[workspace\.package\]$([^]*?)(?=^\[|(?![^]))/mu.exec(manifest)?.[1]; | |
| const version = section && /^version\s*=\s*"([^"]+)"\s*$/mu.exec(section)?.[1]; | |
| if (!version) throw new Error('Cargo.toml has no [workspace.package] version'); | |
| return version; | |
| } | |
| /** | |
| * Stage `Maka.app`: the shell, which carries the workbench, the Info.plist | |
| * and icon macOS reads, and the Host's helper `Maka Host.app`, built beside | |
| * the shell. | |
| */ | |
| export async function stageDesktopBundle( | |
| repositoryRoot, | |
| shell, | |
| destination, | |
| resources = [], | |
| { executables = [], version: versionOverride } = {}, | |
| ) { | |
| const version = | |
| versionOverride ?? workspaceVersion(await readFile(join(repositoryRoot, 'Cargo.toml'), 'utf8')); | |
| const icon = icns(await readFile(join(repositoryRoot, 'assets/logo.png'))); | |
| const metadata = await lstat(shell); | |
| if (!metadata.isFile() || metadata.size === 0) { | |
| throw new Error(`Desktop shell is not a regular file: ${shell}`); | |
| } | |
| // Replace the previous bundle only once the new one is complete. | |
| const stage = await mkdtemp(join(dirname(destination), '.Maka.app-')); | |
| try { | |
| const contents = join(stage, 'Contents'); | |
| await mkdir(join(contents, 'Resources'), { recursive: true }); | |
| await writeFile(join(contents, 'Resources', 'Maka.icns'), icon); | |
| await writeFile(join(contents, 'Info.plist'), infoPlist({ ...desktopApp, version })); | |
| await mkdir(join(contents, 'MacOS')); | |
| const executable = join(contents, 'MacOS', 'maka-desktop-shell'); | |
| for (const resource of resources) { | |
| await copyFile(resource, join(contents, 'Resources', basename(resource))); | |
| } | |
| await copyFile(shell, executable, constants.COPYFILE_FICLONE); | |
| await chmod(executable, 0o755); | |
| const entitlements = join(stage, '..', `.maka-jit-${process.pid}-${Date.now()}.entitlements`); | |
| await writeFile(entitlements, jitEntitlementsPlist(), 'utf8'); | |
| try { | |
| for (const extra of executables) { | |
| const target = join(contents, 'MacOS', basename(extra)); | |
| await copyFile(extra, target, constants.COPYFILE_FICLONE); | |
| await chmod(target, 0o755); | |
| signMachO(target, `${desktopApp.id}.${basename(extra)}`, { | |
| entitlements: basename(extra) === 'maka' ? entitlements : undefined, | |
| }); | |
| } | |
| await stageHostBundle(join(dirname(shell), 'maka-host-shell'), contents); | |
| await chmod(stage, 0o755); | |
| signBundle(stage, desktopApp.id, { entitlements }); | |
| } finally { | |
| await rm(entitlements, { force: true }); | |
| } | |
| await rm(destination, { recursive: true, force: true }); | |
| await rename(stage, destination); | |
| } catch (error) { | |
| await rm(stage, { recursive: true, force: true }); | |
| throw error; | |
| } | |
| } | |
| /** | |
| * `Contents/Helpers/Maka Host.app` has no build-specific values and carries | |
| * a complete bundle signature: the linker signature does not bind its plist | |
| * or seal its resources, and carries a build-derived identifier. | |
| */ | |
| async function stageHostBundle(hostShell, contents) { | |
| const metadata = await lstat(hostShell); | |
| if (!metadata.isFile() || metadata.size === 0) { | |
| throw new Error(`Host shell is not a regular file: ${hostShell}`); | |
| } | |
| const host = join(contents, 'Helpers', `${hostApp.name}.app`, 'Contents'); | |
| await mkdir(join(host, 'MacOS'), { recursive: true }); | |
| await writeFile( | |
| join(host, 'Info.plist'), | |
| infoPlist({ | |
| ...hostApp, | |
| executable: 'maka-host-shell', | |
| icon: undefined, | |
| extra: [ | |
| ['LSUIElement', true], | |
| [ | |
| 'NSAccessibilityUsageDescription', | |
| 'Maka Host controls apps on your behalf when you use Computer Use.', | |
| ], | |
| ], | |
| }), | |
| ); | |
| const executable = join(host, 'MacOS', 'maka-host-shell'); | |
| await copyFile(hostShell, executable, constants.COPYFILE_FICLONE); | |
| await chmod(executable, 0o755); | |
| signBundle(dirname(host), hostApp.id); | |
| } | |
| function jitEntitlementsPlist() { | |
| return [ | |
| '<?xml version="1.0" encoding="UTF-8"?>', | |
| '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">', | |
| '<plist version="1.0">', | |
| '<dict>', | |
| ' <key>com.apple.security.cs.allow-jit</key>', | |
| ' <true/>', | |
| '</dict>', | |
| '</plist>', | |
| '', | |
| ].join('\n'); | |
| } | |
| function codesignArgs(identity, id, { entitlements } = {}) { | |
| // Real Developer ID signatures need hardened runtime and a secure timestamp for notarization; | |
| // V8 on Apple Silicon needs MAP_JIT (allow-jit) under hardened runtime. | |
| const hardened = identity !== '-' && /Developer ID/u.test(identity); | |
| return [ | |
| '--force', | |
| '--sign', | |
| identity, | |
| '--identifier', | |
| id, | |
| ...(hardened | |
| ? [ | |
| '--options', | |
| 'runtime', | |
| '--timestamp', | |
| ...(entitlements ? ['--entitlements', entitlements] : []), | |
| ] | |
| : ['--timestamp=none']), | |
| ]; | |
| } | |
| function signMachO(binary, id, options = {}) { | |
| const identity = process.env.MAKA_CODESIGN_IDENTITY || '-'; | |
| execFileSync('/usr/bin/codesign', [...codesignArgs(identity, id, options), binary], { | |
| stdio: 'pipe', | |
| }); | |
| } | |
| // Ad-hoc signatures change with every build, so macOS forgets privacy grants; | |
| // a stable local identity keeps them. | |
| function signBundle(bundle, id, options = {}) { | |
| const identity = process.env.MAKA_CODESIGN_IDENTITY || '-'; | |
| execFileSync('/usr/bin/codesign', [...codesignArgs(identity, id, options), bundle], { | |
| stdio: 'pipe', | |
| }); | |
| execFileSync( | |
| '/usr/bin/codesign', | |
| ['--verify', '--strict', '--test-requirement', `=identifier "${id}"`, bundle], | |
| { stdio: 'pipe' }, | |
| ); | |
| } | |
| function infoPlist({ | |
| id, | |
| name, | |
| version, | |
| executable = 'maka-desktop-shell', | |
| icon = 'Maka', | |
| extra = [], | |
| }) { | |
| // `CFBundleVersion` takes up to three integers, so a pre-release or build | |
| // suffix stays in the short version only. | |
| const build = /^[0-9]+\.[0-9]+\.[0-9]+/u.exec(version)?.[0] ?? version; | |
| const escape = (value) => | |
| value | |
| .replaceAll('&', '&') | |
| .replaceAll('<', '<') | |
| .replaceAll('>', '>') | |
| .replaceAll('"', '"'); | |
| return [ | |
| '<?xml version="1.0" encoding="UTF-8"?>', | |
| '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">', | |
| '<plist version="1.0">', | |
| '<dict>', | |
| ...[ | |
| ['CFBundleDevelopmentRegion', 'en'], | |
| ['CFBundleDisplayName', name], | |
| ['CFBundleExecutable', executable], | |
| ...(icon ? [['CFBundleIconFile', icon]] : []), | |
| ['CFBundleIdentifier', id], | |
| ['CFBundleInfoDictionaryVersion', '6.0'], | |
| ['CFBundleName', name], | |
| ['CFBundlePackageType', 'APPL'], | |
| ['CFBundleShortVersionString', version], | |
| ['CFBundleVersion', build], | |
| ['LSMinimumSystemVersion', '11.0'], | |
| ].map(([key, value]) => ` <key>${key}</key>\n <string>${escape(value)}</string>`), | |
| ' <key>NSHighResolutionCapable</key>', | |
| ' <true/>', | |
| ...extra.map(([key, value]) => | |
| value === true | |
| ? ` <key>${key}</key>\n <true/>` | |
| : ` <key>${key}</key>\n <string>${escape(value)}</string>`, | |
| ), | |
| '</dict>', | |
| '</plist>', | |
| '', | |
| ].join('\n'); | |
| } | |
| /** One 1024-pixel PNG entry; macOS scales it to the smaller sizes. */ | |
| function icns(png) { | |
| if ( | |
| png.length < 24 || | |
| png.toString('latin1', 0, 8) !== '\x89PNG\r\n\x1a\n' || | |
| png.toString('latin1', 12, 16) !== 'IHDR' || | |
| png.readUInt32BE(16) !== 1024 || | |
| png.readUInt32BE(20) !== 1024 | |
| ) { | |
| throw new Error('assets/logo.png must be a 1024x1024 PNG'); | |
| } | |
| const header = Buffer.alloc(16); | |
| header.write('icns', 0, 'latin1'); | |
| header.writeUInt32BE(header.length + png.length, 4); | |
| header.write('ic10', 8, 'latin1'); | |
| header.writeUInt32BE(8 + png.length, 12); | |
| return Buffer.concat([header, png]); | |
| } | |
| if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { | |
| const { values } = parseArgs({ | |
| options: { | |
| release: { type: 'boolean' }, | |
| profile: { type: 'string' }, | |
| target: { type: 'string' }, | |
| timings: { type: 'boolean' }, | |
| }, | |
| }); | |
| console.log((await buildCli(values)).binary); | |
| } |