build/llvmPackages: support mathlib configuration / mcpuVariant

This commit is contained in:
2025-02-21 11:02:18 +08:00
parent f4c8deda6b
commit 00d57ff13b
3 changed files with 75 additions and 17 deletions

View File

@@ -1,9 +1,10 @@
import path from "path";
import { GeneralVariable, LLVMVariable, command, variable } from "../commands/cmake.js";
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 { SpawnOptions, spawn } from "child_process";
import * as sunway from '../sunway.js';
export interface LLVMPackageOverrides {
cmakeDefinitionOverrides?: {
@@ -60,7 +61,9 @@ export interface LLVMPackageOptions {
platform: PackagePlatform;
enableLibpgmathSIMD: boolean;
mathSIMD: boolean;
cpuVariant: sunway.SunwayGeneration;
};
export function llvmPackages({
@@ -70,7 +73,8 @@ export function llvmPackages({
buildDir,
cmakeDefinitionOverrides,
spawnOverrides,
enableLibpgmathSIMD = false,
mathSIMD,
cpuVariant,
}: LLVMPackageOptions & LLVMPackageOverrides): PackageTask[] {
const rm = "rm";
const cmake = "cmake";
@@ -107,6 +111,16 @@ export function llvmPackages({
await promisifySpawn(spawn(cmake, ["--build", build, "--target", "install"], spawnOptions));
});
const mcpuFlag = sunway.mcpu(cpuVariant);
// Shared flags for compiling libpgmath & cflang.
// The toolchain of these targets are clang.
const clangCXFlags = [
"-Wno-sign-compare",
mcpuFlag,
];
function mkLLVMPackage(): PackageTask {
const build = buildDir('llvm');
return {
@@ -139,18 +153,19 @@ export function llvmPackages({
await promisifySpawn(spawn(rm, ["-rf", build], spawnOptions));
// Flags shared between C and C++ compiler.
const cxFlags = ["-mlong-double-64"];
if (enableLibpgmathSIMD) {
cxFlags.push("-msimd");
}
const cxFlags = [
...clangCXFlags,
"-mlong-double-64",
...mathSIMD ? ["-msimd"] : [],
];
// Configure libpgmath.
await promisifySpawn(spawn(cmake, command({
definitions: variable({
...general,
...{
CMAKE_C_FLAGS: cxFlags.join(" "),
CMAKE_CXX_FLAGS: cxFlags.join(" "),
CMAKE_C_FLAGS: buildCompilerFlags(cxFlags),
CMAKE_CXX_FLAGS: buildCompilerFlags(cxFlags),
CMAKE_C_COMPILER: path.join(installPrefix, "bin", "clang"),
CMAKE_CXX_COMPILER: path.join(installPrefix, "bin", "clang++"),
} as GeneralVariable,
@@ -172,12 +187,17 @@ export function llvmPackages({
name: "cflang",
configurePhase: async () => {
await promisifySpawn(spawn(rm, ["-rf", build], spawnOptions));
const cxFlags = [
...clangCXFlags,
];
// Configure cflang.
await promisifySpawn(spawn(cmake, command({
definitions: variable({
...general,
...{
CMAKE_C_FLAGS: buildCompilerFlags(cxFlags),
CMAKE_CXX_FLAGS: buildCompilerFlags(cxFlags),
CMAKE_Fortran_FLAGS: buildCompilerFlags([mcpuFlag]),
CMAKE_C_COMPILER: path.join(installPrefix, "bin", "clang"),
CMAKE_CXX_COMPILER: path.join(installPrefix, "bin", "clang++"),
CMAKE_Fortran_COMPILER: path.join(installPrefix, "bin", "flang"),