Skip to content

Commit

Permalink
Slim docker musl (#233)
Browse files Browse the repository at this point in the history
* Cleanup and bump rust-embedded cross base image from upstream in cross-rs/cross#452 to fix the debug symbols issue observed in #231

* Add test for non-release builds against all features (previously failing because of old toolchain)

* Clean all the libs that are already handled via -sys crates. zlib cloudflare must be fixed upstream, not here. Only LLVM is needed at this point via docker, the (broken) htslib Makefile to build.rs transition that @pmarks did, simplified all this significantly, kudos
  • Loading branch information
brainstorm authored Aug 12, 2020
1 parent 8d0a761 commit 1cbd8cb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 124 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,10 @@ jobs:
use-cross: true
command: build
args: --release --target x86_64-unknown-linux-musl --all-features --verbose

- name: Test musl build with all features and debug symbols (non --release)
uses: actions-rs/cargo@v1
with:
use-cross: true
command: build
args: --target x86_64-unknown-linux-musl --all-features --verbose
5 changes: 2 additions & 3 deletions Cross.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ passthrough = [
"CFLAGS"
]


[target.x86_64-unknown-linux-musl]
image = "brainstorm/cross-x86_64-unknown-linux-musl:1.0.2"
image = "brainstorm/cross-x86_64-unknown-linux-musl:1.0.4"
[target.x86_64-unknown-linux-gnu]
image = "brainstorm/cross-x86_64-unknown-linux-gnu:1.0.0"
image = "brainstorm/cross-x86_64-unknown-linux-gnu:1.0.4"
4 changes: 0 additions & 4 deletions docker/Dockerfile.gnu
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ ENV LLVM_CONFIG_PATH /usr/bin
RUN apt-get update
RUN apt-get install -y build-essential wget gnupg lsb-release software-properties-common apt-transport-https ca-certificates # Otherwise LLVM bump below fails
RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
RUN apt-get install -y libssl-dev libcurl4-openssl-dev zlib1g-dev libbz2-dev liblzma-dev # htslib deps
RUN git clone --depth 1 https://github.com/ebiggers/libdeflate.git && \
cd libdeflate && make -j40 CFLAGS="-fPIC -O3" install && \
cd .. && rm -rf libdeflate
119 changes: 3 additions & 116 deletions docker/Dockerfile.musl
Original file line number Diff line number Diff line change
@@ -1,129 +1,16 @@
FROM rustembedded/cross:x86_64-unknown-linux-musl
FROM brainstorm/cross-x86_64-unknown-linux-musl:upstream

ENV MUSL_CROSS_MAKE_VERSION 0.9.9
ENV DEBIAN_FRONTEND noninteractive
ENV PKG_CONFIG_ALLOW_CROSS 1
ENV LZMA_VERSION 5.2.5
ENV ZLIB_VERSION 1.2.11
ENV OPENSSL_VERSION 1_1_1g
ENV CURL_VERSION 7.70.0

ENV LIBCLANG_PATH /usr/lib/llvm-10/lib
ENV LLVM_CONFIG_PATH /usr/bin


# The default includes and packages from the Ubuntu distro that cross uses will generate all sorts of linux-headers related include errors, see:
# https://github.com/rust-bio/rust-htslib/pull/184#commitcomment-37496651
# Those are the packages installed, hopefully someone will find a good way to use the distro ones instead of compiling everything under /usr/local :/
# apt-get install -y libssl-dev libcurl4-openssl-dev zlib1g-dev libbz2-dev liblzma-dev musl musl-dev musl-tools linux-libc-dev linux-headers-4.15.0-20-generic

# Install basics to locally compile htslib dependencies
RUN apt-get update && \
apt-get install -y build-essential autoconf automake autotools-dev git wget
WORKDIR /root

# Otherwise LLVM bump below fails
RUN apt-get install -y wget gnupg lsb-release software-properties-common apt-transport-https ca-certificates

# Autodetect and fetch latest LLVM repos for the current distro, avoids LLVM warnings and other issues, might generate slower builds for now though, see:
# https://www.phoronix.com/scan.php?page=news_item&px=Rust-Hurt-On-LLVM-10
RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
#RUN apt-get install -y libssl-dev libcurl4-openssl-dev zlib1g-dev \
# libbz2-dev liblzma-dev musl musl-dev musl-tools # htslib deps

# Remove pre-installed musl, to avoid cross-musl-make interference
RUN apt-get remove -y musl

# For now we'll have to go nuts and not only build musl-1.2.0 from scratch but all the other libs too...
# Updated musl-cross toolchain that does not fail on OpenSSL: https://github.com/openssl/openssl/issues/7207
WORKDIR /root
RUN wget https://github.com/richfelker/musl-cross-make/archive/v$MUSL_CROSS_MAKE_VERSION.tar.gz \
&& tar xvfz v$MUSL_CROSS_MAKE_VERSION.tar.gz
WORKDIR /root/musl-cross-make-$MUSL_CROSS_MAKE_VERSION
COPY config-musl-cross-make.mak config.mak
RUN make -j40 install && cd .. && rm v$MUSL_CROSS_MAKE_VERSION.tar.gz
#&& rm -rf musl-cross-make-*

# Now we assume we have a properly configured musl-cross...
ENV PATH "/usr/local/musl/bin:$PATH"
ENV CFLAGS "-fPIC"
ENV CROSS_COMPILE x86_64-linux-musl-
ENV CC ${CROSS_COMPILE}cc
ENV AR ${CROSS_COMPILE}ar
ENV RANLIB ${CROSS_COMPILE}ranlib
ENV CXX ${CROSS_COMPILE}g++
ENV CPPFLAGS "-I/usr/local/musl/include -I/usr/local/include"
ENV LDFLAGS "-L/usr/local/musl/lib -L/usr/local/lib"

# .. and carry on with the htslib deps
WORKDIR /root
RUN git clone --depth 1 https://github.com/ebiggers/libdeflate.git && \
cd libdeflate && make -j40 CFLAGS="-fPIC -O3" PREFIX="/usr/local/musl" install && \
cd .. && rm -rf libdeflate
RUN git clone https://github.com/cloudflare/zlib cloudflare-zlib && \
cd cloudflare-zlib && ./configure --static --prefix=/usr/local/musl && \
make install && cd .. && rm -rf cloudflare-zlib
#RUN wget https://www.zlib.net/zlib-$ZLIB_VERSION.tar.gz \
# && tar xvfz zlib-$ZLIB_VERSION.tar.gz \
# && cd zlib-$ZLIB_VERSION \
# && ./configure --static --prefix=/usr/local/musl \
# && make -j40 install
RUN git clone git://sourceware.org/git/bzip2 \
&& cd bzip2 \
&& make -j40 CC=$CC AR=$AR RANLIB=$RANLIB CFLAGS=$CFLAGS bzip2 \
&& make PREFIX=/usr/local/musl -j40 bzip2 \
&& make -j40 install && cd .. && rm -rf bzip2
RUN wget https://tukaani.org/xz/xz-$LZMA_VERSION.tar.bz2 \
&& tar xvfj xz-$LZMA_VERSION.tar.bz2 \
&& cd xz-$LZMA_VERSION \
&& ./configure --prefix=/usr/local/musl \
--enable-static --disable-shared \
--host x86_64-unknown-linux-musl \
&& make -j40 install && cd .. && rm -rf xz-$LZMA_VERSION xz-$LZMA_VERSION.tar.bz2

# A few gems from: https://wiki.openssl.org/index.php/Compilation_and_Installation
# "OpenSSL has been around a long time, and it carries around a lot of cruft"
# "SSLv2 is completely broken, and you should disable it during configuration"
# "You should specify both --prefix and --openssldir to ensure make install works as expected."
#
# And also this:
# https://github.com/openssl/openssl/issues/11362
#
# And having to redefine AR,CC & RANLIB because otherwise, this:
#
# make[1]: x86_64-linux-musl-x86_64-linux-musl-ar: Command not found
RUN wget https://github.com/openssl/openssl/archive/OpenSSL_$OPENSSL_VERSION.tar.gz \
&& tar xvfz OpenSSL_$OPENSSL_VERSION.tar.gz && cd openssl-OpenSSL_$OPENSSL_VERSION \
&& ./Configure --prefix=/usr/local/musl --openssldir=/usr/local/musl \
--with-zlib-lib=/usr/local/lib/zlib-$ZLIB_VERSION linux-x86_64 \
no-shared no-dso no-gost no-engine no-ssl2 no-srp no-srtp no-tests zlib \
no-weak-ssl-ciphers \
&& make AR=x86_64-linux-musl-ar \
CC=x86_64-linux-musl-cc \
-j40 \
&& make RANLIB=x86_64-linux-musl-ranlib -j40 install && cd .. \
&& rm -rf OpenSSL_$OPENSSL_VERSION.tar.gz openssl-OpenSSL_$OPENSSL_VERSION

RUN wget https://curl.haxx.se/download/curl-$CURL_VERSION.tar.gz \
&& tar xvfz curl-$CURL_VERSION.tar.gz && cd curl-$CURL_VERSION \
&& ./configure --prefix=/usr/local/musl --host x86_64-linux-musl \
--with-ssl=/usr/local/musl --with-zlib=/usr/local/musl --enable-static \
--disable-dict --disable-ftp --disable-gopher --disable-imap \
--disable-pop3 --disable-rtsp --disable-smb --disable-smtp --disable-telnet \
--disable-tftp --disable-ntlm --disable-ldap \
&& make -j40 install && cd .. \
&& rm -rf curl-$CURL_VERSION.tar.gz curl-$CURL_VERSION

# To cater Rust's openssl-sys needs...
ENV OPENSSL_DIR /usr/local/musl

# Hack to force ld stick to musl on the hts-sys/build.rs side, otherwise:
# = note: /usr/bin/ld: /target/debug/deps/liblibloading-689161fea10b6234.rlib(global_static.o): unable to initialize decompress status for section .debug_info
RUN rm /usr/bin/ld && ln -sf /usr/local/musl/bin/x86_64-linux-musl-ld /usr/bin/ld

# Prepare rustup and toolchain locally for easy manual intervention in this container
#RUN wget https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init \
# && chmod +x rustup-init \
# && ./rustup-init -y \
# && . $HOME/.cargo/env \
# && rustup target add x86_64-unknown-linux-musl

CMD ["bash"]
4 changes: 3 additions & 1 deletion docker/config-musl-cross-make.mak
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
TARGET = x86_64-linux-musl
OUTPUT = /usr/local/musl
GCC_VER = 9.2.0
BINUTILS_VER = 2.33.1
BINUTILS_VER = 2.33.1
MUSL_VER=1.2.0

0 comments on commit 1cbd8cb

Please sign in to comment.