[swift-dev] C Macros and Variadic functions

Ryan Lovelett swift-dev at ryan.lovelett.me
Tue Jan 5 14:32:59 CST 2016

Response inline:

On Tue, Jan 5, 2016, at 03:12 PM, Xi Ge wrote:
> > On Jan 5, 2016, at 11:54 AM, Ryan Lovelett via swift-dev <swift-dev at swift.org> wrote:
> > 
> > I'm beginning to try to port some of my existing C code over to Swift
> > (you have no idea how excited that makes me). Unfortunately, I've hit a
> > stumbling block.
> > 
> > When interacting with some of the system/hardware interfaces in Linux it
> > sometimes (often?) becomes necessary to interact with I/O Control
> > (ioctl). This introduces two complexities that at least appear to me as
> > needing resolution.
> > 
> > The first is C macros. A lot ioctl interfaces use headers which define
> > macros that are specific ioctl request operations. These macros
> > typically take the form of `_IO`, `_IOR` or `_IOW` (there are more but
> > this should give you a picture). These values are typically bound to a
> > specific kernel version and thus are subject to change. I point that out
> > because obviously one could just calculate the value and hard-code it
> > into the Swift source (which is what I plan to do in the short term) but
> > this isn't really a portable solution across kernels. The question is
> > what, if anything, is planned around this?
> > 
> > The second is variadic C functions. In my case I went ahead and
> > calculated the result of a C macro and hard coded it into a Swift
> > variable (e.g., `let DMX_SET_BUFFER_SIZE: CUnsignedInt = 28461`). This
> > was so I could send it to an ioctl call (e.g., `ioctl(fileReference!,
> > DMX_SET_BUFFER_SIZE, DVBDVR.DVR_BUFFER_SIZE)`). Unfortunately, this died
> > in the compiler with `error: 'ioctl' is unavailable: Variadic function
> > is unavailable`. The interesting part of the compiler error was this
> > though `SwiftGlibc.ioctl:2:13: note: 'ioctl' has been explicitly marked
> > unavailable here`. This seems to imply that it _could_ be on and it just
> > isn't. I searched around a bit and found a few PRs [1] [2] [3] that seem
> > to indicate that Swift does support variadic C functions. Is there a
> > reason why ioctl is not?
> Though generally unavailable as variadic C functions (Jordan might be the
> better person to explain why), 
> we still have other reasons to import them as unavailable functions
> instead of ignoring them. One example is for printing
> the complete interface of the underlying Clang module. This message does
> NOT imply the variadic function is available
> in certain circumstances. Do you any suggestions about better error
> message?

Not exactly the take-away that I was expecting to my question. That
having been said, the part that made my feel like it _could_ and wasn't
was the note. What is the point of the note, if not to say, "Hey we know
this is a variadic function and that's why we've disabled it."? Which to
me implicitly says this could be enabled in the future just, for
whatever reason, not right now. Therefore, my recommendation would be
just to drop the note. The compiler error was enough to say variadic C
functions are unavailable.
> > 
> > Sorry if this has been asked or addressed before but I'm at a loss as to
> > how to search these mailing lists.
> > 
> > [1] https://github.com/apple/swift/pull/196
> > [2] https://github.com/apple/swift/pull/413
> > [3] https://github.com/apple/swift-evolution/pull/38
> > _______________________________________________
> > swift-dev mailing list
> > swift-dev at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-dev

Just to be clear though the intent of my question was not to quibble
with compiler error messages. My real question is how are we meant to do
systems programming with Swift on Linux if we cannot call ioctl?

More information about the swift-dev mailing list