为`swift package generate-xcodeproj`设置macOS目标?

时间:2023-01-23 23:58:08

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

有没有办法为swift包设置macOS目标generate-xcodeproj生成Xcode项目?

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

例如,将default target设置为“x86_64-apple-macosx10.12”。

Background

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

目前,Xcode 9.2 Swift工具链在命令行上显示以下版本:

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.

swift包generate-xcodeproj的结果是一个Xcode项目,macOS目标为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.

注意:生成的Xcode 10.10目标是swift命令行编译器目标10.9的一个版本。因此,显然,swift和swift包generate-xcodeproj的默认macOS目标是彼此独立确定的。

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

到目前为止,任何使用Package.swift, - xcconfig-overrides或其他方法的解决方案都是难以捉摸的。

2 个解决方案

#1


7  

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

我最终创建了swiftxcode,swiftbuild和swifttest bash别名作为解决方法来简化:

  1. setting the macOS target with swift package generate-xcodeproj, and
  2. 用swift包generate-xcodeproj设置macOS目标,和

  3. hiding swift build -Xswiftc "-target" -Xswiftc "x86_64-…"
  4. 隐藏swift build -Xswiftc“-target”-Xswiftc“x86_64- ......”

Setup

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

编辑并获取〜/ .bash_profile以将swiftxcode,swiftbuild和swifttest别名添加到命令行。

###################################
### 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 Package Manager工具版本4编辑项目中的Package.swift:

// swift-tools-version:4.0
import PackageDescription

let package = Package(…

Add Package.xcconfig to project folder:

将Package.xcconfig添加到项目文件夹:

/// 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: The BuildSettingExtractor (xcodeproj → xcconfig) created by James Dempsey can be helpful to explore other settings to add to the Package.xcconfig file.

注意:James Dempsey创建的BuildSettingExtractor(xcodeproj→xcconfig)可以帮助您探索要添加到Package.xcconfig文件的其他设置。

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仅在Xcode项目中的目标级别设置Build Settings值。整个项目级别的“构建设置”值保持不变。

  3. 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.
  4. Package.xcconfig项目值对所有目标设置相同。目前似乎没有基于每个目标的xcconfig机制。

  5. Some xcconfig values, such as SKIP_INSTALL, may need to be set manually in the Xcode project.
  6. 某些xcconfig值(例如SKIP_INSTALL)可能需要在Xcode项目中手动设置。

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.

希望上面显示的当前有限的xcconfig方法可以通过在程序包清单中指定自定义构建设置的能力来取代,如Swift 4程序包管理器路线图中所述。

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.

另外值得注意的是,用Swift编写并随Xcode 9发布的Apple的“新构建系统(预览版)”可以使用xcconfig文件来定义构建设置。

Resources

#2


1  

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.

如果官方swift工具不允许某些Xcode项目操作,您可能希望重新编写将为您更新生成的Xcode项目的脚本。

You can use xcodeproj rubygem. See an example script.

你可以使用xcodeproj ruby​​gem。查看示例脚本。

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

查看相关问题Swift Package Manager和Xcode:保留Xcode设置?

#1


7  

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

我最终创建了swiftxcode,swiftbuild和swifttest bash别名作为解决方法来简化:

  1. setting the macOS target with swift package generate-xcodeproj, and
  2. 用swift包generate-xcodeproj设置macOS目标,和

  3. hiding swift build -Xswiftc "-target" -Xswiftc "x86_64-…"
  4. 隐藏swift build -Xswiftc“-target”-Xswiftc“x86_64- ......”

Setup

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

编辑并获取〜/ .bash_profile以将swiftxcode,swiftbuild和swifttest别名添加到命令行。

###################################
### 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 Package Manager工具版本4编辑项目中的Package.swift:

// swift-tools-version:4.0
import PackageDescription

let package = Package(…

Add Package.xcconfig to project folder:

将Package.xcconfig添加到项目文件夹:

/// 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: The BuildSettingExtractor (xcodeproj → xcconfig) created by James Dempsey can be helpful to explore other settings to add to the Package.xcconfig file.

注意:James Dempsey创建的BuildSettingExtractor(xcodeproj→xcconfig)可以帮助您探索要添加到Package.xcconfig文件的其他设置。

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仅在Xcode项目中的目标级别设置Build Settings值。整个项目级别的“构建设置”值保持不变。

  3. 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.
  4. Package.xcconfig项目值对所有目标设置相同。目前似乎没有基于每个目标的xcconfig机制。

  5. Some xcconfig values, such as SKIP_INSTALL, may need to be set manually in the Xcode project.
  6. 某些xcconfig值(例如SKIP_INSTALL)可能需要在Xcode项目中手动设置。

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.

希望上面显示的当前有限的xcconfig方法可以通过在程序包清单中指定自定义构建设置的能力来取代,如Swift 4程序包管理器路线图中所述。

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.

另外值得注意的是,用Swift编写并随Xcode 9发布的Apple的“新构建系统(预览版)”可以使用xcconfig文件来定义构建设置。

Resources

#2


1  

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.

如果官方swift工具不允许某些Xcode项目操作,您可能希望重新编写将为您更新生成的Xcode项目的脚本。

You can use xcodeproj rubygem. See an example script.

你可以使用xcodeproj ruby​​gem。查看示例脚本。

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

查看相关问题Swift Package Manager和Xcode:保留Xcode设置?