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

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