mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
73c88c3af0
Prints out something like this on macOS: ``` Version: 0.49.0-rc.1 Build ID: B43931F3-D096-3704-B41C-FC40673A3F96 ``` This can be used to determine if two `swiftlint` executables are identical.
43 lines
1.4 KiB
Swift
43 lines
1.4 KiB
Swift
#if os(macOS)
|
|
import Foundation
|
|
import MachO
|
|
|
|
/// Information about this executable.
|
|
public enum ExecutableInfo {
|
|
/// A stable identifier for this executable. Uses the Mach-O header UUID on macOS. Nil on Linux.
|
|
public static let buildID: String? = {
|
|
if let handle = dlopen(nil, RTLD_LAZY) {
|
|
defer { dlclose(handle) }
|
|
|
|
if let ptr = dlsym(handle, MH_EXECUTE_SYM) {
|
|
return getUUID(pointer: ptr)?.uuidString
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}()
|
|
|
|
private static func getUUID(pointer: UnsafeRawPointer) -> UUID? {
|
|
var offset: UInt64 = 0
|
|
let header = pointer.bindMemory(to: mach_header_64.self, capacity: 1)
|
|
offset += UInt64(MemoryLayout<mach_header_64>.size)
|
|
for _ in 0..<header.pointee.ncmds {
|
|
let loadCommand = pointer.load(fromByteOffset: Int(offset), as: load_command.self)
|
|
if loadCommand.cmd == LC_UUID {
|
|
let uuidCommand = pointer.load(fromByteOffset: Int(offset), as: uuid_command.self)
|
|
return UUID(uuid: uuidCommand.uuid)
|
|
}
|
|
offset += UInt64(loadCommand.cmdsize)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
#else
|
|
/// Information about this executable.
|
|
public enum ExecutableInfo {
|
|
/// A stable identifier for this executable. Uses the Mach-O header UUID on macOS. Nil on Linux.
|
|
public static let buildID: String? = nil
|
|
}
|
|
#endif
|