|
| 1 | +# NOTE: Most of Dockerfile and related files were borrowed from |
| 2 | +# https://hub.docker.com/r/ekidd/rust-musl-builder |
| 3 | + |
| 4 | +# Use Ubuntu 18.04 LTS as our base image. |
| 5 | +FROM ubuntu:$UBUNTU_VERSION |
| 6 | + |
| 7 | +LABEL maintainer="Jose Quintana <joseluisq.net>" |
| 8 | + |
| 9 | +# The Rust toolchain to use when building our image. Set by `hooks/build`. |
| 10 | +ARG TOOLCHAIN=stable |
| 11 | + |
| 12 | +# The OpenSSL version to use. We parameterize this because many Rust |
| 13 | +# projects will fail to build with 1.1. |
| 14 | +ARG OPENSSL_VERSION=1.0.2r |
| 15 | + |
| 16 | +# Make sure we have basic dev tools for building C libraries. Our goal |
| 17 | +# here is to support the musl-libc builds and Cargo builds needed for a |
| 18 | +# large selection of the most popular crates. |
| 19 | +# |
| 20 | +# We also set up a `rust` user by default, in whose account we'll install |
| 21 | +# the Rust toolchain. This user has sudo privileges if you need to install |
| 22 | +# any more software. |
| 23 | +# |
| 24 | +# `mdbook` is the standard Rust tool for making searchable HTML manuals. |
| 25 | +RUN apt-get update && \ |
| 26 | + apt-get install -y \ |
| 27 | + build-essential \ |
| 28 | + cmake \ |
| 29 | + curl \ |
| 30 | + file \ |
| 31 | + git \ |
| 32 | + musl-dev \ |
| 33 | + musl-tools \ |
| 34 | + libpq-dev \ |
| 35 | + libsqlite-dev \ |
| 36 | + libssl-dev \ |
| 37 | + linux-libc-dev \ |
| 38 | + pkgconf \ |
| 39 | + sudo \ |
| 40 | + xutils-dev \ |
| 41 | + gcc-multilib-arm-linux-gnueabihf \ |
| 42 | + clang \ |
| 43 | + zlib1g-dev \ |
| 44 | + libmpc-dev \ |
| 45 | + libmpfr-dev \ |
| 46 | + libgmp-dev \ |
| 47 | + libxml2-dev \ |
| 48 | + nano \ |
| 49 | + && \ |
| 50 | + apt-get clean && rm -rf /var/lib/apt/lists/* && \ |
| 51 | + useradd rust --user-group --create-home --shell /bin/bash --groups sudo && \ |
| 52 | + MDBOOK_VERSION=0.2.1 && \ |
| 53 | + curl -LO https://github.com/rust-lang-nursery/mdBook/releases/download/v$MDBOOK_VERSION/mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \ |
| 54 | + tar xf mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \ |
| 55 | + mv mdbook /usr/local/bin/ && \ |
| 56 | + rm -f mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz |
| 57 | + |
| 58 | +# Static linking for C++ code |
| 59 | +RUN sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++" |
| 60 | + |
| 61 | +# Allow sudo without a password. |
| 62 | +ADD sudoers /etc/sudoers.d/nopasswd |
| 63 | + |
| 64 | +# Run all further code as user `rust`, and create our working directories |
| 65 | +# as the appropriate user. |
| 66 | +USER rust |
| 67 | +RUN mkdir -p /home/rust/libs /home/rust/src |
| 68 | + |
| 69 | +# Set up our path with all our binary directories, including those for the |
| 70 | +# musl-gcc toolchain and for our Rust toolchain. |
| 71 | +ENV PATH=/home/rust/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
| 72 | + |
| 73 | +# Install our Rust toolchain and the `musl` target. We patch the |
| 74 | +# command-line we pass to the installer so that it won't attempt to |
| 75 | +# interact with the user or fool around with TTYs. We also set the default |
| 76 | +# `--target` to musl so that our users don't need to keep overriding it |
| 77 | +# manually. |
| 78 | +RUN curl https://sh.rustup.rs -sSf | \ |
| 79 | + sh -s -- -y --default-toolchain $TOOLCHAIN && \ |
| 80 | + rustup target add x86_64-unknown-linux-musl && \ |
| 81 | + rustup target add armv7-unknown-linux-musleabihf && \ |
| 82 | + rustup target add x86_64-apple-darwin |
| 83 | +ADD cargo-config.toml /home/rust/.cargo/config |
| 84 | + |
| 85 | +# Set up a `git credentials` helper for using GH_USER and GH_TOKEN to access |
| 86 | +# private repositories if desired. |
| 87 | +ADD git-credential-ghtoken /usr/local/bin |
| 88 | +RUN git config --global credential.https://github.com.helper ghtoken |
| 89 | + |
| 90 | +# Build a static library version of OpenSSL using musl-libc. This is needed by |
| 91 | +# the popular Rust `hyper` crate. |
| 92 | +# |
| 93 | +# We point /usr/local/musl/include/linux at some Linux kernel headers (not |
| 94 | +# necessarily the right ones) in an effort to compile OpenSSL 1.1's "engine" |
| 95 | +# component. It's possible that this will cause bizarre and terrible things to |
| 96 | +# happen. There may be "sanitized" header |
| 97 | +RUN echo "Building OpenSSL..." && \ |
| 98 | + ls /usr/include/linux && \ |
| 99 | + sudo mkdir -p /usr/local/musl/include && \ |
| 100 | + sudo ln -s /usr/include/linux /usr/local/musl/include/linux && \ |
| 101 | + sudo ln -s /usr/include/x86_64-linux-gnu/asm /usr/local/musl/include/asm && \ |
| 102 | + sudo ln -s /usr/include/asm-generic /usr/local/musl/include/asm-generic && \ |
| 103 | + cd /tmp && \ |
| 104 | + curl -LO "https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz" && \ |
| 105 | + tar xvzf "openssl-$OPENSSL_VERSION.tar.gz" && cd "openssl-$OPENSSL_VERSION" && \ |
| 106 | + env CC=musl-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/musl -DOPENSSL_NO_SECURE_MEMORY linux-x86_64 && \ |
| 107 | + env C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \ |
| 108 | + env C_INCLUDE_PATH=/usr/local/musl/include/ make && \ |
| 109 | + sudo make install && \ |
| 110 | + sudo rm /usr/local/musl/include/linux /usr/local/musl/include/asm /usr/local/musl/include/asm-generic && \ |
| 111 | + rm -r /tmp/* |
| 112 | + |
| 113 | +RUN echo "Building zlib..." && \ |
| 114 | + cd /tmp && \ |
| 115 | + ZLIB_VERSION=1.2.11 && \ |
| 116 | + curl -LO "http://zlib.net/zlib-$ZLIB_VERSION.tar.gz" && \ |
| 117 | + tar xzf "zlib-$ZLIB_VERSION.tar.gz" && cd "zlib-$ZLIB_VERSION" && \ |
| 118 | + CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \ |
| 119 | + make && sudo make install && \ |
| 120 | + rm -r /tmp/* |
| 121 | + |
| 122 | +RUN echo "Building libpq..." && \ |
| 123 | + cd /tmp && \ |
| 124 | + POSTGRESQL_VERSION=11.2 && \ |
| 125 | + curl -LO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \ |
| 126 | + tar xzf "postgresql-$POSTGRESQL_VERSION.tar.gz" && cd "postgresql-$POSTGRESQL_VERSION" && \ |
| 127 | + CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \ |
| 128 | + cd src/interfaces/libpq && make all-static-lib && sudo make install-lib-static && \ |
| 129 | + cd ../../bin/pg_config && make && sudo make install && \ |
| 130 | + rm -r /tmp/* |
| 131 | + |
| 132 | +ENV OPENSSL_DIR=/usr/local/musl/ \ |
| 133 | + OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \ |
| 134 | + DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \ |
| 135 | + OPENSSL_LIB_DIR=/usr/local/musl/lib/ \ |
| 136 | + OPENSSL_STATIC=1 \ |
| 137 | + PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=1 \ |
| 138 | + PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU=/usr/bin/pg_config \ |
| 139 | + PKG_CONFIG_ALLOW_CROSS=true \ |
| 140 | + PKG_CONFIG_ALL_STATIC=true \ |
| 141 | + LIBZ_SYS_STATIC=1 \ |
| 142 | + TARGET=musl |
| 143 | + |
| 144 | +# (Please feel free to submit pull requests for musl-libc builds of other C |
| 145 | +# libraries needed by the most popular and common Rust crates, to avoid |
| 146 | +# everybody needing to build them manually.) |
| 147 | + |
| 148 | +ENV OSXCROSS_SDK_VERSION 10.11 |
| 149 | + |
| 150 | +RUN echo "Building osxcross..." && \ |
| 151 | + cd /home/rust && \ |
| 152 | + git clone --depth 1 https://github.com/tpoechtrager/osxcross && \ |
| 153 | + cd osxcross && \ |
| 154 | + curl -L -o ./tarballs/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz \ |
| 155 | + https://s3.amazonaws.com/andrew-osx-sdks/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz && \ |
| 156 | + env UNATTENDED=yes OSX_VERSION_MIN=10.7 ./build.sh && \ |
| 157 | + rm -rf ./tarballs/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz && \ |
| 158 | + rm -rf /tmp/* |
| 159 | + |
| 160 | +ENV PATH $PATH:/home/rust/osxcross/target/bin |
| 161 | + |
| 162 | +# Install some useful Rust tools from source. This will use the static linking |
| 163 | +# toolchain, but that should be OK. |
| 164 | +# |
| 165 | +# We include cargo-audit for compatibility with earlier versions of this image, |
| 166 | +# but cargo-deny provides a super-set of cargo-audit's features. |
| 167 | +RUN cargo install -f cargo-audit && \ |
| 168 | + cargo install -f cargo-deny && \ |
| 169 | + rm -rf /home/rust/.cargo/registry/ |
| 170 | + |
| 171 | +# Expect our source code to live in /home/rust/src. We'll run the build as |
| 172 | +# user `rust`, which will be uid 1000, gid 1000 outside the container. |
| 173 | +WORKDIR /home/rust/src |
| 174 | + |
| 175 | +CMD ["bash"] |
| 176 | + |
| 177 | +# Metadata |
| 178 | +LABEL org.opencontainers.image.vendor="Jose Quintana" \ |
| 179 | + org.opencontainers.image.url="registry.joseluisq.net/rust-oscross-builder" \ |
| 180 | + org.opencontainers.image.title="Rust Linux / Darwin Builder" \ |
| 181 | + org.opencontainers.image.description="Use same Docker image for compiling Rust programs for Linux (musl libc) & macOS (osxcross)." \ |
| 182 | + org.opencontainers.image.version="$VERSION" \ |
| 183 | + org.opencontainers.image.documentation="registry.joseluisq.net/rust-oscross-builder" |
0 commit comments