Announcing Swift support in CMake

Hello again fellow Swift users,

I know that build systems are generally contentious and inspire heated discussions. I do really hope that people will be kind enough to NOT derail this thread into discussions of build systems and the merits and demerits of CMake. There are existing projects which use CMake and this can be valuable for them. I am in no way instructing that this be the way that everyone builds their software.

Over the past few years, CMake has really gained a significant amount of traction in the Open Source communities as well as outside of it. It is incredibly flexible and enables some uses cases which are difficult to support in many other build systems e.g. cross-compilation. In fact, the LLVM (the umbrella project) eventually moved over to CMake as the canonical build system. Swift inherited that as well, and uses CMake for the build system. However, the current state of building Swift code bases with CMake is not particularly convenient for build projects incorporating Swift code.

I have been working with upstream to integrate Swift support into CMake with the Ninja generator so that it can be integrated into existing projects much more conveniently. I would like to announce that the last of the initial set of changes needed to support Swift in CMake have been merged now into CMake and I expect that the next major release of CMake (3.15) to include the Swift support. Note that this requires that you use the Ninja generator if you wish to use Swift support. There is interest from upstream in adding support to the Makefile backend (so patches will be welcomed).

As a means of showing what this support is capable of doing, I have created the traditional demo project - Hello World (albeit a rather convoluted and overly engineered version) - to demonstrate the support. It is available on GitHub. I tried to use modern CMake idioms in the example, but, I welcome any suggestions to improve things. Note that this is capable of building for release/debug/size as is traditionally done with CMake (using -DCMAKE_BUILD_TYPE). Additionally, it has support for testing, and demonstrates how to configure libraries for @testable builds as well. Furthermore, if you build the standard library for foreign targets, you can also use this to cross-compile the project to various other targets!

This demo project builds on at least Linux, Windows, and macOS (at least, those are the targets where I have tested the project). This requires building CMake from the current top of tree or downloading a nightly builds from CMake. The minimum version required is 3.14.20190522.

Finally, I would like to thank @jrose and @Michael_Gottesman for their incredibly helpful discussion on the approaches and how to actually get this to function properly with CMake.

‐ compnerd

67 Likes

@compnerd @John_McCall should this thread rather be moved into #general-announce, #evolution:announce or #evolution:discuss? Having it in #swift-users feels strange.

@DevAndArtist, yes, #general-announce makes sense, but I do not have the ability to post there AFAIK.

Agreed, I've moved it.

3 Likes

This is awesome! We can potentially use this for bootstrapping Swift packages on Swift CI.

5 Likes

This is great news, thanks for all the hard work!

2 Likes

You're a hero :trophy:. This is great.

1 Like

good job.

This is great!

Congratulations, great news!

This is great! Thanks @compnerd, most of my Swift projects use CMake so this is a welcome announcement.