如何使用Swift Package Manager确定Swift代码中的配置

时间:2023-01-24 00:21:11

I'm creating an application using Swift Package Manager and I need to know the configuration of which the project was built under, i.e Debug or Release. I'm trying to stay away from using the .xcodeproj file. Please, someone let me know if this is possible. I'll paste a simple example below, but just know if there's a better way to handling configuration besides the code below, please submit as answer.

我正在使用Swift Package Manager创建一个应用程序,我需要知道构建项目的配置,即Debug或Release。我正试图远离使用.xcodeproj文件。如果有可能,有人让我知道。我将在下面粘贴一个简单的示例,但只要知道除了下面的代码之外是否有更好的方法来处理配置,请提交作为答案。

Example:

// main.swift

#if DEBUG
  print("Im in Debug!!")
#else
  print("Im in Release!!")
#endif

1 个解决方案

#1


7  

As of Swift 3.1, the Package Manager doesn't offer the option to customize build settings in the Package.swift file (this feature is part of the team's roadmap for Swift 4). However, you can use the -Xswiftc flag to pass custom build settings to the compiler when you invoke swift build or swift test.

从Swift 3.1开始,程序包管理器不提供在Package.swift文件中自定义构建设置的选项(此功能是团队Swift 4路线图的一部分)。但是,在调用swift build或swift test时,可以使用-Xswiftc标志将自定义构建设置传递给编译器。

To set the DEBUG flag in debug mode, invoke swift build like this:

要在调试模式下设置DEBUG标志,请像这样调用swift build:

swift build --configuration debug -Xswiftc "-D" -Xswiftc "DEBUG"

And for release builds you would do the usual:

对于发布版本,您可以按照惯例执行:

swift build --configuration release

There is also -Xlinker and Xcc to pass flags to the linker and C compiler, respectively.

还有-Xlinker和Xcc分别将标志传递给链接器和C编译器。

#1


7  

As of Swift 3.1, the Package Manager doesn't offer the option to customize build settings in the Package.swift file (this feature is part of the team's roadmap for Swift 4). However, you can use the -Xswiftc flag to pass custom build settings to the compiler when you invoke swift build or swift test.

从Swift 3.1开始,程序包管理器不提供在Package.swift文件中自定义构建设置的选项(此功能是团队Swift 4路线图的一部分)。但是,在调用swift build或swift test时,可以使用-Xswiftc标志将自定义构建设置传递给编译器。

To set the DEBUG flag in debug mode, invoke swift build like this:

要在调试模式下设置DEBUG标志,请像这样调用swift build:

swift build --configuration debug -Xswiftc "-D" -Xswiftc "DEBUG"

And for release builds you would do the usual:

对于发布版本,您可以按照惯例执行:

swift build --configuration release

There is also -Xlinker and Xcc to pass flags to the linker and C compiler, respectively.

还有-Xlinker和Xcc分别将标志传递给链接器和C编译器。