forked from unixgeek/docker-rust-min-libc-target
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
141 lines (123 loc) · 7.46 KB
/
Dockerfile
File metadata and controls
141 lines (123 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
FROM debian:bookworm-20250428-slim AS builder
LABEL org.opencontainers.image.source=https://github.com/manticoresoftware/rust-min-libc
# Define build arguments for target architecture detection
ARG TARGETARCH
ARG TARGETPLATFORM
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates build-essential bison flex texinfo unzip help2man gawk libtool-bin libncurses-dev \
&& groupadd rust -g 2000 && useradd -m -g rust -u 2000 rust
USER rust
WORKDIR /home/rust
# Build crosstool-ng and toolchain - keep AMD64 exactly as original, add ARM64
RUN curl http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.27.0.tar.xz | tar -xJf - \
&& cd crosstool-ng-1.27.0 \
&& ./configure --prefix=/home/rust/ct-ng \
&& make -j8 \
&& make install \
&& cd .. \
&& case "${TARGETARCH}" in \
"amd64") \
/home/rust/ct-ng/bin/ct-ng x86_64-ubuntu14.04-linux-gnu \
&& sed -i 's/CT_GLIBC_VERSION="[^"]*"/CT_GLIBC_VERSION="2.27"/' .config \
&& /home/rust/ct-ng/bin/ct-ng build -j8 \
&& chmod u+w /home/rust/x-tools/x86_64-ubuntu14.04-linux-gnu \
&& chmod u+w /home/rust/x-tools/x86_64-ubuntu14.04-linux-gnu/*; \
;; \
"arm64") \
/home/rust/ct-ng/bin/ct-ng aarch64-unknown-linux-gnu \
&& sed -i 's/CT_GLIBC_VERSION="[^"]*"/CT_GLIBC_VERSION="2.27"/' .config \
&& sed -i 's/CT_LINUX_VERSION="[^"]*"/CT_LINUX_VERSION="4.20.8"/' .config \
&& sed -i 's/CT_ARCH_64="[^"]*"/CT_ARCH_64="y"/' .config \
&& sed -i 's/CT_ARCH_ARCH="[^"]*"/CT_ARCH_ARCH="armv8-a"/' .config \
&& sed -i 's/CT_ARCH_arm_AVAILABLE="[^"]*"/CT_ARCH_arm_AVAILABLE="y"/' .config \
&& sed -i 's/CT_GDB_CROSS=y/CT_GDB_CROSS=n/' .config \
&& /home/rust/ct-ng/bin/ct-ng build -j8 \
&& chmod u+w /home/rust/x-tools/aarch64-unknown-linux-gnu \
&& chmod u+w /home/rust/x-tools/aarch64-unknown-linux-gnu/*; \
;; \
esac
# Build OpenSSL - keep AMD64 exactly as original, add ARM64 equivalent
RUN case "${TARGETARCH}" in \
"amd64") \
curl --location https://www.openssl.org/source/old/1.0.1/openssl-1.1.1k.tar.gz | tar -xzf - \
&& cd openssl-1.1.1k \
&& export PATH=/home/rust/x-tools/x86_64-ubuntu14.04-linux-gnu/bin:$PATH \
&& ./config -fPIC no-shared --prefix=/home/rust/x-tools/x86_64-ubuntu14.04-linux-gnu \
&& make -j8 CC=x86_64-ubuntu14.04-linux-gnu-cc \
&& make install_sw; \
;; \
"arm64") \
curl --location https://www.openssl.org/source/openssl-1.1.1w.tar.gz | tar -xzf - \
&& cd openssl-1.1.1w \
&& export PATH=/home/rust/x-tools/aarch64-unknown-linux-gnu/bin:$PATH \
&& ./Configure linux-aarch64 -fPIC no-shared --prefix=/home/rust/x-tools/aarch64-unknown-linux-gnu \
--cross-compile-prefix=aarch64-unknown-linux-gnu- \
&& make -j8 CC=aarch64-unknown-linux-gnu-cc \
&& make install_sw; \
;; \
esac
FROM rust:1.94.1-slim-bookworm
# Pass build arguments to final stage
ARG TARGETARCH
ARG TARGETPLATFORM
COPY --from=builder /home/rust/x-tools /usr/local/x-tools
# Setup exactly as original for AMD64, equivalent for ARM64
RUN groupadd rust -g 2000 \
&& useradd -m -g rust -u 2000 rust \
&& apt-get update \
&& apt-get install -y --no-install-recommends make libfindbin-libs-perl gcc g++ protobuf-compiler
# Install oneMKL static libraries for x86_64 builds.
# Provides hgemm_ (half-precision GEMM) which is missing from the older
# MKL 2020.1 bundled by intel-mkl-src v0.8.1.
RUN if [ "${TARGETARCH}" = "amd64" ]; then \
apt-get install -y --no-install-recommends wget gnupg \
&& wget -qO- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \
| gpg --dearmor > /usr/share/keyrings/oneapi-archive-keyring.gpg \
&& echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" \
> /etc/apt/sources.list.d/oneAPI.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends intel-oneapi-mkl-devel \
&& find /opt/intel -name 'libiomp5.so*' -delete \
&& apt-get purge -y wget gnupg \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*; \
fi
ENV MKLROOT=/opt/intel/oneapi/mkl/latest
# Configure Cargo for the target architecture with build-id support.
# Use both config.toml AND env vars — env vars work even when CARGO_HOME is overridden.
RUN case "${TARGETARCH}" in \
"amd64") \
echo "[target.x86_64-unknown-linux-gnu]" > /usr/local/cargo/config.toml \
&& echo "linker = 'x86_64-ubuntu14.04-linux-gnu-cc'" >> /usr/local/cargo/config.toml \
&& echo "rustflags = [\"-C\", \"link-arg=-Wl,--build-id=sha1\", \"-C\", \"link-arg=-L/usr/local/x-tools/x86_64-ubuntu14.04-linux-gnu/x86_64-ubuntu14.04-linux-gnu/sysroot/lib\"]" >> /usr/local/cargo/config.toml \
&& echo "export OPENSSL_DIR=/usr/local/x-tools/x86_64-ubuntu14.04-linux-gnu" >> /etc/environment \
&& echo "export ARCH_TARGET=x86_64-ubuntu14.04-linux-gnu" >> /etc/environment \
&& echo "export RUST_TARGET=x86_64-unknown-linux-gnu" >> /etc/environment; \
;; \
"arm64") \
echo "[target.aarch64-unknown-linux-gnu]" > /usr/local/cargo/config.toml \
&& echo "linker = 'aarch64-unknown-linux-gnu-cc'" >> /usr/local/cargo/config.toml \
&& echo "rustflags = [\"-C\", \"link-arg=-Wl,--build-id=sha1\", \"-C\", \"link-arg=-L/usr/local/x-tools/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot/lib\"]" >> /usr/local/cargo/config.toml \
&& echo "export OPENSSL_DIR=/usr/local/x-tools/aarch64-unknown-linux-gnu" >> /etc/environment \
&& echo "export ARCH_TARGET=aarch64-unknown-linux-gnu" >> /etc/environment \
&& echo "export RUST_TARGET=aarch64-unknown-linux-gnu" >> /etc/environment; \
;; \
esac
# Env vars that override config.toml (works even when CARGO_HOME is changed)
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER="x86_64-ubuntu14.04-linux-gnu-cc"
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=-Wl,--build-id=sha1 -C link-arg=-L/usr/local/x-tools/x86_64-ubuntu14.04-linux-gnu/x86_64-ubuntu14.04-linux-gnu/sysroot/lib"
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER="aarch64-unknown-linux-gnu-cc"
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=-Wl,--build-id=sha1 -C link-arg=-L/usr/local/x-tools/aarch64-unknown-linux-gnu/aarch64-unknown-linux-gnu/sysroot/lib"
# Set PATH to include both toolchains (only the appropriate one will exist)
ENV PATH=/usr/local/x-tools/x86_64-ubuntu14.04-linux-gnu/bin:/usr/local/x-tools/aarch64-unknown-linux-gnu/bin:${PATH}
# Set default RUSTFLAGS with build-id (can be overridden at runtime)
ENV RUSTFLAGS="-C link-arg=-Wl,--build-id=sha1"
# Remove ALL system libstdc++ (both .so and .a) so the linker can only find
# the cross-toolchain's libstdc++.a (built against glibc 2.27).
# The system libstdc++ is built against glibc 2.36 and would pull in GLIBC_2.34 symbols.
# Must be the LAST step — nothing after this can use apt-get or system g++.
RUN find /usr/lib -name 'libstdc++*' -not -name '*.py' -delete 2>/dev/null; \
find /usr/local/x-tools -name 'libstdc++.so*' -not -name '*.py' -delete 2>/dev/null; true
USER rust
WORKDIR /src
ENTRYPOINT ["cargo"]