Swift compiler: printing AST from a Swift file with third-party import

I am trying to print an abstract syntax tree (AST) from a Swift file using a Swift compiler with a flag -print-ast. This is without Xcode and xcodebuild.

I am stuck in importing third-party frameworks built through Carthage. Given the source file with the following code:

Source

import Foundation
import BrightFutures // 3rd party framework

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

Compilation for macOS works

By setting the wireframe search path ( -F) with the following command:

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

Produces the expected output:

import Foundation
import BrightFutures

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

Compilation for iOS?

I need to print an AST for an iOS-only project with dependencies created for iOS.

When I try to point to frameworks created for iOS:

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

Team Failure:

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()
}

I also tried adding a flag -sdk:

-sdk $(xcrun --sdk iphoneos --show-sdk-path) to iphoneos SDK, .

?

+4
1

, AST Swift SourceKit.

CLI SourceKitten, Swift JSON, .

+3

All Articles