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 the default target value to "x86_64-apple-macosx10.12."

Background

The Xcode 9.2 Swift toolbox currently displays 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 the goal of macOS 10.10.

Note: The generated Xcode 10.10 target is one version higher than the swift 10.9 command-line compiler target. Thus, apparently, the default macOS targets for swift package generate-xcodeproj swift and swift package generate-xcodeproj defined independently.

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

+9
source share
4 answers

The Swift Evolution SE-0236 proposal adds a minimum deployment target configuration for each platform:

 // 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 causes the MyTool target in the Xcode project to have a Deployment Target set to 10.13.

+5
source

Swift 5

Swift 5 Package Manager now allows you to specify the minimum required target platform using platforms in Package.swift .... see the answer provided by Klaas.

Swift 4

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

  1. setting a macOS target using swift package generate-xcodeproj and
  2. hiding swift build -Xswiftc "-target" -Xswiftc "x86_64-…"

Customization

Edit and source ~/.bash_profile to add the aliases swiftxcode , swiftbuild and swifttest 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"' 

Modify Package.swift in the project to specify it using the Swift Package Manager version 4 tools:

 // swift-tools-version:4.0 import PackageDescription let package = Package(… 

Add Package.xcconfig to the 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. If desired, the BuildSettingExtractor (xcodeproj β†’ xcconfig) created by James Dempsey can be useful for exploring other settings added 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 

Warnings

  1. Package.xcconfig sets assembly parameter values ​​only at the target level in an Xcode project. The values ​​of the assembly parameters at the project level remain unchanged.
  2. The values ​​of the Package.xcconfig project are set the same for all purposes. It seems that there is currently no mechanism for xcconfig for each purpose.
  3. Some xcconfig values, such as SKIP_INSTALL , may need to be set manually in an Xcode project.

Road map

We hope that the current limited xcconfig approach shown above will be replaced by the ability to specify custom build options in the package manifest, as discussed in the Swift 4 Package Management Scheme .

Configuration file support

We are considering adding a configuration file mechanism, which may be user or package specific, to allow you to customize specific package manager behaviors. One of the direct applications of this may be a temporary mechanism for passing overriding compiler flags for a package, although we hope to solve this problem with a real build settings model. Other options for using the configuration file are under consideration.

It's also worth noting that Apple's β€œNew Build System (Preview)”, written in Swift and released with Xcode 9, can use xcconfig files to determine build settings.

Resources

+15
source

In case the official quick tools do not allow you to perform some manipulations with the Xcode project, you can refuse the script entry that will update the generated Xcode project for you.

You can use xcodeproj rubygem . See an example script .

See related Swift Package Manager and Xcode question : Saving Xcode settings?

+1
source

Steps to achieve this without third-party tools.

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

 MACOSX_DEPLOYMENT_TARGET = 10.12 

2) Run this command at the root of your project

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

Source: https://habr.com/ru/post/1274012/


All Articles