|
| 1 | +#!/usr/bin/env swift |
| 2 | +import Foundation |
| 3 | + |
| 4 | +/// Runs the specified program at the provided path. |
| 5 | +/// - parameter path: The full path of the executable you |
| 6 | +/// wish to run. |
| 7 | +/// - parameter args: The arguments you wish to pass to the |
| 8 | +/// process. |
| 9 | +/// - returns: The standard output of the process, or nil if it was empty. |
| 10 | +func run(_ path: String, args: [String] = []) -> String? { |
| 11 | + print("Running \(path) \(args.joined(separator: " "))...") |
| 12 | + let pipe = Pipe() |
| 13 | + let process = Process() |
| 14 | + process.launchPath = path |
| 15 | + process.arguments = args |
| 16 | + process.standardOutput = pipe |
| 17 | + process.launch() |
| 18 | + process.waitUntilExit() |
| 19 | + |
| 20 | + let data = pipe.fileHandleForReading.readDataToEndOfFile() |
| 21 | + guard let result = String(data: data, encoding: .utf8)? |
| 22 | + .trimmingCharacters(in: .whitespacesAndNewlines), |
| 23 | + !result.isEmpty else { return nil } |
| 24 | + return result |
| 25 | +} |
| 26 | + |
| 27 | +/// Finds the location of the provided binary on your system. |
| 28 | +func which(_ name: String) -> String? { |
| 29 | + return run("/usr/bin/which", args: [name]) |
| 30 | +} |
| 31 | + |
| 32 | +extension String: Error { |
| 33 | + /// Replaces all occurrences of characters in the provided set with |
| 34 | + /// the provided string. |
| 35 | + func replacing(charactersIn characterSet: CharacterSet, |
| 36 | + with separator: String) -> String { |
| 37 | + let components = self.components(separatedBy: characterSet) |
| 38 | + return components.joined(separator: separator) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func makeFile() throws { |
| 43 | + let pkgConfigPath = "/usr/local/lib/pkgconfig" |
| 44 | + let pkgConfigDir = URL(fileURLWithPath: pkgConfigPath) |
| 45 | + |
| 46 | + // Make /usr/local/lib/pkgconfig if it doesn't already exist |
| 47 | + if !FileManager.default.fileExists(atPath: pkgConfigPath) { |
| 48 | + try FileManager.default.createDirectory(at: pkgConfigDir, |
| 49 | + withIntermediateDirectories: true) |
| 50 | + } |
| 51 | + let cllvmPath = pkgConfigDir.appendingPathComponent("cclang.pc") |
| 52 | + |
| 53 | + /// Ensure we have llvm-config in the PATH |
| 54 | + guard let llvmConfig = which("llvm-config-3.9") ?? which("llvm-config") else { |
| 55 | + throw "Failed to find llvm-config. Ensure llvm-config is installed and " + |
| 56 | + "in your PATH" |
| 57 | + } |
| 58 | + |
| 59 | + /// Extract the info we need from llvm-config |
| 60 | + |
| 61 | + print("Found llvm-config at \(llvmConfig)...") |
| 62 | + |
| 63 | + let version = run(llvmConfig, args: ["--version"])! |
| 64 | + .replacing(charactersIn: .newlines, with: "") |
| 65 | + |
| 66 | + guard version.hasPrefix("3.9") else { |
| 67 | + throw "ClangSwift requires LLVM version >=3.9.0, but you have \(version)" |
| 68 | + } |
| 69 | + |
| 70 | + let libFlags = [ |
| 71 | + "-L/usr/local/Cellar/llvm/3.9.1/lib", |
| 72 | + "-lclangEdit", |
| 73 | + "-lclangFrontendTool", |
| 74 | + "-lclang", |
| 75 | + "-lclangAST", |
| 76 | + "-lclangLex", |
| 77 | + "-lclangBasic", |
| 78 | + "-lclangDriver", |
| 79 | + "-lclangAnalysis", |
| 80 | + "-lclangIndex", |
| 81 | + "-lclangASTMatchers", |
| 82 | + "-lclangSema", |
| 83 | + "-lclangParse", |
| 84 | + ].joined(separator: " ") |
| 85 | + |
| 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 | + let s = [ |
| 93 | + "Name: cclang", |
| 94 | + "Description: The llvm library", |
| 95 | + "Version: \(version)", |
| 96 | + "Libs: \(libFlags)", |
| 97 | + "Requires.private:", |
| 98 | + "Cflags: \(cFlags)", |
| 99 | + ].joined(separator: "\n") |
| 100 | + |
| 101 | + print("Writing pkg-config file to \(cllvmPath.path)...") |
| 102 | + |
| 103 | + try s.write(toFile: cllvmPath.path, atomically: true, encoding: .utf8) |
| 104 | + |
| 105 | + print("\nSuccessfully wrote pkg-config file!") |
| 106 | + print("Make sure to re-run this script when you update LLVM.") |
| 107 | +} |
| 108 | + |
| 109 | +do { |
| 110 | + try makeFile() |
| 111 | +} catch { |
| 112 | +#if os(Linux) |
| 113 | + // FIXME: Printing the thrown error that here crashes on Linux. |
| 114 | + print("Unexpected error occured while writing the config file. Check permissions and try again.") |
| 115 | +#else |
| 116 | + print("error: \(error)") |
| 117 | +#endif |
| 118 | + exit(-1) |
| 119 | +} |
0 commit comments