Importing Swift packages via CMake

I’m trying to learn more about how to build things in Swift. I come from a C/C++ background and am very familiar with CMake, and I understand that CMake does have language support for Swift. Being able to lean on CMake would be extremely useful for me, given the projects I tend to work with.

I’ve read and understood some resources on building Swift applications in CMake which link to C/C++ libraries, or which build Swift libraries and link them with a Swift application. The missing link for me at the moment is one last permutation of this: how to use CMake to link a Swift application against an external Swift package.

For example, I’d like to be able to build a Swift application using CMake, and use something like https://swiftpackageindex.com/apple/swift-argument-parser to parse the arguments. I’ve seen examples that add external packages into Package.swift, but can’t find anything on how to use them in CMake. The closest I’ve found is swift-cmake-examples/BuildDependencies/CMakeLists.txt at main · compnerd/swift-cmake-examples · GitHub, but this is 5 years old and so I imagine is quite out of date by now.

Is there an easy way to accomplish this in CMake, or do I have to use the Swift package manager?

We have an example of use of SAP with CMake in WasmKit: WasmKit/CMakeLists.txt at 8eed37f037894e80a4b27e3307f79ac2242096dc · swiftwasm/WasmKit · GitHub

The dependency is pulled via FetchContent_Declare.

2 Likes

Aha, that’s much simpler than what I was trying. Thank you!

1 Like