import hashlib, os, shutil, subprocess from pathlib import Path ROOT=Path(__file__).resolve().parents[1] def digest(path):return hashlib.sha256(path.read_bytes()).hexdigest() def cpp_environment(output): env = os.environ.copy() if os.name != 'nt': return os.environ.get('CXX', 'c++'), env finder = Path(os.environ.get('ProgramFiles(x86)', r'C:\Program Files (x86)')) / 'Microsoft Visual Studio/Installer/vswhere.exe' vs = subprocess.check_output([str(finder), '-latest', '-version', '[17.0,18.0)', '-products', '*', '-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64', '-property', 'installationPath'], text=True).strip() if not vs or any(c in vs for c in '%!"&^<>\r\n'): raise RuntimeError('No usable VS2022 C++ toolchain') batch = output / 'vs-environment.cmd' batch.write_text(f'@echo off\ncall "{vs}/Common7/Tools/VsDevCmd.bat" -arch=x64 -host_arch=x64 >nul\nif errorlevel 1 exit /b %errorlevel%\nset\n', encoding='utf-8') setup = subprocess.check_output([os.environ.get('COMSPEC', 'cmd.exe'), '/d', '/c', str(batch)], text=True, errors='replace') # Developer environment may contain private values; never print or save it. env.update((key.upper(), value) for key, value in (line.split('=', 1) for line in setup.splitlines() if '=' in line and not line.startswith('='))) compiler = shutil.which('cl.exe', path=env['PATH']) if not compiler: raise RuntimeError('VS environment did not provide cl.exe') return compiler, env