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

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