From fbcf280f42b40f9fa9f88a929feaeef6ee2b9804 Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Sun, 20 Apr 2025 13:12:11 +0800 Subject: [PATCH] spec: simplify specTemplate options, provide a lower interface --- assets/specTemplate.cfg | 4 --- src/spec/index.ts | 56 ++++++++--------------------------------- src/toolchain/index.ts | 20 +++++++++++++++ 3 files changed, 30 insertions(+), 50 deletions(-) diff --git a/assets/specTemplate.cfg b/assets/specTemplate.cfg index 6ae0b1e..d7a373d 100644 --- a/assets/specTemplate.cfg +++ b/assets/specTemplate.cfg @@ -31,10 +31,6 @@ intspeed,fpspeed: #------- Compilers ------------------------------------------------------------ default: # EDIT: the directory where your compiler is installed -%ifndef %{gcc_dir} -#% define gcc_dir /SW/compilers/GCC/Linux/x86_64/gcc-6.3.0 -% define sysroot_dir @@SYSROOT@@ -%endif LDFLAGS = @@LDFLAGS@@ LIBS = @@LIBS@@ diff --git a/src/spec/index.ts b/src/spec/index.ts index a527cba..38cf09c 100644 --- a/src/spec/index.ts +++ b/src/spec/index.ts @@ -60,62 +60,26 @@ export function mkBench(spec: SPEC, bench: SPECBenchData): Bench { const writeFile = promisify(fs.writeFile); export interface ConfigOptions { - prefix: string; optimize: string[]; ldflags: string[]; - sysroot: string; - compiler: "llvm" | "gcc"; + libs: string[]; + compilerPaths: { + CXX: string, + CC: string, + FC: string, + }; } -// Pure function to get compiler names based on the compiler type -const getCompilerNames = (compiler: ConfigOptions["compiler"]) => -({ - llvm: { CXX: "clang++", CC: "clang", FC: "flang" }, - gcc: { CXX: "g++", CC: "gcc", FC: "gfortran" }, -}[compiler]); - -// Pure function to get compiler directory structure -const getCompilerDir = (compiler: ConfigOptions["compiler"]) => - compiler === "llvm" ? ["bin"] : ["usr", "bin"]; - -// Helper function to resolve paths for compilers -const resolveCompilerPaths = ( - prefix: string, - compiler: ConfigOptions["compiler"], - compilerNames: ReturnType -) => ({ - CC: path.resolve(prefix, ...getCompilerDir(compiler), compilerNames.CC), - CXX: path.resolve(prefix, ...getCompilerDir(compiler), compilerNames.CXX), - FC: path.resolve(prefix, ...getCompilerDir(compiler), compilerNames.FC), -}); - -// Helper function to generate library paths for LLVM -const getLlvmLibs = (prefix: string) => - [ - "pgmath", - "flangrti", - "flang", - ].map(lib => path.resolve(prefix, "lib", `lib${lib}.a`)).concat(["-lompstub"]); - // Pure function to render configuration export const renderConfig = (options: ConfigOptions): string => { - const compilerNames = getCompilerNames(options.compiler); - const compilerPaths = resolveCompilerPaths(options.prefix, options.compiler, compilerNames); - - const libs = - options.compiler === "llvm" - ? getLlvmLibs(options.prefix) - : []; - return `# Rendered from TypeScript ${new Date().toLocaleString()}, do not edit!\n\n\n` + specTemplate .replace("@@OPTIMIZE@@", options.optimize.join(" ")) .replace("@@LDFLAGS@@", options.ldflags.join(" ")) - .replace("@@SYSROOT@@", options.sysroot) - .replace("@@CompilerCC@@", compilerPaths.CC) - .replace("@@CompilerCXX@@", compilerPaths.CXX) - .replace("@@CompilerFC@@", compilerPaths.FC) - .replace("@@LIBS@@", libs.join(" ")) + .replace("@@CompilerCC@@", options.compilerPaths.CC) + .replace("@@CompilerCXX@@", options.compilerPaths.CXX) + .replace("@@CompilerFC@@", options.compilerPaths.FC) + .replace("@@LIBS@@", options.libs.join(" ")) ; }; diff --git a/src/toolchain/index.ts b/src/toolchain/index.ts index 294ed05..345429e 100644 --- a/src/toolchain/index.ts +++ b/src/toolchain/index.ts @@ -47,6 +47,26 @@ export function gccToolchain(prefix: string) { }; } +export type ToolchainSuite = "llvm" | "gcc"; + +export function toolchain(prefix: string, suite: ToolchainSuite) { + if (suite === "llvm") { + return llvmToolchain(prefix); + } else if (suite === "gcc") { + return gccToolchain(prefix); + } else { + throw new Error(`Invalid toolchain suite: "${suite}"`); + } +} + +export function cflangLibs(prefix: string) { + return [ + "pgmath", + "flang", + "flangrti", + ].map(lib => path.resolve(prefix, "lib", `lib${lib}.a`)).concat(["-lompstub"]); +} + /** * Give an LLVM toolchain, and a desired IR, a list of function names. * Generate a function that takes vFnPath and sFnPath, used for path calculation.