Skip to content

Commit 58b2f80

Browse files
KallynGowdytritao
authored andcommitted
Implement xcode-select based path selection
1 parent fb79742 commit 58b2f80

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/Core/Toolchains/XcodeToolchain.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Diagnostics;
34
using System.Linq;
45

56
namespace CppSharp
@@ -8,15 +9,7 @@ public static class XcodeToolchain
89
{
910
public static string GetXcodePath()
1011
{
11-
var toolchains = Directory.EnumerateDirectories("/Applications", "Xcode*")
12-
.ToList();
13-
toolchains.Sort();
14-
15-
var toolchainPath = toolchains.LastOrDefault();
16-
if (toolchainPath == null)
17-
throw new Exception("Could not find a valid Xcode SDK");
18-
19-
return toolchainPath;
12+
return GetXcodePathFromXcodeSelect() ?? GetXcodePathFromFileSystem();
2013
}
2114

2215
public static string GetXcodeToolchainPath()
@@ -73,5 +66,41 @@ public static string GetXcodeIncludesFolder()
7366

7467
return Path.Combine(sdkPath, "usr/include");
7568
}
69+
70+
private static string GetXcodePathFromXcodeSelect()
71+
{
72+
try
73+
{
74+
var xcodeSelect = Process.Start(new ProcessStartInfo("xcode-select", "--print-path") {
75+
RedirectStandardOutput = true,
76+
UseShellExecute = false
77+
});
78+
79+
var result = xcodeSelect.StandardOutput.ReadToEnd();
80+
81+
if(!string.IsNullOrEmpty(result))
82+
{
83+
var extraStuffIndex = result.LastIndexOf("/Contents/Developer");
84+
if(extraStuffIndex >= 0)
85+
return result.Remove(extraStuffIndex);
86+
}
87+
} catch {
88+
// TODO: Log exception?
89+
}
90+
return null;
91+
}
92+
93+
private static string GetXcodePathFromFileSystem()
94+
{
95+
var toolchains = Directory.EnumerateDirectories("/Applications", "Xcode*")
96+
.ToList();
97+
toolchains.Sort();
98+
99+
var toolchainPath = toolchains.LastOrDefault();
100+
if (toolchainPath == null)
101+
throw new Exception("Could not find a valid Xcode SDK");
102+
103+
return toolchainPath;
104+
}
76105
}
77106
}

0 commit comments

Comments
 (0)