emk / rust-musl-builder

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.
Apache License 2.0
1.54k stars 193 forks source link

Failing to build rocksdb crate with musl target #88

Closed stephanbuys closed 4 years ago

stephanbuys commented 4 years ago

Hi @emk,

I'm trying to get a project using the rocksdb crate to compile using your image, but I'm pretty stuck and thus reaching out for help.

A minimal project would look something like this (lifter out of the rocksdb docs):

Cargo.toml should contain:

[dependencies]
rocksdb = "0.13.0"

main.rs:

fn main() {
    use rocksdb::{DB, Options};

    let path = "_path_for_rocksdb_storage";
    {
        let db = DB::open_default(path).unwrap();
        db.put(b"my key", b"my value").unwrap();
        match db.get(b"my key") {
            Ok(Some(value)) => println!("retrieved value {}", String::from_utf8(value).unwrap()),
            Ok(None) => println!("value not found"),
            Err(e) => println!("operational problem encountered: {}", e),
        }
        db.delete(b"my key").unwrap();
    }
    let _ = DB::destroy(&Options::default(), path);
}

Tried to compile this using:

docker ...see docker image below...  cargo build --target x86_64-unknown-linux-musl

What happened?

  = note: /usr/bin/ld: /home/rust/src/target/x86_64-unknown-linux-musl/debug/deps/liblibrocksdb_sys-25337d346d6f138b.rlib(env_posix.o): undefined reference to symbol 'gnu_dev_major@@GLIBC_2.3.3'
          //lib/x86_64-linux-gnu/libc.so.6: error adding symbols: DSO missing from command line
          collect2: error: ld returned 1 exit status

What did you hope to happen?

Compile binary using the x86_64-unknown-linux-musl target.

Additional information

I added dependencies to the container as such but am still stuck:

Dockerfile:

FROM ekidd/rust-musl-builder:1.41.0

USER root

ENV DEBIAN_FRONTEND=noninteractive

RUN sed -i 's/http:\/\/archive./http:\/\/za.archive./g' /etc/apt/sources.list && \
apt-get update && \
apt-get --assume-yes install wget llvm clang software-properties-common build-essential apt-utils curl iputils-ping tzdata unzip sudo && \
apt-get clean

RUN apt-get install -y clang
RUN apt-get update
RUN apt-get install -y build-essential curl llvm-dev libclang-dev linux-headers-generic libsnappy-dev liblz4-dev libzstd-dev libgflags-dev zlib1g-dev libbz2-dev
# RUN apt-get install -y librocksdb-dev

RUN rustup default 1.41.1
RUN rustup target add x86_64-unknown-linux-musl

ENV ROCKSDB_STATIC 1
ENV CFLAGS "-lm -ldl"

Any help/pointers would be greatly appreciated.

emk commented 4 years ago

I suspect that the problem is that you installed RocksDB in the Debian build environment, which uses glibc, and not in the musl-libc cross-compilation environment. See here:

RUN apt-get install -y build-essential curl llvm-dev libclang-dev linux-headers-generic libsnappy-dev liblz4-dev libzstd-dev libgflags-dev zlib1g-dev libbz2-dev
# RUN apt-get install -y librocksdb-dev

You need to manually download all the different C/C++ libraries, and build them using the cross-compilation toolchain. Here's an example to get you started. However, this process is subject to much trial and error, and I generally don't have enough free time these days to figure out all the tricks for a library I don't use. RocksDB seems to have a pretty long list of native dependencies, and you may need to submit upstream patches for some of them to support cross-compilation.

Good luck, and I'm sorry I can't help you more with this.