Xcode SwiftPM integration ignores cSettings?

I work on the MongoDB Swift driver, which has a vendored copy of the MongoDB C driver. Both the library itself and the vendored C code are built via SwiftPM.

In order for it to build correctly, we have to define a few macros in our Package.swift:

cSettings: [
    .define("MONGO_SWIFT_OS_LINUX", .when(platforms: [.linux])),
    .define("MONGO_SWIFT_OS_DARWIN", .when(platforms: [.macOS])),
    .define("BSON_COMPILATION"),
    .define("MONGOC_COMPILATION")
]

Everything builds just fine from the command line via swift build, but if you open the folder in Xcode and try to build the project that way, it appears the cSettings are not respected; there are a number of errors resulting from MONGOC_COMPILATION and BSON_COMPILATION being undefined.

The same issue occurs if you add the library as a dependency of another project via File > Swift Packages > Add Package Dependency...

This has unfortunately provided a pretty bad initial installation experience for anyone trying out our library who develops in Xcode and a number of users have brought it up with us (see #387).

A couple of workarounds are discussed on that GitHub thread, both relying on swift package generate-xcodeproj. I'm posting here mainly in hopes of getting more eyes on this issue and to see if there are any updates on the status of this, as well to see if anyone has any better suggested workarounds. I rarely develop in Xcode myself and I'm not sure if there are other options for working around this that I'm not aware of.

Alternatively, maybe there are ways to work around this by updating our vendored code or Package.swift?

FWIW we filed a Swift bug about this which looks like it got turned into a Radar ticket (which I can't see of course), and as mentioned in the linked GitHub issue one of our users also filed a bug report as well (FB7777476), but I'm not sure what the status of any of those are.

Thanks for reading!

2 Likes

Thanks for your detailed explanation. We are aware of this issue and are investigating it.

In Xcode, cSettings are currently only applied to C-family targets, but not to Swift targets that depend on them. This is a bug in the implementation of the build settings proposal in Xcode.

2 Likes

Is there a chance that Xcode 12 will fix it before shipping? What can be done to raise its priority?

Great to hear! Thanks for the quick response and explanation of the issue

I'm still seeing this in Xcode 15.0 (15A240d). Does this bug still exist or am I holding it wrong?

My setup is comparable, I have SwiftPM package that builds a custom SQLite version that needs a bunch of cSettings applied. Adding this as a package dependency in Xcode still does not seem to work?

Is there a suggested way of doing this or a suitable workaround?

It looks like the issue linked here is considered resolved from the Xcode side, so if that's not actually the case, the best course of action would be to submit a new feedback report through http://feedbackassistant.apple.com

I'm having the same issue, an iOS app depending on a swift package with cSettings which doesn't seem to be applying them. FB13404264

@_stefan, sorry to ping but have you ever found a workaround for this?

Not really, apart from deleting the build output and forcing a rebuild from scratch.

2 Likes