1
1
using System ;
2
2
using System . IO ;
3
+ using System . Diagnostics ;
3
4
using System . Linq ;
4
5
5
6
namespace CppSharp
@@ -8,15 +9,7 @@ public static class XcodeToolchain
8
9
{
9
10
public static string GetXcodePath ( )
10
11
{
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 ( ) ;
20
13
}
21
14
22
15
public static string GetXcodeToolchainPath ( )
@@ -73,5 +66,41 @@ public static string GetXcodeIncludesFolder()
73
66
74
67
return Path . Combine ( sdkPath , "usr/include" ) ;
75
68
}
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
+ }
76
105
}
77
106
}
0 commit comments