Skip to content

Commit f665e3a

Browse files
authored
Use what's returned by 'xcode-select -p' as the configured Xcode if none is specified in Visual Studio's settings. (#84)
Use what's returned by 'xcode-select -p' as the configured Xcode if none is specified in Visual Studio's settings. It looks like this was the intention in the code, but the code that calls 'xcode-select -p' would never execute, because we'd only run into it if GetConfiguredSdkLocation returned null/empty, which it never did because it returned the default location '/Applications/Xcode.app' if nothing was configured in VSfM. With this change, GetConfiguredSdkLocation will try to get the system's Xcode location ('xcode-select -p') before returning /Applications/Xcode.app. Also remove /Developer as a default location, Xcode hasn't been there in many, many years. Now the order is: 1. Settings in Visual Studio's Preferences. 2. System's Xcode (xcode-select --print-path). 3. /Applications/Xcode.app This avoids strange problems if the system's Xcode is not /Applications/Xcode.app and there's no Xcode configured in Visual Studio's settings. Ref: dotnet/macios#10003
1 parent c490a36 commit f665e3a

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

Xamarin.MacDev/AppleSdkSettings.cs

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public static class AppleSdkSettings
3838
// Put newer SDKs at the top as we scan from 0 -> List.Count
3939
public static readonly string[] DefaultRoots = new string[] {
4040
"/Applications/Xcode.app",
41-
"/Developer"
4241
};
4342
static DateTime lastWritten;
4443

@@ -107,23 +106,25 @@ public static void SetConfiguredSdkLocation (string location)
107106

108107
public static string GetConfiguredSdkLocation ()
109108
{
110-
PDictionary plist;
109+
PDictionary plist = null;
111110
PString value;
112-
bool binary;
113111

114112
try {
115113
if (File.Exists (SettingsPath))
116-
plist = PDictionary.FromFile (SettingsPath, out binary);
117-
else
118-
plist = new PDictionary ();
114+
plist = PDictionary.FromFile (SettingsPath, out var _);
119115
} catch (FileNotFoundException) {
120-
plist = new PDictionary ();
121116
}
122117

123-
if (!plist.TryGetValue ("AppleSdkRoot", out value))
124-
return DefaultRoots[0];
118+
// First try the configured location in Visual Studio
119+
if (plist != null && plist.TryGetValue ("AppleSdkRoot", out value) && !string.IsNullOrEmpty (value?.Value))
120+
return value.Value;
125121

126-
return value.Value;
122+
// Then check the system's default Xcode
123+
if (TryGetSystemXcode (out var path))
124+
return path;
125+
126+
// Finally return the hardcoded default
127+
return DefaultRoots [0];
127128
}
128129

129130
static void SetInvalid ()
@@ -152,6 +153,40 @@ static AppleSdkSettings ()
152153
Init ();
153154
}
154155

156+
static bool TryGetSystemXcode (out string path)
157+
{
158+
path = null;
159+
if (!File.Exists ("/usr/bin/xcode-select"))
160+
return false;
161+
162+
try {
163+
using var process = new Process ();
164+
process.StartInfo.FileName = "/usr/bin/xcode-select";
165+
process.StartInfo.Arguments = "--print-path";
166+
process.StartInfo.RedirectStandardOutput = true;
167+
process.StartInfo.UseShellExecute = false;
168+
process.Start ();
169+
var stdout = process.StandardOutput.ReadToEnd ();
170+
process.WaitForExit ();
171+
172+
stdout = stdout.Trim ();
173+
if (Directory.Exists (stdout)) {
174+
if (stdout.EndsWith ("/Contents/Developer", StringComparison.Ordinal))
175+
stdout = stdout.Substring (0, stdout.Length - "/Contents/Developer".Length);
176+
177+
path = stdout;
178+
return true;
179+
}
180+
181+
LoggingService.LogInfo ("The system's Xcode location {0} does not exist", stdout);
182+
183+
return false;
184+
} catch (Exception e) {
185+
LoggingService.LogInfo ("Could not get the system's Xcode location: {0}", e);
186+
return false;
187+
}
188+
}
189+
155190
public static void Init ()
156191
{
157192
string devroot = null, vplist = null, xcode = null;
@@ -160,37 +195,9 @@ public static void Init ()
160195
SetInvalid ();
161196

162197
DeveloperRoot = Environment.GetEnvironmentVariable ("MD_APPLE_SDK_ROOT");
163-
if (string.IsNullOrEmpty (DeveloperRoot)) {
198+
if (string.IsNullOrEmpty (DeveloperRoot))
164199
DeveloperRoot = GetConfiguredSdkLocation ();
165200

166-
if (string.IsNullOrEmpty (DeveloperRoot) && File.Exists ("/usr/bin/xcode-select")) {
167-
var startInfo = new ProcessStartInfo ("/usr/bin/xcode-select", "--print-path");
168-
startInfo.RedirectStandardOutput = true;
169-
startInfo.UseShellExecute = false;
170-
171-
var process = new Process ();
172-
var stdout = string.Empty;
173-
174-
try {
175-
process.StartInfo = startInfo;
176-
process.OutputDataReceived += (sender, e) => stdout += e.Data;
177-
process.Start ();
178-
process.WaitForExit ();
179-
} catch (Win32Exception) {
180-
stdout = string.Empty;
181-
}
182-
183-
stdout = stdout.Trim ();
184-
185-
if (!string.IsNullOrEmpty (stdout) && Directory.Exists (stdout)) {
186-
if (stdout.EndsWith ("/Contents/Developer", StringComparison.Ordinal))
187-
stdout = stdout.Substring (0, stdout.Length - "/Contents/Developer".Length);
188-
189-
DeveloperRoot = stdout;
190-
}
191-
}
192-
}
193-
194201
if (string.IsNullOrEmpty (DeveloperRoot)) {
195202
foreach (var v in DefaultRoots) {
196203
if (ValidateSdkLocation (v, out xcode, out vplist, out devroot)) {

Xamarin.MacDev/Xamarin.MacDev.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>net451;net461;netstandard2.0</TargetFrameworks>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
7+
<LangVersion>latest</LangVersion>
78
</PropertyGroup>
89
<ItemGroup>
910
<PackageReference Include="MicroBuild.Core" Version="0.3.0">

0 commit comments

Comments
 (0)