| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os |
| 7 |
import argparse |
| 8 |
import subprocess |
| 9 |
from pathlib import Path |
| 10 |
import typing as T |
| 11 |
|
| 12 |
from ..mesonlib import Popen_safe, split_args, determine_worker_count |
| 13 |
from .. import mlog |
| 14 |
|
| 15 |
class ExternalProject: |
| 16 |
def __init__(self, options: argparse.Namespace): |
| 17 |
self.name = options.name |
| 18 |
self.src_dir = options.srcdir |
| 19 |
self.build_dir = options.builddir |
| 20 |
self.install_dir = options.installdir |
| 21 |
self.log_dir = options.logdir |
| 22 |
self.verbose = options.verbose |
| 23 |
self.stampfile = options.stampfile |
| 24 |
self.depfile = options.depfile |
| 25 |
self.make = split_args(options.make) |
| 26 |
|
| 27 |
def write_depfile(self) -> None: |
| 28 |
with open(self.depfile, 'w', encoding='utf-8') as f: |
| 29 |
f.write(f'{self.stampfile}: \\\n') |
| 30 |
for dirpath, dirnames, filenames in os.walk(self.src_dir): |
| 31 |
dirnames[:] = [d for d in dirnames if not d.startswith('.')] |
| 32 |
for fname in filenames: |
| 33 |
if fname.startswith('.'): |
| 34 |
continue |
| 35 |
path = Path(dirpath, fname) |
| 36 |
f.write(' {} \\\n'.format(path.as_posix().replace(' ', '\\ '))) |
| 37 |
|
| 38 |
def write_stampfile(self) -> None: |
| 39 |
with open(self.stampfile, 'w', encoding='utf-8'): |
| 40 |
pass |
| 41 |
|
| 42 |
def supports_jobs_flag(self) -> bool: |
| 43 |
p, o, e = Popen_safe(self.make + ['--version']) |
| 44 |
if p.returncode == 0 and ('GNU Make' in o or 'waf' in o): |
| 45 |
return True |
| 46 |
return False |
| 47 |
|
| 48 |
def build(self) -> int: |
| 49 |
make_cmd = self.make.copy() |
| 50 |
if self.supports_jobs_flag(): |
| 51 |
make_cmd.append(f'-j{determine_worker_count()}') |
| 52 |
rc = self._run('build', make_cmd) |
| 53 |
if rc != 0: |
| 54 |
return rc |
| 55 |
|
| 56 |
install_cmd = self.make.copy() |
| 57 |
install_env = {} |
| 58 |
install_env['DESTDIR'] = self.install_dir |
| 59 |
install_cmd.append('install') |
| 60 |
rc = self._run('install', install_cmd, install_env) |
| 61 |
if rc != 0: |
| 62 |
return rc |
| 63 |
|
| 64 |
self.write_depfile() |
| 65 |
self.write_stampfile() |
| 66 |
|
| 67 |
return 0 |
| 68 |
|
| 69 |
def _run(self, step: str, command: T.List[str], env: T.Optional[T.Dict[str, str]] = None) -> int: |
| 70 |
m = 'Running command ' + str(command) + ' in directory ' + str(self.build_dir) + '\n' |
| 71 |
logfile = Path(self.log_dir, f'{self.name}-{step}.log') |
| 72 |
output = None |
| 73 |
if not self.verbose: |
| 74 |
output = open(logfile, 'w', encoding='utf-8') |
| 75 |
output.write(m + '\n') |
| 76 |
output.flush() |
| 77 |
else: |
| 78 |
print(m) |
| 79 |
run_env = os.environ.copy() |
| 80 |
if env: |
| 81 |
run_env.update(env) |
| 82 |
p, o, e = Popen_safe(command, stderr=subprocess.STDOUT, stdout=output, |
| 83 |
cwd=self.build_dir, |
| 84 |
env=run_env) |
| 85 |
if p.returncode != 0: |
| 86 |
m = f'{step} step returned error code {p.returncode}.' |
| 87 |
if not self.verbose: |
| 88 |
m += '\nSee logs: ' + str(logfile) |
| 89 |
print(m) |
| 90 |
contents = mlog.ci_fold_file(logfile, f'CI platform detected, click here for {os.path.basename(logfile)} contents.') |
| 91 |
if contents: |
| 92 |
print(contents) |
| 93 |
return p.returncode |
| 94 |
|
| 95 |
def run(args: T.List[str]) -> int: |
| 96 |
parser = argparse.ArgumentParser() |
| 97 |
parser.add_argument('--name') |
| 98 |
parser.add_argument('--srcdir') |
| 99 |
parser.add_argument('--builddir') |
| 100 |
parser.add_argument('--installdir') |
| 101 |
parser.add_argument('--logdir') |
| 102 |
parser.add_argument('--make') |
| 103 |
parser.add_argument('--verbose', action='store_true') |
| 104 |
parser.add_argument('stampfile') |
| 105 |
parser.add_argument('depfile') |
| 106 |
|
| 107 |
options = parser.parse_args(args) |
| 108 |
ep = ExternalProject(options) |
| 109 |
return ep.build() |