From 65be59b69660def395fac7a49ac602c94c028815 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Wed, 19 Jun 2024 15:30:48 +0800 Subject: [PATCH] llvmPackages: split from build-llvm.ts --- build-llvm.ts | 165 +----------------------------------------------- llvmPackages.ts | 142 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 162 deletions(-) create mode 100644 llvmPackages.ts diff --git a/build-llvm.ts b/build-llvm.ts index b2aac75..30f4b40 100644 --- a/build-llvm.ts +++ b/build-llvm.ts @@ -1,164 +1,5 @@ -import path from "path"; -import { GeneralVariable, LLVMVariable, command, variable } from "./commands/cmake"; -import { LLVM_SRC, SYSROOT_PREFIX } from "./environment"; -import { PackageTask, buildPackage } from "./build"; -import { checkedSpawnSync } from "./cli"; +import { LLVM_SRC } from "./environment"; +import { buildLLVMHash } from "./llvmPackages"; -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.join(process.cwd(), "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()]; -} - - -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); -} - -[ - // "85e4fed0c9e4cd4ab8bce89f307127ccbad31294", - "09eed7875e9a5fa8597a3ba88bd5c5bcc18fff9b", - "1231573a5c29fc1ea5783b0f8cdd7442d48111b5", - "0fef7853ab023a4752c84f889f98461bd7f37728", - "cc2144e0cb4e09ca4c4e77ddb124148d3a50ed30", - "49025c97a834df250cec6b6c9d292951284308d6", - "5a48db93c5ae983513da54929f0600441c2584e7", - "76318811282fdfc65435628026636a748b015cb8", - "20713c101717338706b502d25a9c57e73b4d1dc9", - "33661f021f637512c3a986f1cf8f5828dd974114", - "793edc371ded601900fac19144cbddfd9a881b00", - "a44d0af59e3a8bac5e2f6c0d02b95ae1853928f2", - "9dbcdda68d41590485da61fe9fc09e92da9cadad", - "77bfe439d08adccf7a07b3393d8dc0281b7e4792", - "a5449bdd6d7df432a5cced42cd137d54eb027e03", - "adf1aaa1e498957a367c3052bb1195cef92a01c0", - "4a844972f38e2b44519b894ccfe54b6c92051cb7", -].forEach(hash => buildLLVMHash(LLVM_SRC, hash)); +buildLLVMHash(LLVM_SRC, "639a757627d1b28fa03b9438083bc3a163b8bbe8"); diff --git a/llvmPackages.ts b/llvmPackages.ts new file mode 100644 index 0000000..70e21ca --- /dev/null +++ b/llvmPackages.ts @@ -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); +}