Swift Build Generates Unexpected File

Jun 5, 2020

Yesterday, I started playing around with Swift 5.2 on Linux (Ubuntu 20.04). This is mostly motivated by my desire to see if I can create a Swift module for the Godot engine. I started - as you’re supposed to - by using the Swift Package Manager to create a new package. The final form of this package is supposed to be a dynamic shared library, one that will be loaded by the game engine. However, in the interest of faster iteration, I created the package as an application rather than a library for initial testing. Once I was satisfied with the initial testing, I changed the product from .application to .library, added a type: .dynamic and expected that would suffice.

However, running swift build continued to produce a binary instead of producing a dynamic library (.dynlib, .dll or .so). The desired .so file was missing. I tried debugging it by creating a brand new package structured as a library from the get-go, and swift build correctly produced a .so file for that. The Package.swift files for both packages were virtually identical. After an hour or so, I discovered that the main difference was the name of the swift source file in the packages. In the package I’d created first - the one that started as an application and then turned into a library - the source file was named main.swift. In the second package - the one that was a library from the outset - the source file was named MyPackage.swift.

I renamed the file in the first package from main.swift to MyPackage.swift, and swift build magically started producing a MyPackage.so shared library.