mirror of
https://github.com/0O0o0oOoO00/Alas.git
synced 2026-05-14 17:29:24 +08:00
142 lines
4.0 KiB
Python
142 lines
4.0 KiB
Python
import subprocess
|
|
from typing import Union, List
|
|
|
|
|
|
class CMakeOption:
|
|
def add_to_commandline(self, commandline: List[str]):
|
|
...
|
|
|
|
|
|
class CMakeDefine(CMakeOption):
|
|
def __init__(self, name, value):
|
|
self.name = name
|
|
self.value = value
|
|
|
|
def add_to_commandline(self, commandline: List[str]):
|
|
commandline.append(f"-D{self.name}={self.value}")
|
|
|
|
|
|
class CMakeCCompiler(CMakeDefine):
|
|
def __init__(self, compiler):
|
|
super().__init__("CMAKE_C_COMPILER", compiler)
|
|
|
|
|
|
class CMakeCxxCompiler(CMakeDefine):
|
|
def __init__(self, compiler):
|
|
super().__init__("CMAKE_CXX_COMPILER", compiler)
|
|
|
|
|
|
class CMakeToolChainFile(CMakeDefine):
|
|
def __init__(self, file):
|
|
super().__init__("CMAKE_TOOLCHAIN_FILE", file)
|
|
|
|
|
|
class CMakeSystemName(CMakeDefine):
|
|
def __init__(self, name):
|
|
super().__init__("CMAKE_SYSTEM_NAME", name)
|
|
|
|
|
|
class CMakeGenerator:
|
|
Visual_Studio_17_2022 = "Visual Studio 17 2022"
|
|
NMake_Makefiles = "NMake Makefiles"
|
|
Unix_Makefiles = "Unix Makefiles"
|
|
MinGW_Makefiles = "MinGW Makefiles"
|
|
|
|
|
|
class CMakeLists:
|
|
def __init__(self, source_dir=None):
|
|
self.source_dir = source_dir
|
|
self.build_dir = None
|
|
self.install_dir = None
|
|
self.cmake_options: List[CMakeOption] = []
|
|
self.build_type = "Release"
|
|
self.generator = None
|
|
|
|
def add_option(self, option: Union[CMakeOption, List[CMakeOption]]):
|
|
if option is None:
|
|
return
|
|
if isinstance(option, CMakeOption):
|
|
self.cmake_options.append(option)
|
|
elif isinstance(option, list):
|
|
self.cmake_options.extend(option)
|
|
else:
|
|
raise TypeError(f"option must be CMakeOption or list[CMakeOption]")
|
|
return self
|
|
|
|
def set_source_dir(self, source_dir):
|
|
self.source_dir = source_dir
|
|
return self
|
|
|
|
def set_build_type(self, build_type):
|
|
self.build_type = build_type
|
|
return self
|
|
|
|
def set_generator(self, generator):
|
|
self.generator = generator
|
|
return self
|
|
|
|
def set_build_dir(self, build_dir):
|
|
self.build_dir = build_dir
|
|
return self
|
|
|
|
def set_install_dir(self, install_dir):
|
|
self.install_dir = install_dir
|
|
return self
|
|
|
|
def __get_configure_options(self) -> List[str]:
|
|
commandline = []
|
|
for option in self.cmake_options:
|
|
option.add_to_commandline(commandline)
|
|
return commandline
|
|
|
|
def configure(self):
|
|
if self.build_dir is None:
|
|
raise RuntimeError("build dir not set")
|
|
|
|
cmd = [
|
|
"cmake",
|
|
*self.__get_configure_options(),
|
|
]
|
|
if self.build_type is not None:
|
|
cmd.append(f"-DCMAKE_BUILD_TYPE={self.build_type}")
|
|
if self.install_dir is not None:
|
|
cmd.append(f"-DCMAKE_INSTALL_PREFIX={self.install_dir}")
|
|
if self.generator is not None:
|
|
cmd.extend([
|
|
"-G", self.generator
|
|
])
|
|
cmd.extend([
|
|
"-S", self.source_dir,
|
|
"-B", self.build_dir,
|
|
])
|
|
cmake_process = subprocess.run(cmd)
|
|
if cmake_process.returncode != 0:
|
|
raise RuntimeError("cmake configure failed")
|
|
return self
|
|
|
|
def build(self):
|
|
if self.build_dir is None:
|
|
raise RuntimeError("build dir not set")
|
|
cmd = [
|
|
"cmake",
|
|
"--build", self.build_dir,
|
|
"--config", self.build_type,
|
|
]
|
|
cmake_process = subprocess.run(cmd)
|
|
if cmake_process.returncode != 0:
|
|
raise RuntimeError("cmake build failed")
|
|
return self
|
|
|
|
def install(self):
|
|
if self.install_dir is None:
|
|
raise RuntimeError("install dir not set")
|
|
cmd = [
|
|
"cmake",
|
|
"--install", self.build_dir,
|
|
"--config", self.build_type,
|
|
]
|
|
cmake_process = subprocess.run(cmd)
|
|
if cmake_process.returncode != 0:
|
|
raise RuntimeError("cmake build failed")
|
|
return self
|