rust-bitcoin / rust-bitcoinconsensus

Bitcoin's libbitcoinconsenus.a with Rust binding. Built from Bitcoin sources with cargo.
Apache License 2.0
46 stars 33 forks source link

fails to compile to wasm #32

Open ulrichard opened 2 years ago

ulrichard commented 2 years ago
$ wasm-pack build --target web
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
   Compiling bitcoinconsensus v0.19.0-3
The following warnings were emitted during compilation:

warning: error: invalid argument '-std=c++11' not allowed with 'C'

error: failed to run custom build command for `bitcoinconsensus v0.19.0-3`

Caused by:
  process didn't exit successfully: `/home/richi/sourcecode/seba/github/bdk-reserves-web/target/release/build/bitcoinconsensus-438c8c5f1d9ce627/build-script-build` (exit status: 1)
  --- stdout
  OPT_LEVEL = Some("3")
  TARGET = Some("wasm32-unknown-unknown")
  HOST = Some("x86_64-unknown-linux-gnu")
  CXX_wasm32-unknown-unknown = None
  CXX_wasm32_unknown_unknown = None
  TARGET_CXX = None
  CXX = None
  CXXFLAGS_wasm32-unknown-unknown = None
  CXXFLAGS_wasm32_unknown_unknown = None
  TARGET_CXXFLAGS = None
  CXXFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  CXX_wasm32-unknown-unknown = None
  CXX_wasm32_unknown_unknown = None
  TARGET_CXX = None
  CXX = None
  CXXFLAGS_wasm32-unknown-unknown = None
  CXXFLAGS_wasm32_unknown_unknown = None
  TARGET_CXXFLAGS = None
  CXXFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  running: "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "depend/bitcoin/src" "-I" "depend/bitcoin/src/secp256k1/include" "-I" "depend/bitcoin/src/secp256k1" "-Wall" "-Wextra" "-std=c++11" "-Wno-unused-parameter" "-D__STDC_FORMAT_MACROS" "-DSECP256K1_BUILD=1" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DENABLE_MODULE_RECOVERY=1" "-DUSE_FIELD_10X26=1" "-DUSE_SCALAR_8X32=1" "-o" "/home/richi/sourcecode/seba/github/bdk-reserves-web/target/wasm32-unknown-unknown/release/build/bitcoinconsensus-8310f8dedcb0d87a/out/depend/bitcoin/src/secp256k1/src/secp256k1.o" "-c" "depend/bitcoin/src/secp256k1/src/secp256k1.c"
  cargo:warning=error: invalid argument '-std=c++11' not allowed with 'C'
  exit status: 1

  --- stderr

  error occurred: Command "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "depend/bitcoin/src" "-I" "depend/bitcoin/src/secp256k1/include" "-I" "depend/bitcoin/src/secp256k1" "-Wall" "-Wextra" "-std=c++11" "-Wno-unused-parameter" "-D__STDC_FORMAT_MACROS" "-DSECP256K1_BUILD=1" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DENABLE_MODULE_RECOVERY=1" "-DUSE_FIELD_10X26=1" "-DUSE_SCALAR_8X32=1" "-o" "/home/richi/sourcecode/seba/github/bdk-reserves-web/target/wasm32-unknown-unknown/release/build/bitcoinconsensus-8310f8dedcb0d87a/out/depend/bitcoin/src/secp256k1/src/secp256k1.o" "-c" "depend/bitcoin/src/secp256k1/src/secp256k1.c" with args "clang" did not execute successfully (status code exit status: 1).

Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
make: *** [Makefile:3: build] Fehler 1

Shell beendet 2
TheBlueMatt commented 2 years ago

This sounds like a bug in cc - it should be building as C++ not C.

tcharding commented 1 year ago

Hi @ulrichard, are you still hitting this issue?

sebastianmontero commented 1 year ago

Hi I'm getting this issue when compliling to wasm:

error: failed to run custom build command for `bitcoinconsensus v0.20.2-0.5.0`

