Home > Net >  Swift Package Manager fails to build Objective-C package when public headers use angle bracket (<
Swift Package Manager fails to build Objective-C package when public headers use angle bracket (<

Time:06-20

I have an open source, Objective-C library that I maintain. It has been around for a long time, and I've always distributed it as an embeddable Xcode project that builds a framework, as well as through Carthage and Cocoapods. (The library in question is ORSSerialPort.)

I recently added support for installing it with Swift Package Manager (see this commit), by creating a Package.swift file. I was able to do so without making any source or structure changes, which was helpful because I need to continue to distribute it as a framework as well for the foreseeable future.

However, Xcode 12 included a new (or at least newly on by default?) warning when you do #import with double quotes in public headers in a framework. I had done that in a couple places, so switched to angle brackets as is correct for a framework (see this commit).

I've only now discovered that SwiftPM no longer builds the package because of that. It fails with the following error:

In file included from /Users/andrewmadsen/Developer/ORSSerialPort/Source/ORSSerialPort.m:28:
/Users/andrewmadsen/Developer/ORSSerialPort/Source/ORSSerialRequest.h:28:9: fatal error: 'ORSSerial/ORSSerialPacketDescriptor.h' file not found
#import <ORSSerial/ORSSerialPacketDescriptor.h>
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[0/1] Compiling ORSSerial ORSSerialPort.m

One possible solution (that does work, but is kinda gross), is to conditionalize any import with an #ifdef, then pass in a define in the cSettings argument when creating the target in Package.swift, but that seems kind of gross and unwieldy. In this project it's not bad, but in another project I have many, many more headers affected by this issue.

So, to the root of my question: Is there a way to convince SPM to build an Objective-C package where the public headers use <> angle brackets to include other public headers in the package?

I've already tried specifying a header search path in cSettings, but this produces the same error:

.target(
    name: "ORSSerial",
    path: "Source",
    exclude: ["ORSSerialBuffer.h"],
    publicHeadersPath: ".",
    cSettings: [
        .headerSearchPath(".")
    ]
)

(All .h and .m files are in the same Source folder.)

CodePudding user response:

I had the same issue recently. The only way I found to work around this was to change the includes to standard user-style includes

#import "ORSSerialPacketDescriptor.h"

Of course, this breaks the header for Objective-C clients. But since Swift uses the module map, you can still use it from Swift code.

  • Related