build (refactor): Add cmake_utils.py in py/core

So it can be referenced by both client/server build scripts
This commit is contained in:
John Colvin committed 2022-10-12 20:13:18 -04:00
1 parent e24c767c41
commit ccbb1dd5c9
2 files changed
+67 -50

No files matched your search

+2 -50
View File
@@ -3,60 +3,12 @@ from typing import cast
import subprocess
from core.argparse_utils import add_toggle_opt, get_arg
from core.cmake_utils import CMakeCompilerPair, CMakeConfigureCommand
from core.file_utils import CWD, SparkPaths, as_path, create_dir, ensure_exists, link_dir, unlink
from core.iter_utils import flatten
from core.logging import exit_msg
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, src_dir, build_dir, generator, build_type, compiler: CMakeCompilerPair):
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)
self.add_opt('CMAKE_BUILD_TYPE', build_type)
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
def _parse_compiler(arg: str or None):
'''Parses compiler input (from cli) and returns a tuple of c, cxx compilers respectively'''
known_compilers = {
@@ -228,7 +180,7 @@ def _configure(
cmake_build_type = _get_cmake_build_type(build_type)
cmd = CMakeConfigureCommand(
src_dir=src_dir,
source_dir=src_dir,
build_dir=build_dir,
generator='Ninja',
build_type=cmake_build_type,
+65
View File
@@ -0,0 +1,65 @@
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