Dynamic Swift Framework Without Xcode

I came up with this question recently:

And I'm going to give the answer in this post (spoiler alert: yes, sort of).

Here's the content of a framework created by Xcode:

Just Framework Structure

Some of the files, such as Info.plist, are obviously construct of Xcode. Play with swiftc long enough, one would find that the .swiftdocs, the .swiftmodules and the one binary file came from the Swift compiler.

Instead of listing the relevant swiftc command options, I've created a sample project to demonstrate how one can complie and link to frameworks so that they can be imported in the application code. The key ingredient for achieving it lies in Makefile. In summary, swiftc can do these for us:

  1. generate a binary as a library/framework
  2. emit a .swiftmodule file, which Swift needs to understand that binary.
  3. assign a path the Swift runtime needs to locate this framework.
  4. compile source code that imports the framework, given that they exist in the paths relative to the app binary in step 3.

Based on these observations, it's not hard to imagine more sophisticated build systems, such as IDEs and package/dependency management systems.