aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/mesonlib/vsenv.py
diff options
context:
space:
mode:
authorXavier Claessens <xavier.claessens@collabora.com>2022-09-13 09:38:19 -0400
committerEli Schwartz <eschwartz93@gmail.com>2022-09-28 19:36:13 -0400
commit2dfd952eb99590878430510644ffe99dd4a6a41d (patch)
tree53001a096d997511a8d12a44cf5019a2ff8d5a2b /mesonbuild/mesonlib/vsenv.py
parenta58ec322b3a9a07af83ea268341c16f972f52791 (diff)
downloadmeson-2dfd952eb99590878430510644ffe99dd4a6a41d.zip
meson-2dfd952eb99590878430510644ffe99dd4a6a41d.tar.gz
meson-2dfd952eb99590878430510644ffe99dd4a6a41d.tar.bz2
Move classes used by scripts to their own module
Those classes are used by wrapper scripts and we should not have to import the rest of mesonlib, build.py, and all their dependencies for that. This renames mesonlib/ directory to utils/ and add a mesonlib.py module that imports everything from utils/ to not have to change `import mesonlib` everywhere. It allows to import utils.core without importing the rest of mesonlib.
Diffstat (limited to 'mesonbuild/mesonlib/vsenv.py')
-rw-r--r--mesonbuild/mesonlib/vsenv.py115
1 files changed, 0 insertions, 115 deletions
diff --git a/mesonbuild/mesonlib/vsenv.py b/mesonbuild/mesonlib/vsenv.py
deleted file mode 100644
index 4eab428..0000000
--- a/mesonbuild/mesonlib/vsenv.py
+++ /dev/null
@@ -1,115 +0,0 @@
-import os
-import subprocess
-import json
-import pathlib
-import platform
-import shutil
-import tempfile
-
-from .. import mlog
-from .universal import MesonException, is_windows
-
-
-bat_template = '''@ECHO OFF
-
-call "{}"
-
-ECHO {}
-SET
-'''
-
-# If on Windows and VS is installed but not set up in the environment,
-# set it to be runnable. In this way Meson can be directly invoked
-# from any shell, VS Code etc.
-def _setup_vsenv(force: bool) -> bool:
- if not is_windows():
- return False
- if os.environ.get('OSTYPE') == 'cygwin':
- return False
- if 'MESON_FORCE_VSENV_FOR_UNITTEST' not in os.environ:
- # VSINSTALL is set when running setvars from a Visual Studio installation
- # Tested with Visual Studio 2012 and 2017
- if 'VSINSTALLDIR' in os.environ:
- return False
- # Check explicitly for cl when on Windows
- if shutil.which('cl.exe'):
- return False
- if not force:
- if shutil.which('cc'):
- return False
- if shutil.which('gcc'):
- return False
- if shutil.which('clang'):
- return False
- if shutil.which('clang-cl'):
- return False
-
- root = os.environ.get("ProgramFiles(x86)") or os.environ.get("ProgramFiles")
- bat_locator_bin = pathlib.Path(root, 'Microsoft Visual Studio/Installer/vswhere.exe')
- if not bat_locator_bin.exists():
- raise MesonException(f'Could not find {bat_locator_bin}')
- bat_json = subprocess.check_output(
- [
- str(bat_locator_bin),
- '-latest',
- '-prerelease',
- '-requiresAny',
- '-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
- '-requires', 'Microsoft.VisualStudio.Workload.WDExpress',
- '-products', '*',
- '-utf8',
- '-format',
- 'json'
- ]
- )
- bat_info = json.loads(bat_json)
- if not bat_info:
- # VS installer instelled but not VS itself maybe?
- raise MesonException('Could not parse vswhere.exe output')
- bat_root = pathlib.Path(bat_info[0]['installationPath'])
- if platform.machine() == 'ARM64':
- bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsx86_arm64.bat'
- else:
- bat_path = bat_root / 'VC/Auxiliary/Build/vcvars64.bat'
- # if VS is not found try VS Express
- if not bat_path.exists():
- bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsx86_amd64.bat'
- if not bat_path.exists():
- raise MesonException(f'Could not find {bat_path}')
-
- mlog.log('Activating VS', bat_info[0]['catalog']['productDisplayVersion'])
- bat_separator = '---SPLIT---'
- bat_contents = bat_template.format(bat_path, bat_separator)
- bat_file = tempfile.NamedTemporaryFile('w', suffix='.bat', encoding='utf-8', delete=False)
- bat_file.write(bat_contents)
- bat_file.flush()
- bat_file.close()
- bat_output = subprocess.check_output(bat_file.name, universal_newlines=True)
- os.unlink(bat_file.name)
- bat_lines = bat_output.split('\n')
- bat_separator_seen = False
- for bat_line in bat_lines:
- if bat_line == bat_separator:
- bat_separator_seen = True
- continue
- if not bat_separator_seen:
- continue
- if not bat_line:
- continue
- try:
- k, v = bat_line.split('=', 1)
- except ValueError:
- # there is no "=", ignore junk data
- pass
- else:
- os.environ[k] = v
- return True
-
-def setup_vsenv(force: bool = False) -> bool:
- try:
- return _setup_vsenv(force)
- except MesonException as e:
- if force:
- raise
- mlog.warning('Failed to activate VS environment:', str(e))
- return False