@@ -14,7 +14,6 @@ public class Builder : IDisposable
1414 const string fixed_linux_xbuild_path = "/usr/bin" ;
1515 const string xbuildapp = "xbuild" ;
1616 const string msbuildapp = "msbuild" ;
17- string msbuildExe ;
1817
1918 public bool IsUnix { get ; set ; }
2019 public bool RunningMSBuild { get ; set ; }
@@ -36,11 +35,57 @@ string GetUnixBuildExe ()
3635 return File . Exists ( path ) ? path : msbuildapp ;
3736 }
3837
38+ string GetVisualStudio2017Directory ( )
39+ {
40+ var editions = new [ ] {
41+ "Enterprise" ,
42+ "Professional" ,
43+ "Community" ,
44+ "BuildTools"
45+ } ;
46+
47+ var x86 = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) ;
48+ foreach ( var edition in editions ) {
49+ var dir = Path . Combine ( x86 , "Microsoft Visual Studio" , "2017" , edition ) ;
50+ if ( Directory . Exists ( dir ) )
51+ return dir ;
52+ }
53+
54+ return null ;
55+ }
56+
57+ string GetWindowsBuildExe ( )
58+ {
59+ RunningMSBuild = true ;
60+
61+ //First try environment variable
62+ string msbuildExe = Environment . GetEnvironmentVariable ( "XA_MSBUILD_EXE" ) ;
63+ if ( ! string . IsNullOrEmpty ( msbuildExe ) && File . Exists ( msbuildExe ) )
64+ return msbuildExe ;
65+
66+ //Next try VS 2017, MSBuild 15.0
67+ var visualStudioDirectory = GetVisualStudio2017Directory ( ) ;
68+ if ( ! string . IsNullOrEmpty ( visualStudioDirectory ) ) {
69+ msbuildExe = Path . Combine ( visualStudioDirectory , "MSBuild" , "15.0" , "Bin" , "MSBuild.exe" ) ;
70+
71+ if ( File . Exists ( msbuildExe ) )
72+ return msbuildExe ;
73+ }
74+
75+ //Try older than VS 2017, MSBuild 14.0
76+ msbuildExe = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) , "MSBuild" , "14.0" , "Bin" , "MSBuild.exe" ) ;
77+ if ( File . Exists ( msbuildExe ) )
78+ return msbuildExe ;
79+
80+ //MSBuild 4.0 last resort
81+ return Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . Windows ) , "Microsoft.NET" , "Framework" , "v4.0.30319" , "MSBuild.exe" ) ;
82+ }
83+
3984 public string MSBuildExe {
4085 get {
4186 return IsUnix
4287 ? GetUnixBuildExe ( )
43- : msbuildExe ;
88+ : GetWindowsBuildExe ( ) ;
4489 }
4590 }
4691
@@ -59,6 +104,10 @@ public string FrameworkLibDirectory {
59104 return Path . Combine ( outdir , "lib" , "xamarin.android" ) ;
60105 }
61106 else {
107+ var visualStudioDirectory = GetVisualStudio2017Directory ( ) ;
108+ if ( ! string . IsNullOrEmpty ( visualStudioDirectory ) )
109+ return Path . Combine ( visualStudioDirectory , "MSBuild" , "Xamarin" , "Android" ) ;
110+
62111 var x86 = Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) ;
63112 return Path . Combine ( x86 , "MSBuild" , "Xamarin" , "Android" ) ;
64113 }
@@ -113,13 +162,6 @@ public Builder ()
113162 {
114163 IsUnix = Environment . OSVersion . Platform != PlatformID . Win32NT ;
115164 BuildLogFile = "build.log" ;
116- // Allow the override of the location of MSBuild and try a couple of backup paths for
117- // MSBuild 14.0 and 4.0
118- msbuildExe = Environment . GetEnvironmentVariable ( "XA_MSBUILD_EXE" ) ;
119- if ( String . IsNullOrEmpty ( msbuildExe ) || ! File . Exists ( msbuildExe ) )
120- msbuildExe = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ProgramFilesX86 ) , "MSBuild" , "14.0" , "Bin" , "MSBuild.exe" ) ;
121- if ( ! File . Exists ( msbuildExe ) )
122- msbuildExe = string . Format ( "{0}\\ Microsoft.NET\\ Framework\\ v4.0.30319\\ msbuild.exe" , Environment . GetEnvironmentVariable ( "WINDIR" ) ) ;
123165 }
124166
125167 public void Dispose ( )
0 commit comments