【问题标题】:What are the differences between xcodebuild, xcrun and swift command line tools?xcodebuild、xcrun 和 swift 命令行工具有什么区别?
【发布时间】:2021-10-31 22:49:20
【问题描述】:

我们可以通过多个命令从命令行构建和运行 Swift 代码/项目,而无需使用 Xcode。我听说过用于 Xcode 开发的 xcodebuildxcrunswift。我确实使用fastlane,但我并不真正了解为其提供动力的工具。

我是一名使用 Mac 的 iOS 开发人员。我使用 Xcode 进行开发,所以我以前没有使用过这些命令行工具。

每个命令之间有什么区别?在任何情况下,我最好使用一种而不是另一种?

【问题讨论】:

    标签: ios swift xcode macos xcodebuild


    【解决方案1】:

    Tl;DR

    xcodebuildxcrun 帮助在无头上下文中构建 Xcode 项目,例如在 CI 设置中。 swift 是 Swift REPL,主要用于服务器应用程序上的 Swift。因此,我们可以在不了解或不经常使用移动应用程序开发工具的情况下构建应用程序。我们在底层使用 xcodebuildxcrun 与 Xcode 交互,尽管我们没有意识到这一点,因为它们捆绑在 Xcode's Command Line tools 中(文档存档,但仍然相关)。

    fastlane 是一个示例 CI 工具,它使用这些工具自动执行构建过程、证书签名以及与 App Store Connect 的交互。

    xcodebuild

    xcodebuild 是 Xcode 捆绑的命令行工具包的一部分。来自manpages

    构建 Xcode 项目和工作区

    xcodebuild 构建一个或多个包含在 Xcode 项目中的目标, 或构建一个包含在 Xcode 工作区或 Xcode 项目中的方案。

    xcodebuild 有很多选项和用例。这些选项等效于 Xcode IDE 中的某些用户操作。示例用法:

    xcodebuild -workspace MyWorkspace.xcworkspace -scheme MyScheme
    

    在 Xcode 工作区 MyWorkspace.xcworkspace 中构建方案 MyScheme。

    在上面的命令中,我们在没有 Xcode 的情况下构建工作区,使用 Xcode 在内部运行的内容进行编译。 Xcode 只能安装在 macOS 上,我们使用它的命令行工具也有同样的限制,包括xcodebuildxcrun

    xcrun

    xcrun 是 Xcode 的 CLI 工具的另一个 Xcode 命令行工具部分。来自manpages

    运行或定位开发工具

    xcrun 提供了一种定位或调用共存的方法 - 和 来自命令行的平台感知开发工具,无需 要求用户修改makefile或以其他方式带来不便 支持多个 Xcode 工具链的措施。

    xcrun [-sdk SDK] -find <tool_name>
    

    xcrun 也常与Xcode-select 一起用于管理同一台机器上的多个 Xcode 版本。每个版本的 Xcode 都捆绑了自己的开发工具,我们可以使用 xcrun 获取它们的当前路径:

    xcrun xcode-select --print-path
    

    迅速

    swift 是 Swift REPL。 swift 是一个命令行工具,它包含 Swift 工具链,但也可以安装在 Xcode 捆绑工具之外。 swiftxcodebuildxcrun 不同,因为它是用 Swift 而不是 C 编译的。swift 在 MacOS manpages 文档中没有很好地记录,但是,Apple 在其 blog 中记录了这些工具:

    Xcode 6.1 引入了另一种在 交互式 Read Eval Print Loop 或 REPL 的形式。

    REPL 本质上是一个交互式编译环境或 shell。首先,REPL 读取代码,然后对其进行评估、打印并重复该过程。可以想象,与 IDE 相比,我们可以使用 REPL 开发的东西要少得多。但是,除了 iOS、watchOS 和 macOS 开发之外,Swift 还有其他用例。

    swift 包括standard library,但不包括FoundationUIKit 等库。 iOS 或 macOS 开发几乎肯定需要这些 Swift 库,因此我们不能单独使用swift REPL 开发应用程序。但是,Swift on Server 项目经常使用 swift 在 Linux 甚至 Windows 机器上运行 Swift 代码。

    为了更广泛地采用,Apple 已将swift 提供在 Xcode 不易使用的不同操作系统上。 Swift 现在还有 Docker support 和 Windows。 Docker 使我们能够在任何机器上运行swift,而不管底层操作系统如何。 swift 在这些应用程序中用作脚本语言。

    SoS 奖励说明

    swift 主要用于Swift on Server。 Swift on Server 对服务器应用程序具有出色的性能,具有更低的内存占用、快速的启动时间和确定性的性能。虽然它在某些任务上不如 .NET core 快,但这是因为 Swift 具有严格的类型系统、ARC 的垃圾收集以及针对特定于服务器的应用程序的较少优化,因此更加安全。许多早期采用者称赞改进的语言类型系统、内存效率和算法性能。事实上,Swift vapor 是comparable to Python 和 Ruby 在 JSON 序列化任务上的效率。 Swift 在极少数任务上可与 Java 媲美,但随着语言和生态系统的发展,这种情况可能会发生变化。

    【讨论】:

    • 感谢您的精彩文章。唯一的问题是您的 SoS 笔记:ARC 不是垃圾收集。在您提供的 SoS 链接中,有这句话“Swift 使用 ARC(而不是跟踪垃圾收集)......”
    • 不。 ARC 是垃圾收集的一种形式。跟踪垃圾收集是不同的。见:softwareengineering.stackexchange.com/questions/285333/…
    【解决方案2】:

    xcrun

    xcrun 视为方便的前缀,以调用当前活动的Xcode 工具链中的各种命令行工具

    多个 Xcode 工具链可以安装在同一台机器上(例如最新版本、测试版和/或旧版)。使用xcode-select 设置使用哪个Xcode 工具链路径。或者,可以通过设置环境变量SDKROOTDEVELOPER_DIR 来指定活动的开发者工具链

    ### open 'bin' directory for the currently active Xcode toolchain 
    open `xcode-select --print-path`/usr/bin
    

    可以使用xcrun 调用的工具包括:

    • xcrun simctl &lt;subcommand&gt; ~ iOS 模拟器控制
    • xcrun xcodebuild … ~ 构建/测试 Xcode 项目和工作区
    • xcrun xctrack &lt;commands&gt; [options] ~ 记录、导入和导出 Instruments .trace 文件
    xcrun not_a_tool  help
    
    # xcrun: error: 
    #   sh -c '/Applications/Xcode.app/…/bin/xcodebuild 
    #         -sdk /Applications/Xcode.app/…/MacOSX.sdk 
    #         -find not_a_tool 2> /dev/null' 
    # failed with exit code 17664: (null) (errno=Invalid argument)
    

    xcodebuild

    xcodebuild 视为一个命令行工具,专门用于利用多个其他工具来管理整个项目构建过程xcodebuild 的调用将执行许多任务,就像在创建和/或测​​试项目时 Xcode GUI 菜单中的 BuildTest 所做的那样。

    xcodebuild -help
    

    注意:xcodebuild 可以在没有继续 xcrun 的情况下调用。

    xcrun xcodebuild -version
    # Xcode 13.2.1
    # Build version 13C100
    
    xcodebuild -version 
    # Xcode 13.2.1
    # Build version 13C100
    

    迅速

    swift(单独)视为swiftc 编译器的解释型、交互式、可编写脚本的“REPL”版本。

    然后,扩大范围,将swift 视为不需要 Xcode 存在的扩展生态系统的基础(除非需要 AppleOS SDK 来针对 Apple 硬件)。

    swift --help
    
    # SUBCOMMANDS (swift <subcommand> [arguments]):
    #   build:   SwiftPM - Build sources into binary products
    #   package: SwiftPM - Perform operations on Swift packages
    #   run:     SwiftPM - Build and run an executable product
    #   test:    SwiftPM - Build and run tests
    

    命令行示例

    > swift
    Welcome to Swift version 5.5.2-dev.
    Type :help for assistance.
      1> 8 + 9 
    $R0: Int = 17
      2> :quit
    

    注意:Swift REPL works in conjuction with the LLDB debugger,因此在 REPL 会话期间,: 会先于 helpquit 等命令。

    Swift 脚本示例

    脚本example_script.swift文件:

    #!/usr/bin/swift
    
    // File: example_script.swift
    // reminder: chmod to executable before invoking the script
    
    import Foundation
    
    func printHelp() {
        print("Please add an argument to the command line. Thanks.")
    }
    
    // --- main ---
    if CommandLine.argc < 2 {
        printHelp()
    } else {
        for i: Int in 1 ..< Int(CommandLine.argc) {
            print( CommandLine.arguments[i] )
        }
    }
    

    解释和编译脚本执行:

    ls -l example_script.swift 
    # -rwxr--r--@ 1 user staff  321 Dec 30 16:56 example_script.swift
    
    ./example_script.swift 
    # Please add an argument to the command line. Thanks.
    
    ./example_script.swift a bcd
    # a
    # bcd
    
    ## Let's compile the script...
    swiftc example_script.swift 
    
    ls -l ex*
    # -rwxr-xr-x  1 user staff  55136 Dec 30 16:57 example_script
    # -rwxr--r--  1 user staff    321 Dec 30 16:56 example_script.swift
    
    ./example_script
    # Please add an argument to the command line. Thanks.
    
    ./example_script xyx
    # xyz  
    

    REPL:读取评估打印循环

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 2023-01-26
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多