From 4264b4aff4b477ed060e3d926de6a7e10fa79871 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 29 Mar 2020 11:02:52 +0000 Subject: project.cmake: insignificant refactoring --- project/ci/appveyor/cmake.py | 63 +++++++++---------------- project/ci/travis/cmake.py | 24 ++++------ project/cmake/build.py | 109 ++++++++++++++++++++++++++----------------- 3 files changed, 96 insertions(+), 100 deletions(-) diff --git a/project/ci/appveyor/cmake.py b/project/ci/appveyor/cmake.py index 66e89f0..1473ddc 100644 --- a/project/ci/appveyor/cmake.py +++ b/project/ci/appveyor/cmake.py @@ -17,7 +17,9 @@ import logging import os import sys -from project.cmake.build import build +from project.cmake.build import BuildParameters, build +from project.configuration import Configuration +from project.platform import Platform import project.utils @@ -30,12 +32,12 @@ class Image(Enum): def __str__(self): return self.value - -def _parse_image(s): - try: - return Image(s) - except ValueError as e: - raise ValueError(f'unsupported AppVeyor image: {s}') from e + @staticmethod + def parse(s): + try: + return Image(s) + except ValueError as e: + raise ValueError(f'unsupported AppVeyor image: {s}') from e class Generator(Enum): @@ -60,21 +62,6 @@ class Generator(Enum): raise RuntimeError(f"don't know which generator to use for image: {image}") -class Platform(Enum): - x86 = 'Win32' - X64 = 'x64' - - def __str__(self): - return self.value - - -def _parse_platform(s): - try: - return Platform(s) - except ValueError as e: - raise ValueError(f'unsupported AppVeyor platform: {s}') from e - - def _env(name): if name not in os.environ: raise RuntimeError(f'undefined environment variable: {name}') @@ -95,16 +82,16 @@ def _get_build_dir(): def _get_generator(): - image = _parse_image(_env('APPVEYOR_BUILD_WORKER_IMAGE')) - return str(Generator.from_image(image)) + image = Image.parse(_env('APPVEYOR_BUILD_WORKER_IMAGE')) + return Generator.from_image(image) def _get_platform(): - return str(_parse_platform(_env('PLATFORM'))) + return Platform.parse(_env('PLATFORM')) def _get_configuration(): - return _env('CONFIGURATION') + return Configuration.parse(_env('CONFIGURATION')) def _parse_args(argv=None): @@ -127,21 +114,15 @@ def build_appveyor(argv=None): args = _parse_args(argv) _check_appveyor() - appveyor_argv = [ - '--build', _get_build_dir(), - '--configuration', _get_configuration(), - ] - if args.install_dir is not None: - appveyor_argv += [ - '--install', args.install_dir, - ] - appveyor_argv += [ - '--', - _get_src_dir(), - '-G', _get_generator(), - '-A', _get_platform(), - ] - build(appveyor_argv + args.cmake_args) + cmake_args = ['-G', str(_get_generator()), '-A', str(_get_platform())] + cmake_args += args.cmake_args + + params = BuildParameters(_get_src_dir(), + build_dir=_get_build_dir(), + install_dir=args.install_dir, + configuration=_get_configuration(), + cmake_args=cmake_args) + build(params) def main(argv=None): diff --git a/project/ci/travis/cmake.py b/project/ci/travis/cmake.py index cb96941..c7c61ca 100644 --- a/project/ci/travis/cmake.py +++ b/project/ci/travis/cmake.py @@ -17,7 +17,8 @@ import os import os.path import sys -from project.cmake.build import build +from project.cmake.build import BuildParameters, build +from project.configuration import Configuration import project.utils @@ -41,7 +42,7 @@ def _get_build_dir(): def _get_configuration(): - return _env('configuration') + return Configuration.parse(_env('configuration')) def _parse_args(argv=None): @@ -64,19 +65,12 @@ def build_travis(argv=None): args = _parse_args(argv) _check_travis() - travis_argv = [ - '--build', _get_build_dir(), - '--configuration', _get_configuration(), - ] - if args.install_dir is not None: - travis_argv += [ - '--install', args.install_dir, - ] - travis_argv += [ - '--', - _get_src_dir(), - ] - build(travis_argv + args.cmake_args) + params = BuildParameters(_get_src_dir(), + build_dir=_get_build_dir(), + install_dir=args.install_dir, + configuration=_get_configuration(), + cmake_args=args.cmake_args) + build(params) def main(argv=None): diff --git a/project/cmake/build.py b/project/cmake/build.py index 2400db2..77242ec 100644 --- a/project/cmake/build.py +++ b/project/cmake/build.py @@ -45,40 +45,24 @@ from project.configuration import Configuration import project.utils -@contextmanager -def _create_build_dir(args): - if args.build_dir is not None: - logging.info('Build directory: %s', args.build_dir) - yield args.build_dir - return - - with tempfile.TemporaryDirectory(dir=os.path.dirname(args.src_dir)) as build_dir: - logging.info('Build directory: %s', build_dir) - try: - yield build_dir - finally: - logging.info('Removing build directory: %s', build_dir) - return - - class GenerationPhase: - def __init__(self, build_dir, args): + def __init__(self, build_dir, params): self.build_dir = build_dir - self.args = args + self.params = params def _cmake_args(self): - return self._to_cmake_args(self.build_dir, self.args) + return self._to_cmake_args(self.build_dir, self.params) @staticmethod - def _to_cmake_args(build_dir, args): + def _to_cmake_args(build_dir, params): result = [] - if args.install_dir is not None: - result += ['-D', f'CMAKE_INSTALL_PREFIX={args.install_dir}'] - if args.configuration is not None: - result += ['-D', f'CMAKE_BUILD_TYPE={args.configuration}'] - if args.cmake_args is not None: - result += args.cmake_args - result += [f'-B{build_dir}', f'-H{args.src_dir}'] + if params.install_dir is not None: + result += ['-D', f'CMAKE_INSTALL_PREFIX={params.install_dir}'] + if params.configuration is not None: + result += ['-D', f'CMAKE_BUILD_TYPE={params.configuration}'] + if params.cmake_args is not None: + result += params.cmake_args + result += [f'-B{build_dir}', f'-H{params.src_dir}'] return result def run(self): @@ -86,19 +70,19 @@ class GenerationPhase: class BuildPhase: - def __init__(self, build_dir, args): + def __init__(self, build_dir, params): self.build_dir = build_dir - self.args = args + self.params = params def _cmake_args(self): - return self._to_cmake_args(self.build_dir, self.args) + return self._to_cmake_args(self.build_dir, self.params) @staticmethod - def _to_cmake_args(build_dir, args): + def _to_cmake_args(build_dir, params): result = ['--build', build_dir] - if args.configuration is not None: - result += ['--config', str(args.configuration)] - if args.install_dir is not None: + if params.configuration is not None: + result += ['--config', str(params.configuration)] + if params.install_dir is not None: result += ['--target', 'install'] return result @@ -106,6 +90,52 @@ class BuildPhase: project.utils.run_cmake(self._cmake_args()) +class BuildParameters: + def __init__(self, src_dir, build_dir=None, install_dir=None, + configuration=Configuration.DEBUG, cmake_args=None): + + src_dir = project.utils.normalize_path(src_dir) + if build_dir is not None: + build_dir = project.utils.normalize_path(build_dir) + if install_dir is not None: + install_dir = project.utils.normalize_path(install_dir) + if cmake_args is None: + cmake_args = [] + + self.src_dir = src_dir + self.build_dir = build_dir + self.install_dir = install_dir + self.configuration = configuration + self.cmake_args = cmake_args + + @staticmethod + def from_args(args): + return BuildParameters(**vars(args)) + + @contextmanager + def create_build_dir(self): + if self.build_dir is not None: + logging.info('Build directory: %s', self.build_dir) + yield self.build_dir + return + + with tempfile.TemporaryDirectory(dir=os.path.dirname(self.src_dir)) as build_dir: + logging.info('Build directory: %s', build_dir) + try: + yield build_dir + finally: + logging.info('Removing build directory: %s', build_dir) + return + + +def build(params): + with params.create_build_dir() as build_dir: + gen_phase = GenerationPhase(build_dir, params) + gen_phase.run() + build_phase = BuildPhase(build_dir, params) + build_phase.run() + + def _parse_args(argv=None): if argv is None: argv = sys.argv[1:] @@ -133,18 +163,9 @@ def _parse_args(argv=None): return args -def build(argv=None): - args = _parse_args(argv) - with _create_build_dir(args) as build_dir: - gen_phase = GenerationPhase(build_dir, args) - gen_phase.run() - build_phase = BuildPhase(build_dir, args) - build_phase.run() - - def main(argv=None): with project.utils.setup_logging(): - build(argv) + build(BuildParameters.from_args(_parse_args(argv))) if __name__ == '__main__': -- cgit v1.2.3