From ccbb1dd5c95b831aaee7fb42cf9c7ca36d6bdcd2 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 12 Oct 2022 10:27:38 -0400 Subject: [PATCH] build (refactor): Add cmake_utils.py in py/core So it can be referenced by both client/server build scripts --- tools/py/build_sparkd.py | 52 ++--------------------------- tools/py/core/cmake_utils.py | 65 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 50 deletions(-) create mode 100644 tools/py/core/cmake_utils.py diff --git a/tools/py/build_sparkd.py b/tools/py/build_sparkd.py index d839a9987..e12fe5699 100644 --- a/tools/py/build_sparkd.py +++ b/tools/py/build_sparkd.py @@ -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, diff --git a/tools/py/core/cmake_utils.py b/tools/py/core/cmake_utils.py new file mode 100644 index 000000000..5cc35ffda --- /dev/null +++ b/tools/py/core/cmake_utils.py @@ -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