Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way to set the macOS target for swift package generate-xcodeproj generates Xcode projects?

For example, set default target to "x86_64-apple-macosx10.12".

Background

Currently, the Xcode 9.2 Swift toolchain shows the following versions on the command line:

swift --version
# Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
# Target: x86_64-apple-macosx10.9
swift package --version
# Apple Swift Package Manager - Swift 4.0.0-dev (swiftpm-13752)

The result of swift package generate-xcodeproj is an Xcode project with a macOS target of 10.10.

Note: the generated Xcode 10.10 target is one version up from the swift command line compiler target of 10.9. So, apparently, the default macOS targets for swift and swift package generate-xcodeproj are determined independent of each other.

So far, any solution with Package.swift, --xcconfig-overrides, or something else has been elusive.

like image 341
l --marc l Avatar asked Dec 12 '17 23:12

l --marc l


2 Answers

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
like image 95
l --marc l Avatar answered Sep 22 '22 14:09

l --marc l


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 .

like image 31
Klaas Avatar answered Sep 23 '22 14:09

Klaas