Files
work-ts/src/commands/cmake.ts
2024-06-20 15:25:43 +08:00

132 lines
4.0 KiB
TypeScript

import { optFlag, undefList } from "./common"
export type CMakeDefinition = {
name: string,
value: string,
type?: CMakeVariableType
}
export type CMakeVariableType = "BOOL" | "FILEPATH" | "PATH" | "STRING" | "INTERNAL"
/**
* General CMake variables.
*/
export interface GeneralVariable {
CMAKE_BUILD_TYPE?: "Debug" | "Release" | "RelWithDebInfo" | "MinSizeRel"
BUILD_SHARED_LIBS?: boolean,
CMAKE_INSTALL_PREFIX?: string,
CMAKE_SYSROOT?: string,
CMAKE_C_COMPILER?: string,
CMAKE_CXX_COMPILER?: string,
CMAKE_Fortran_COMPILER?: string,
CMAKE_Fortran_COMPILER_ID?: string,
CMAKE_C_FLAGS?: string,
CMAKE_CXX_FLAGS?: string,
}
/**
* CMake variables suitable for LLVM project.
*/
export interface LLVMVariable {
LLVM_TARGETS_TO_BUILD?: string[]
LLVM_ENABLE_CLASSIC_FLANG?: boolean,
LLVM_ENABLE_PROJECTS?: ("clang" | "openmp" | "lld" | "clang-tools-extra")[]
LIBOMP_ARCH?: "Sw64"
LIBOMP_USE_ITT_NOTIFY?: boolean
LIBOMP_ENABLE_SHARED?: boolean
OPENMP_ENABLE_LIBOMPTARGET?: boolean
}
export interface CFlangCMakeVariable {
FLANG_INCLUDE_DIRS?: boolean
FLANG_LLVM_EXTENSIONS?: boolean
WITH_WERROR?: boolean
LLVM_CONFIG?: string
}
/**
* Generate cmake definitions, take care of funny cmake string lists.
*
* @param object A set of cmake variables to generate
* @returns A list of cmake definitions, could be passed to CLI
*/
export function variable(object: Object): CMakeDefinition[] {
return Object.entries(object).map(([k, v]) => {
if (typeof v === "boolean") {
return {
name: k,
value: v ? "ON" : "OFF",
type: "BOOL"
}
} else if (typeof v === "object" && Array.isArray(v)) {
return {
name: k,
value: v.join(";"),
type: "STRING"
}
} else {
return {
name: k,
value: v,
type: "STRING"
}
}
}
)
}
/**
* Options for "Generate a Project Buildsystem"
* @see https://cmake.org/cmake/help/latest/manual/cmake.1.html#generate-a-project-buildsystem
*/
export interface CMakeGenerateOptions {
pathToBuild?: string
pathToSource?: string
/**
* Pre-load a script to populate the cache.
* When CMake is first run in an empty build tree, it creates a
* CMakeCache.txt file and populates it with customizable settings for the project.
* This option may be used to specify a file from which to load cache entries
* before the first pass through the project's CMake listfiles.
* The loaded entries take priority over the project's default values.
* The given file should be a CMake script containing set() commands that use the CACHE option,
* not a cache-format file.
*/
preloadCache?: string
definitions?: CMakeDefinition[]
/**
* Specify a build system generator.
*
* CMake may support multiple native build systems on certain platforms.
* A generator is responsible for generating a particular build system.
* Possible generator names are specified in the [cmake-generators(7)](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#manual:cmake-generators(7)) manual.
*/
generator?: "Unix Makefiles" | "Ninja"
/**
* Specify the cross compiling toolchain file, equivalent to setting CMAKE_TOOLCHAIN_FILE variable.
* Relative paths are interpreted as relative to the build directory,
* and if not found, relative to the source directory.
*
* New in version 3.21.
*/
toolchain?: string
}
export function command(o: CMakeGenerateOptions): string[] {
return [
...optFlag("-G", o.generator),
...undefList(o.definitions, defs => defs.flatMap(def => [
"-D",
`${def.name}${def.type ? `:${def.type}` : ""}=${def.value}`
])),
...optFlag("-B", o.pathToBuild),
...optFlag("-S", o.pathToSource),
]
}