@@ -197,6 +197,7 @@ def __init__(self, cmd_line_args=None):
197
197
self .options .refresh_all = self .cmd_line_args .get ("refresh_all" , False )
198
198
self .prompted_options = set ()
199
199
self .compiler_info = {}
200
+ self .env = None
200
201
201
202
def prompt_string (self , prompt , default ):
202
203
"""
@@ -419,7 +420,7 @@ def run_cmd(self, cmd, cwd=None):
419
420
print (f"{ color } { cwd } > { cmd_str } { reset } " )
420
421
else :
421
422
print (f"{ color } { cwd } > { cmd } { reset } " )
422
- r = subprocess .run (cmd , shell = isinstance (cmd , str ), check = True , cwd = cwd )
423
+ r = subprocess .run (cmd , shell = isinstance (cmd , str ), check = True , cwd = cwd , env = self . env )
423
424
if r .returncode != 0 :
424
425
raise RuntimeError (f"Command '{ cmd } ' failed with return code { r .returncode } ." )
425
426
@@ -668,6 +669,8 @@ def is_non_empty_dir(self, path):
668
669
669
670
@lru_cache (maxsize = 1 )
670
671
def get_vs_install_locations (self ):
672
+ if not self .is_windows ():
673
+ return []
671
674
p = os .environ .get ('ProgramFiles(x86)' , r"C:\Program Files (x86)" )
672
675
path_vswhere = os .path .join (p ,
673
676
"Microsoft Visual Studio" , "Installer" , "vswhere.exe" )
@@ -684,6 +687,8 @@ def get_vs_install_locations(self):
684
687
return [inst .get ("installationPath" ) for inst in info ]
685
688
686
689
def find_vs_tool (self , tool ):
690
+ if not self .is_windows ():
691
+ return None
687
692
vs_tools = ["cmake" , "ninja" , "git" , "python" ]
688
693
if tool not in vs_tools :
689
694
return None
@@ -923,6 +928,61 @@ def probe_compilers(self):
923
928
print (f"Default C++ compiler: { self .compiler_info .get ('CMAKE_CXX_COMPILER_ID' , 'unknown' )} ({ self .compiler_info .get ('CMAKE_CXX_COMPILER' , 'unknown' )} )" )
924
929
print (f"Default C++ build system: { self .compiler_info .get ('CMAKE_GENERATOR' , 'unknown' )} " )
925
930
931
+ @lru_cache (maxsize = 1 )
932
+ def probe_msvc_dev_env (self ):
933
+ if not self .is_windows ():
934
+ return
935
+ print ("Probing MSVC development environment variables..." )
936
+ vs_roots = self .get_vs_install_locations ()
937
+ vcvarsall_path = None
938
+ for vs_root in vs_roots or []:
939
+ vcvarsall_path_candidate = os .path .join (vs_root , "VC" , "Auxiliary" , "Build" , "vcvarsall.bat" )
940
+ if os .path .exists (vcvarsall_path_candidate ):
941
+ vcvarsall_path = vcvarsall_path_candidate
942
+ print (f"Found vcvarsall.bat at { vcvarsall_path } ." )
943
+ break
944
+ if not vcvarsall_path :
945
+ print ("No vcvarsall.bat found. MSVC development environment variables will not be set." )
946
+ return
947
+ # Run it with x64 argument and VSCMD_DEBUG=2 set and get the output
948
+ cmd = [vcvarsall_path , "x64" ]
949
+ env = os .environ .copy ()
950
+ env ["VSCMD_DEBUG" ] = "2"
951
+ result = subprocess .run (cmd , env = env , text = True , capture_output = True , shell = True )
952
+ # print the output
953
+ if result .returncode != 0 :
954
+ raise RuntimeError (f"vcvarsall.bat failed:\n { result .stdout } \n { result .stderr } " )
955
+
956
+ # Get the lines in the output between the two lines that among other things say
957
+ post_env = {}
958
+ in_post_init_header = False
959
+ for line in result .stdout .splitlines ():
960
+ contains_post_init_header = "--------------------- VS Developer Command Prompt Environment [post-init] ---------------------" in line
961
+ if contains_post_init_header :
962
+ if in_post_init_header :
963
+ break
964
+ in_post_init_header = True
965
+ continue
966
+ if not in_post_init_header :
967
+ continue
968
+ if '=' in line :
969
+ key , value = line .split ('=' , 1 )
970
+ post_env [key .strip ()] = value .strip ()
971
+ if not in_post_init_header or not post_env :
972
+ print ("No post-init environment variables found in vcvarsall.bat output." )
973
+ return
974
+
975
+ # Populate the environment with anything that changed, and later use it
976
+ # for any other commands.
977
+ self .env = os .environ .copy ()
978
+ for key , value in post_env .items ():
979
+ if key not in self .env :
980
+ print (f"* Inserting { key } ={ value } " )
981
+ elif value != self .env [key ]:
982
+ print (f"* Updating { key } ={ value } " )
983
+ self .env [key ] = value
984
+ print ("MSVC development environment variables extracted successfully." )
985
+
926
986
927
987
@lru_cache (maxsize = 1 )
928
988
def is_homebrew_clang (self ):
@@ -2255,6 +2315,7 @@ def generate_run_configs(self):
2255
2315
2256
2316
def install_all (self ):
2257
2317
self .check_compilers ()
2318
+ self .probe_msvc_dev_env ()
2258
2319
self .check_tools ()
2259
2320
self .setup_mrdocs_src_dir ()
2260
2321
self .setup_third_party_dir ()
0 commit comments