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

fails to compile to wasm #32

Open
ulrichard opened this issue Dec 6, 2021 · 23 comments
Open

fails to compile to wasm #32

ulrichard opened this issue Dec 6, 2021 · 23 comments

Comments

@ulrichard
Copy link

ulrichard commented Dec 6, 2021

$ 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
Copy link
Member

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

@tcharding
Copy link
Member

Hi @ulrichard, are you still hitting this issue?

@sebastianmontero
Copy link

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
Copy link
Author

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
Copy link
Member

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
Copy link

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
Copy link
Author

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

@tcharding
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

TheBlueMatt commented Dec 5, 2023

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
Copy link
Member

tcharding commented Dec 5, 2023

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
Copy link
Author

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
Copy link
Member

@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
Copy link
Author

@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
Copy link
Member

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

@benthecarman
Copy link

Yes

@ulrichard
Copy link
Author

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
Copy link
Member

Sweet, nice one! This might be in the process of getting solved upstream: bitcoin-core/secp256k1#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
Copy link

jbesraa commented May 30, 2024

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
Copy link
Member

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

@jbesraa
Copy link

jbesraa commented Jun 3, 2024

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

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

No branches or pull requests

9 participants