samael icon indicating copy to clipboard operation
samael copied to clipboard

Building with xmlsec fails on alpine when linking statically

Open FSMaxB opened this issue 10 months ago • 4 comments

When building on alpine and enabling the xmlsec feature, the build fails with Unable to find libclang: "the libclang shared library at /usr/lib/llvm16/lib/libclang.so.16.0.6 could not be opened: Dynamic loading not supported".

I don't think this is bindgen's fault necessarily, because I've been using bindgen successfully on alpine many times and I've never seen this error, but I also don't understand enough to understand what's actually going on there.

How to reproduce

FROM rust:alpine
RUN apk add --no-cache libc-dev xmlsec-dev clang-dev openssl-dev git
RUN git clone https://github.com/njaremko/samael
RUN cd samael && cargo build --features xmlsec
$ docker build -t samael-on-alpine . -f Dockerfile

Output

  --- stderr
  thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.68.1/lib.rs:611:31:
  Unable to find libclang: "the `libclang` shared library at /usr/lib/llvm16/lib/libclang.so.16.0.6 could not be opened: Dynamic loading not supported"
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Full output: https://gist.github.com/FSMaxB/363e540739494c4cde6354370a2be8f3

FSMaxB avatar Oct 17 '23 10:10 FSMaxB

https://github.com/rust-lang/rust-bindgen/issues/2360

njaremko avatar Oct 17 '23 23:10 njaremko

https://github.com/rust-lang/rust-bindgen/issues/2360

I've seen that one, but not sure how it is related because that issue was fixed in bindgen 0.64 and samael uses 0.68. I've tried with clang 14, 15 and 16 and none of them seem to work.

FSMaxB avatar Oct 18 '23 12:10 FSMaxB

You're compiling on alpine, which doesn't support dynamic loading. You have to build with bindgen set to statically link, as described in that ticket. It's not a bug, and it wasn't fixed in 0.64

njaremko avatar Oct 19 '23 14:10 njaremko

Sorry for the misunderstanding and thanks for the explanation! I guess I just didn't fully understand that statically linking the musl libc implies not being able to load other dynamic libraries anymore.

Linking dynamically works

I got things working by linking that dynamically with:

FROM rust:alpine
# Disable static linking to enable loading xmlsec
ENV RUSTFLAGS -Ctarget-feature=-crt-static
RUN apk add --no-cache libc-dev openssl-dev xmlsec-dev clang-dev
COPY . .
RUN cargo install --path .
CMD ["saml-test"]

Linking statically still doesn't

I tried to get static linking working by depending on bindgen = { version = "0.68", default-features = false, features = ["static"] } directly, but that still won't work.

Even after adding clang-static and llvm-dev, I get the following error:

error: failed to run custom build command for `samael v0.0.13`

Caused by:
  process didn't exit successfully: `/target/release/build/samael-b0898edd9e752400/build-script-bindings` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=xmlsec_dynamic
  cargo:rustc-link-lib=xmlsec1
  cargo:rustc-link-lib=xml2
  cargo:rustc-link-lib=ssl
  cargo:rustc-link-lib=crypto
  cargo:rerun-if-env-changed=XMLSEC1_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=XMLSEC1_STATIC
  cargo:rerun-if-env-changed=XMLSEC1_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=SYSROOT
  cargo:rerun-if-env-changed=XMLSEC1_STATIC
  cargo:rerun-if-env-changed=XMLSEC1_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rustc-link-search=native=/usr/lib
  cargo:rustc-link-lib=xmlsec1
  cargo:rustc-link-lib=ltdl
  cargo:rustc-link-lib=xslt
  cargo:rustc-link-lib=xml2
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=XMLSEC1_STATIC
  cargo:rerun-if-env-changed=XMLSEC1_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR

  --- stderr
  thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.68.1/lib.rs:611:31:
  Unable to find libclang: "the `libclang` shared library at /usr/lib/llvm16/lib/libclang.so.16.0.6 could not be opened: Dynamic loading not supported"
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: failed to compile `saml-test v0.1.0 (/)`, intermediate artifacts can be found at `/target`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.
Error: building at STEP "RUN cargo install --path . --features static": while running runtime: exit status 101

I think that is because the runtime feature of bindgen is enabled by default, but not entirely sure. Maybe samael can add a static and a dynamic feature flag as well to make that work and set dynamic as a default?

FSMaxB avatar Oct 19 '23 17:10 FSMaxB