Caused by:
  process didn't exit successfully: `/home/sebastian/vsc-workspace/coinstr-wasm/target/debug/build/bitcoinconsensus-3fe2c1af365d97c8/build-script-build` (exit status: 1)
  --- stdout
  OPT_LEVEL = Some("0")
  TARGET = Some("wasm32-unknown-unknown")
  HOST = Some("x86_64-unknown-linux-gnu")
  cargo:rerun-if-env-changed=CXX_wasm32-unknown-unknown
  CXX_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CXX_wasm32_unknown_unknown
  CXX_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CXX
  TARGET_CXX = None
  cargo:rerun-if-env-changed=CXX
  CXX = None
  cargo:rerun-if-env-changed=CXXFLAGS_wasm32-unknown-unknown
  CXXFLAGS_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CXXFLAGS_wasm32_unknown_unknown
  CXXFLAGS_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CXXFLAGS
  TARGET_CXXFLAGS = None
  cargo:rerun-if-env-changed=CXXFLAGS
  CXXFLAGS = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  cargo:rerun-if-env-changed=CXX_wasm32-unknown-unknown
  CXX_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CXX_wasm32_unknown_unknown
  CXX_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CXX
  TARGET_CXX = None
  cargo:rerun-if-env-changed=CXX
  CXX = None
  cargo:rerun-if-env-changed=CXXFLAGS_wasm32-unknown-unknown
  CXXFLAGS_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CXXFLAGS_wasm32_unknown_unknown
  CXXFLAGS_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CXXFLAGS
  TARGET_CXXFLAGS = None
  cargo:rerun-if-env-changed=CXXFLAGS
  CXXFLAGS = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/bitcoin/src" "-I" "depend/bitcoin/src/secp256k1/include" "-I" "depend/bitcoin/src/secp256k1" "-Wall" "-Wextra" "-std=c++11" "-Wno-unused-parameter" "-D__STDC_FORMAT_MACROS" "-DSECP256K1_BUILD=1" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DENABLE_MODULE_RECOVERY=1" "-DUSE_FIELD_10X26=1" "-DUSE_SCALAR_8X32=1" "-o" "/home/sebastian/vsc-workspace/coinstr-wasm/target/wasm32-unknown-unknown/debug/build/bitcoinconsensus-e49a0edf6d52a536/out/depend/bitcoin/src/secp256k1/src/secp256k1.o" "-c" "depend/bitcoin/src/secp256k1/src/secp256k1.c"
  cargo:warning=error: invalid argument '-std=c++11' not allowed with 'C'
  exit status: 1

  --- stderr

  error occurred: Command "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/bitcoin/src" "-I" "depend/bitcoin/src/secp256k1/include" "-I" "depend/bitcoin/src/secp256k1" "-Wall" "-Wextra" "-std=c++11" "-Wno-unused-parameter" "-D__STDC_FORMAT_MACROS" "-DSECP256K1_BUILD=1" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DENABLE_MODULE_RECOVERY=1" "-DUSE_FIELD_10X26=1" "-DUSE_SCALAR_8X32=1" "-o" "/home/sebastian/vsc-workspace/coinstr-wasm/target/wasm32-unknown-unknown/debug/build/bitcoinconsensus-e49a0edf6d52a536/out/depend/bitcoin/src/secp256k1/src/secp256k1.o" "-c" "depend/bitcoin/src/secp256k1/src/secp256k1.c" with args "clang" did not execute successfully (status code exit status: 1).

Do you know what might be the cause?

@ulrichard did you manage to solve your issue?

ulrichard commented 1 year ago

I still get the same error as above with the current master. It is not important to me TBH. It would have been cool to integrate it in a website, but not essential for me. I guess that's easier with pure rust stuff.

TheBlueMatt commented 1 year ago

Note that in general you cannot safely link most C code with wasm_bindegn. While we should fix this, you'll need to use a different wasm build target to make it work.

ssantos21 commented 11 months ago

I am getting the same error with wasm-pack build --release --target web.

warning: error: invalid argument '-std=c++11' not allowed with 'C'

