# Embedding rustc on iOS

**URL:** <https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824>\
**Category:** compiler\
**Created:** [November 12, 2018, 4:07pm UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824 "2018-11-12T16:07:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![swiftcoder](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/swiftcoder/32/2431_2.png) [@swiftcoder](https://internals.rust-lang.org/u/swiftcoder)\
**Post date:** [November 12, 2018, 4:07pm UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/1 "2018-11-12T16:07:24Z")

</div>

Hi folks,

I’m trying to embed rustc in an iOS app (roughly to produce a “learn rust” IDE-like environment for iPad). Assuming that the rust compiler is fully embedded in the app, and that I only compile to WASM and execute the result in an embedded WebKit view, it ought to pass muster in AppStore review - but I’ll cross that bridge when I get to it.

In the meantime, I’m running into issues that affect progress:

1. The compiler crates are only built as dynamiclib, so can’t be built into a staticlib suitable for embedding.
2. The compiler won’t produce dynamiclibs for any `*-ios` targets.

I can trivially correct (1) by adding rlib targets to each compiler crate, as in [https://github.com/rust-lang/rust/pull/55860](https://github.com/rust-lang/rust/pull/55860). Alex doesn’t sound very positive on the topic of building these by default (and sadly cargo doesn’t provide target type overrides for dependencies).

There’s a possibility that I wouldn’t need to solve (1) if it weren’t for (2). I presume the `*-ios` targets refuse to build dylibs because Apple traditionally didn’t support dynamic linking on iOS, but to the best of my knowledge they relaxed that restriction after iOS 8, and dylibs can be linked if packaged as a codesigned embedded framework. I haven’t had a chance to test whether this would actually pass App Store review, and the internet has mixed opinions on the topic.

Regardless, I’d like to understand why the compiler crates are built as dylibs in the first place. Is this a space optimisation because multiple tools include the same crates? A startup-time optimisation?

How much overhead are we talking about if I were to introduce the rlib targets in addition? The intermediates should all be shared between the dynamiclib and the rlib, no?

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [November 12, 2018, 6:50pm UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/2 "2018-11-12T18:50:11Z")

</div>

> Regardless, I’d like to understand why the compiler crates are built as dylibs in the first place.

So that compiler plugins can be dynamically linked to rustc.

> The intermediates should all be shared between the dynamiclib and the rlib, no?

Those are included in both. An rlib consists of those intermediate object files, while a dynamiclib consists of them linked together, so that would mean 2x size.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [November 12, 2018, 7:59pm UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/3 "2018-11-12T19:59:59Z")

</div>

> [@swiftcoder](#):
>
> 1. The compiler crates are only built as dynamiclib, so can’t be built into a staticlib suitable for embedding

Starting with iOS 8, you can build dynamic libraries into your application.

---

<div class="post-metadata">

**Author:** ![swiftcoder](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/swiftcoder/32/2431_2.png) [@swiftcoder](https://internals.rust-lang.org/u/swiftcoder)\
**Post date:** [November 13, 2018, 3:31am UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/4 "2018-11-13T03:31:29Z")

</div>

> [@bjorn3](#):
>
> So that compiler plugins can be dynamically linked to rustc.

Don't those plugins eventually get linked together into rustc? Or are they also used in other binaries?

> [@H2CO3](#):
>
> Starting with iOS 8, you can build dynamic libraries into your application.

Indeed. However rustc refuses to generate dynamiclibs for ios targets:

```
error: cannot produce dylib for `..` as the target `x86_64-apple-ios` does not support these crate types

```

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [November 13, 2018, 7:29am UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/5 "2018-11-13T07:29:21Z")

</div>

> [@swiftcoder](#):
>
> Don’t those plugins eventually get linked together into rustc? Or are they also used in other binaries?

These plugins get `dlopen`ed by rustc.

---

<div class="post-metadata">

**Author:** ![swiftcoder](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/swiftcoder/32/2431_2.png) [@swiftcoder](https://internals.rust-lang.org/u/swiftcoder)\
**Post date:** [November 14, 2018, 2:54pm UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/6 "2018-11-14T14:54:11Z")

</div>

All right, well, assuming that static linking is unlikely to be supported, what would be required to enable dylib generation on ios targets?

---

<div class="post-metadata">

**Author:** ![QuietMisdreavus](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quietmisdreavus/32/2425_2.png) [@QuietMisdreavus](https://internals.rust-lang.org/u/QuietMisdreavus)\
**Post date:** [November 14, 2018, 3:33pm UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/7 "2018-11-14T15:33:39Z")

</div>

> [@swiftcoder](#):
>
> All right, well, assuming that static linking is unlikely to be supported, what would be required to enable dylib generation on ios targets?

My naive answer would be to modify the target spec for ios, which is defined [in the `rustc_target` crate here](https://github.com/rust-lang/rust/blob/6f244c9011eb01ae4d167b631ba5e3e163bafa72/src/librustc_target/spec/apple_ios_base.rs#L93-L104). It looks like it sets a `dynamic_linking` flag to false there, so it could be as simple as changing that. I'm not sure if that will require other changes elsewhere, but that's where i would start.

---

<div class="post-metadata">

**Author:** ![swiftcoder](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/swiftcoder/32/2431_2.png) [@swiftcoder](https://internals.rust-lang.org/u/swiftcoder)\
**Post date:** [December 2, 2018, 6:46pm UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/8 "2018-12-02T18:46:38Z")

</div>

> [@QuietMisdreavus](#):
>
> My naive answer would be to modify the target spec for ios, which is defined [in the `rustc_target` crate here](https://github.com/rust-lang/rust/blob/6f244c9011eb01ae4d167b631ba5e3e163bafa72/src/librustc_target/spec/apple_ios_base.rs#L93-L104). It looks like it sets a `dynamic_linking` flag to false there, so it could be as simple as changing that. I’m not sure if that will require other changes elsewhere, but that’s where i would start.

That does seem to work, however there are several other interesting barriers along this path.

There doesn't seem to be a supported mechanism for cross-compiling the compiler crates. There isn't a technical problem with doing this - if I take a local path dependency on the compiler crates (and add `#![feature(rustc_private)]` to all of them), they compile just fine for the ios targets, but the `x.py` build system doesn't seem to provide a way to actually build the compiler crates for any target except the host triple.

Since the entire dependency chain is dylibs in this scenario, I'm going to have to write a dynamic equivalent to the `cargo lipo` tool, to lipo up the multiple architectures of every single dependent dylib, and export them in a form suitable for XCode to consume/link.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:31am UTC](https://internals.rust-lang.org/t/embedding-rustc-on-ios/8824/9 "2019-03-25T08:31:08Z")

</div>

Closed on March 25, 2019, 8:31am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
