AnarchyTools / atbuild

The Anarchy Tools build tool
Apache License 2.0
6 stars 1 forks source link

Add support for C language to atllbuild #115

Closed drewcrawford closed 8 years ago

drewcrawford commented 8 years ago

This PR lets you mix .swift, .h, and .c files all in the same atllbuild task. It works a lot like Xcode's behavior, if you've used that.

Rationale

I feel the need to defend this feature, since I have been previously on the record as saying "the entire value is debatable" (https://www.mail-archive.com/swift-evolution@swift.org/msg01829.html).

There are 5 cases where I think it makes sense to add a little C to your Swift project:

Additional rationale:

I would like to be very clear about my goals:

The standard link-options sets link options for both C and Swift; since they are linked into the same library there is no individual control. So if you want to link your C (and Swift) code against curl, you could say :link-options ["-lcurl"] for example.

The problem with this approach is that everybody who depends on you might also need -lcurl. Traditionally we've solved this with overlays that we expose to callers.

SwiftPM avoids this problem by requiring everyone to create e.g. CCurl everywhere: https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

And in fact people do: https://github.com/IBM-Swift/CCurl

The problem is now you have to import CCurl everywhere (even in files that don't directly use it). See generally, https://bugs.swift.org/browse/SR-655, https://gist.github.com/briancroom/5d0f1b966fa9ef0ae4950e97f9d76f77

Here is the cool part though. This PR adds a new option :module-map-link ["curl"]. That will inject a link directive into both the module map we use at buildtime and the one we export e.g. into an atbin.

Emitting that link directive has the effect of injecting :link-options ["-lcurl"]. However, it will also inject that link option into any Swift module that imports this one. The result is that downstream no longer needs to add :link-options ["-lcurl"] anymore.

Additonally, since we achieve this in a single module, there is no CCurl to import anymore. The details of linking to the C library are more effectively hidden.

For these reasons, I believe using the C support in this PR is way more effective for writing bindings than any other solution.

Known issues

owensd commented 8 years ago

Seems reasonable. I'm still in the process of moving to CA, so I've only taken a quick look.

:thumbsup: