karhm
(Karim)
1
Hello
I have a library written in Objective-C that I'm trying to add a Package.swift to it.
When I add this library as a Swift package to other projects (written in Swift or Objective-C) using Xcode I get an error because the SWIFT_PACKAGE flag is not defined, this flag is used to conditionally import some headers.
#if SWIFT_PACKAGE
#import "../Defines.h"
#else
#import "Defines.h"
#endif // SWIFT_PACKAGE
I tried adding the flag to Package.swift but this did not solve the issue.
cSettings: [
.define("SWIFT_PACKAGE")
]
After some debugging I found this in Xcode 11.4 (11E146) where the SWIFT_PACKAGE flag is defined and not defined at the same time.
When I try to build the project using swift build or using the Xcode project everything work fine.
1 Like
NeoNacho
(Boris Buegling)
2
This seems concerning, but I wasn't able to reproduce it. Could you please file a bug for this including your example?
This may be a difference in C's #if vs. #ifdef, given how you've manually defined SWIFT_PACKAGE. Using #ifdef should give you consistent behavior.
karhm
(Karim)
6
Im Adding Package.swift to the following library: https://github.com/karimhm/google-api-objectivec-client-for-rest.
Add it to any Objective-C or Swift project and choose swift-pm swift-pm-issue branch.
The double definition problem occur when adding this flag to Package.swift:
cSettings: [
.define("SWIFT_PACKAGE")
]
Otherwise SWIFT_PACKAGE does not seem to be defined which is odd given that this is a Swift Package.
karhm
(Karim)
7
The issue only happen inside .h header files and nothing else. Not inside Objective-C/C++ source files or C++ header or source files.
karhm
(Karim)
8
@NeoNacho I just filed a new bug regarding this issue SR-14011 including an example. The bug still occur on the latest Xcode at the time.
paulb777
(Paul Beusterien)
9
I've seen that SWIFT_PACKAGE is not enabled when Xcode constructs modules from public headers. I've restructured headers to avoid the need for differentiating between SPM and CocoaPods clients. Details here.
NeoNacho
(Boris Buegling)
10
Good point, I think that requires some thinking on how it should work in that context. Since the client of a package is not necessarily a package, it probably should not get the SWIFT_PACKAGE define. We could impart the setting on clients, but there isn't really a way to restrict it to individual modules/headers, so it would affect any headers that are imported by the clients, also ones from non-packages.
karhm
(Karim)
11
The original bug report was deleted so I created a new one SR-14469