diff options
author | Xavier Claessens <xavier.claessens@collabora.com> | 2021-02-04 16:02:03 -0500 |
---|---|---|
committer | Xavier Claessens <xavier.claessens@collabora.com> | 2021-02-05 17:53:09 -0500 |
commit | 522392e7553823e6b3ac38cadc4fbee72eae9540 (patch) | |
tree | 0a86dd0524aa3456b6a52443db54f7c5b84102e0 /mesonbuild/scripts | |
parent | 5d94d161ff0abf72ed7c771ab3bb86e34560762e (diff) | |
download | meson-522392e7553823e6b3ac38cadc4fbee72eae9540.zip meson-522392e7553823e6b3ac38cadc4fbee72eae9540.tar.gz meson-522392e7553823e6b3ac38cadc4fbee72eae9540.tar.bz2 |
run_target: Add env kwarg
Re-implement it in backend using the same code path as for
custom_target(). This for example handle setting PATH on Windows when
command is an executable.
Diffstat (limited to 'mesonbuild/scripts')
-rw-r--r-- | mesonbuild/scripts/commandrunner.py | 84 | ||||
-rw-r--r-- | mesonbuild/scripts/meson_exe.py | 11 |
2 files changed, 8 insertions, 87 deletions
diff --git a/mesonbuild/scripts/commandrunner.py b/mesonbuild/scripts/commandrunner.py deleted file mode 100644 index aeeaa3b..0000000 --- a/mesonbuild/scripts/commandrunner.py +++ /dev/null @@ -1,84 +0,0 @@ -# Copyright 2014 The Meson development team - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""This program is a wrapper to run external commands. It determines -what to run, sets up the environment and executes the command.""" - -import sys, os, subprocess, shutil, shlex -import re -import typing as T - -def run_command(source_dir: str, build_dir: str, subdir: str, meson_command: T.List[str], command: str, arguments: T.List[str]) -> subprocess.Popen: - env = {'MESON_SOURCE_ROOT': source_dir, - 'MESON_BUILD_ROOT': build_dir, - 'MESON_SUBDIR': subdir, - 'MESONINTROSPECT': ' '.join([shlex.quote(x) for x in meson_command + ['introspect']]), - } - cwd = os.path.join(source_dir, subdir) - child_env = os.environ.copy() - child_env.update(env) - - # Is the command an executable in path? - exe = shutil.which(command) - if exe is not None: - command_array = [exe] + arguments - else:# No? Maybe it is a script in the source tree. - fullpath = os.path.join(source_dir, subdir, command) - command_array = [fullpath] + arguments - try: - return subprocess.Popen(command_array, env=child_env, cwd=cwd) - except FileNotFoundError: - print('Could not execute command "%s". File not found.' % command) - sys.exit(1) - except PermissionError: - print('Could not execute command "%s". File not executable.' % command) - sys.exit(1) - except OSError as err: - print('Could not execute command "{}": {}'.format(command, err)) - sys.exit(1) - except subprocess.SubprocessError as err: - print('Could not execute command "{}": {}'.format(command, err)) - sys.exit(1) - -def is_python_command(cmdname: str) -> bool: - end_py_regex = r'python(3|3\.\d+)?(\.exe)?$' - return re.search(end_py_regex, cmdname) is not None - -def run(args: T.List[str]) -> int: - if len(args) < 4: - print('commandrunner.py <source dir> <build dir> <subdir> <command> [arguments]') - return 1 - src_dir = args[0] - build_dir = args[1] - subdir = args[2] - meson_bin = args[3] - if is_python_command(meson_bin): - meson_command = [meson_bin, args[4]] - command = args[5] - arguments = args[6:] - else: - meson_command = [meson_bin] - command = args[4] - arguments = args[5:] - pc = run_command(src_dir, build_dir, subdir, meson_command, command, arguments) - while True: - try: - pc.wait() - break - except KeyboardInterrupt: - pass - return pc.returncode - -if __name__ == '__main__': - sys.exit(run(sys.argv[1:])) diff --git a/mesonbuild/scripts/meson_exe.py b/mesonbuild/scripts/meson_exe.py index 620f579..27db144 100644 --- a/mesonbuild/scripts/meson_exe.py +++ b/mesonbuild/scripts/meson_exe.py @@ -52,10 +52,13 @@ def run_exe(exe: ExecutableSerialisation, extra_env: T.Optional[dict] = None) -> ['Z:' + p for p in exe.extra_paths] + child_env.get('WINEPATH', '').split(';') ) + pipe = subprocess.PIPE + if exe.verbose: + assert not exe.capture, 'Cannot capture and print to console at the same time' + pipe = None + p = subprocess.Popen(cmd_args, env=child_env, cwd=exe.workdir, - close_fds=False, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + close_fds=False, stdout=pipe, stderr=pipe) stdout, stderr = p.communicate() if p.returncode == 0xc0000135: @@ -65,6 +68,8 @@ def run_exe(exe: ExecutableSerialisation, extra_env: T.Optional[dict] = None) -> if p.returncode != 0: if exe.pickled: print('while executing {!r}'.format(cmd_args)) + if exe.verbose: + return p.returncode if not exe.capture: print('--- stdout ---') print(stdout.decode()) |