66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
from os import PathLike
|
|
from typing import Optional
|
|
|
|
|
|
class CMakeCompilerPair:
|
|
'''Represents a pair of c, cxx compilers'''
|
|
def __init__(self, name, c, cxx):
|
|
self.name = name
|
|
self.c = c
|
|
self.cxx = cxx
|
|
|
|
def to_opts(self):
|
|
opts = {}
|
|
if self.c:
|
|
opts['CMAKE_C_COMPILER'] = self.c
|
|
if self.cxx:
|
|
opts['CMAKE_CXX_COMPILER'] = self.cxx
|
|
return opts
|
|
|
|
def __str__(self):
|
|
return f'{self.name} ({self.c}, {self.cxx})'
|
|
|
|
|
|
class CMakeConfigureCommand:
|
|
def __init__(
|
|
self,
|
|
source_dir: Optional[PathLike] = None,
|
|
build_dir: Optional[PathLike] = None,
|
|
generator: Optional[str] = None,
|
|
build_type: Optional[str] = None,
|
|
compiler: Optional[CMakeCompilerPair] = None):
|
|
if source_dir is None:
|
|
raise ValueError('Source dir cannot be None')
|
|
|
|
def add_if_defined(name, value):
|
|
if value is not None:
|
|
self.add_opt(name, value)
|
|
|
|
self.cmd = 'cmake'
|
|
add_if_defined('-S', source_dir)
|
|
add_if_defined('-B', build_dir)
|
|
add_if_defined('-G', generator)
|
|
add_if_defined('CMAKE_BUILD_TYPE', build_type)
|
|
if compiler is not None:
|
|
for name, value in compiler.to_opts().items():
|
|
self.add_opt(name, value)
|
|
|
|
def add_opt(self, name: str, value):
|
|
if name is None:
|
|
raise ValueError('opt name cannot be None')
|
|
if value is None:
|
|
return self
|
|
if not name.startswith('-'):
|
|
name = f'-D{name}'
|
|
self.cmd = f'{self.cmd} {name}={value}'
|
|
else:
|
|
self.cmd = f'{self.cmd} {name} {value}'
|
|
return self
|
|
|
|
def add_opts(self, opts: dict):
|
|
for name, value in opts.items():
|
|
self.add_opt(name, value)
|
|
|
|
def __str__(self):
|
|
return self.cmd
|