build-exe-for-python-sdk.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /**
  2. * Build the dsh executables and development Node carrier for the Python runtime wheel. The fixed
  3. * `@yao-pkg/pkg --sea` route, deploy flags, and artifact layout are owned by
  4. * .agents/notes/implemented/architecture/2026-07-10-single-file-executable-sdk-runtime-distribution.md.
  5. * The staged closure is symlink-free, and whole-tree assets cover Cordis's
  6. * runtime imports that pkg cannot discover statically.
  7. */
  8. import { spawn } from 'node:child_process'
  9. import { existsSync, statSync } from 'node:fs'
  10. import { chmod, copyFile, cp, lstat, mkdir, readFile, readdir, realpath, rm, writeFile } from 'node:fs/promises'
  11. import { basename, dirname, extname, join, resolve, sep } from 'node:path'
  12. import { parseArgs } from 'node:util'
  13. import { resolveLinuxNodePtyAddon, resolveWindowsNodePtyAddons } from './build-exe-for-python-sdk-native-pty.ts'
  14. const root = resolve(import.meta.dirname, '..')
  15. /** The closure manifest whose dependencies define the executable. */
  16. const DEPLOY_ROOT_PACKAGE = 'dsh-python-runtime-closure'
  17. /** The sole application launcher inside the deployed closure. */
  18. const ENTRY_BIN = 'node_modules/@deepseek-ai/dsh/lib/bin.js'
  19. /** Python-visible executable basename. */
  20. const OUTPUT_BASENAME = 'deepseek-harness-sdk-runtime'
  21. /** Default Node major; SEA mode requires at least Node 22. */
  22. const DEFAULT_NODE_RANGE = 'node24'
  23. const OUT_DIR = 'dist-exe'
  24. /** Python package destination; created when absent. */
  25. const PYTHON_RUNTIME_DIR = 'python/sdk-runtime/src/deepseek_harness_runtime/runtime'
  26. /** The deployed closure doubles as the node-mode carrier. */
  27. const PYTHON_NODE_SUBDIR = 'node'
  28. /** Legacy deploy may hoist peer-specialized workspace packages back here. */
  29. const DEPLOY_SOURCE_NODE_MODULES = 'python/sdk-runtime/node_modules'
  30. /** Documentation excluded from the generated runtime directory. */
  31. const DEPLOY_ONLY_DOCS = ['README.md', 'README.zh.md', 'README.i18n.yaml']
  32. /**
  33. * Whole-tree assets cover Cordis's runtime bare-package imports, which pkg's
  34. * static analysis cannot see. Package manifests are explicit because bare-name
  35. * resolution depends on them.
  36. */
  37. const ASSET_GLOBS = [
  38. 'package.json',
  39. 'node_modules/**/*.js',
  40. 'node_modules/**/*.cjs',
  41. 'node_modules/**/*.mjs',
  42. 'node_modules/**/package.json',
  43. 'node_modules/**/*.json',
  44. // Package-owned Markdown includes runtime skill instructions and badge content.
  45. 'node_modules/**/*.md',
  46. 'node_modules/**/*.dylib',
  47. 'node_modules/**/*.dll',
  48. 'node_modules/**/*.node',
  49. 'node_modules/**/*.so',
  50. 'node_modules/**/*.so.*',
  51. 'node_modules/**/*.wasm',
  52. 'node_modules/**/*.yaml',
  53. 'node_modules/**/*.yml',
  54. // web-app builds this path dynamically, so pkg cannot discover the static frontend.
  55. 'node_modules/@deepseek-ai/dsh-web-frontend/dist/**/*',
  56. // skill-badge resolves both Markdown and image resources through import.meta.url.
  57. 'node_modules/@deepseek-ai/dsh-skill-badge/assets/**/*',
  58. ]
  59. const PLATFORMS = ['linux', 'macos', 'win'] as const
  60. const ARCHES = ['x64', 'arm64'] as const
  61. type Platform = (typeof PLATFORMS)[number]
  62. type Arch = (typeof ARCHES)[number]
  63. function isPlatform(value: string): value is Platform {
  64. return (PLATFORMS as readonly string[]).includes(value)
  65. }
  66. function isArch(value: string): value is Arch {
  67. return (ARCHES as readonly string[]).includes(value)
  68. }
  69. /**
  70. * A parsed pkg target triple, constructed from `--targets` or the host.
  71. */
  72. class Target {
  73. private constructor(
  74. /** pkg Node range (`node<major>`). */
  75. readonly nodeRange: string,
  76. /** pkg platform tag. */
  77. readonly platform: Platform,
  78. /** pkg CPU tag. */
  79. readonly arch: Arch,
  80. ) {}
  81. /** The pkg `--targets` spec string `<nodeRange>-<platform>-<arch>`. */
  82. get spec(): string {
  83. return `${this.nodeRange}-${this.platform}-${this.arch}`
  84. }
  85. /**
  86. * Parse one target spec, rejecting malformed triples and unsupported platform or architecture.
  87. * @param spec - the raw triple, e.g. `node24-linux-x64`.
  88. * @returns the parsed target.
  89. */
  90. static parse(spec: string): Target {
  91. const parts = spec.split('-')
  92. const [nodeRange, platform, arch] = parts
  93. if (parts.length !== 3 || nodeRange === undefined || platform === undefined || arch === undefined) {
  94. throw new Error(`build-exe-for-python-sdk: target ${JSON.stringify(spec)} must be <nodeRange>-<platform>-<arch>, e.g. node24-linux-x64.`)
  95. }
  96. if (!/^node\d+$/.test(nodeRange)) {
  97. throw new Error(`build-exe-for-python-sdk: target ${JSON.stringify(spec)}: node range must look like node24, got ${JSON.stringify(nodeRange)}.`)
  98. }
  99. if (!isPlatform(platform)) {
  100. throw new Error(`build-exe-for-python-sdk: target ${JSON.stringify(spec)}: platform must be one of ${PLATFORMS.join(', ')}, got ${JSON.stringify(platform)}.`)
  101. }
  102. if (!isArch(arch)) {
  103. throw new Error(`build-exe-for-python-sdk: target ${JSON.stringify(spec)}: arch must be one of ${ARCHES.join(', ')}, got ${JSON.stringify(arch)}.`)
  104. }
  105. if (platform === 'win' && arch !== 'x64') {
  106. throw new Error(`build-exe-for-python-sdk: target ${JSON.stringify(spec)}: Windows supports x64 only.`)
  107. }
  108. return new Target(nodeRange, platform, arch)
  109. }
  110. /**
  111. * Resolve the host-platform default on Node 24.
  112. * @returns the host target; throws on an unsupported host platform or arch.
  113. */
  114. static host(): Target {
  115. const platform = process.platform === 'darwin'
  116. ? 'macos'
  117. : process.platform === 'linux'
  118. ? 'linux'
  119. : process.platform === 'win32'
  120. ? 'win'
  121. : undefined
  122. if (platform === undefined) {
  123. throw new Error(`build-exe-for-python-sdk: unsupported host platform ${process.platform}; pass --targets explicitly.`)
  124. }
  125. const arch = process.arch === 'x64' || process.arch === 'arm64' ? process.arch : undefined
  126. if (arch === undefined) {
  127. throw new Error(`build-exe-for-python-sdk: unsupported host arch ${process.arch}; pass --targets explicitly.`)
  128. }
  129. if (platform === 'win' && arch !== 'x64') {
  130. throw new Error('build-exe-for-python-sdk: Windows supports x64 only; use an x64 Node process.')
  131. }
  132. return new Target(DEFAULT_NODE_RANGE, platform, arch)
  133. }
  134. }
  135. /**
  136. * Validated CLI configuration; construction owns help and parse-error exits.
  137. */
  138. class BuildCli {
  139. private constructor(
  140. /** Build targets; defaults to the host platform only. */
  141. readonly targets: readonly Target[],
  142. /** Skip step 1 (`pnpm run build`); lib/ artifacts must already exist. */
  143. readonly skipBuild: boolean,
  144. /** Print every command and config patch instead of executing. */
  145. readonly dryRun: boolean,
  146. ) {}
  147. /**
  148. * Parse argv. Help exits 0; malformed flags exit 1; invalid or colliding
  149. * targets throw.
  150. * @param argv - the raw arguments (`process.argv.slice(2)`).
  151. * @returns the parsed, validated configuration.
  152. */
  153. static parse(argv: string[]): BuildCli {
  154. let values: ReturnType<typeof BuildCli.parseRaw>
  155. try {
  156. values = BuildCli.parseRaw(argv)
  157. } catch (error) {
  158. console.error(`build-exe-for-python-sdk: ${error instanceof Error ? error.message : String(error)}\n`)
  159. console.error(BuildCli.usage())
  160. process.exit(1)
  161. }
  162. if (values.help) {
  163. console.log(BuildCli.usage())
  164. process.exit(0)
  165. }
  166. const targets = values.targets === undefined
  167. ? [Target.host()]
  168. : values.targets.split(',').map(part => part.trim()).filter(part => part !== '').map(spec => Target.parse(spec))
  169. if (targets.length === 0) throw new Error('build-exe-for-python-sdk: --targets is empty.')
  170. const seen = new Set<string>()
  171. for (const target of targets) {
  172. const key = `${target.platform}-${target.arch}`
  173. if (seen.has(key)) {
  174. throw new Error(`build-exe-for-python-sdk: duplicate platform-arch ${key} in --targets; canonical product names would collide.`)
  175. }
  176. seen.add(key)
  177. }
  178. return new BuildCli(targets, values['skip-build'], values['dry-run'])
  179. }
  180. private static parseRaw(argv: string[]) {
  181. return parseArgs({
  182. args: argv,
  183. options: {
  184. 'targets': { type: 'string' },
  185. 'skip-build': { type: 'boolean', default: false },
  186. 'dry-run': { type: 'boolean', default: false },
  187. 'help': { type: 'boolean', default: false },
  188. },
  189. }).values
  190. }
  191. private static usage(): string {
  192. return [
  193. 'Usage: pnpm exec tsx scripts/build-exe-for-python-sdk.ts [flags]',
  194. '',
  195. ' --targets=<t1,t2,...> pkg targets, e.g. node24-linux-x64,node24-linux-arm64,node24-macos-arm64,node24-macos-x64,node24-win-x64.',
  196. ' Default: the host platform only (on node24).',
  197. ' --skip-build skip `pnpm run build` (lib/ artifacts must already exist).',
  198. ' --dry-run print every command and config patch without executing.',
  199. ' --help print this help.',
  200. '',
  201. 'Build route: @yao-pkg/pkg --sea (root devDependency, pnpm-patched); see .agents/notes/implemented/architecture/2026-07-10-single-file-executable-sdk-runtime-distribution.md.',
  202. `Stages the node carrier in ${PYTHON_RUNTIME_DIR}/${PYTHON_NODE_SUBDIR} and writes executables to ${OUT_DIR}/.`,
  203. ].join('\n')
  204. }
  205. }
  206. function pnpmInvocation(args: string[]): [command: string, args: string[]] {
  207. const entrypoint = process.env.npm_execpath?.trim()
  208. if (entrypoint !== undefined && entrypoint !== '') {
  209. const extension = extname(entrypoint).toLowerCase()
  210. if (extension === '.js' || extension === '.cjs' || extension === '.mjs') {
  211. return [process.execPath, [entrypoint, ...args]]
  212. }
  213. if (extension !== '.cmd') return [entrypoint, args]
  214. }
  215. const home = process.env.PNPM_HOME?.trim()
  216. if (home !== undefined && home !== '') {
  217. const packageBin = resolve(home, '..', 'pnpm', 'bin')
  218. for (const filename of ['pnpm.mjs', 'pnpm.cjs']) {
  219. const candidate = resolve(packageBin, filename)
  220. if (existsSync(candidate)) return [process.execPath, [candidate, ...args]]
  221. }
  222. }
  223. if (process.platform === 'win32') {
  224. throw new Error('build-exe-for-python-sdk: pnpm must expose a JavaScript entrypoint through npm_execpath or PNPM_HOME on Windows.')
  225. }
  226. return ['pnpm', args]
  227. }
  228. /**
  229. * Render a command for logs and errors, quoting arguments with spaces.
  230. * @param command - the executable.
  231. * @param args - its arguments.
  232. * @returns the printable command line.
  233. */
  234. function formatCommand(command: string, args: string[]): string {
  235. return [command, ...args].map(part => (part.includes(' ') ? JSON.stringify(part) : part)).join(' ')
  236. }
  237. /**
  238. * Sequential build pipeline. Subprocesses inherit stdio and errors include
  239. * the command; dry runs print commands and filesystem changes.
  240. */
  241. class SingleExeBuild {
  242. /**
  243. * The cleared deploy target, pkg input, and Python node-mode carrier.
  244. */
  245. readonly staging = resolve(root, PYTHON_RUNTIME_DIR, PYTHON_NODE_SUBDIR)
  246. private readonly outDir = resolve(root, OUT_DIR)
  247. constructor(private readonly cli: BuildCli) {}
  248. /** Verify the closure before compiling or packaging. */
  249. async verifyClosure(): Promise<void> {
  250. await this.runPnpm('runtime dependency closure', ['run', 'verify-runtime-closure'])
  251. }
  252. /** Build all package artifacts unless `--skip-build` was passed. */
  253. async build(): Promise<void> {
  254. if (this.cli.skipBuild) {
  255. console.log('build-exe-for-python-sdk: skipping pnpm run build (--skip-build)')
  256. return
  257. }
  258. await this.runPnpm('build', ['run', 'build'])
  259. }
  260. /** Clear and deploy the runtime closure into the node carrier. */
  261. async deployStaging(): Promise<void> {
  262. if (this.staging === root || root.startsWith(this.staging + sep)) {
  263. throw new Error(`build-exe-for-python-sdk: refusing to clear staging dir ${this.staging}: it contains the repo root.`)
  264. }
  265. if (this.cli.dryRun) console.log(`build-exe-for-python-sdk: [dry-run] rm -rf ${this.staging}`)
  266. else await rm(this.staging, { recursive: true, force: true })
  267. await this.runPnpm('deploy', [
  268. '--filter',
  269. DEPLOY_ROOT_PACKAGE,
  270. 'deploy',
  271. '--legacy',
  272. '--prod',
  273. '--config.node-linker=hoisted',
  274. '--config.auto-install-peers=false',
  275. '--config.link-workspace-packages=true',
  276. this.staging,
  277. ])
  278. await this.restoreLegacyHoists()
  279. await this.materializeStagedLinks()
  280. if (this.cli.dryRun) {
  281. for (const name of DEPLOY_ONLY_DOCS) console.log(`build-exe-for-python-sdk: [dry-run] rm -f ${join(this.staging, name)}`)
  282. } else {
  283. await Promise.all(DEPLOY_ONLY_DOCS.map(name => rm(join(this.staging, name), { force: true })))
  284. }
  285. }
  286. /**
  287. * Restore direct packages that pnpm's legacy hoister places beside the deploy
  288. * source instead of in the target. The runtime manifest supplies every peer,
  289. * so package-local node_modules trees are omitted to preserve one flat Cordis
  290. * instance and a symlink-free packaged payload.
  291. */
  292. private async restoreLegacyHoists(): Promise<void> {
  293. if (this.cli.dryRun) {
  294. console.log('build-exe-for-python-sdk: [dry-run] restore direct dependencies omitted by legacy deploy')
  295. return
  296. }
  297. const manifestPath = join(this.staging, 'package.json')
  298. const manifest = JSON.parse(await readFile(manifestPath, 'utf8')) as {
  299. dependencies?: Record<string, string>
  300. }
  301. const sourceNodeModules = resolve(root, DEPLOY_SOURCE_NODE_MODULES)
  302. const restored: string[] = []
  303. for (const dependency of Object.keys(manifest.dependencies ?? {}).sort()) {
  304. const destination = join(this.staging, 'node_modules', dependency)
  305. if (existsSync(destination)) continue
  306. const source = join(sourceNodeModules, dependency)
  307. if (!existsSync(source)) {
  308. throw new Error(
  309. `build-exe-for-python-sdk: deployed dependency ${dependency} is absent from both ${destination} and ${source}.`,
  310. )
  311. }
  312. await mkdir(dirname(destination), { recursive: true })
  313. const nestedNodeModules = join(source, 'node_modules')
  314. await cp(source, destination, {
  315. recursive: true,
  316. dereference: true,
  317. filter: path => path !== nestedNodeModules && !path.startsWith(nestedNodeModules + sep),
  318. })
  319. restored.push(dependency)
  320. }
  321. const stillMissing = Object.keys(manifest.dependencies ?? {})
  322. .filter(dependency => !existsSync(join(this.staging, 'node_modules', dependency)))
  323. if (stillMissing.length > 0) {
  324. throw new Error(`build-exe-for-python-sdk: staged dependencies remain missing: ${stillMissing.join(', ')}.`)
  325. }
  326. if (restored.length > 0) {
  327. console.log(`build-exe-for-python-sdk: restored legacy deploy hoists: ${restored.join(', ')}`)
  328. }
  329. }
  330. /** Replace deploy-time package links with files and reject any remaining link. */
  331. private async materializeStagedLinks(): Promise<void> {
  332. if (this.cli.dryRun) {
  333. console.log('build-exe-for-python-sdk: [dry-run] materialize staged package links')
  334. return
  335. }
  336. const nodeModules = join(this.staging, 'node_modules')
  337. let remaining = await this.findSymlink(nodeModules)
  338. while (remaining !== undefined) {
  339. const segments = remaining.slice(nodeModules.length + 1).split(sep)
  340. const binIndex = segments.lastIndexOf('.bin')
  341. if (binIndex >= 0) {
  342. await rm(join(nodeModules, ...segments.slice(0, binIndex + 1)), { recursive: true, force: true })
  343. remaining = await this.findSymlink(nodeModules)
  344. continue
  345. }
  346. const destination = remaining
  347. const source = await realpath(destination)
  348. const nestedNodeModules = join(source, 'node_modules')
  349. await rm(destination, { recursive: true, force: true })
  350. await cp(source, destination, {
  351. recursive: true,
  352. dereference: true,
  353. filter: path => path !== nestedNodeModules && !path.startsWith(nestedNodeModules + sep),
  354. })
  355. remaining = await this.findSymlink(nodeModules)
  356. }
  357. }
  358. /** Return the first symbolic link below a directory, if one exists. */
  359. private async findSymlink(directory: string): Promise<string | undefined> {
  360. for (const entry of await readdir(directory, { withFileTypes: true })) {
  361. const path = join(directory, entry.name)
  362. const metadata = await lstat(path)
  363. if (metadata.isSymbolicLink()) return path
  364. if (metadata.isDirectory()) {
  365. const nested = await this.findSymlink(path)
  366. if (nested !== undefined) return nested
  367. }
  368. }
  369. return undefined
  370. }
  371. /** Add the executable entry and pkg assets to the staged manifest. */
  372. async injectPkgConfig(): Promise<void> {
  373. const patch = { bin: ENTRY_BIN, pkg: { assets: ASSET_GLOBS } }
  374. const manifestPath = join(this.staging, 'package.json')
  375. if (this.cli.dryRun) {
  376. console.log(`build-exe-for-python-sdk: [dry-run] patch ${manifestPath} with ${JSON.stringify(patch)}`)
  377. return
  378. }
  379. if (!existsSync(manifestPath)) {
  380. throw new Error(`build-exe-for-python-sdk: ${manifestPath} missing — pnpm deploy did not produce a staged package.`)
  381. }
  382. if (!existsSync(join(this.staging, ENTRY_BIN))) {
  383. throw new Error(`build-exe-for-python-sdk: ${join(this.staging, ENTRY_BIN)} missing — run without --skip-build so lib/ artifacts exist.`)
  384. }
  385. const manifest = JSON.parse(await readFile(manifestPath, 'utf8')) as Record<string, unknown>
  386. await writeFile(manifestPath, `${JSON.stringify({ ...manifest, ...patch }, null, 2)}\n`)
  387. console.log(`build-exe-for-python-sdk: injected pkg config into ${manifestPath}`)
  388. }
  389. /**
  390. * Package one target; SEA mode accepts one target per invocation.
  391. * @param target - the pkg target triple to build.
  392. * @returns the executable and ripgrep sidecar paths, plus the macOS spawn helper path when required.
  393. */
  394. async pack(target: Target): Promise<string[]> {
  395. const productBase = join(this.outDir, `${OUTPUT_BASENAME}-${target.platform}-${target.arch}`)
  396. const product = target.platform === 'win' ? `${productBase}.exe` : productBase
  397. await this.prepareNativePty(target)
  398. if (!this.cli.dryRun) await mkdir(this.outDir, { recursive: true })
  399. await this.runPnpm(`pkg ${target.spec}`, [
  400. 'exec',
  401. 'pkg',
  402. this.staging,
  403. '--sea',
  404. '--targets',
  405. target.spec,
  406. '--output',
  407. product,
  408. ])
  409. if (!this.cli.dryRun && !existsSync(product)) {
  410. throw new Error(`build-exe-for-python-sdk: product ${product} is missing after the pkg run; inspect ${this.outDir}.`)
  411. }
  412. const ripgrep = await this.copyRipgrepSidecar(target, product)
  413. if (target.platform !== 'macos') return [product, ripgrep]
  414. const spawnHelper = `${product}-spawn-helper`
  415. const source = join(this.staging, 'node_modules', 'node-pty', 'prebuilds', `darwin-${target.arch}`, 'spawn-helper')
  416. if (this.cli.dryRun) {
  417. console.log(`build-exe-for-python-sdk: [dry-run] cp ${source} ${spawnHelper}`)
  418. } else {
  419. await copyFile(source, spawnHelper)
  420. await chmod(spawnHelper, 0o755)
  421. }
  422. return [product, ripgrep, spawnHelper]
  423. }
  424. /** Copy the target ripgrep binary beside the executable so Node can spawn it outside pkg's virtual filesystem. */
  425. private async copyRipgrepSidecar(target: Target, product: string): Promise<string> {
  426. const platform = target.platform === 'macos' ? 'darwin' : target.platform === 'win' ? 'win32' : target.platform
  427. const executable = target.platform === 'win' ? 'rg.exe' : 'rg'
  428. const source = join(
  429. this.staging,
  430. 'node_modules',
  431. '@vscode',
  432. `ripgrep-${platform}-${target.arch}`,
  433. 'bin',
  434. executable,
  435. )
  436. const destination = target.platform === 'win'
  437. ? `${product.slice(0, -'.exe'.length)}-rg.exe`
  438. : `${product}-rg`
  439. if (this.cli.dryRun) {
  440. console.log(`build-exe-for-python-sdk: [dry-run] cp ${source} ${destination}`)
  441. return destination
  442. }
  443. if (!existsSync(source)) {
  444. throw new Error(`build-exe-for-python-sdk: target ripgrep binary is missing at ${source}.`)
  445. }
  446. await copyFile(source, destination)
  447. await chmod(destination, 0o755)
  448. return destination
  449. }
  450. /**
  451. * Put the target node-pty addon in the staged closure. The release workflow
  452. * provides a manylinux build; ordinary installs use node-pty's target prebuild.
  453. * @param target - the pkg target whose native addon is being staged.
  454. */
  455. private async prepareNativePty(target: Target): Promise<void> {
  456. const stagedBuild = join(this.staging, 'node_modules', 'node-pty', 'build')
  457. if (this.cli.dryRun) console.log(`build-exe-for-python-sdk: [dry-run] rm -rf ${stagedBuild}`)
  458. else await rm(stagedBuild, { recursive: true, force: true })
  459. const packageDirectory = join(
  460. root,
  461. 'packages',
  462. 'subprocess',
  463. 'subprocess-local',
  464. 'node_modules',
  465. 'node-pty',
  466. )
  467. if (target.platform === 'win') {
  468. if (target.arch !== 'x64') {
  469. throw new Error('build-exe-for-python-sdk: Windows supports x64 only.')
  470. }
  471. const host = Target.host()
  472. if (target.platform !== host.platform || target.arch !== host.arch) {
  473. throw new Error(
  474. 'build-exe-for-python-sdk: build the Windows runtime under x64 Node on its target host; '
  475. + `target ${target.platform}-${target.arch} does not match host ${host.platform}-${host.arch}.`,
  476. )
  477. }
  478. resolveWindowsNodePtyAddons(join(this.staging, 'node_modules', 'node-pty'), target.arch)
  479. return
  480. }
  481. if (target.platform !== 'linux') return
  482. const destination = join(stagedBuild, 'Release', 'pty.node')
  483. const source = resolveLinuxNodePtyAddon(packageDirectory, target.arch)
  484. if (this.cli.dryRun) {
  485. console.log(`build-exe-for-python-sdk: [dry-run] cp ${source} ${destination}`)
  486. return
  487. }
  488. const host = Target.host()
  489. if (target.platform !== host.platform || target.arch !== host.arch) {
  490. throw new Error(
  491. 'build-exe-for-python-sdk: build the Linux runtime on its target architecture; '
  492. + `target ${target.platform}-${target.arch} does not match host ${host.platform}-${host.arch}.`,
  493. )
  494. }
  495. await mkdir(dirname(destination), { recursive: true })
  496. await copyFile(source, destination)
  497. }
  498. /**
  499. * Print each product path and, outside dry-run mode, its size.
  500. * @param products - the product paths returned by {@link pack}.
  501. */
  502. printProducts(products: string[]): void {
  503. console.log(this.cli.dryRun ? 'build-exe-for-python-sdk: [dry-run] would produce:' : 'build-exe-for-python-sdk: products:')
  504. for (const path of products) {
  505. if (this.cli.dryRun) {
  506. console.log(` ${path}`)
  507. continue
  508. }
  509. const megabytes = statSync(path).size / (1024 * 1024)
  510. console.log(` ${path} (${megabytes.toFixed(1)} MB)`)
  511. }
  512. }
  513. /**
  514. * Copy each product into the Python runtime package. The deployed node
  515. * carrier is already in place, and `dist-exe/` retains upload copies.
  516. * @param products - the product paths returned by {@link pack}.
  517. */
  518. async syncToPythonRuntime(products: string[]): Promise<void> {
  519. const destDir = resolve(root, PYTHON_RUNTIME_DIR)
  520. if (this.cli.dryRun) {
  521. for (const path of products) {
  522. console.log(`build-exe-for-python-sdk: [dry-run] cp ${path} ${join(destDir, basename(path))}`)
  523. }
  524. return
  525. }
  526. await mkdir(destDir, { recursive: true })
  527. for (const path of products) {
  528. const destination = join(destDir, basename(path))
  529. await copyFile(path, destination)
  530. await chmod(destination, statSync(path).mode & 0o777)
  531. console.log(`build-exe-for-python-sdk: synced ${destination}`)
  532. }
  533. }
  534. /**
  535. * Run one subprocess with inherited stdio. Spawn and non-zero-exit errors
  536. * include the command; dry runs only print it.
  537. * @param label - the step name used in logs and error messages.
  538. * @param command - the executable.
  539. * @param args - its arguments.
  540. */
  541. private async run(label: string, command: string, args: string[]): Promise<void> {
  542. const printable = formatCommand(command, args)
  543. if (this.cli.dryRun) {
  544. console.log(`build-exe-for-python-sdk: [dry-run] ${printable}`)
  545. return
  546. }
  547. console.log(`build-exe-for-python-sdk: ${label}: ${printable}`)
  548. await new Promise<void>((resolvePromise, reject) => {
  549. const child = spawn(command, args, {
  550. cwd: root,
  551. stdio: 'inherit',
  552. // Artifact builds must not mutate or validate a developer's Git hooks.
  553. env: { ...process.env, CI: 'true' },
  554. })
  555. child.once('error', (error) => {
  556. reject(new Error(`build-exe-for-python-sdk: ${label} failed to spawn: ${error.message} (${printable})`))
  557. })
  558. child.once('exit', (code, signal) => {
  559. if (code === 0) {
  560. resolvePromise()
  561. return
  562. }
  563. const cause = code === null ? `signal ${signal ?? 'unknown'}` : `exit code ${code}`
  564. reject(new Error(`build-exe-for-python-sdk: ${label} failed (${cause}): ${printable}`))
  565. })
  566. })
  567. }
  568. /** Run pnpm through its JavaScript entrypoint when the caller supplies one. */
  569. private async runPnpm(label: string, args: string[]): Promise<void> {
  570. const [command, invocationArgs] = pnpmInvocation(args)
  571. await this.run(label, command, invocationArgs)
  572. }
  573. }
  574. async function main(): Promise<void> {
  575. const cli = BuildCli.parse(process.argv.slice(2))
  576. const pipeline = new SingleExeBuild(cli)
  577. console.log(`build-exe-for-python-sdk: targets: ${cli.targets.map(target => target.spec).join(', ')}`)
  578. console.log(`build-exe-for-python-sdk: staging: ${pipeline.staging}`)
  579. await pipeline.verifyClosure()
  580. await pipeline.build()
  581. await pipeline.deployStaging()
  582. await pipeline.injectPkgConfig()
  583. const products: string[] = []
  584. for (const target of cli.targets) products.push(...await pipeline.pack(target))
  585. pipeline.printProducts(products)
  586. await pipeline.syncToPythonRuntime(products)
  587. }
  588. await main()