1
1
#!/usr/bin/env swift
2
2
import Foundation
3
3
4
+ #if os(Linux)
5
+ typealias Process = Task
6
+ let libCPP = " -L/usr/lib -lc++ "
7
+ #elseif os(macOS)
8
+ let libCPP = " -lc++ "
9
+ #endif
10
+
4
11
/// Runs the specified program at the provided path.
5
12
/// - parameter path: The full path of the executable you
6
13
/// wish to run.
@@ -48,10 +55,10 @@ func makeFile() throws {
48
55
try FileManager . default. createDirectory ( at: pkgConfigDir,
49
56
withIntermediateDirectories: true )
50
57
}
51
- let cllvmPath = pkgConfigDir. appendingPathComponent ( " cclang.pc " )
58
+ let cclangPath = pkgConfigDir. appendingPathComponent ( " cclang.pc " )
52
59
53
60
/// Ensure we have llvm-config in the PATH
54
- guard let llvmConfig = which ( " llvm-config-3.9 " ) ?? which ( " llvm-config " ) else {
61
+ guard let llvmConfig = which ( " llvm-config-4.0 " ) ?? which ( " llvm-config- 3.9" ) ?? which ( " llvm-config " ) else {
55
62
throw " Failed to find llvm-config. Ensure llvm-config is installed and " +
56
63
" in your PATH "
57
64
}
@@ -60,15 +67,35 @@ func makeFile() throws {
60
67
61
68
print ( " Found llvm-config at \( llvmConfig) ... " )
62
69
63
- let version = run ( llvmConfig, args: [ " --version " ] ) !
64
- . replacing ( charactersIn: . newlines, with: " " )
70
+ let versionStr = run ( llvmConfig, args: [ " --version " ] ) !
71
+ . replacing ( charactersIn: . newlines, with: " " )
72
+ let components = versionStr. components ( separatedBy: " . " )
73
+ . flatMap { Int ( $0) }
74
+
75
+ guard components. count == 3 else {
76
+ throw " Invalid version number \( versionStr) "
77
+ }
78
+
79
+ let version = ( components [ 0 ] , components [ 1 ] , components [ 2 ] )
80
+
81
+ guard version > ( 3 , 9 , 0 ) else {
82
+ throw " LLVMSwift requires LLVM version >=3.9.0, but you have \( versionStr) "
83
+ }
84
+
85
+ print ( " LLVM version is \( versionStr) " )
86
+
87
+ guard let includeDir = run ( llvmConfig, args: [ " --includedir " ] ) else {
88
+ throw " Could not find LLVM include dir "
89
+ }
65
90
66
- guard version . hasPrefix ( " 3.9 " ) else {
67
- throw " ClangSwift requires LLVM version >=3.9.0, but you have \( version ) "
91
+ guard let libDir = run ( llvmConfig , args : [ " --libdir " ] ) else {
92
+ throw " Could not find LLVM library dir "
68
93
}
69
94
95
+ /// Emit the pkg-config file to the path
96
+
70
97
let libFlags = [
71
- " -L/usr/local/Cellar/llvm/3.9.1/lib " ,
98
+ " -L \( libDir ) " ,
72
99
" -lclangEdit " ,
73
100
" -lclangFrontendTool " ,
74
101
" -lclang " ,
@@ -83,27 +110,21 @@ func makeFile() throws {
83
110
" -lclangParse " ,
84
111
] . joined ( separator: " " )
85
112
86
- let cFlags = " -I/usr/local/Cellar/llvm/3.9.1/include "
87
- // SwiftPM has a whitelisted set of cflags that it understands, and
88
- // unfortunately that includes almost everything but the include dir.
89
-
90
- /// Emit the pkg-config file to the path
91
-
92
113
let s = [
93
114
" Name: cclang " ,
94
- " Description: The llvm library " ,
95
- " Version: \( version ) " ,
115
+ " Description: The clang C library " ,
116
+ " Version: \( versionStr ) " ,
96
117
" Libs: \( libFlags) " ,
97
118
" Requires.private: " ,
98
- " Cflags: \( cFlags ) " ,
119
+ " Cflags: -I \( includeDir ) " ,
99
120
] . joined ( separator: " \n " )
100
121
101
- print ( " Writing pkg-config file to \( cllvmPath . path) ... " )
122
+ print ( " Writing pkg-config file to \( cclangPath . path) ... " )
102
123
103
- try s. write ( toFile: cllvmPath . path, atomically: true , encoding: . utf8)
124
+ try s. write ( toFile: cclangPath . path, atomically: true , encoding: . utf8)
104
125
105
126
print ( " \n Successfully wrote pkg-config file! " )
106
- print ( " Make sure to re-run this script when you update LLVM . " )
127
+ print ( " Make sure to re-run this script when you update Clang . " )
107
128
}
108
129
109
130
do {
0 commit comments