In my last post, I expressed some reservations about the method I was using to generate the correct compiler target for the AST command. Well, I appealed to a higher power (the swift-users mailing list) and got back this incredibly helpful response from Jordan Rose:
The Swift target triples are the same as LLVM’s target triples. Your logic matches what Xcode’s currently doing to invoke Swift (and I assume that’s where you got it from :-) ), but there’s no guarantee that it will continue to work. If you really want something stable-ish, I’d suggest doing a -dry-run xcodebuild and extracting the swiftc lines from that.
What an awesome suggestion! And that got me thinking – this would also be a better way to find all the Swift source files that should be included in the AST! Read on for an updated solution.
A Brief Aside While I BASH My Head Against The Wall
One of my goals in writing this script was to keep it as simple as possible, and not require any additional dependencies or installed tools. In that vein, I wrote it as a Bash shell script because you can easily enter it directly into the content area of the Run Script Build Phase.
However, time and again I found myself running into Bash’s limitations. Especially it’s deficiencies in handling regular expressions. While working on the new and improved version, I finally reached a breaking point and decided to abandon the Bash script altogether. But what to replace it with?
Well, why not Swift? I know Swift and (presumably) so do you. And one of the great things about Swift is that it can be executed from the command line, like a shell script. So I forged ahead and re-wrote the entire script in Swift. Using Swift in this way still has a few rough edges, but I would definitely consider it for my future scripting needs.
The New And Improved Script
Without further ado, here is the new and improved script:
This script does an xcodebuild dry run and extracts some important info from it. It then feeds that into the swiftc command as before.
To execute it from the Run Script Build Phase, you can just use this command:
/usr/bin/env xcrun --sdk macosx swift path_to/GenerateAST.swift
Now if you use the proper shebang, you can actually execute a Swift file directly. However, that requires setting the executable bit on the file, a bit of housekeeping I’d rather avoid. So I went with the above invocation. You could also type Swift code directly into the content area of your Run Script Build Phase, if you set the shell to /usr/bin/env xcrun –sdk macosx swift. But then you wouldn’t get any syntax highlighting or other niceties.
So there we go – dumping the Swift AST using Swift. Now I’m going to be able to move on to the fun stuff – code generation!