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

Add Wasm build for MacOS #254

Closed
wants to merge 2 commits into from
Closed

Conversation

bonomat
Copy link

@bonomat bonomat commented Dec 11, 2020

This PR adds MacOS build to CI to ensure that the library also build on MacOS.

The library does not compile to wasm using the default clang setup on MacOS.
We are able to work around this by setting the environment variables CC and AR to use clang and llvm-ar provided by llvm.

//edit// outdated description below
Hi,

we depend on this library in one of our projects and fail to compile it to wasm on MacOS.
Do you have an idea what's going wrong?

I was able to reproduce it on CI (see below or here for more information here: https://github.com/bonomat/rust-secp256k1/pull/1/checks?check_run_id=1534192675):

    Compiling secp256k1 v0.19.0 (/Users/runner/work/rust-secp256k1/rust-secp256k1)
    Finished test [unoptimized + debuginfo] target(s) in 1.37s
     Running target/wasm32-unknown-unknown/debug/deps/secp256k1-3f31b356cd36a038.wasm
Set timeout to 20 seconds...
Executing bindgen...                              
internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'env'
Require stack:
- /Users/runner/work/rust-secp256k1/rust-secp256k1/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js
- /Users/runner/work/rust-secp256k1/rust-secp256k1/target/wasm32-unknown-unknown/wbg-tmp/run.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/runner/work/rust-secp256k1/rust-secp256k1/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js:3:18)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/runner/work/rust-secp256k1/rust-secp256k1/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js',
    '/Users/runner/work/rust-secp256k1/rust-secp256k1/target/wasm32-unknown-unknown/wbg-tmp/run.js'
  ]
}
error: test failed, to rerun pass '--lib'
Error: Running Wasm tests with wasm-bindgen-test failed
Caused by: failed to execute `cargo test`: exited with exit code: 1
  full command: "cargo" "test" "--target" "wasm32-unknown-unknown"

@apoelstra
Copy link
Member

Can you open up the offending file and see what it's trying to read from env?

My guess is that somehow something from libc has slipped into the library. Or perhaps clang is inserting something from libc.

@bonomat
Copy link
Author

bonomat commented Dec 11, 2020

