Skip to content

Commit

Permalink
Replace gnu_dev_makedev with Swift implementation
Browse files Browse the repository at this point in the history
On systems with an up to date libc, gnu_dev_makedev is not imported anymore
(check out bits/sysmacros.h to see how it changed), so for now I'm re-implementing it here.
Not the best solution since the underlying implementation could change or could
be even different depending on the system you are on.
Will look into how to fix this directly in Swift.
  • Loading branch information
uraimo committed Jul 2, 2019
1 parent 6e2483b commit bfc6f88
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Sources/SwiftyGPIO/Vendor/Mailbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,8 @@ public struct MailBox {
filename = "/tmp/swifty-mailbox-" + String(getpid())
unlink(filename)

//makedev is a macro not imported by Swift that resolves to gnu_dev_makedev
#if arch(arm) // 32-bit Raspi
if mknod(filename, S_IFCHR|0600, gnu_dev_makedev(100, 0)) < 0 {
perror("Failed to create mailbox device\n")
return -1
}
#elseif os(Linux)
if mknod(filename, S_IFCHR|0600, UInt(gnu_dev_makedev(100, 0))) < 0 {
#if os(Linux)
if mknod(filename, S_IFCHR|0600, swift_makedev(100, 0)) < 0 {
perror("Failed to create mailbox device\n")
return -1
}
Expand Down Expand Up @@ -307,11 +301,17 @@ public struct MailBox {
}
}

// Swift implementation of gnu_dev_makedev, that right now is not imported by Swift on systems with recent libc (bits/sysmacros.h)
func swift_makedev(_ major: UInt, _ minor: UInt) -> UInt64 {
var dev: UInt64 = 0
dev = (UInt64(major) & 0x00000fff) << 8
dev |= (UInt64(major) & 0xfffff000) << 32
dev |= (UInt64(minor) & 0x000000ff)
dev |= (UInt64(minor) & 0xffffff00) << 12
return dev
}

// MARK: - Darwin / Xcode Support
#if os(OSX) || os(iOS)
private var O_SYNC: CInt { fatalError("Linux only") }

func gnu_dev_makedev(_ maj: UInt, _ min: UInt) -> Int32 {
fatalError("Linux only")
}
#endif

0 comments on commit bfc6f88

Please sign in to comment.