From 8e489591d84ee4e4b177fae45efce1e4c8e504ce Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Mon, 21 Jul 2025 16:35:49 +0800 Subject: [PATCH] build/llvmPackages: abstract buildLLVMPackagesUUID --- src/bin/build-llvm.ts | 36 ++----------------------- src/build/llvmPackages.ts | 56 +++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 39 deletions(-) diff --git a/src/bin/build-llvm.ts b/src/bin/build-llvm.ts index 57f17fa..1187d57 100644 --- a/src/bin/build-llvm.ts +++ b/src/bin/build-llvm.ts @@ -1,12 +1,6 @@ import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; -import { llvmPackages } from '../build/llvmPackages.js'; -import path from 'path'; -import { buildPackage } from '../build/build.js'; -import { randomUUID } from 'crypto'; -import { arch } from 'os'; -import * as sunway from '../sunway.js'; -import chalk from 'chalk'; +import { buildLLVMPackagesUUID, BuildOptions } from '../build/llvmPackages.js'; const argv = await yargs(hideBin(process.argv)) .version("0.0.0") @@ -37,30 +31,4 @@ const argv = await yargs(hideBin(process.argv)) .help() .parse(); -const src = path.resolve(argv.src); -const build = argv.build ?? path.resolve(src, "build"); -const prefix = argv.prefix ?? path.resolve(src, "install", randomUUID()); - -const cpuVariant = argv.mcpu === "host" ? sunway.getHostGeneration() : argv.platform as sunway.SunwayGeneration; - -console.log(`prefix: ${chalk.red(prefix)}`); -console.log(`mcpu: ${cpuVariant}`); - -const packages = llvmPackages({ - src: argv.src, - buildDir(packageName) { - return path.join(build, packageName); - }, - installPrefix: prefix, - // FIXME: arguments to platform? - platform: { - buildPlatform: { arch: arch() }, - hostPlatform: { arch: arch() }, - }, - mathSIMD: argv['math-simd'], - cpuVariant, -}); - -for (const pkg of packages) { - await buildPackage(pkg); -} \ No newline at end of file +buildLLVMPackagesUUID(argv as BuildOptions); diff --git a/src/build/llvmPackages.ts b/src/build/llvmPackages.ts index b5a8042..d108f9c 100644 --- a/src/build/llvmPackages.ts +++ b/src/build/llvmPackages.ts @@ -1,10 +1,13 @@ -import path from "path"; -import { GeneralVariable, LLVMVariable, buildCompilerFlags, command, variable } from "../commands/cmake.js"; -import { PackagePlatform, PackageTask } from "./build.js"; -import { promisifySpawn } from "../cli.js"; -import os from "os"; +import chalk from 'chalk'; import { SpawnOptions, spawn } from "child_process"; +import { randomUUID } from 'crypto'; +import os, { arch } from "os"; +import path from "path"; +import { buildPackage } from '../build/build.js'; +import { promisifySpawn } from "../cli.js"; +import { GeneralVariable, LLVMVariable, buildCompilerFlags, command, variable } from "../commands/cmake.js"; import * as sunway from '../sunway.js'; +import { PackagePlatform, PackageTask } from "./build.js"; export interface LLVMPackageOverrides { cmakeDefinitionOverrides?: { @@ -226,3 +229,46 @@ export function llvmPackages({ return [mkLLVMPackage(), mkLibpgmathPackage(), mkCflangPackage()]; } + + +export interface BuildOptions { + src: string; + build?: string; + prefix?: string; + 'math-simd': boolean; + mcpu: '8a' | '6b' | 'host'; +} + + +/// 构建带 UUID 的 LLVM 包 +export async function buildLLVMPackagesUUID(options: BuildOptions): Promise { + const { src, build, prefix, 'math-simd': mathSIMD, mcpu } = options; + + const resolvedSrc = path.resolve(src); + const resolvedBuild = build ?? path.resolve(resolvedSrc, "build"); + const resolvedPrefix = prefix ?? path.resolve(resolvedSrc, "install", randomUUID()); + + const cpuVariant = mcpu === "host" + ? sunway.getHostGeneration() + : mcpu as sunway.SunwayGeneration; + + const packages = llvmPackages({ + src: resolvedSrc, + buildDir(packageName: string) { + return path.join(resolvedBuild, packageName); + }, + installPrefix: resolvedPrefix, + platform: { + buildPlatform: { arch: arch() }, + hostPlatform: { arch: arch() }, + }, + mathSIMD, + cpuVariant, + }); + + for (const pkg of packages) { + await buildPackage(pkg); + } + + return resolvedPrefix; +}