jedisct1 / libsodium

A modern, portable, easy to use crypto library.
https://libsodium.org
Other
12.22k stars 1.74k forks source link

How to build for wasm-wasi? #872

Closed kpp closed 5 years ago

kpp commented 5 years ago

In https://github.com/sodiumoxide/sodiumoxide/issues/347 we want to embed libsodium.wasm into sodiumoxide. Where can I find prebuilt binaries for wasm?

jedisct1 commented 5 years ago

WASM/WASI modules can be created by running the dist-build/wasm32-wasi.sh build script.

This requires a recent LLVM toolchain, as well as the builtins.

Pre-built modules for stable versions can be downloaded from the Azure Pipelines CI (ex: https://jedisct1.visualstudio.com/Libsodium/_build/results?buildId=124 - Then click on "Artifacts").

WASM and WASI are still moving targets, so these pre-built modules are not distributed with other files for now.

kpp commented 5 years ago

Thanks. I compiled it locally with the help of your azure script. Also I downloaded the artifact from the build. The question is how do I use libsodium-wasm32-wasi/lib/libsodium.a? It is an ar archive not a .wasm file.

jedisct1 commented 5 years ago

.wasm files are applications, where everything has been linked already.

The ar archive contains WebAssembly objects. When using rust with the wasm32-wasi target, you should be able to link it like on other platforms.

kpp commented 5 years ago

Thanks. I'll try.

WASM and WASI are still moving targets, so these pre-built modules are not distributed with other files for now.

Why are they moving? What's left?

jedisct1 commented 5 years ago

Pretty much everything is currently in flux, including the memory model and how functions are represented. WASI in its current form is also just a prototype.

Making an efficient common representation for all languages and all platforms is quite a big task.

jedisct1 commented 5 years ago

I just tried a simple Rust example linking the wasm libsodium.a file and calling sodium_init(). It links without any problem, and the resulting wasm file runs fine.