llvmPackages: split from build-llvm.ts

This commit is contained in:
2024-06-19 15:30:48 +08:00
parent bc57810801
commit 65be59b696
2 changed files with 145 additions and 162 deletions

142
llvmPackages.ts Normal file
View File

@@ -0,0 +1,142 @@
import path from "path";
import { GeneralVariable, LLVMVariable, command, variable } from "./commands/cmake";
import { SYSROOT_PREFIX } from "./environment";
import { PackageTask, buildPackage } from "./build";
import { checkedSpawnSync } from "./cli";
export function llvmPackages(src: string, installPrefix: string): PackageTask[] {
const LLVM_BUILD = path.join(src, "build-dev");
const rm = "rm";
const cmake = "cmake";
const llvmVar: LLVMVariable = {
LLVM_ENABLE_PROJECTS: ["clang", "clang-tools-extra", "lld", "openmp"],
LLVM_TARGETS_TO_BUILD: ["Sw64"],
LIBOMP_ARCH: "Sw64",
LIBOMP_USE_ITT_NOTIFY: false,
};
const general: GeneralVariable = {
CMAKE_BUILD_TYPE: "Release",
CMAKE_INSTALL_PREFIX: installPrefix,
CMAKE_SYSROOT: SYSROOT_PREFIX,
};
const mkBuildPhase = (build: string) => {
checkedSpawnSync(cmake, ["--build", build], { stdio: 'inherit' });
};
const mkInstallPhase = (build: string) => {
checkedSpawnSync(cmake, ["--build", build, "--target", "install"], { stdio: 'inherit' });
};
function mkLLVMPackage(): PackageTask {
const build = path.join(LLVM_BUILD, "llvm");
return {
name: "llvm",
patchPhase: () => {
// Apply llvm omp patch
checkedSpawnSync("git",
[
"apply",
path.resolve(__dirname, "..", "assets", "omp.diff"),
], { stdio: "inherit", cwd: src });
process.env.LD_LIBRARY_PATH = [
path.join(SYSROOT_PREFIX, 'usr', 'lib'),
path.join(SYSROOT_PREFIX, 'lib'),
].join(':');
},
configurePhase: () => {
// Configure
checkedSpawnSync(cmake, command({
definitions: variable({
...llvmVar,
...general,
}),
generator: "Ninja",
// LLVM cmake root is under /llvm directory
pathToSource: path.join(src, "llvm"),
pathToBuild: build,
}), { stdio: "inherit" });
},
buildPhase: () => mkBuildPhase(build),
installPhase: () => mkInstallPhase(build),
};
}
function mkLibpgmathPackage(): PackageTask {
const build = path.join(LLVM_BUILD, "libpgmath");
return {
name: "libpgmath",
configurePhase: () => {
checkedSpawnSync(rm, ["-rf", build], { stdio: "inherit" });
// Configure libpgmath.
checkedSpawnSync(cmake, command({
definitions: variable({
...general,
...{
CMAKE_C_FLAGS: "-msimd",
CMAKE_CXX_FLAGS: "-msimd",
CMAKE_C_COMPILER: path.join(installPrefix, "bin", "clang"),
CMAKE_CXX_COMPILER: path.join(installPrefix, "bin", "clang++"),
} as GeneralVariable,
}),
generator: "Ninja",
pathToSource: path.join(src, "cflang", "runtime", "libpgmath"),
pathToBuild: build,
}), { stdio: "inherit" });
},
buildPhase: () => mkBuildPhase(build),
installPhase: () => mkInstallPhase(build),
};
}
function mkCflangPackage(): PackageTask {
const build = path.join(LLVM_BUILD, "cflang");
return {
name: "cflang",
configurePhase: () => {
checkedSpawnSync(rm, ["-rf", build], { stdio: "inherit" });
// Configure cflang.
checkedSpawnSync(cmake, command({
definitions: variable({
...llvmVar,
...general,
...{
CMAKE_C_COMPILER: path.join(installPrefix, "bin", "clang"),
CMAKE_CXX_COMPILER: path.join(installPrefix, "bin", "clang++"),
CMAKE_Fortran_COMPILER: path.join(installPrefix, "bin", "flang"),
CMAKE_Fortran_COMPILER_ID: "Flang",
} as GeneralVariable,
// Flang specific variables.
FLANG_INCLUDE_DOCS: true,
FLANG_LLVM_EXTENSIONS: true,
WITH_WERROR: false,
LLVM_CONFIG: path.join(installPrefix, "bin", "llvm-config"),
}),
generator: "Unix Makefiles",
pathToSource: path.join(src, "cflang"),
pathToBuild: build,
}), { stdio: "inherit" });
},
buildPhase: () => mkBuildPhase(build),
installPhase: () => mkInstallPhase(build),
};
}
return [mkLLVMPackage(), mkLibpgmathPackage(), mkCflangPackage()];
} export function buildLLVMHash(src: string, rev: string) {
// Checkout a git rev in some git repo.
checkedSpawnSync("git", ["checkout", rev], { stdio: "inherit", cwd: src });
checkedSpawnSync("git", ["reset", "--hard", "HEAD"], { stdio: "inherit", cwd: src });
const llvmInstall = path.join(src, "local", "installed");
llvmPackages(src, path.join(llvmInstall, rev))
.forEach(buildPackage);
}