bytecodealliance / cargo-wasi

A lightweight Cargo subcommand to build Rust code for the `wasm32-wasi` target
https://bytecodealliance.github.io/cargo-wasi/
Apache License 2.0
445 stars 29 forks source link

Lack of support for C dependencies (wasi-sdk?) #109

Open kornelski opened 3 years ago

kornelski commented 3 years ago
cargo add zstd-sys # or any other C-dependent crate
cargo wasi build

Results in:

warning: zstd/lib/common/zstd_deps.h:29:10: fatal error: 'string.h' file not found
warning: #include <string.h>
…
error occurred: Command "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-wasi" "-I" "zstd/lib/" "-I" "zstd/lib/common" "-I" "zstd/lib/legacy" "-fvisibility=hidden" "-DZSTD_LIB_DEPRECATED=0" "-DXXH_PRIVATE_API=" "-DZSTDLIB_VISIBILITY=" "-DZDICTLIB_VISIBILITY=" "-DZSTDERRORLIB_VISIBILITY=" "-DZSTD_LEGACY_SUPPORT=1" "-o" "/private/tmp/testing/target/wasm32-wasi/debug/build/zstd-sys-1e822e2310f0cb71/out/zstd/lib/common/entropy_common.o" "-c" "zstd/lib/common/entropy_common.c" with args "clang" did not execute successfully (status code exit status: 1).

(cargo-wasi v0.1.23, Rust 1.55, macOS 15b/x86-64)

From what I could gather, compiling C deps with headers requires wasi-sdk and adding a flag to clang: --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot".

Could cargo wasi support installing and configuring the SDK? Without it it's hard to build any non-trivial Rust project (about 23% of Rust crates depend on C code).

alexcrichton commented 3 years ago

I agree this would be great to support! I don't know the best way to finagle it but having something like a separate command which installs the sdk is probably the best starting point, and this could probably manage the install via the pre-compiled binaries on wasi-sdk's CI.

inklesspen1rus commented 5 months ago

Target wasm32-wasip1 successfully compiles with zstd-sys dependency via cargo build --target wasm32-wasip1. Probably, we have just to wait.... a bit....

jedisct1 commented 5 months ago

Easier to use cargo-zigbuild, so you can use the same command to cross-compile to any target, including wasi.

cargo-zigbuild --target=wasm32-wasi
inklesspen1rus commented 5 months ago

zigbuild doesn't build zstd-sys for me :cry: