http-rs / surf

Fast and friendly HTTP client framework for async Rust
https://docs.rs/surf
Apache License 2.0
1.46k stars 120 forks source link

Crate won't compile to wasm #210

Open iulianR opened 4 years ago

iulianR commented 4 years ago

Hello, seeing the wasm-client feature available, I expected surf to compile to wasm32-unknown-unknown as well. But it seems it pulls in openssl which does not compile to wasm. Am I missing something? Thanks

✗ cat Cargo.toml                             
[package]
name = "surf-test"
version = "0.1.0"
authors = ["Iulian Gabriel Radu <iulian.radu67@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
surf = { version = "2.0.0-alpha.4", features = ["wasm-client"], default-features = false }
✗ cargo build --target wasm32-unknown-unknown
   Compiling openssl-sys v0.9.58
   Compiling gloo-timers v0.2.1
error: failed to run custom build command for `openssl-sys v0.9.58`

Caused by:
  process didn't exit successfully: `/home/master/github/surf-test/target/debug/build/openssl-sys-609a3c9f29c5de9c/build-script-main` (exit code: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=WASM32_UNKNOWN_UNKNOWN_OPENSSL_LIB_DIR
  WASM32_UNKNOWN_UNKNOWN_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=WASM32_UNKNOWN_UNKNOWN_OPENSSL_INCLUDE_DIR
  WASM32_UNKNOWN_UNKNOWN_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=WASM32_UNKNOWN_UNKNOWN_OPENSSL_DIR
  WASM32_UNKNOWN_UNKNOWN_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_wasm32-unknown-unknown
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_wasm32_unknown_unknown
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_wasm32-unknown-unknown
  cargo:rerun-if-env-changed=PKG_CONFIG_wasm32_unknown_unknown
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_wasm32-unknown-unknown
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_wasm32_unknown_unknown
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  run pkg_config fail: "Cross compilation detected. Use PKG_CONFIG_ALLOW_CROSS=1 to override"

  --- stderr
  thread 'main' panicked at '

  Could not find directory of OpenSSL installation, and this `-sys` crate cannot
  proceed without this knowledge. If OpenSSL is installed and this crate had
  trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
  compilation process.

  Make sure you also have the development packages of openssl installed.
  For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

  If you're in a situation where you think the directory *should* be found
  automatically, please open a bug at https://github.com/sfackler/rust-openssl
  and include information about your system as well as this message.

  $HOST = x86_64-unknown-linux-gnu
  $TARGET = wasm32-unknown-unknown
  openssl-sys = 0.9.58

  ', /home/master/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.58/build/find_normal.rs:157:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed
olanod commented 3 years ago

I ran into this compilation issue as well trying to have surf as dependency in a crate that can compile to both native and wasm. Perhaps I'm missing something silly?

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
surf = { version = "2.1.0", default-features = false, features = ["h1-client"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
surf = { version = "2.1.0", default-features = false, features = ["wasm-client"] }

if I comment the first part and compile to wasm it works but not when both are there. Similar the other way around compiling native won't work if both sections are there. Is it just me not specifying the dependencies correctly?

CryZe commented 3 years ago

Without cargo's resolver version 2 (which isn't stable yet), you can't properly use this crate like that. You'd need to delegate the features to the user of your crate.