error: failed to run custom build command for `bitcoinconsensus v0.20.2-0.5.0`

@TheBlueMatt Could you elaborate? Is there a way to make this library work with wasm-pack build ?

ulrichard commented 9 months ago

I started experimenting: https://github.com/AminaBank/rust-bitcoinconsensus/tree/feature/wasm But so far, it doesn't look too good.

tcharding commented 9 months ago

I had a brief look and I rekon that the problem is the secp build, which requires various things like patching out functions etc. I'd say we have to port all the wasm build stuff from secp256k1 to this repo. At a very minimum.

apoelstra commented 9 months ago

I wonder if we can patch out libsecp entirely and link against the one in rust-secp-sys. We would like to do this anyway to minimize the amount of code we compile.

TheBlueMatt commented 9 months ago

I'm confused why we think this is related to secp - the specific error (passing a C++ argument when building targeting C) sounds like either we're invoking cc wrong (which I doubt, cause it works for non-wasm) or cc is buggy building C++ for wasm, which needs to be fixed in the cc crate.

tcharding commented 9 months ago

I'm confused why we think this is related to secp

By "related to secp" I meant I messed around with the build file and could not get secp to build, and that is the first step before building the C++ code.

I got past the exact error in this issue.

TheBlueMatt commented 9 months ago

Ah, so somehow we shouldn't be passing the std=c++... argument in the secp builds? I assume we'll just hit this error eventually once we get to C++ files though.

tcharding commented 9 months ago

Adding this to C++ builds and not to C builds seems to fix this specific error.

    config.cpp(true)

EDIT: I am not totally confident in this statement.

ulrichard commented 9 months ago

What is the reason that compiling for wasm in rust-secp256k1 is patching the Cargo.toml file just for the test? Would that modification break anything else if applied permanently? I don't think in this state, it could be used as a direct dependency.

apoelstra commented 9 months ago

@ulrichard it wouldn't hurt anything but it would be wasteful. We need to build rust-secp as both a dylib and a cdylib for WASM but we don't need any special handling for non-WASM users. (I'm actually unsure if we need this at all ... the git history shows this line being introduced in April 2020 into travis.yml and we've just preserved it since then.)

I don't think in this state, it could be used as a direct dependency.

Is this a philosophical claim or a technical one? Because many people do use rust-secp as a direct dependency, including WASM users.

ulrichard commented 9 months ago

@apoelstra Can you point me to a project, where rust-secp is used as a dependency for building wasm? I'm fairly new to wasm, and that could help a lot seeing how it can be done.

tcharding commented 9 months ago

I'm guessing mutiny uses wasm, is that right @benthecarman?

benthecarman commented 9 months ago

Yes

ulrichard commented 8 months ago

Ok, I dug a bit deeper. The issue with rust-secp seems solvable. But then the C++ files use the C++ std lib. This seems to be forbidden for wasm32-unknown-unknown and hence wasm-pack.
There might be a way for wasm32-unknown-emscripten or wasm32-wasi but I am not sure about that yet.

tcharding commented 8 months ago

Sweet, nice one! This might be in the process of getting solved upstream: https://github.com/bitcoin-core/secp256k1/pull/1461. For the immediate fix we can do the whole WASM dance that we do in rust-secp256k1. A better solution IMO would be working out how to get rust-bitcoinconsensus and rust-secp256k1 to play together so we don't have to duplicate this work in both places.

jbesraa commented 3 months ago

Jumping to the thread here, I have the same problem when introducing bitcoinconsensus feature here https://github.com/lightningdevkit/ldk-node/pull/301/files#diff-2e9d962a08321605940b5a657135052fbcef87b5e360662bb527c96d9a615542R62 the CI is not happy when building kotlin-android bindings https://github.com/lightningdevkit/ldk-node/actions/runs/9287478277/job/25556603253?pr=301.

tcharding commented 3 months ago

Thanks @jbesraa, FWIW I don't personally have any ideas or spare clock cycles to do anything about this ATM.

jbesraa commented 3 months ago

no worries @tcharding. I am trying to work on this, will report back.