diff options
Diffstat (limited to 'mesonbuild/mcompile.py')
| -rw-r--r-- | mesonbuild/mcompile.py | 123 |
1 files changed, 66 insertions, 57 deletions
diff --git a/mesonbuild/mcompile.py b/mesonbuild/mcompile.py index 7829ffc19..e4576239b 100644 --- a/mesonbuild/mcompile.py +++ b/mesonbuild/mcompile.py @@ -14,20 +14,71 @@ """Entrypoint script for backend agnostic compile.""" -import os -import pathlib -import shutil import sys import typing as T +from pathlib import Path from . import mlog from . import mesonlib +from . import coredata from .mesonlib import MesonException +from mesonbuild.environment import detect_ninja if T.TYPE_CHECKING: import argparse + +def validate_builddir(builddir: Path): + if not (builddir / 'meson-private' / 'coredata.dat' ).is_file(): + raise MesonException('Current directory is not a meson build directory: `{}`.\n' + 'Please specify a valid build dir or change the working directory to it.\n' + 'It is also possible that the build directory was generated with an old\n' + 'meson version. Please regenerate it in this case.'.format(builddir)) + +def get_backend_from_coredata(builddir: Path) -> str: + """ + Gets `backend` option value from coredata + """ + return coredata.load(str(builddir)).get_builtin_option('backend') + +def get_parsed_args_ninja(options: 'argparse.Namespace', builddir: Path): + runner = detect_ninja() + if runner is None: + raise MesonException('Cannot find ninja.') + mlog.log('Found runner:', runner) - + cmd = [runner, '-C', builddir.as_posix()] + + # If the value is set to < 1 then don't set anything, which let's + # ninja/samu decide what to do. + if options.jobs > 0: + cmd.extend(['-j', str(options.jobs)]) + if options.load_average > 0: + cmd.extend(['-l', str(options.load_average)]) + if options.clean: + cmd.append('clean') + + return cmd + +def get_parsed_args_vs(options: 'argparse.Namespace', builddir: Path): + slns = list(builddir.glob('*.sln')) + assert len(slns) == 1, 'More than one solution in a project?' + + sln = slns[0] + cmd = ['msbuild', str(sln.resolve())] + + # In msbuild `-m` with no number means "detect cpus", the default is `-m1` + if options.jobs > 0: + cmd.append('-m{}'.format(options.jobs)) + else: + cmd.append('-m') + + if options.load_average: + mlog.warning('Msbuild does not have a load-average switch, ignoring.') + if options.clean: + cmd.extend(['/t:Clean']) + + return cmd + def add_arguments(parser: 'argparse.ArgumentParser') -> None: """Add compile specific arguments.""" parser.add_argument( @@ -53,69 +104,27 @@ def add_arguments(parser: 'argparse.ArgumentParser') -> None: '-C', action='store', dest='builddir', - type=pathlib.Path, + type=Path, default='.', help='The directory containing build files to be built.' ) def run(options: 'argparse.Namespace') -> int: - bdir = options.builddir # type: pathlib.Path - if not bdir.exists(): - raise MesonException('Path to builddir {} does not exist!'.format(str(bdir.resolve()))) - if not bdir.is_dir(): - raise MesonException('builddir path should be a directory.') + bdir = options.builddir # type: Path + validate_builddir(bdir.resolve()) cmd = [] # type: T.List[str] - runner = None # type T.Optional[str] - slns = list(bdir.glob('*.sln')) - - if (bdir / 'build.ninja').exists(): - runner = os.environ.get('NINJA') - if not runner: - if shutil.which('ninja'): - runner = 'ninja' - elif shutil.which('samu'): - runner = 'samu' - - if runner is None: - raise MesonException('Cannot find either ninja or samu.') - - cmd = [runner, '-C', bdir.as_posix()] - - # If the value is set to < 1 then don't set anything, which let's - # ninja/samu decide what to do. - if options.jobs > 0: - cmd.extend(['-j', str(options.jobs)]) - if options.load_average > 0: - cmd.extend(['-l', str(options.load_average)]) - if options.clean: - cmd.append('clean') - - # TODO: with python 3.8 this could be `elif slns := bdir.glob('*.sln'):` - elif slns: - assert len(slns) == 1, 'More than one solution in a project?' - - sln = slns[0] - cmd = ['msbuild', str(sln.resolve())] - - # In msbuild `-m` with no number means "detect cpus", the default is `-m1` - if options.jobs > 0: - cmd.append('-m{}'.format(options.jobs)) - else: - cmd.append('-m') - - if options.load_average: - mlog.warning('Msbuild does not have a load-average switch, ignoring.') - if options.clean: - cmd.extend(['/t:Clean']) - - # TODO: xcode? + + backend = get_backend_from_coredata(bdir) + if backend == 'ninja': + cmd = get_parsed_args_ninja(options, bdir) + elif backend.startswith('vs'): + cmd = get_parsed_args_vs(options, bdir) else: + # TODO: xcode? raise MesonException( - 'Could not find any runner or backend for directory {}'.format(bdir.resolve().as_posix())) - - mlog.log('Found runner:', runner) + 'Backend `{}` is not yet supported by `compile`. Use generated project files directly instead.'.format(backend)) p, *_ = mesonlib.Popen_safe(cmd, stdout=sys.stdout.buffer, stderr=sys.stderr.buffer) |
