151 lines
4.8 KiB
Python
151 lines
4.8 KiB
Python
import subprocess
|
|
|
|
from build_target import BuildTarget
|
|
from os import PathLike
|
|
from file_utils import copy, SparkPaths
|
|
from table_formatter import fmt_table
|
|
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
|
|
|
|
|
|
class CMakeTarget(BuildTarget):
|
|
def __init__(
|
|
self,
|
|
name,
|
|
preset='spark',
|
|
build_preset='spark-debug',
|
|
source_path=SparkPaths.root(),
|
|
out_path=SparkPaths.default_build(),
|
|
aliases=[],
|
|
verbosity: Optional[str] = None,
|
|
):
|
|
if out_path is None:
|
|
raise ValueError('out_path is None')
|
|
if source_path is None:
|
|
raise ValueError('source_path is None')
|
|
BuildTarget.__init__(self, name, source_path, out_path, aliases)
|
|
self.preset = preset
|
|
self.build_preset = build_preset
|
|
self.verbosity = verbosity
|
|
|
|
def _get_configure_cmd(self):
|
|
cmd_parts = ['cmake']
|
|
if self.preset is not None:
|
|
cmd_parts.append(f'--preset {self.preset}')
|
|
if self.verbosity is not None:
|
|
cmd_parts.append(f'-DCMAKE_MESSAGE_LOG_LEVEL={self.verbosity}')
|
|
if self.source_path is not None:
|
|
cmd_parts.append(f'-S {self.source_path}')
|
|
if self.out_path is not None:
|
|
cmd_parts.append(f'-B {self.out_path}')
|
|
return ' '.join(cmd_parts)
|
|
|
|
def _get_build_cmd(self):
|
|
cmd_parts = ['cmake --build']
|
|
# In CMake, the build dir arg (-B) can't be combined w/ --preset
|
|
if self.build_preset is not None:
|
|
cmd_parts.append(f'--preset {self.build_preset}')
|
|
elif self.out_path is not None:
|
|
cmd_parts.append(f'{self.out_path}')
|
|
return ' '.join(cmd_parts)
|
|
|
|
def configure(self):
|
|
cmd = self._get_configure_cmd()
|
|
print(f'configure_cmd: {cmd}')
|
|
subprocess.run(cmd).check_returncode()
|
|
if self.out_path is not None:
|
|
copy(
|
|
SparkPaths.tools().joinpath('cpp.build.props'),
|
|
self.out_path.joinpath('Directory.Build.Props'))
|
|
|
|
def build(self):
|
|
cmd = self._get_build_cmd()
|
|
print(f'build_cmd: {cmd}')
|
|
subprocess.run(cmd).check_returncode()
|
|
|
|
def __str__(self):
|
|
return fmt_table(
|
|
{
|
|
'name': self.name,
|
|
'configure_cmd': self._get_configure_cmd(),
|
|
'build_cmd': self._get_build_cmd(),
|
|
})
|
|
|
|
|
|
def get_cmake_build_type(build_type: str):
|
|
'''
|
|
Converts a case-insensitive build-type str (from cli) to a case-sensitive CMAKE_BUILD_TYPE var
|
|
'''
|
|
|
|
# The 'build_type' arg is case-sensitive because 'CMAKE_BUILD_TYPE' is substitued for a folder
|
|
# in the externals-libs path. In order to match a real path, it must be 'Debug' or 'Release'.
|
|
# Keep the orig_build_type around for potential use in err msg.
|
|
orig_build_type = build_type
|
|
build_type = build_type or 'debug'
|
|
build_type = build_type.title()
|
|
if build_type not in ['Debug', 'Release']:
|
|
raise ValueError(f"Unexpected build_type '{orig_build_type}'")
|
|
return build_type
|