Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for wasm32-wasi #1568

Closed
wants to merge 12 commits into from
Closed

Support for wasm32-wasi #1568

wants to merge 12 commits into from

Conversation

JanKaul
Copy link
Contributor

@JanKaul JanKaul commented Dec 16, 2022

This PR adds support for the wasm32-wasi target.

To get the CI working, i had to use the dtolnay/rust-toolchain@stable action. The toolchain only seems to work when it is installed with rustup, see a similar issue here: https://bugs.archlinux.org/task/75269.

Currently the clippy and audit CI runs are failing. I didn't include them in this PR because I thought it would make it more difficult to see the actual changes. If I should include the clippy and audit changes, please let me know.

I agree to license my contributions to each file under the terms given at the top of each file I changed.

@xxchan
Copy link

xxchan commented Jul 20, 2023

just kindly fwd: @dynamite-bud said this doesn't work #1043 (comment)

WASI_SDK_PATH=/(...)/wasi/wasi-sdk cargo build --target wasm32-wasi                                                              1.71.0-nightly  2.90  4.88G 14:24:35  100%

   Compiling ring v0.17.0-not-released-yet (/(...)/ring)
error: no rules expected the token `unsafe`
  --> src/arithmetic/bigint/bn_mul_mont_fallback.rs:26:5
   |
26 |     unsafe fn bn_mul_mont(
   |     ^^^^^^ no rules expected this token in macro call
   |
  ::: src/prefixed.rs:1:1
   |
1  | macro_rules! prefixed_extern {
   | ---------------------------- when calling this macro
   |
note: while trying to match `fn`
  --> src/prefixed.rs:6:22
   |
6  |             $vis:vis fn $name:ident ( $( $arg_pat:ident : $arg_ty:ty ),* $(,)? )
   |                      ^^

warning: unused macro definition: `prefixed_export`
  --> src/prefixed.rs:44:14
   |
44 | macro_rules! prefixed_export {
   |              ^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_macros)]` on by default

warning: unused imports: `Limb`, `MODULUS_MAX_LIMBS`, `N0`, `limbs_from_mont_in_place`, `limbs_mul`
  --> src/arithmetic/bigint/bn_mul_mont_fallback.rs:22:13
   |
22 | use super::{limbs_from_mont_in_place, limbs_mul, Limb, MODULUS_MAX_LIMBS, N0};
   |             ^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^  ^^^^  ^^^^^^^^^^^^^^^^^  ^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: unused import: `crate::c`
  --> src/arithmetic/bigint/bn_mul_mont_fallback.rs:23:5
   |
23 | use crate::c;
   |     ^^^^^^^^

warning: `ring` (lib) generated 3 warnings
error: could not compile `ring` (lib) due to previous error; 3 warnings emitted

@JanKaul
Copy link
Contributor Author

JanKaul commented Jul 25, 2023

It works with

export WASI_SDK_PATH=/opt/wasi-sdk CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"

And the wasi tests in the CI also succeed.

@@ -47,7 +47,7 @@ fn digest_misc() {
}

// wasm_bindgen doesn't build this correctly.
#[cfg(not(target_arch = "wsam32"))]
#[cfg(not(target_arch = "wasm32"))]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the typo. I fixed this the right way in PR #1647, so you can remove this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will, Thanks for the review.

&std::env::var("WASI_SDK_PATH").expect("missing environment variable: WASI_SDK_PATH");
c.compiler(format!("{}/bin/clang", wasi_sdk_path).as_str());
c.archiver(format!("{}/bin/llvm-ar", wasi_sdk_path).as_str());
c.flag(format!("--sysroot={}/share/wasi-sysroot", wasi_sdk_path).as_str());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like these changes belong in cc-rs. We traditionally avoid doing this kind of discovery. I suggest we remove this change from this PR.

target: ${{ matrix.target }}
toolchain: ${{ matrix.rust_channel }}

- name: Install wasi-sdk
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic ideally belongs in install-build-tools.sh. The idea is that anybody can run mk/install-build-tools.sh --target=wasm32-wasi and the necessary tools installed. Then CI can use mk/install-build-tools.sh too, and then peoples' local build environment matches CI (more or less).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I install the wasi-sdk with the install-build-tools.sh script, where would you place the install files? On linux you would typically place them in /opt/wasi-sdk but I'm not sure it I can assume that.

@@ -464,6 +464,69 @@ jobs:
# TODO: Do this check on Windows too.
- run: mk/check-symbol-prefixes.sh --target=${{ matrix.target }}

# The wasm32-wasi targets have a different set of feature sets.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand this comment about feature sets, and thus I don't understand why the wasi target needs its own build matrix.

@jedisct1
Copy link

You should consider supporting cargo zigbuild instead. This is less painful than wasi-sdk, and the same command can be used to cross-compile to other targets.

Support for WASI using cargo zigbuild was done in https://github.com/jedisct1/ring-wasi

Without supporting pre-compiled files, it doesn't require any changes to the build scripts, nor messing with environment variables.

run: |
export WASI_URL=$(curl -s https://api.github.com/repos/WebAssembly/wasi-sdk/releases/latest | jq -r '.assets[] | select(.name | contains("linux.tar.gz")) | .browser_download_url')
wget -O ./wasi-sdk-linux.tar.gz $WASI_URL
mkdir wasi-sdk
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, why do we need the WASI SDK? If you look at the existing wasm32 targets, we manage to build them with standard clang, and I don't see anything WASI-specific here besides the rand.rs, which doesn't add any new C stuff. Maybe we can get rid of this SDK dependency by treating the -wasi target just like the -unknown-unknown target?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be even better, as it could run in web browsers without additional modules.

@briansmith
Copy link
Owner

@JanKaul Thanks for your PR. Please see #1745 where I incorporated these changes and added the CI integration. Closing this in favor of that PR.

@briansmith briansmith closed this Oct 14, 2023
@JanKaul
Copy link
Contributor Author

JanKaul commented Oct 16, 2023

Thanks a lot for taking this on. I wasn't entirely sure how to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants