[swift-users] Improving a simple C binding

Max Howell max.howell at apple.com
Wed Dec 9 12:31:19 CST 2015

> Behold, a binding for libpq:
>  https://github.com/solidsnack/CLibPQ
> And a little app that uses it:
>  https://github.com/solidsnack/PGVersion
> CLibPQ is put together in the simplest way: there's a module.modulemap
> at the root and that's it. I would like to make some changes to the
> module hierarchy and I'm not sure how to go about it. Here's what I'd
> like to do:
> * Create `CLibPQ.OSXHomebrew` and `CLibPQ.Ubuntu` that contain the
> right mappings for those platforms (the header files are in different
> places).
> * Create `CLibPQ` that conditionally imports the right one:
>    CLibPQ.swift:
>      #if os(Linux)
>      import CLibPQ.Ubuntu
>      #else
>      import CLibPQ.OSXHomebrew
>      #endif
> * In `PGVersion` we'd be able to `import CLibPQ` as before.
> What's the right project layout to make this work?

You can’t do what you are trying here with a module map sadly.

We need to add explicit support for this sort of thing to swiftpm. Our current ideas are:

1. Mangle the module map for obvious relocations (/usr -> /usr/local)
2. Making it possible to specify platform module maps for exceptions eg. Ubuntu.modulemap, etc.
3. Changing /usr to $ROOT and then having the user have to specify root if it is not /usr

> Namespaced C modules are an interesting topic and worth pursuing in
> their own right; but maybe there is a better way to do what I'm trying
> to accomplish?

I think namespaces needs discussion for Swift in general.

More information about the swift-users mailing list