Add compiler flags to Xcode SPM projects

My package requires a number of XLinker flags to compile properly. Building from the command line with swift build is obviously easy enough but I would prefer to use Xcode. Right now I use generate-xcodeproj and modify the header search and library search paths like in a normal Xcode project. However, I saw that command is being deprecated. Opening the project in Xcode natively does not allow command line flags to be specified. I saw unsafeFlags exists in the package file, but I need the flags to be conditional based on OS. During CI, we just use the appropriate build command. Any thoughts?

1 Like

You could do something along the lines of

let settings: [SwiftSetting]
#if os(Linux)
settings = [.unsafeFlags(["-Xfrontend", "-serialize-debugging-options"], .when(configuration: .debug))]
#else
settings = []
#endif

// Package manifest goes here
let package = Package(
// ...
        .target(
            name: "MyTarget", 
            dependencies: ["some_dependency"],
            swiftSettings: settings
        ),
1 Like

Not sure why I didn’t think of this, thanks!