Swift Runtime

Hello folks,

It's really amazing to see Swift running on Android OS! I want to learn more about the swift binary and runtime.

  • How does one go from swift source code -> binary and load binary -> display content in a terminal.

Is there an existing post of how does the swift runtime work?

Thank you,

2 Likes

We have a getting started page to help you with getting a binary pushed to an Android device and running: Getting Started with the Swift SDK for Android | Swift.org

And some examples: GitHub - swiftlang/swift-android-examples

CC @marcprux

What kind of info are you looking for exactly? Those are two very broad questions.

Swift is a natively-compiled language, and the Getting Started page that Joannis just linked shows you how to use the Swift toolchain to build command-line binaries that will run in an Android terminal easily.

In general, Swift builds on the native C library for each platform, Bionic in the case of Android, to provide abstractions like the Android overlay and Swift Concurrency, which uses pthreads from Bionic underneath. Foundation provides many basic tools you would expect in other languages and works well in Android too.

1 Like
  • How does the swift compiled code calls into the native C code?
  • Also, how does runtime feature like ARC and async/await work, if there are no runtime?

Is there a good blog/article for this? I'm looking in writing one, if there isn't one.

Swift has great support for interfacing with C and Objective-C. The Swift compiler ingests any C headers you provide and will generate the right machine code to call those C functions, no need for you to write any manual bindings like in other languages. It is not foolproof- it can only decipher constants from the C preprocessor, for example- but works very well.

Right now, there is even work under way to go the other way more easily, ie write your code in Swift and call it from C. :smiling_face_with_sunglasses:

Oh, there is definitely a Swift runtime, you can find its source here. It is written in a mix of Swift and C++. I was merely pointing out that it builds on top of your OS C library on most platforms, except maybe less for the embedded targets.

Not that I know of, but difficult to keep track of all the Swift articles and blogs out there. :wink: Use your favorite search engine and see what you find.

1 Like

OK, this is the main reason I'm looking into Swift Runtime. Some of the features aren't available; those are most likely Swift Runtime-enabled features.

Do you know what features will not be available? I think this will also apply to the Android project?

Unavailable where, on Android?

I assume you mean for the embedded subset: there is a vision document linked from the blog post I gave you that lays that out.

No, the Swift SDK for Android provides a full runtime, and does not provide an embedded subset.

I'm fairly sure embedded mode does apply to the Swift SDK for Android - but I don't think anyone tried it out. The main difference is that you can't use existentials, by far the biggest extent of that limitation is the throws keyword which now needs to be a typed throws e.g. throws(MyError)

It depends what you mean by this: if you mean the full Embedded mode can be applied to the current Swift SDK for Android, I don't think it can. For example, those using the Embedded subset for linux do not simply use the existing linux SDK, but I believe have separate swiftmodules and libraries for the Embedded subset.

I have never tried the new Embedded mode though: this is just my surface knowledge from poking around the installed files a bit, maybe @rauhul can enlighten us.

If you mean that such an Embedded subset could also be produced separately for Android one day, that's what I was saying too.

Embedded Swift does require its own .swiftmodules for the stdlib (and any other modules), its unlikely these are present in the Android SDK unless intentionally added.

2 Likes