7

I'm playing around with some front end utilities where Substrate libraries like sp_core would be useful, but I'm getting the following error on compilation.

error: failed to run custom build command for `secp256k1-sys v0.4.2`

Caused by:
  process didn't exit successfully: `/Users/bread/Workspace/playground/wasm-pack-test/target/release/build/secp256k1-sys-64710e640e2fadab/build-script-build` (exit status: 1)
  --- stdout
  TARGET = Some("wasm32-unknown-unknown")
  OPT_LEVEL = Some("s")
  HOST = Some("aarch64-apple-darwin")
  CC_wasm32-unknown-unknown = None
  CC_wasm32_unknown_unknown = None
  TARGET_CC = None
  CC = None
  CFLAGS_wasm32-unknown-unknown = None
  CFLAGS_wasm32_unknown_unknown = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  CC_wasm32-unknown-unknown = None
  CC_wasm32_unknown_unknown = None
  TARGET_CC = None
  CC = None
  CFLAGS_wasm32-unknown-unknown = None
  CFLAGS_wasm32_unknown_unknown = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  running: "clang" "-Os" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-DENABLE_MODULE_RECOVERY=1" "-o" "/Users/bread/Workspace/playground/wasm-pack-test/target/wasm32-unknown-unknown/release/build/secp256k1-sys-923268476155554b/out/depend/secp256k1/contrib/lax_der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c"
  cargo:warning=error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
  cargo:warning=1 error generated.
  exit status: 1

  --- stderr

I figured this should be possible since it can be used in Substrate runtimes that target wasm but perhaps I'm misunderstanding something.

5
  • Just a guess: have you added wasm32-unknown-unknown to your Rust target with rustup target add wasm32-unknown-unknown?
    – kukabi
    Mar 23, 2022 at 21:08
  • @kukabi I have wasm32-unknown-unknown as a target already. I'm using wasm-pack if that's useful. Maybe it's something with how it tries to build?
    – Bread
    Mar 24, 2022 at 0:13
  • I see..no idea then:/ This seems similar: github.com/rustwasm/wasm-pack/issues/747, but no clue there either. I hope someone with more wasm experience steps in, good luck!
    – kukabi
    Mar 24, 2022 at 6:01
  • 1
    I have the same issue with secp256k1-sys and it seems to be related to the Mac M1. I have not had a chance to scratch around yet, however it seems like installing LLVM should help (as least according to this) - github.com/rust-bitcoin/rust-secp256k1/issues/… & github.com/KomodoPlatform/atomicDEX-API/issues/1176
    – Jaco
    Mar 24, 2022 at 8:14
  • I had this same issue and also needed to unset my CPATH
    – liamaharon
    Jun 27, 2023 at 12:16

2 Answers 2

9

This is purely a Mac issue... and had me scratching my head quite a bit when I ran into it...

So delving through the Google archives, it seems that wasm-pack and friends don't quite like the Apple clang. To resolve this issue after reading through https://github.com/rust-bitcoin/rust-secp256k1/issues/283 -

  1. Install llvm via homebrew, brew install llvm
  2. Add the LLVM to your path PATH="/opt/homebrew/opt/llvm/bin:$PATH" (could be in your profile, .zshrc or equivalent)
  3. Ensure you have the AR and CLANG environment vars exported, CC=/opt/homebrew/opt/llvm/bin/clang & AR=/opt/homebrew/opt/llvm/bin/llvm-ar
  4. Run your command and hope for the best...

In my case, I actually added another build target specifically for this that adds all the env variables, i.e. I didn't want to mess around with my global environment, so basically changed my build command to

PATH="/opt/homebrew/opt/llvm/bin:$PATH" CC=/opt/homebrew/opt/llvm/bin/clang AR=/opt/homebrew/opt/llvm/bin/llvm-ar <command goes here>

Slightly messy, but works for me locally and on CI now - https://github.com/polkadot-js/wasm/pull/318/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R20

3
  • 1
    It also happens on my Intel Mac. I don't think it is M1 specific. Mar 24, 2022 at 9:55
  • Interesting to know. From what I read around the Rusts, secp256k1, etc. issues, all seem to point fingers at the Apple clang. So I guess it would make sense if it is not Mac M1-only. Will update my "M1 comment" above.
    – Jaco
    Mar 24, 2022 at 9:59
  • @Jaco that did the trick. Thanks for your help!
    – Bread
    Mar 24, 2022 at 14:17
6

The sp_core crate includes some C dependencies. secp256k1 is one of them. When this crate is compiled in the context of substrate for use inside the runtime those dependencies are not included because they are guarded behind the full-crypto and std feature.

However, when compiling for the web you usually enable std and maybe even full-crypto which then requires your toolchain to compile C code to wasm.

From your pathes I can deduce that you are compiling on a Mac. The LLVM fork of Apple that is used by default does not include a wasm backend. Note that this is not a problem when compiling Rust because it brings its own version of LLVM via Rustup.

What you need to do is install the vanilla version of LLVM (you could do that via brew) and instruct your build to use that CC=/usr/local/opt/llvm/bin/clang.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.