I used wasm2wat (https://webassembly.github.io/wabt/demo/wasm2wat/) and found these lines:

I'm not sure what to make from this.

 (import "env" "rustsecp256k1_v0_3_1_ecdsa_signature_parse_der" (func $rustsecp256k1_v0_3_1_ecdsa_signature_parse_der (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_ecdsa_signature_parse_compact" (func $rustsecp256k1_v0_3_1_ecdsa_signature_parse_compact (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_ecdsa_signature_parse_der_lax" (func $rustsecp256k1_v0_3_1_ecdsa_signature_parse_der_lax (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_ecdsa_signature_normalize" (func $rustsecp256k1_v0_3_1_ecdsa_signature_normalize (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_ecdsa_signature_serialize_der" (func $rustsecp256k1_v0_3_1_ecdsa_signature_serialize_der (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_ecdsa_signature_serialize_compact" (func $rustsecp256k1_v0_3_1_ecdsa_signature_serialize_compact (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_keypair_create" (func $rustsecp256k1_v0_3_1_keypair_create (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_keypair_xonly_tweak_add" (func $rustsecp256k1_v0_3_1_keypair_xonly_tweak_add (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_keypair_xonly_pub" (func $rustsecp256k1_v0_3_1_keypair_xonly_pub (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_xonly_pubkey_tweak_add" (func $rustsecp256k1_v0_3_1_xonly_pubkey_tweak_add (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_xonly_pubkey_from_pubkey" (func $rustsecp256k1_v0_3_1_xonly_pubkey_from_pubkey (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_xonly_pubkey_parse" (func $rustsecp256k1_v0_3_1_xonly_pubkey_parse (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_xonly_pubkey_serialize" (func $rustsecp256k1_v0_3_1_xonly_pubkey_serialize (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_ecdh" (func $rustsecp256k1_v0_3_1_ecdh (type $t18)))
  (import "env" "rustsecp256k1_v0_3_1_context_preallocated_create" (func $rustsecp256k1_v0_3_1_context_preallocated_create (type $t11)))
  (import "env" "rustsecp256k1_v0_3_1_schnorrsig_sign" (func $rustsecp256k1_v0_3_1_schnorrsig_sign (type $t18)))
  (import "env" "rustsecp256k1_v0_3_1_schnorrsig_verify" (func $rustsecp256k1_v0_3_1_schnorrsig_verify (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_context_preallocated_size" (func $rustsecp256k1_v0_3_1_context_preallocated_size (type $t16)))
  (import "env" "rustsecp256k1_v0_3_1_context_randomize" (func $rustsecp256k1_v0_3_1_context_randomize (type $t11)))
  (import "env" "rustsecp256k1_v0_3_1_ecdsa_sign" (func $rustsecp256k1_v0_3_1_ecdsa_sign (type $t18)))
  (import "env" "rustsecp256k1_v0_3_1_ecdsa_verify" (func $rustsecp256k1_v0_3_1_ecdsa_verify (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_ec_seckey_verify" (func $rustsecp256k1_v0_3_1_ec_seckey_verify (type $t11)))
  (import "env" "rustsecp256k1_v0_3_1_ec_privkey_negate" (func $rustsecp256k1_v0_3_1_ec_privkey_negate (type $t11)))
  (import "env" "rustsecp256k1_v0_3_1_ec_seckey_tweak_add" (func $rustsecp256k1_v0_3_1_ec_seckey_tweak_add (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_ec_seckey_tweak_mul" (func $rustsecp256k1_v0_3_1_ec_seckey_tweak_mul (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_ec_pubkey_parse" (func $rustsecp256k1_v0_3_1_ec_pubkey_parse (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_ec_pubkey_serialize" (func $rustsecp256k1_v0_3_1_ec_pubkey_serialize (type $t15)))
  (import "env" "rustsecp256k1_v0_3_1_ec_pubkey_combine" (func $rustsecp256k1_v0_3_1_ec_pubkey_combine (type $t13)))
  (import "env" "rustsecp256k1_v0_3_1_ec_pubkey_create" (func $rustsecp256k1_v0_3_1_ec_pubkey_create (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_ec_pubkey_negate" (func $rustsecp256k1_v0_3_1_ec_pubkey_negate (type $t11)))
  (import "env" "rustsecp256k1_v0_3_1_ec_pubkey_tweak_add" (func $rustsecp256k1_v0_3_1_ec_pubkey_tweak_add (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_ec_pubkey_tweak_mul" (func $rustsecp256k1_v0_3_1_ec_pubkey_tweak_mul (type $t17)))
  (import "env" "rustsecp256k1_v0_3_1_context_preallocated_destroy" (func $rustsecp256k1_v0_3_1_context_preallocated_destroy (type $t4)))

@TheBlueMatt
Copy link
Member

Error: Cannot find module 'env'

This doesn't sound like an issue in the library, more like a general node issue than a secp-specific one. I do wonder if @arik-so has seen anything similar, I believe he has.

@arik-so
Copy link
Contributor

arik-so commented Dec 12, 2020

Yes, I have. Experimenting with various optimization settings in Cargo.toml might be able to fix it, see rustwasm/wasm-pack#743 (comment)

@bonomat
Copy link
Author

bonomat commented Dec 14, 2020

Yes, I have. Experimenting with various optimization settings in Cargo.toml might be able to fix it, see rustwasm/wasm-pack#743 (comment)

I played around with various combinations but no luck so far. Did you have any success?

@rishflab
Copy link

MacOS build fixed: bonomat@74effb7

The default clang installation provided in MacOS seems to be causing the issue. Fixed by installing clang using brew and using the new installation via env variables.

brew install llvm
CC=/usr/local/opt/llvm/bin/clang
AR=/usr/local/opt/llvm/bin/llvm-ar

before running

wasm-pack build
wasm-pack test --node

@bonomat
Copy link
Author

bonomat commented Jul 15, 2021

Thanks @rishflab 👏👏. I've updated this branch with your proposed changes.

@bonomat bonomat changed the title Wasm build for MacOS fails Add Wasm build for MacOS Jul 15, 2021
.github/workflows/rust.yml Outdated Show resolved Hide resolved
.github/workflows/rust.yml Outdated Show resolved Hide resolved
rishflab added a commit to rishflab/rust-secp256k1-zkp that referenced this pull request Jul 15, 2021
rishflab added a commit to rishflab/rust-secp256k1-zkp that referenced this pull request Jul 20, 2021
The default clang installation provided in MacOS does not work. We fixed
 this locally by installing clang via llvm through brew and setting the
 CC and AR env variables to point at the new clang installation.The CI
 MacOS image seems to already have llvm installed so we did not need to
 install llvm on the CI.

See rust-bitcoin/rust-secp256k1#254 for more
details
rishflab added a commit to rishflab/rust-secp256k1-zkp that referenced this pull request Jul 20, 2021
The default clang installation provided in MacOS does not work. We fixed
 this locally by installing clang via llvm through brew and setting the
 CC and AR env variables to point at the new clang installation.The CI
 MacOS image seems to already have llvm installed so we did not need to
 install llvm on the CI.

See rust-bitcoin/rust-secp256k1#254 for more
details
rishflab added a commit to rishflab/rust-secp256k1-zkp that referenced this pull request Jul 20, 2021
The default clang installation provided in MacOS does not work. We fixed
 this locally by installing clang via llvm through brew and setting the
 CC and AR env variables to point at the new clang installation.The CI
 MacOS image seems to already have llvm installed so we did not need to
 install llvm on the CI.

See rust-bitcoin/rust-secp256k1#254 for more
details
The library does not compile to wasm using the default clang setup on MacOS. We are able to work around this by setting the environment variables CC and AR to use clang and llvm-ar provided by llvm.
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.

6 participants