# Build Swift app to WebAssembly using SwiftPM

**URL:** <https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551>\
**Category:** Swift for WebAssembly\
**Tags:** packagemanager\
**Created:** [June 19, 2025, 12:10am UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551 "2025-06-19T00:10:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![padm](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@padm](https://forums.swift.org/u/padm)\
**Post date:** [June 19, 2025, 12:10am UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/1 "2025-06-19T00:10:47Z")

</div>

Hi,

I'm trying to build my Swift project to WebAssembly without WASI (app will run in browser on client side). Building single file works great (took example from [Some feedback from my short experience with SwiftWasm - #5 by Max\_Desiatov](https://forums.swift.org/t/some-feedback-from-my-short-experience-with-swiftwasm/69605/5)):

```swift
swiftc -Osize -Xcc -fdeclspec -target wasm32-unknown-none-wasm -enable-experimental-feature Extern -enable-experimental-feature Embedded -wmo Sources/main.swift -c -o main.o
/opt/homebrew/opt/lld/bin/wasm-ld --no-entry main.o -o main.wasm

```

But when I'm trying to do it using `swift build` :

```swift
swift build --triple wasm32-unknown-none-wasm --product app -c release -Xswiftc -enable-experimental-feature -Xswiftc Extern -Xswiftc -enable-experimental-feature -Xswiftc Embedded -Xswiftc -Xcc -Xswiftc -fdeclspec

```

with Package.swift:

```swift
import PackageDescription

let package = Package(
  name: "app",
  products: [
    .executable(name: "app", targets: ["app"])
  ],
  targets: [
    .executableTarget(name: "app"),
  ]
)

```

it throws an error:

```swift
error: link command failed with exit code 1 (use -v to see invocation)
wasm-ld: error: cannot open crt1.o: No such file or directory
wasm-ld: error: unable to find library -lc
wasm-ld: error: cannot open /Users/user/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2025-06-12-a.xctoolchain/usr/lib/clang/17/lib/wasm32-unknown-none-wasm/libclang_rt.builtins.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

```

So for some reason builder adds to the linker command this parameters:

```swift
   crt1.o \
   -lc /Users/user/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2025-06-12-a.xctoolchain/usr/lib/clang/17/lib/wasm32-unknown-none-wasm/libclang_rt.builtins.a \

```

They are not required for successful linking. Is it possible somehow to tell SwiftPM to remove it from linking process?

---

<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:** [June 19, 2025, 12:23am UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/2 "2025-06-19T00:23:18Z")

</div>

The toolchain itself does not include compiler-rt for Wasm, but it's included in the Swift SDK for Wasm instead.  
I recommend to use `swift-<version>_wasm-embedded` variant [Swift.org - Getting Started with Swift SDKs for WebAssembly](https://www.swift.org/documentation/articles/wasm-getting-started.html)  
If you really don't want to use Swift SDK, you can pass `linkerSettings: [.unsafeFlags(["-Xclang-linker", "-nodefaultlibs"])`, but note that you will lose a lot of functionalities.

---

<div class="post-metadata">

**Author:** ![padm](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@padm](https://forums.swift.org/u/padm)\
**Post date:** [June 20, 2025, 9:08pm UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/3 "2025-06-20T21:08:41Z")

</div>

Thank you for your reply. I'd prefer to use Swift SDK, but when I'm building using wasm-embedded SDK it'd builds with WASI. Build command:

```swift
swift build --swift-sdk swift-6.2-DEVELOPMENT-SNAPSHOT-2025-06-17-a_wasm-embedded --product app -c release -Xswiftc -enable-experimental-feature -Xswiftc Extern -Xswiftc -enable-experimental-feature -Xswiftc Embedded -Xswiftc -Xcc -Xswiftc -fdeclspec

```

Do I need to specify exact triple for building without WASI? Like I did it previously with `-target wasm32-unknown-none-wasm` ?

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [June 21, 2025, 3:36pm UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/4 "2025-06-21T15:36:41Z")

</div>

> [@padm](#):
>
> Like I did it previously with `-target wasm32-unknown-none-wasm` ?

No, you don't need to specify any of those options with the Embedded Swift SDK. `--swift-sdk` is enough, `-c release` may be still required with 6.2 snapshots, but not with `main` snapshots where debug builds are now possible.

Thus with the `main` snapshots of the toolchain, this is the whole command for debug build:

```swift
swift build --swift-sdk swift-DEVELOPMENT-SNAPSHOT-2025-06-17-a_wasm-embedded

```

and nothing else is needed, unless you build a specific product with `--product`.

> [@padm](#):
>
> Do I need to specify exact triple for building without WASI?

This is not something I would recommend at this point, as WASI-libc provides you an allocator, RNG, basic I/O facilities, and much more, most importantly compatibility with many existing libraries and packages. The binary size is still quite small (10 kB for `print("Hello, World!")`), and you get a stable imports ABI that almost all Wasm runtimes support or have shims for.

What's your use case for building without WASI?

---

<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:** [June 22, 2025, 6:56am UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/5 "2025-06-22T06:56:21Z")

</div>

FYI: Some tools provide a way to replace WASI syscalls with stub implementations. [GitHub - near/wasi-stub: This tool takes a wasm file, replace all wasi\_snapshot\_preview1 import to (stub) functions defines in the same module. This is useful when executing wasm in sandbox enviroment where wasi is not available.](https://github.com/near/wasi-stub)

---

<div class="post-metadata">

**Author:** ![vlmoon99](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vlmoon99/32/41359_2.png) [@vlmoon99](https://forums.swift.org/u/vlmoon99)\
**Post date:** [June 22, 2025, 8:37am UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/6 "2025-06-22T08:37:16Z")

</div>

Yes, but it still doesn't work on the NEAR blockchain. I compile the WebAssembly (WASM) file, stub the WASI imports, and disable all WASM features, but I still encounter errors when calling the function on-chain. So far, I've only had success with WASM files compiled using the `wasm32-unknown-unknown` target. Every language that supports this target allows me to upload the code to the blockchain and call functions. However, with languages that use the `wasm32-unknown-wasi` target, I haven’t been able to get it working for some reason.

---

<div class="post-metadata">

**Author:** ![vlmoon99](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vlmoon99/32/41359_2.png) [@vlmoon99](https://forums.swift.org/u/vlmoon99)\
**Post date:** [June 22, 2025, 8:38am UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/7 "2025-06-22T08:38:29Z")

</div>

But I also want to mention that compiling Swift code to WebAssembly was a great experience for me.

---

<div class="post-metadata">

**Author:** ![vlmoon99](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vlmoon99/32/41359_2.png) [@vlmoon99](https://forums.swift.org/u/vlmoon99)\
**Post date:** [June 29, 2025, 1:33pm UTC](https://forums.swift.org/t/build-swift-app-to-webassembly-using-swiftpm/80551/9 "2025-06-29T13:33:35Z")

</div>

[vlmoon99/near-sdk-swift: Experimental Near SDK for Swift lang](https://github.com/vlmoon99/near-sdk-swift)  
I built and deployed it on the chain, and it works well. I believe this can be very useful in the future for developers who want to write smart contracts in Swift and communicate with the blockchain using Swift and WebAssembly. Thanks to everyone who made this possible!
