1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import TSCBasic
import Foundation
/// Get clang's version from the given version output string on Ubuntu.
public func getClangVersion(versionOutput: String) -> Version? {
// Clang outputs version in this format on Ubuntu:
// Ubuntu clang version 3.6.0-2ubuntu1~trusty1 (tags/RELEASE_360/final) (based on LLVM 3.6.0)
let versionStringPrefix = "Ubuntu clang version "
let versionStrings = versionOutput.utf8.split(separator: UInt8(ascii: "-")).compactMap(String.init)
guard let clangVersionString = versionStrings.first,
clangVersionString.hasPrefix(versionStringPrefix) else {
return nil
}
let versionStartIndex = clangVersionString.index(clangVersionString.startIndex,
offsetBy: versionStringPrefix.utf8.count)
let versionString = clangVersionString[versionStartIndex...]
// Split major minor patch etc.
let versions = versionString.utf8.split(separator: UInt8(ascii: ".")).compactMap(String.init)
guard versions.count > 1, let major = Int(versions[0]), let minor = Int(versions[1]) else {
return nil
}
return Version(major, minor, versions.count > 2 ? Int(versions[2]) ?? 0 : 0)
}
/// Prints the time taken to execute a closure.
///
/// Note: Only for debugging purposes.
public func measure<T>(_ label: String = "", _ f: () throws -> (T)) rethrows -> T {
let startTime = Date()
let result = try f()
let endTime = Date().timeIntervalSince(startTime)
print("\(label): Time taken", endTime)
return result
}
// for internal usage
extension NSLock {
internal func withLock<T> (_ body: () throws -> T) rethrows -> T {
self.lock()
defer { self.unlock() }
return try body()
}
}
|