2222using Microsoft . Win32 ;
2323
2424using System ;
25+ using System . Collections . Concurrent ;
2526using System . Collections . Generic ;
2627using System . IO ;
2728using System . Runtime . InteropServices ;
@@ -31,7 +32,10 @@ namespace DNNE.BuildTasks
3132{
3233 public class Windows
3334 {
34- private static readonly Lazy < string > g_VsInstallPath = new Lazy < string > ( GetLatestVSWithVCInstallPath , true ) ;
35+ // Simple lazy dictionary for storing VS install paths by architecture
36+ private static readonly ConcurrentDictionary < string , string > g_VsInstallPaths = new ConcurrentDictionary < string , string > ( ) ;
37+ private static string GetVsInstallPath ( string arch ) => g_VsInstallPaths . GetOrAdd ( arch , GetLatestVSWithVCInstallPath ) ;
38+
3539 private static readonly Lazy < SDK > g_WinSdk = new Lazy < SDK > ( GetLatestWinSDK , true ) ;
3640 private static readonly Lazy < SDK > g_NetFxSdk = new Lazy < SDK > ( GetLatestNetFxSDK , true ) ;
3741
@@ -41,19 +45,17 @@ public static void ConstructCommandLine(CreateCompileCommand export, out string
4145
4246 SDK winSdk = g_WinSdk . Value ;
4347 SDK netFxSdk = default ;
44- string vsInstall = g_VsInstallPath . Value ;
48+ string vcArch = ConvertToVCArchString ( export . Architecture , export . RuntimeID ) ;
49+ string vsInstall = GetVsInstallPath ( vcArch ) ;
4550 string vcToolDir = GetVCToolsRootDir ( vsInstall ) ;
4651 export . Report ( CreateCompileCommand . DevImportance , $ "VS Install: { vsInstall } \n VC Tools: { vcToolDir } \n WinSDK Version: { winSdk . Version } ") ;
4752
4853 bool isDebug = IsDebug ( export . Configuration ) ;
4954
50- string archDir = ConvertToVCArchSubDir ( export . Architecture , export . RuntimeID ) ;
51-
5255 // VC inc and lib paths
5356 var vcIncDir = Path . Combine ( vcToolDir , "include" ) ;
54- var libDir = Path . Combine ( vcToolDir , "lib" , archDir ) ;
55-
56- var binDir = GetVCHostBinDir ( vcToolDir , archDir ) ;
57+ var libDir = Path . Combine ( vcToolDir , "lib" , vcArch ) ;
58+ var binDir = GetVCHostBinDir ( vcToolDir , vcArch ) ;
5759
5860 string compileAsFlag ;
5961 string hostLib ;
@@ -144,15 +146,15 @@ public static void ConstructCommandLine(CreateCompileCommand export, out string
144146 // Add WinSDK lib paths
145147 foreach ( var libPath in winSdk . LibPaths )
146148 {
147- linkerFlags . Append ( $ "/LIBPATH:\" { Path . Combine ( libPath , archDir ) } \" ") ;
149+ linkerFlags . Append ( $ "/LIBPATH:\" { Path . Combine ( libPath , vcArch ) } \" ") ;
148150 }
149151
150152 if ( export . IsTargetingNetFramework )
151153 {
152154 // Add NetFx lib paths
153155 foreach ( var libPath in netFxSdk . LibPaths )
154156 {
155- linkerFlags . Append ( $ "/LIBPATH:\" { Path . Combine ( libPath , archDir ) } \" ") ;
157+ linkerFlags . Append ( $ "/LIBPATH:\" { Path . Combine ( libPath , vcArch ) } \" ") ;
156158 }
157159 }
158160
@@ -219,7 +221,7 @@ static string HostSubDirectory(Architecture arch)
219221 }
220222 }
221223
222- private static string ConvertToVCArchSubDir ( string arch , string rid )
224+ private static string ConvertToVCArchString ( string arch , string rid )
223225 {
224226 return arch . ToLower ( ) switch
225227 {
@@ -283,10 +285,17 @@ private static string GetVCToolsRootDir(string vsInstallDir)
283285 return latestPath ?? throw new Exception ( "Unknown VC Tools version found." ) ;
284286 }
285287
286- private static string GetLatestVSWithVCInstallPath ( )
288+ private static string GetLatestVSWithVCInstallPath ( string arch )
287289 {
288290 var setupConfig = new SetupConfiguration ( ) ;
289291 IEnumSetupInstances enumInst = setupConfig . EnumInstances ( ) ;
292+ var neededPkgId = arch switch
293+ {
294+ "x64" => "Microsoft.VisualStudio.Component.VC.Tools.x86.x64" ,
295+ "x86" => "Microsoft.VisualStudio.Component.VC.Tools.x86.x64" ,
296+ "arm64" => "Microsoft.VisualStudio.Component.VC.Tools.ARM64" ,
297+ _ => throw new NotImplementedException ( $ "Unsupported architecture: '{ arch } '")
298+ } ;
290299
291300 var latestVersion = new Version ( ) ;
292301 ISetupInstance latestVsInstance = null ;
@@ -307,8 +316,7 @@ private static string GetLatestVSWithVCInstallPath()
307316 foreach ( var n in pkgs )
308317 {
309318 var pkgId = n . GetId ( ) ;
310- if ( pkgId . Equals ( "Microsoft.VisualStudio.Component.VC.Tools.x86.x64" )
311- || pkgId . Equals ( "Microsoft.VisualStudio.Component.VC.Tools.ARM64" ) )
319+ if ( pkgId . Equals ( neededPkgId ) )
312320 {
313321 if ( latestVersion < ver )
314322 {
0 commit comments