Search code examples
rustlibrdkafka

How to build rdkafka for x86_64-unknown-linux-musl


I have this in a Cargo.toml

rdkafka = { version = "0.29.0", features = [ "ssl", "cmake-build"] }

I tried to compile to x86_64-unknown-linux-musl using 2 options. Both failed.

cargo build --target x86_64-unknown-linux-musl --release

OUTPUT:

linking with cc failed: exit status: 1
rdkafka_ssl.c:(.text.rd_kafka_ssl_ctx_term+0x2b): undefined reference to `ENGINE_free'
collect2: error: ld returned 1 exit status
cross build --target=x86_64-unknown-linux-musl --release

OUTPUT:

rdkafka_ssl.c:(.text.rd_kafka_ssl_ctx_term+0x2b): undefined reference to `ENGINE_free'
collect2: error: ld returned 1 exit status

They both give the same error.

Does anyone know how to fix this? Or can someone try this on their machine.. I'd really appreciate it.

I expected the build to pass.

POST

I have added github repo and recreated the issue. It uses Dockerfile https://github.com/0xDjole/rust-rdkafka-musl To test just docker build -t muslkafka .

Here is the screenshot of the issue

enter image description here


Solution

  • I am able to build a rust crate depending on rdkafka on x86_64-unknown-linux-musl by disabling the cmake-build feature flag and using the following Dockerfile:

    FROM clux/muslrust:1.63.0 AS chef
    RUN cargo install cargo-chef --locked
    RUN ln -s /usr/bin/musl-gcc /usr/bin/musl-g++
    WORKDIR /usr/src/foo
    
    FROM chef AS planner
    COPY . .
    RUN cargo chef prepare
    
    FROM chef AS builder 
    COPY --from=planner /usr/src/foo/recipe.json recipe.json
    RUN cargo chef cook --release
    COPY . .
    RUN cargo build --release \
        && strip --strip-debug target/*/release/foo \
        && cp target/*/release/foo /usr/local/bin/
    
    FROM alpine as final
    COPY --from=builder /usr/local/bin/foo /usr/local/bin
    ENTRYPOINT ["/usr/local/bin/foo"]
    

    (I'm somewhat surprised to see that this does seem to work with the ssl feature and produces a statically linked binary, I hadn't tested this before and openssl is notorious for being "difficult". I can't test right now whether said binary also actually works, so… please test.)