+
Skip to content

Conversation

HarryR
Copy link

@HarryR HarryR commented Sep 18, 2025

When building for WASM targets we don't want to generate the bindings As compiling the project e.g. via wasm-pack, complains "stddef.h" isn't found.

We solve this by a feature flag "c_bindings", which will be enabled on non-wasm targets by default, thus when included in a project being built for wasm it will compile and work as expected when we use the library directly.

I also flagged the timer code in graph.rs as that's not compatible with wasm.

Example output without this patch:

$ make pkg/wasm_example.js
wasm-pack build --target web
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...
warning: patch for `circom-witnesscalc` uses the features mechanism. default-features and features will not take effect because the patch dependency does not support this mechanism
   Compiling circom-witnesscalc v0.2.2 (/src/circom-witnesscalc)
error: failed to run custom build command for `circom-witnesscalc v0.2.2 (/src/circom-witnesscalc)`

Caused by:
  process didn't exit successfully: `/src/circuits/target/release/build/circom-witnesscalc-f66e624381c95aa5/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=protos/messages.proto
  cargo:rerun-if-changed=protos/vm.proto
  cargo:rerun-if-env-changed=TARGET
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_wasm32-unknown-unknown
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_wasm32_unknown_unknown
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS
  cargo:rerun-if-changed=include/graph_witness.h

  --- stderr
  include/graph_witness.h:1:10: fatal error: 'stddef.h' file not found

  thread 'main' panicked at /src/circom-witnesscalc/build.rs:36:14:
  Unable to generate bindings: ClangDiagnostic("include/graph_witness.h:1:10: fatal error: 'stddef.h' file not found\n")
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/src/circuits" && "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
make: *** [Makefile:32: pkg/wasm_example.js] Error 1

Example output with this patch:

...
    Finished `release` profile [optimized] target(s) in 4.56s
[INFO]: ⬇️  Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
[INFO]: ✨   Done in 9.62s
[INFO]: 📦   Your wasm pkg is ready to publish at /src/circuits/pkg.

One note, in Cargo.toml, I took this approach:

[features]
default = []
print_opcode = []
c_bindings = []

# Target-specific defaults - only enable c_bindings for non-WASM
[target.'cfg(not(target_arch = "wasm32"))'.features]
default = ["c_bindings"]

Because I'm using [patch.crates-io] in the test project to override the dependency, and I can't set default-features = false this way, so I ... defaulted it in a target-specific way.

When building for WASM targets we don't want to generate the bindings
As compiling the project e.g. via wasm-pack, complains "stddef.h" isn't found.

We solve this by a feature flag "c_bindings", which will be enabled on non-wasm
targets by default, thus when included in a project being built for wasm it
will compile and work as expected when we use the library directly.

I also flagged the timer code in graph.rs as that's not compatible with wasm.
@demonsh demonsh requested a review from olomix September 25, 2025 10:34
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.

1 participant

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载