CosmWasm / optimizer

Dockerfile and script to deterministically produce the smallest possible Wasm for your Rust contract
Apache License 2.0
118 stars 57 forks source link

latest optimizer failed to build contract due to clang dependency #145

Closed vbhattaccmu closed 7 months ago

vbhattaccmu commented 7 months ago

When I compile and optimize a contract wasm binary using the latest rust-optimize release, I am running into this error:-

error: failed to run custom build command for `ring v0.17.7`

Caused by:
  process didn't exit successfully: `/target/release/build/ring-0d53c703305694d9/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-env-changed=RING_PREGENERATE_ASM
  cargo:rustc-env=RING_CORE_PREFIX=ring_core_0_17_7_
  OPT_LEVEL = Some("3")
  TARGET = Some("wasm32-unknown-unknown")
  HOST = Some("x86_64-unknown-linux-musl")
  cargo:rerun-if-env-changed=CC_wasm32-unknown-unknown
  CC_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CC_wasm32_unknown_unknown
  CC_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  cargo:rerun-if-env-changed=CFLAGS_wasm32-unknown-unknown
  CFLAGS_wasm32-unknown-unknown = None
  cargo:rerun-if-env-changed=CFLAGS_wasm32_unknown_unknown
  CFLAGS_wasm32_unknown_unknown = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  running: "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "include" "-I" "/target/wasm32-unknown-unknown/release/build/ring-16080fb01804566c/out" "-Wall" "-Wextra" "-fvisibility=hidden" "-std=c1x" "-pedantic" "-Wall" "-Wextra" "-Wbad-function-cast" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wnested-externs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wstrict-prototypes" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-g3" "-nostdlibinc" "-DNDEBUG" "-DRING_CORE_NOSTDLIBINC=1" "-o" "/target/wasm32-unknown-unknown/release/build/ring-16080fb01804566c/out/crypto/curve25519/curve25519.o" "-c" "crypto/curve25519/curve25519.c"

  --- stderr

  error occurred: Failed to find tool. Is `clang` installed?

I am running the optimizer using this command

docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/rust-optimizer:0.15.0

Seems like the image itself needs clang. Surprisingly I can build the binary inside a container built from the image hosted in main. Would appreciate if I could get any hints to resolving this.

maurolacy commented 7 months ago

The way to solve this at the moment is precisely to build your own derived image from rust-optimizer, adding clang support to it.

@webmaster128 I think it could be a good idea to add clang support to the optimiser by default. It's being used by a number of crates that have C / C++ modules or dependencies. When compiling to wasm (and if these foreign modules support the wasm32 target), what seems to be happening is that the ffi links are being resolved to wasm code instead of native code.

Which is pretty cool, makes sense, and aligns pretty well with the WebAssembly paradigm.

You end up with an artefact in which some functionality comes from Rust, and some other from C / C++, all bundled together in a coherent, functional WebAssembly whole.

maurolacy commented 7 months ago

Related work / discussions: https://github.com/CosmWasm/rust-optimizer/pull/118.

vbhattaccmu commented 7 months ago

Related work / discussions: #118.

Thanks @maurolacy , this solution worked for me!