SwiftPM does not create a .dll file

Sorry, I could have been clearer that I was agreeing with you. :slightly_smiling_face:

Back to my original question:
Is there any way to build a shared dynamic library using SwiftPM?
Do I just need to add a stub executable file target somewhere in the Swift.package or this is not possible at all?

When you declare a library product, you can specify a dynamic library:

products: [
  .library(name: "...", type: .dynamic, targets: ["..."])
]

and this will (on macOS) produce a .dylib in the build folder.

2 Likes

Thank you very much!
Works on a Mac, works on Windows.

Mostly I meant that people use SwiftPM to make executables. Which it can do perfectly fine, depending on the “object library” targets in other packages.

(Personally I think if SwiftPM wants to be used for building any sort of distributable binary, it should put them outside of its secret build directory, but that’s just not what it does today.)

Maybe swift package init should produce a package for an executable by default. I always wondered why this is not the case.

Or it could still produce a package for a library, but a dynamic one. That would make much more sense for using swift build on it.

Just my personal opinion.

Is this actually true? How many SwiftPM packages are dependencies of executables versus executables themselves? Surely the default being “library” wasn’t arbitrary.

Yeah, that’s fair. Apple is used to there being an order of magnitude more apps than libraries, but that may not translate to a general principle, and Xcode being able to consume SwiftPM packages muddies the statistics a lot. Apps and executables are the leaves of the tree* of All Dependencies In The World, the stdlib is the root, and everything in the middle is a library.

* technically a forest, with no-stdlib libraries and now the embedded variant of the stdlib, not to mention if you count different platforms as different stdlibs, but close enough

Yeah, when I mean 'platform', I don't mean 'Linux' here in the general sense, I mean e.g., Ubuntu 22.04.3 LTS, a very specific distribution and patch version.

Here maybe I'm missing something, if e.g., libcurl or limbxml2 are statically linked into e.g., swift-corelibs-foundation and are being used as an internal supporting library and not as a currency type in the actual API exposed by Foundation, why would it matter?

Even the "same-state-of-your-unique-system-installation" would be highly useful for our use case - deployment on a heterogenous cluster where all nodes really will be the same - the same would go for someone deploying on e.g. standardised EC2 containers or similar, where you know that the nodes will be identical - even if it would be an opt-in with caveat/warning documentation, it'd be something we'd be happy to use.

An environment variable "PLEASE_ENABLE_XCFRAMEWORK_FOR_LINUX_WE_WILL_BE_VERY_CAREFUL=1" would be good to avoid having to roll a separate toolchain ;-)

Thanks Saleem, much appreciated - we'll have a look if that could be an option for us.