39 lines
1.1 KiB
Swift
39 lines
1.1 KiB
Swift
#if os(macOS)
|
|
import Foundation
|
|
import MachO
|
|
|
|
enum ExecutableInfo {
|
|
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
|
|
enum ExecutableInfo {
|
|
static let buildID: String? = nil
|
|
}
|
|
#endif
|