11#!/usr/bin/env swift
22import Foundation
33
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+
411/// Runs the specified program at the provided path.
512/// - parameter path: The full path of the executable you
613/// wish to run.
@@ -48,10 +55,10 @@ func makeFile() throws {
4855 try FileManager . default. createDirectory ( at: pkgConfigDir,
4956 withIntermediateDirectories: true )
5057 }
51- let cllvmPath = pkgConfigDir. appendingPathComponent ( " cclang.pc " )
58+ let cclangPath = pkgConfigDir. appendingPathComponent ( " cclang.pc " )
5259
5360 /// 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 {
5562 throw " Failed to find llvm-config. Ensure llvm-config is installed and " +
5663 " in your PATH "
5764 }
@@ -60,15 +67,35 @@ func makeFile() throws {
6067
6168 print ( " Found llvm-config at \( llvmConfig) ... " )
6269
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+ }
6590
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 "
6893 }
6994
95+ /// Emit the pkg-config file to the path
96+
7097 let libFlags = [
71- " -L/usr/local/Cellar/llvm/3.9.1/lib " ,
98+ " -L \( libDir ) " ,
7299 " -lclangEdit " ,
73100 " -lclangFrontendTool " ,
74101 " -lclang " ,
@@ -83,27 +110,21 @@ func makeFile() throws {
83110 " -lclangParse " ,
84111 ] . joined ( separator: " " )
85112
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-
92113 let s = [
93114 " Name: cclang " ,
94- " Description: The llvm library " ,
95- " Version: \( version ) " ,
115+ " Description: The clang C library " ,
116+ " Version: \( versionStr ) " ,
96117 " Libs: \( libFlags) " ,
97118 " Requires.private: " ,
98- " Cflags: \( cFlags ) " ,
119+ " Cflags: -I \( includeDir ) " ,
99120 ] . joined ( separator: " \n " )
100121
101- print ( " Writing pkg-config file to \( cllvmPath . path) ... " )
122+ print ( " Writing pkg-config file to \( cclangPath . path) ... " )
102123
103- try s. write ( toFile: cllvmPath . path, atomically: true , encoding: . utf8)
124+ try s. write ( toFile: cclangPath . path, atomically: true , encoding: . utf8)
104125
105126 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 . " )
107128}
108129
109130do {
0 commit comments