58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
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['C_COMPILER'] = self.c
|
|
if self.cxx:
|
|
opts['CXX_COMPILER'] = self.cxx
|
|
return opts
|
|
|
|
def __str__(self):
|
|
return f'{self.name} ({self.c}, {self.cxx})'
|
|
|
|
|
|
class CMakeConfigureCommand:
|
|
def __init__(
|
|
self,
|
|
src_dir,
|
|
build_dir,
|
|
generator: Optional[str] = None,
|
|
build_type: Optional[str] = None,
|
|
compiler: Optional[CMakeCompilerPair] = None):
|
|
if src_dir is None:
|
|
raise ValueError('Source dir cannot be None')
|
|
self.cmd = 'cmake'
|
|
self.add_opt('-S', src_dir)
|
|
self.add_opt('-B', build_dir)
|
|
if generator is not None:
|
|
self.add_opt('-G', generator)
|
|
if build_type is not None:
|
|
self.add_opt('CMAKE_BUILD_TYPE', build_type)
|
|
if compiler is not None:
|
|
self.add_opt('CMAKE_C_COMPILER', compiler.c)
|
|
self.add_opt('CMAKE_CXX_COMPILER', compiler.cxx)
|
|
|
|
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 __str__(self):
|
|
return self.cmd
|