Swift 5.something
swift package generate-xcodeproj is no longer available. See the accepted answer with platforms: [ … ] for what currently works.
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:
- setting the macOS target with swift package generate-xcodeproj, and
- 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
- Package.xcconfigsets Build Settings values only at the target level in the Xcode project.  The overall project level Build Settings values remain unchanged.
- Package.xcconfigproject 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.
- Some xcconfigvalues, such asSKIP_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