Files
SwiftLint/Source/SwiftLintFramework/Helpers/ExecutableInfo.swift
T
JP Simard 73c88c3af0 Add version --verbose command (#4102)
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.
2022-08-16 14:12:44 +00:00

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