I'm trying to print the Abstract Syntax Tree (AST) from a Swift file using the Swift compiler with the -print-ast
flag. This is without Xcode & xcodebuild
.
I'm stuck handling imports of 3rd party frameworks, built via Carthage. Given a source file with the following code:
import Foundation
import BrightFutures // 3rd party framework
class MyAsyncService {
func asyncFunc() -> Future<String, NSError> {
return Promise<String, NSError>().future
}
}
By specifying the framework search path (-F
) 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()
}
I need to print the AST for a project only targeting iOS, with dependencies built for iOS.
When I try pointing to the frameworks built for iOS:
swiftc -print-ast Source.swift -F Carthage/Build/iOS
The command fails:
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've tried also tried adding the -sdk
flag:
-sdk $(xcrun --sdk iphoneos --show-sdk-path)
to specify the iphoneos SDK, but this spits out an error about unsupported architectures.
How can I get this to build?
Looks like an alternative to printing the AST with the Swift compiler is to use SourceKit.
There's a CLI tool called SourceKitten which allows you to parse the structure of a Swift source file into JSON format, without needing to handle imports of 3rd party frameworks and other source files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With