草庐IT

ios - Swift 编译器 : Printing the AST of a Swift file with 3rd party imports

coder 2023-09-07 原文

我正在尝试 print the Abstract Syntax Tree (AST) from a Swift file使用带有 -print-ast 标志的 Swift 编译器。这没有 Xcode & xcodebuild

我无法处理通过 Carthage 构建的第 3 方框架的导入。给定一个包含以下代码的源文件:

来源

import Foundation
import BrightFutures // 3rd party framework

class MyAsyncService {
    func asyncFunc() -> Future<String, NSError> {
        return Promise<String, NSError>().future
    }
}

为 MacOS 编译

通过指定框架搜索路径(-F)执行以下命令:

swiftc -print-ast Source.swift -F Carthage/Build/Mac

产生预期的输出:

import Foundation
import BrightFutures

internal class MyAsyncService {
  internal func asyncFunc() -> Future<String, NSError>
  @objc deinit
  internal init()
}

为 iOS 编译?

我需要为一个只针对 iOS 的项目打印 AST,并为 iOS 构建依赖项。

当我尝试指向为 iOS 构建的框架时:

swiftc -print-ast Source.swift -F Carthage/Build/iOS

命令失败:

Source.swift:2:12: error: module file was created for incompatible target x86_64-apple-ios8.0: [...]/Carthage/Build/iOS/BrightFutures.framework/Modules/BrightFutures.swiftmodule/x86_64.swiftmodule
    import BrightFutures // 3rd party framework
           ^
import Foundation
import BrightFutures

internal class MyAsyncService {
  internal func asyncFunc() -> <<error type>>
  @objc deinit
  internal init()
}

我也尝试过添加 -sdk 标志:

-sdk $(xcrun --sdk iphoneos --show-sdk-path)specify the iphoneos SDK ,但这会吐出有关不受支持的体系结构的错误。

我怎样才能构建它?

最佳答案

看起来用 Swift 编译器打印 AST 的替代方法是使用 SourceKit .

有一个名为 SourceKitten 的 CLI 工具它允许您将 Swift 源文件的结构解析为 JSON 格式,而无需处理 3rd 方框架和其他源文件的导入。

关于ios - Swift 编译器 : Printing the AST of a Swift file with 3rd party imports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36102364/

有关ios - Swift 编译器 : Printing the AST of a Swift file with 3rd party imports的更多相关文章

随机推荐