# WebAssembly + SwiftPM

**URL:** <https://forums.swift.org/t/webassembly-swiftpm/34343>\
**Category:** Package Manager\
**Tags:** packagemanager, webassembly\
**Created:** [March 6, 2020, 12:03am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343 "2020-03-06T00:03:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 6, 2020, 12:03am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/1 "2020-03-06T00:03:30Z")

</div>

(This is mostly a question for @kateinoigakukun, but I thought I’d ask publicly for the many eavesdroppers that will come by wondering the same thing.)

I saw the [recent PR](https://github.com/apple/swift-package-manager/pull/2640) adding WebAssembly as a declarable platform in SwiftPM manifests. I’m curious how close it is to being usable. Is that the first of a long series of additions? Or does the existing cross‐compilation support mean it should pretty much already work?

A few months ago I set out [I set out to set up package testing on Android and Windows](https://forums.swift.org/t/ci-for-packages-on-windows-android/32295). I considered WebAssembly at the time too, but from what I could gather then, it appeared it was not even possible to link multiple modules together yet. But if it is now approaching usability with SwiftPM, I would be very curious to know how one would go about building a package (i.e. where to get or how to build toolchains and SDKs, what kinds of extra flags and patches are necessary, etc.).

---

<div class="post-metadata">

**Author:** ![kateinoigakukun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kateinoigakukun/32/16706_2.png) [@kateinoigakukun](https://forums.swift.org/u/kateinoigakukun)\
**Post date:** [March 6, 2020, 12:21am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/2 "2020-03-06T00:21:33Z")

</div>

SwiftPM with WebAssembly works well with that patch.

I already used SwiftPM to build a Swift project which is targeting for `wasm32-unknown-wasi` [GitHub - swiftwasm/JavaScriptKit: Swift framework to interact with JavaScript through WebAssembly.](https://github.com/kateinoigakukun/JavaScriptKit)

WebAssembly doesn't support linking multiple modules **at runtime** but wasm-ld which is a relocatable object file linker can link multiple modules **at build time**.

Now, Swift for WebAseembly depends on WASI and wasi-sysroot as SDK, but it's not installed in fixed absolute path, so users need to pass `--sysroot` flag when building.  
In addition, when exporting or importing symbols with host environment, users need to pass additional flags to allow undefined symbol at link time and to export specific symbols.  
(e.g. [https://github.com/kateinoigakukun/JavaScriptKit/blob/master/script/build-package.sh](https://github.com/kateinoigakukun/JavaScriptKit/blob/master/script/build-package.sh))

FYI: [Wasm Support - #14 by kateinoigakukun](https://forums.swift.org/t/wasm-support/16087/14)

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [March 6, 2020, 3:41am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/3 "2020-03-06T03:41:27Z")

</div>

That’s great news! If there are ways we can continue to improve SwiftPM to make WebAssembly use easier please let us know... what are other systems doing here? Would it ever make sense for SwiftPM to download the toolchain/SDK automatically and manage its location, for example?

---

<div class="post-metadata">

**Author:** ![kateinoigakukun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kateinoigakukun/32/16706_2.png) [@kateinoigakukun](https://forums.swift.org/u/kateinoigakukun)\
**Post date:** [March 6, 2020, 5:37am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/4 "2020-03-06T05:37:39Z")

</div>

I'm planning to embed WASI sysroot in Swift toolchain and pass those sysroot flag as default when targeting WebAssembly.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 12, 2020, 4:27am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/5 "2020-03-12T04:27:48Z")

</div>

Okay, I started setting it up and managed to get a heavily stripped‐down package to build. `Foundation` and `Dispatch` didn’t work, but that wasn’t much of a surprise at this early stage.

Two issues that did surprise me were:

1. The Linux toolchain doesn’t seem to work. (macOS did.)

2. It crashed when I attempted to use an imported C function.

But I must say, this is vastly simpler to install and use than the comparable Android and Windows toolchains. Apparently I had it backwards when I decided to start easy and try the others first. Well done!

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [March 12, 2020, 6:09am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/6 "2020-03-12T06:09:36Z")

</div>

> [@kateinoigakukun](#):
>
> I'm planning to embed WASI sysroot in Swift toolchain and pass those sysroot flag as default when targeting WebAssembly.

It would be nice to create a kind of “swift SDK” spec - I’m thinking basically a directory with a file specifying default compiler options and containing (or symlinking to) whatever files or tools it needs.

Perhaps we can then offer a built-in command to easily download and install SDKs so people can share cross-compiling toolchains.

The long commands full of various paths and flags you need when cross-compiling can get kind of annoying.

I mean, even having to write the full triple is a bit ugly. It would be nicer to just say `swift build -target wasi32`, where “wasi32” could be the name of the SDK.

---

<div class="post-metadata">

**Author:** ![kateinoigakukun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kateinoigakukun/32/16706_2.png) [@kateinoigakukun](https://forums.swift.org/u/kateinoigakukun)\
**Post date:** [March 12, 2020, 6:56am UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/7 "2020-03-12T06:56:13Z")

</div>

Thanks for investigation!

> [@SDGGiesbrecht](#):
>
> 1. The Linux toolchain doesn’t seem to work. (macOS did.)

Sorry, I forgot to set rpath for `swift-build` to find dynamic libraries relative to itself path. I fixed this issue in this PR [[WASM] Pass -rpath flag to find dynamic library from relative to it's executable path by kateinoigakukun · Pull Request #374 · swiftwasm/swift · GitHub](https://github.com/swiftwasm/swift/pull/374)

> [@SDGGiesbrecht](#):
>
> 1. It crashed when I attempted to use an imported C function.

This crash is expected behavior because clang doesn't allow non-prototyped function definition without varargs. (But I know we should diagnose before crash 😅 )

After adding explicit `void` parameter to indicate that the [`helloC`](https://github.com/SDGGiesbrecht/Workspace/blob/be25ea1b4e19f13e4917bfa8b0268b63fe77aacc/Tests/WSCrossPlatformC/include/WSCrossPlatformC.h#L17) doesn't take any argument, building the target will be succeeded.

This explicit prototype declaration is necessary because WebAssembly requires that callee and caller signatures are same.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 12, 2020, 8:01pm UTC](https://forums.swift.org/t/webassembly-swiftpm/34343/8 "2020-03-12T20:01:53Z")

</div>

> [@Karl](#):
>
> It would be nice to create a kind of “swift SDK” spec

I think @compnerd from the core team has been working on that.

My personal opinion is that in the long run SwiftPM should be able to simply do `swift build --platform android`. (`--target` already means something else.) At that point SwiftPM would check standard install locations for matching SDKs and build each architecture for which you’ve installed an SDK. This would essentially mirror what `xcodebuild` does with iOS.

I don’t know if the SDK arrangement is ready for that though, because right now if you unzip multiple architectures’ SDKs to `/`, it looks to me like they would stomp on each other’s files, since they share the same SDK name right now. I think the goal in the end was to have a single multi‐architecture SDK, (like iOS), but it doesn’t look like that will be a reality for a while.

In the meantime I have been adding some of the flags to SwiftPM so that fewer of them need to be supplied manually each time.

@ddunbar’s idea of having SwiftPM (or some other tool) manage SDK downloads and updates could be interesting, but the first step is to get them actually working cleanly. Not much sense having a managed download if you still have to manually look up the extra flags.
