Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Google Engineer Tailors Swift for Windows

Google Engineer Tailors Swift for Windows

This item in japanese


The vision for Swift to become a cross-platform programming language has moved one step further with the introduction of a Swift toolchain for Windows 10. Early adopters can now use Swift to create Windows 10 programs that interoperate through the C ABI with existing libraries available on the platform.

As Google engineer and Swift core team member Saleem Abdulrasool explained, his work to complete the port took over one year, thanks also to the help and support of several other Swift engineers.

Porting Swift to Windows is not about simply porting the compiler, but rather ensuring that the full ecosystem is available on the platform. This includes the compiler, the standard library, and the core libraries (dispatch, Foundation, XCTest).

One component still missing from the picture is the Swift package manager, but, says Abdulrasool, it is coming soon. For the moment, developers can build their Swift programs on Windows using CMake or Ninja.

Abdulrasool mentions demand for Swift on Windows from the community as one of his main motivators but the inherent technical challenges somehow contributed to slowing down this effort until now. Previous attempts at porting Swift to Windows had focused on relying on portability layers such as cygwin, MinGW, or Window Subsystem for Linux. Each had its own set of advantages and disadvantages, but Abdulrasool wanted to bridge to the system libraries and make the Windows API directly available to Swift programmers, which set him in a completely different spot.

The effort of porting Swift to Windows had to deal with a lot of mismatches between the Windows development environment and Linux's, including dependencies on CMake, autotools, and custom build systems that Swift required, coupled with inferior tooling on the Windows platform. This led Abdulrasool to opt for cross-compilation as a way to keep working on Linux and make his life easier on a number of accounts, including integrating in the developer workflow required by the Swift core team.

Cross-compiling Swift for Window on a Linux system posed its own set of challenges, though, from Windows SDK header and library filename mismatches due to case-sensitiveness on Linux, to differences in C++ implementations. Abdulrasool provided a lot of detail about this in his 2019 talk at the LLVM Dev Meeting.

As proof that Swift can be used to create real experiences on Windows, Abdulrasool presented a demo calculator whose source code is available on GitHub and the porting of Readdle Spark, a cross-platform email client, to Windows. Readdle had been using Swift to share code across Android, iOS, and macOS when they decided to port their app to Windows.

Despite some functionality being unready as of yet, Swift on Windows turned out to be fully satisfying our needs. In fact, some third party C/C++ dependencies gave us more headaches than Swift did itself. All business logic of Spark is located in a separate Core module. A pack of modules, actually, but we refer to them as Core. This allows us to use any UI framework on the target platform: AppKit on macOS, UIKit on iOS, native UI Toolkit on Android.

In particular, speaking of Swift library integration, using pure Swift modules like CryptoSwift and OAuthSwift was straightforward. For GUI development, Electron was the go-to option, which required building Spark Core as a loadable module for Node.js. This was made possible by Swift support for the C ABI, so the Node module was implemented as Swift code wrapped by a few C lines.

Work on Swift for Windows is not complete yet. The next milestones, according to Abdulrasool, will be the Swift package manager and lldb, which will be used to power a REPL and debugging experience. If you are insterested, you can follow progress and discuss future development in the relevant Swift forum. Bugs can be reported to the Swift Bug Tracker.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p