| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2025 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os |
| 7 |
import subprocess |
| 8 |
import json |
| 9 |
import pathlib |
| 10 |
import shutil |
| 11 |
import tempfile |
| 12 |
import pickle |
| 13 |
|
| 14 |
from .. import mlog |
| 15 |
from .core import MesonException |
| 16 |
from .universal import (is_windows, windows_detect_native_arch, windows_proof_rm, |
| 17 |
get_meson_command, join_args) |
| 18 |
|
| 19 |
|
| 20 |
__all__ = [ |
| 21 |
'setup_vsenv', |
| 22 |
] |
| 23 |
|
| 24 |
|
| 25 |
# If on Windows and VS is installed but not set up in the environment, |
| 26 |
# set it to be runnable. In this way Meson can be directly invoked |
| 27 |
# from any shell, VS Code etc. |
| 28 |
def _setup_vsenv(force: bool) -> bool: |
| 29 |
if not is_windows(): |
| 30 |
return False |
| 31 |
if os.environ.get('OSTYPE') == 'cygwin': |
| 32 |
return False |
| 33 |
if 'MESON_FORCE_VSENV_FOR_UNITTEST' not in os.environ: |
| 34 |
# VSINSTALL is set when running setvars from a Visual Studio installation |
| 35 |
# Tested with Visual Studio 2012 and 2017 |
| 36 |
if 'VSINSTALLDIR' in os.environ: |
| 37 |
return False |
| 38 |
# Check explicitly for cl when on Windows |
| 39 |
if shutil.which('cl.exe'): |
| 40 |
return False |
| 41 |
if not force: |
| 42 |
if shutil.which('cc'): |
| 43 |
return False |
| 44 |
if shutil.which('gcc'): |
| 45 |
return False |
| 46 |
if shutil.which('clang'): |
| 47 |
return False |
| 48 |
if shutil.which('clang-cl'): |
| 49 |
return False |
| 50 |
|
| 51 |
root = os.environ.get("ProgramFiles(x86)") or os.environ.get("ProgramFiles") |
| 52 |
bat_locator_bin = pathlib.Path(root, 'Microsoft Visual Studio/Installer/vswhere.exe') |
| 53 |
if not bat_locator_bin.exists(): |
| 54 |
raise MesonException(f'Could not find {bat_locator_bin}') |
| 55 |
bat_json = subprocess.check_output( |
| 56 |
[ |
| 57 |
str(bat_locator_bin), |
| 58 |
'-latest', |
| 59 |
'-prerelease', |
| 60 |
'-requiresAny', |
| 61 |
'-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64', |
| 62 |
'-requires', 'Microsoft.VisualStudio.Workload.WDExpress', |
| 63 |
'-products', '*', |
| 64 |
'-utf8', |
| 65 |
'-format', |
| 66 |
'json' |
| 67 |
] |
| 68 |
) |
| 69 |
bat_info = json.loads(bat_json) |
| 70 |
if not bat_info: |
| 71 |
# VS installer installed but not VS itself maybe? |
| 72 |
raise MesonException('Could not parse vswhere.exe output') |
| 73 |
bat_root = pathlib.Path(bat_info[0]['installationPath']) |
| 74 |
if windows_detect_native_arch() == 'arm64': |
| 75 |
bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsarm64.bat' |
| 76 |
if not bat_path.exists(): |
| 77 |
bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsx86_arm64.bat' |
| 78 |
else: |
| 79 |
bat_path = bat_root / 'VC/Auxiliary/Build/vcvars64.bat' |
| 80 |
# if VS is not found try VS Express |
| 81 |
if not bat_path.exists(): |
| 82 |
bat_path = bat_root / 'VC/Auxiliary/Build/vcvarsx86_amd64.bat' |
| 83 |
if not bat_path.exists(): |
| 84 |
raise MesonException(f'Could not find {bat_path}') |
| 85 |
|
| 86 |
mlog.log('Activating VS', bat_info[0]['catalog']['productDisplayVersion']) |
| 87 |
# Write a bat file that first activates VS environment, and then calls |
| 88 |
# a Meson script that pickles the environment into a temp file. |
| 89 |
with tempfile.NamedTemporaryFile(delete=False) as env_file: |
| 90 |
pass |
| 91 |
vcvars_cmd = ['call', str(bat_path)] |
| 92 |
pickle_cmd = get_meson_command() + ['--internal', 'pickle_env', env_file.name] |
| 93 |
with tempfile.NamedTemporaryFile('w', suffix='.bat', encoding='utf-8', delete=False) as bat_file: |
| 94 |
bat_file.write(join_args(vcvars_cmd) + '\n') |
| 95 |
bat_file.write(join_args(pickle_cmd)) |
| 96 |
try: |
| 97 |
subprocess.check_call([bat_file.name], stdout=subprocess.DEVNULL) |
| 98 |
with open(env_file.name, 'rb') as f: |
| 99 |
vsenv = pickle.load(f) |
| 100 |
for k, v in vsenv.items(): |
| 101 |
os.environ[k] = v |
| 102 |
finally: |
| 103 |
windows_proof_rm(env_file.name) |
| 104 |
windows_proof_rm(bat_file.name) |
| 105 |
return True |
| 106 |
|
| 107 |
def setup_vsenv(force: bool = False) -> bool: |
| 108 |
try: |
| 109 |
return _setup_vsenv(force) |
| 110 |
except MesonException as e: |
| 111 |
if force: |
| 112 |
raise |
| 113 |
mlog.warning('Failed to activate VS environment:', str(e)) |
| 114 |
return False |