Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Boomboomdunce committed Dec 1, 2024
1 parent 5a1430a commit 539aed1
Showing 1 changed file with 43 additions and 47 deletions.
90 changes: 43 additions & 47 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Stalwart Dockerfile
# Credits: https://github.com/33KK

FROM --platform=$BUILDPLATFORM docker.io/lukemathwalker/cargo-chef:latest-rust-slim-bookworm AS chef
WORKDIR /build

Expand All @@ -10,67 +7,66 @@ RUN cargo chef prepare --recipe-path /recipe.json

FROM --platform=$BUILDPLATFORM chef AS builder
ARG TARGETPLATFORM
RUN case "${TARGETPLATFORM}" in \
"linux/arm64") echo "aarch64-unknown-linux-gnu" > /target.txt && echo "-C linker=aarch64-linux-gnu-gcc" > /flags.txt ;; \
"linux/amd64") echo "x86_64-unknown-linux-gnu" > /target.txt && echo "-C linker=x86_64-linux-gnu-gcc" > /flags.txt ;; \
*) exit 1 ;; \
esac

# 安装基本依赖
RUN export DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install -yq build-essential libclang-16-dev pkg-config openssl libssl-dev bash

# 根据目标平台安装特定的编译工具
# 设置目标架构和编译器标志
RUN case "${TARGETPLATFORM}" in \
"linux/arm64") \
apt-get install -yq g++-aarch64-linux-gnu binutils-aarch64-linux-gnu && \
mkdir -p /usr/aarch64-linux-gnu && \
ln -s /usr/include/aarch64-linux-gnu/openssl /usr/include/openssl ;; \
echo "aarch64-unknown-linux-gnu" > /target.txt && \
echo "aarch64-linux-gnu" > /arch.txt ;; \
"linux/amd64") \
apt-get install -yq g++-x86-64-linux-gnu binutils-x86-64-linux-gnu ;; \
echo "x86_64-unknown-linux-gnu" > /target.txt && \
echo "x86-64-linux-gnu" > /arch.txt ;; \
*) exit 1 ;; \
esac

RUN rustup target add "$(cat /target.txt)"
COPY --from=planner /recipe.json /recipe.json

# 设置持久的环境变量
ENV PKG_CONFIG_ALLOW_CROSS=1
ENV OPENSSL_NO_VENDOR=1
# 设置环境变量
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc \
PKG_CONFIG_ALLOW_CROSS=1 \
OPENSSL_NO_VENDOR=1

# 创建环境变量文件
# 根据目标平台设置特定的环境变量
RUN case "${TARGETPLATFORM}" in \
"linux/arm64") \
echo "PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig" > /env.sh && \
echo "OPENSSL_DIR=/usr" >> /env.sh && \
echo "OPENSSL_LIB_DIR=/usr/lib/aarch64-linux-gnu" >> /env.sh && \
echo "OPENSSL_INCLUDE_DIR=/usr/include/openssl" >> /env.sh ;; \
echo "export PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig" >> /env && \
echo "export OPENSSL_DIR=/usr" >> /env && \
echo "export OPENSSL_LIB_DIR=/usr/lib/aarch64-linux-gnu" >> /env && \
echo "export OPENSSL_INCLUDE_DIR=/usr/include" >> /env ;; \
"linux/amd64") \
echo "PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig" > /env.sh && \
echo "OPENSSL_DIR=/usr" >> /env.sh && \
echo "OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu" >> /env.sh && \
echo "OPENSSL_INCLUDE_DIR=/usr/include/openssl" >> /env.sh ;; \
echo "export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig" >> /env && \
echo "export OPENSSL_DIR=/usr" >> /env && \
echo "export OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu" >> /env && \
echo "export OPENSSL_INCLUDE_DIR=/usr/include" >> /env ;; \
*) exit 1 ;; \
esac
esac && \
. /env

# 使用 bash 包装命令以确保环境变量生效
RUN echo '#!/bin/bash' > /build.sh && \
echo 'set -e' >> /build.sh && \
echo 'while read line; do export "$line"; done < /env.sh' >> /build.sh && \
echo 'RUSTFLAGS="$(cat /flags.txt)" cargo chef cook --target "$(cat /target.txt)" --release --recipe-path /recipe.json' >> /build.sh && \
chmod +x /build.sh
# 安装基本依赖
RUN export DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install -yq \
build-essential \
pkg-config \
libssl-dev \
libclang-16-dev \
gcc-aarch64-linux-gnu \
g++-aarch64-linux-gnu \
gcc-x86-64-linux-gnu \
g++-x86-64-linux-gnu

# 添加目标架构
RUN rustup target add "$(cat /target.txt)"

RUN /build.sh
# 复制和构建依赖
COPY --from=planner /recipe.json /recipe.json
RUN . /env && RUSTFLAGS="-C linker=$(cat /arch.txt)-gcc" \
cargo chef cook --target "$(cat /target.txt)" --release --recipe-path /recipe.json

# 复制源代码并构建
COPY . .
RUN echo '#!/bin/bash' > /build-final.sh && \
echo 'set -e' >> /build-final.sh && \
echo 'while read line; do export "$line"; done < /env.sh' >> /build-final.sh && \
echo 'RUSTFLAGS="$(cat /flags.txt)" cargo build --target "$(cat /target.txt)" --release -p mail-server -p stalwart-cli' >> /build-final.sh && \
chmod +x /build-final.sh
RUN . /env && RUSTFLAGS="-C linker=$(cat /arch.txt)-gcc" \
cargo build --target "$(cat /target.txt)" --release -p mail-server -p stalwart-cli

RUN /build-final.sh
RUN mv "/build/target/$(cat /target.txt)/release" "/output"

FROM docker.io/debian:bookworm-slim
Expand Down

0 comments on commit 539aed1

Please sign in to comment.