|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { spawnSync } = require("child_process"); |
| 4 | +const crypto = require("crypto"); |
| 5 | +const fs = require("fs"); |
| 6 | +const path = require("path"); |
| 7 | + |
| 8 | +const isMac = process.platform === "darwin"; |
| 9 | +if (!isMac) { |
| 10 | + process.exit(0); |
| 11 | +} |
| 12 | + |
| 13 | +// Support cross-compilation via --arch flag or TARGET_ARCH env var |
| 14 | +const archIndex = process.argv.indexOf("--arch"); |
| 15 | +const targetArch = |
| 16 | + (archIndex !== -1 && process.argv[archIndex + 1]) || process.env.TARGET_ARCH || process.arch; |
| 17 | + |
| 18 | +const ARCH_TO_TARGET = { |
| 19 | + arm64: "arm64-apple-macosx11.0", |
| 20 | + x64: "x86_64-apple-macosx10.15", |
| 21 | +}; |
| 22 | +const swiftTarget = ARCH_TO_TARGET[targetArch]; |
| 23 | +if (!swiftTarget) { |
| 24 | + console.error(`[clipboard-marker] Unsupported architecture: ${targetArch}`); |
| 25 | + process.exit(1); |
| 26 | +} |
| 27 | + |
| 28 | +const projectRoot = path.resolve(__dirname, ".."); |
| 29 | +const swiftSource = path.join(projectRoot, "resources", "macos-clipboard-marker.swift"); |
| 30 | +const outputDir = path.join(projectRoot, "resources", "bin"); |
| 31 | +const outputBinary = path.join(outputDir, "macos-clipboard-marker"); |
| 32 | +const hashFile = path.join(outputDir, `.macos-clipboard-marker.${targetArch}.hash`); |
| 33 | +const moduleCacheDir = path.join(outputDir, ".swift-module-cache"); |
| 34 | + |
| 35 | +const ARCH_CPU_TYPE = { |
| 36 | + arm64: 0x0100000c, // CPU_TYPE_ARM64 |
| 37 | + x64: 0x01000007, // CPU_TYPE_X86_64 |
| 38 | +}; |
| 39 | + |
| 40 | +function log(message) { |
| 41 | + console.log(`[clipboard-marker] ${message}`); |
| 42 | +} |
| 43 | + |
| 44 | +function ensureDir(dirPath) { |
| 45 | + if (!fs.existsSync(dirPath)) { |
| 46 | + fs.mkdirSync(dirPath, { recursive: true }); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function verifyBinaryArch(binaryPath, expectedArch) { |
| 51 | + try { |
| 52 | + const fd = fs.openSync(binaryPath, "r"); |
| 53 | + const header = Buffer.alloc(8); |
| 54 | + fs.readSync(fd, header, 0, 8, 0); |
| 55 | + fs.closeSync(fd); |
| 56 | + |
| 57 | + const magic = header.readUInt32LE(0); |
| 58 | + if (magic !== 0xfeedfacf) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + const cpuType = header.readInt32LE(4); |
| 62 | + return cpuType === ARCH_CPU_TYPE[expectedArch]; |
| 63 | + } catch { |
| 64 | + return false; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +if (!fs.existsSync(swiftSource)) { |
| 69 | + console.error(`[clipboard-marker] Swift source not found at ${swiftSource}`); |
| 70 | + process.exit(1); |
| 71 | +} |
| 72 | + |
| 73 | +ensureDir(outputDir); |
| 74 | +ensureDir(moduleCacheDir); |
| 75 | + |
| 76 | +let needsBuild = true; |
| 77 | +if (fs.existsSync(outputBinary)) { |
| 78 | + if (!verifyBinaryArch(outputBinary, targetArch)) { |
| 79 | + log(`Existing binary is wrong architecture (expected ${targetArch}), rebuild needed`); |
| 80 | + needsBuild = true; |
| 81 | + } else { |
| 82 | + try { |
| 83 | + const binaryStat = fs.statSync(outputBinary); |
| 84 | + const sourceStat = fs.statSync(swiftSource); |
| 85 | + if (binaryStat.mtimeMs >= sourceStat.mtimeMs) { |
| 86 | + needsBuild = false; |
| 87 | + } |
| 88 | + } catch { |
| 89 | + needsBuild = true; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +if (!needsBuild && fs.existsSync(outputBinary)) { |
| 95 | + try { |
| 96 | + const sourceContent = fs.readFileSync(swiftSource, "utf8"); |
| 97 | + const currentHash = crypto.createHash("sha256").update(sourceContent).digest("hex"); |
| 98 | + |
| 99 | + if (fs.existsSync(hashFile)) { |
| 100 | + const savedHash = fs.readFileSync(hashFile, "utf8").trim(); |
| 101 | + if (savedHash !== currentHash) { |
| 102 | + log("Source hash changed, rebuild needed"); |
| 103 | + needsBuild = true; |
| 104 | + } |
| 105 | + } else { |
| 106 | + log(`No hash file for ${targetArch}, rebuild needed`); |
| 107 | + needsBuild = true; |
| 108 | + } |
| 109 | + } catch (err) { |
| 110 | + log(`Hash check failed: ${err.message}, forcing rebuild`); |
| 111 | + needsBuild = true; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +if (!needsBuild) { |
| 116 | + process.exit(0); |
| 117 | +} |
| 118 | + |
| 119 | +function attemptCompile(command, args) { |
| 120 | + log(`Compiling with ${[command, ...args].join(" ")}`); |
| 121 | + return spawnSync(command, args, { |
| 122 | + stdio: "inherit", |
| 123 | + env: { |
| 124 | + ...process.env, |
| 125 | + SWIFT_MODULE_CACHE_PATH: moduleCacheDir, |
| 126 | + }, |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +const compileArgs = [ |
| 131 | + swiftSource, |
| 132 | + "-O", |
| 133 | + "-target", |
| 134 | + swiftTarget, |
| 135 | + "-module-cache-path", |
| 136 | + moduleCacheDir, |
| 137 | + "-o", |
| 138 | + outputBinary, |
| 139 | +]; |
| 140 | + |
| 141 | +let result = attemptCompile("xcrun", ["swiftc", ...compileArgs]); |
| 142 | + |
| 143 | +if (result.status !== 0) { |
| 144 | + result = attemptCompile("swiftc", compileArgs); |
| 145 | +} |
| 146 | + |
| 147 | +if (result.status !== 0) { |
| 148 | + console.error("[clipboard-marker] Failed to compile macOS clipboard-marker binary."); |
| 149 | + process.exit(result.status ?? 1); |
| 150 | +} |
| 151 | + |
| 152 | +try { |
| 153 | + fs.chmodSync(outputBinary, 0o755); |
| 154 | +} catch (error) { |
| 155 | + console.warn(`[clipboard-marker] Unable to set executable permissions: ${error.message}`); |
| 156 | +} |
| 157 | + |
| 158 | +if (!verifyBinaryArch(outputBinary, targetArch)) { |
| 159 | + console.error( |
| 160 | + `[clipboard-marker] FATAL: Compiled binary architecture does not match target (${targetArch}). ` + |
| 161 | + `This can happen when cross-compiling without setting TARGET_ARCH env var.` |
| 162 | + ); |
| 163 | + process.exit(1); |
| 164 | +} |
| 165 | + |
| 166 | +try { |
| 167 | + const sourceContent = fs.readFileSync(swiftSource, "utf8"); |
| 168 | + const hash = crypto.createHash("sha256").update(sourceContent).digest("hex"); |
| 169 | + fs.writeFileSync(hashFile, hash); |
| 170 | +} catch (err) { |
| 171 | + log(`Warning: Could not save source hash: ${err.message}`); |
| 172 | +} |
| 173 | + |
| 174 | +log(`Successfully built macOS clipboard-marker binary (${targetArch}).`); |
0 commit comments