@@ -119,13 +119,13 @@ def find_visual_studio(version_ranges):
119119 for version_range in version_ranges :
120120 command = [
121121 str (vswhere ), "-version" , version_range , "-requires" , "Microsoft.VisualStudio.Component.VC.Tools.x86.x64" ,
122- "-property" , "installationPath" , "-prerelease"
122+ "-products" , "*" , "- property" , "installationPath" , "-prerelease"
123123 ]
124124
125125 try :
126126 output = subprocess .check_output (command , text = True ).strip ()
127127 if output :
128- return output
128+ return output . split ( " \n " )[ 0 ]
129129 except subprocess .CalledProcessError :
130130 continue
131131
@@ -146,6 +146,13 @@ def set_env_vars(vs_path, arch="x64"):
146146 os .environ [var ] = value
147147
148148
149+ def initialize_visual_studio_env (version_ranges , arch = "x64" ):
150+ vs_path = find_visual_studio (version_ranges )
151+ if not vs_path :
152+ raise EnvironmentError ("Visual Studio not found in specified version ranges." )
153+ set_env_vars (vs_path , arch )
154+
155+
149156# Taken from https://github.com/pytorch/pytorch/blob/master/tools/setup_helpers/env.py
150157def check_env_flag (name : str , default : str = "" ) -> bool :
151158 return os .getenv (name , default ).upper () in ["ON" , "1" , "YES" , "TRUE" , "Y" ]
@@ -447,10 +454,7 @@ def build_extension(self, ext):
447454 lit_dir = shutil .which ('lit' )
448455 ninja_dir = shutil .which ('ninja' )
449456 if platform .system () == "Windows" :
450- vs_path = find_visual_studio (["[17.0,18.0)" , "[16.0,17.0)" ])
451- env = set_env_vars (vs_path )
452- if not vs_path :
453- raise EnvironmentError ("Visual Studio 2019 or 2022 not found." )
457+ initialize_visual_studio_env (["[17.0,18.0)" , "[16.0,17.0)" ])
454458 # lit is used by the test suite
455459 thirdparty_cmake_args = get_thirdparty_packages ([get_llvm_package_info ()])
456460 thirdparty_cmake_args += self .get_pybind11_cmake_args ()
0 commit comments