Set macOS target for `swift package generate-xcodeproj`?

The Swift evolution proposal SE-0236 added the configuration of a per-platform minimum required deployment target:

// swift-tools-version:5.0
import PackageDescription

let package = Package(
    name: "MyTool",
    platforms: [
        // specify each minimum deployment requirement, 
        //otherwise the platform default minimum is used.
       .macOS(.v10_13),
    ],
     targets: [
        .target(name: "MyTool", dependencies: ["foo", "bar"]),
    ],
    swiftLanguageVersions: [.v5]
)

This results in the target "MyTool" within the Xcode project having the "Deloyment Target" set to 10.13 .


In case the official swift tools do not allow some Xcode project manipulation, you may want to fall back on writing a script that will update the generated Xcode project for you.

You can use xcodeproj rubygem. See an example script.

See a related question Swift Package Manager and Xcode: Retaining Xcode Settings?


Steps to achieve this without 3rd party tools.

1) Create macos.xcconfig file in /Sources folder with following contents

MACOSX_DEPLOYMENT_TARGET = 10.12

2) Run this command in the root of your project

swift package generate-xcodeproj --xcconfig-overrides Sources/macos.xcconfig

Swift 5

Swift 5 Package manager now enables specification of the minimum required target platform with via platforms in Package.swift. ... see answer provided by Klaas.

Swift 4

I ended up creating swiftxcode, swiftbuild, and swifttest bash aliases as a workaround to simplify:

  1. setting the macOS target with swift package generate-xcodeproj, and
  2. hiding swift build -Xswiftc "-target" -Xswiftc "x86_64-…"

Setup

Edit and source ~/.bash_profile to add swiftxcode, swiftbuild, and swifttest aliases to the command line.

###################################
### Swift Package Manager (SPM) ###
###################################
alias swiftxcode="swift package generate-xcodeproj --xcconfig-overrides Package.xcconfig"
alias swiftbuild='swift build -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.12"'
alias swifttest='swift test -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.12"'

Edit Package.swift in the for the project to specify using Swift Package Manager tools version 4:

// swift-tools-version:4.0
import PackageDescription

let package = Package(…

Add Package.xcconfig to project folder:

/// Package.xcconfig

/// macOS Deployment Target
/// 
/// Code will load on this and later versions of macOS. 
/// Framework APIs that are unavailable in earlier versions will be weak-linked; 
/// your code should check for `null` function pointers 
/// or specific system versions before calling newer APIs.

MACOSX_DEPLOYMENT_TARGET=10.12

Note: Optionally, the BuildSettingExtractor (xcodeproj → xcconfig) created by James Dempsey can be helpful to explore other settings to add to the Package.xcconfig file.

Use

cd to/some_project_folder/
swiftxcode     # create Xcode project
swiftbuild     # build from command line
swifttest      # build and run tests from command line

Caveats

  1. Package.xcconfig sets Build Settings values only at the target level in the Xcode project. The overall project level Build Settings values remain unchanged.
  2. Package.xcconfig project values are set the same for all targets. There does not appear to currently be a mechanism for the xcconfig on a per target basis.
  3. Some xcconfig values, such as SKIP_INSTALL, may need to be set manually in the Xcode project.

Roadmap

Hopefully, the current limited xcconfig approach shown above will be superceded by the ability to specify custom build settings in the package manifest, as discussed in the Swift 4 Package Manager Roadmap.

Configuration File Support

We are considering adding a configuration file mechanism, which could be user-specific or package-specific, to allow customizing certain package manager behaviors. One immediate use for this would be as a temporary mechanism for passing overriding compiler flags for a package, though we hope to solve that need with a real build settings model. Other uses for a configuration file are up for consideration.

Also worth noting is that Apple's "New Build System (Preview)", written in Swift and released with Xcode 9, can use xcconfig files to define build settings.

Resources

  • Apple: Configuration Settings File (xcconfig) format
  • New Xcode Build System and BuildSettingExtractor
  • The Unofficial Guide to xcconfig files
  • Five Things You Must Know About Xcode 10 New Build System