diff --git a/server/Dockerfile b/server/Dockerfile index a2e916ce2..a01e25355 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,75 +1,209 @@ -# Build the main Kanidmd server -ARG BASE_IMAGE=opensuse/tumbleweed:latest -# ARG BASE_IMAGE=opensuse/leap:15.5 +ARG RUST_VERSION=1.84 -FROM ${BASE_IMAGE} AS repos -ADD scripts/zypper_fixing.sh /zypper_fixing.sh -RUN --mount=type=cache,id=zypp,target=/var/cache/zypp /zypper_fixing.sh +FROM --platform=$BUILDPLATFORM docker.io/tonistiigi/xx AS xx +FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-slim-bookworm AS base +FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-slim-bookworm AS toolchain -# ====================== -FROM repos AS builder -ARG KANIDM_FEATURES -ARG KANIDM_BUILD_PROFILE="container_generic" +# Prevent deletion of apt cache +RUN rm -f /etc/apt/apt.conf.d/docker-clean + +# Match Rustc version as close as possible +# rustc -vV +ARG LLVM_VERSION=19 +ENV RUSTUP_TOOLCHAIN=${RUST_VERSION} + +# Install repo tools +# Line one: compiler tools +# Line two: curl, for downloading binaries +# Line three: for xx-verify +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ + --mount=type=cache,target=/var/lib/apt,sharing=locked \ +apt-get update && apt-get install -y \ + clang-${LLVM_VERSION} lld-${LLVM_VERSION} pkg-config make perl jq \ + curl \ + file \ + libssl-dev +# libssl is needed in the host architecture due to an issue with the headers + +# Create symlinks for LLVM tools +RUN <> /etc/environment + +# Configure pkg-config +RUN <> /etc/environment + echo "PKG_CONFIG=/usr/bin/$(xx-info)-pkg-config" >> /etc/environment + echo "PKG_CONFIG_ALLOW_CROSS=true" >> /etc/environment +EOF + +# Configure cc to use clang version +RUN <> /etc/environment + echo "CXX=clang++" >> /etc/environment +EOF + +# Cross-language LTO +RUN <> /etc/environment + echo "CXXFLAGS=-flto" >> /etc/environment + # Linker is set to target-compatible clang by xx + echo "RUSTFLAGS='-Clinker-plugin-lto -Clink-arg=-fuse-ld=lld'" >> /etc/environment +EOF + +# Apply CPU-specific optimizations if TARGET_CPU is provided +ARG TARGET_CPU= +RUN <> /etc/environment + echo "CXXFLAGS='${CXXFLAGS} -march=${TARGET_CPU}'" >> /etc/environment + echo "RUSTFLAGS='${RUSTFLAGS} -C target-cpu=${TARGET_CPU}'" >> /etc/environment + fi +EOF + +# Prepare output directories +RUN mkdir /out + +FROM toolchain AS builder + +ARG TARGETPLATFORM + +ARG KANIDM_FEATURES="" +ARG KANIDM_BUILD_PROFILE="" ARG KANIDM_BUILD_OPTIONS="" # Set the build profile ENV KANIDM_BUILD_PROFILE=${KANIDM_BUILD_PROFILE:-container_generic} -ENV RUSTFLAGS="-Clinker=clang -Clink-arg=-fuse-ld=/usr/bin/ld.mold" -RUN \ - --mount=type=cache,id=zypp,target=/var/cache/zypp \ - zypper install -y --no-recommends \ - sccache \ - cargo \ - clang \ - gawk \ - make \ - automake \ - autoconf \ - libopenssl-3-devel \ - pam-devel \ - sqlite3-devel \ - systemd-devel \ - rsync \ - findutils \ - which \ - mold +# Verify environment configuration +RUN cat /etc/environment +RUN xx-cargo --print-target-triple -COPY . /usr/src/kanidm +# Get source +COPY . . -# ====================== +# Build the binary +RUN --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/usr/local/cargo/git/db \ + --mount=type=cache,target=/app/target \ + bash < /out/sbom/\$PACKAGE.spdx.json + done +EOF -# Exports don't persist through RUN statements. -RUN --mount=type=cache,id=cargo,target=/cargo \ - --mount=type=cache,id=sccache,target=/sccache \ - export CARGO_HOME=/cargo && \ - export SCCACHE_DIR=/sccache && \ - export RUSTC_WRAPPER=/usr/bin/sccache && \ - export CC="/usr/bin/clang" && \ - cargo build -p daemon ${KANIDM_BUILD_OPTIONS} \ - --target-dir="/usr/src/kanidm/target/" \ - --features="${KANIDM_FEATURES}" \ - --release; \ - sccache -s +# Extract dynamically linked dependencies +RUN <> /etc/environment + +# Configure pkg-config +RUN <> /etc/environment + echo "PKG_CONFIG=/usr/bin/$(xx-info)-pkg-config" >> /etc/environment + echo "PKG_CONFIG_ALLOW_CROSS=true" >> /etc/environment +EOF + +# Configure cc to use clang version +RUN <> /etc/environment + echo "CXX=clang++" >> /etc/environment +EOF + +# Cross-language LTO +RUN <> /etc/environment + echo "CXXFLAGS=-flto" >> /etc/environment + # Linker is set to target-compatible clang by xx + echo "RUSTFLAGS='-Clinker-plugin-lto -Clink-arg=-fuse-ld=lld'" >> /etc/environment +EOF + +# Apply CPU-specific optimizations if TARGET_CPU is provided +ARG TARGET_CPU= +RUN <> /etc/environment + echo "CXXFLAGS='${CXXFLAGS} -march=${TARGET_CPU}'" >> /etc/environment + echo "RUSTFLAGS='${RUSTFLAGS} -C target-cpu=${TARGET_CPU}'" >> /etc/environment + fi +EOF + +# Prepare output directories +RUN mkdir /out + +FROM toolchain AS builder + +ARG TARGETPLATFORM + +ARG KANIDM_FEATURES="" +ARG KANIDM_BUILD_PROFILE="" ARG KANIDM_BUILD_OPTIONS="" # Set the build profile ENV KANIDM_BUILD_PROFILE=${KANIDM_BUILD_PROFILE:-container_generic} -ENV RUSTFLAGS="-Clinker=clang -Clink-arg=-fuse-ld=/usr/bin/ld.mold" -RUN \ - --mount=type=cache,id=zypp,target=/var/cache/zypp \ - zypper install -y --no-recommends \ - sccache \ - cargo \ - clang \ - make \ - automake \ - autoconf \ - libopenssl-3-devel \ - pam-devel \ - libudev-devel \ - sqlite3-devel \ - rsync \ - mold +# Verify environment configuration +RUN cat /etc/environment +RUN xx-cargo --print-target-triple -COPY . /usr/src/kanidm +# Get source +COPY . . -WORKDIR /usr/src/kanidm/ +# Build the binary +RUN --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/usr/local/cargo/git/db \ + --mount=type=cache,target=/app/target \ + /bin/bash < /out/sbom/\$PACKAGE.spdx.json + done +EOF -# == Construct the tools container -FROM repos - -ENV RUST_BACKTRACE=1 - -RUN \ - --mount=type=cache,id=zypp,target=/var/cache/zypp \ - zypper install -y \ - timezone \ - openssl-3 - -COPY --from=builder /usr/src/kanidm/target/release/kanidm /sbin/ -COPY --from=builder /usr/src/kanidm/target/release/kanidm-ipa-sync /sbin/ -COPY --from=builder /usr/src/kanidm/target/release/kanidm-ldap-sync /sbin/ -COPY --from=builder /usr/src/kanidm/target/release/fido-mds-tool /sbin/ -RUN chmod +x /sbin/kanidm -RUN chmod +x /sbin/kanidm-ipa-sync -RUN chmod +x /sbin/kanidm-ldap-sync -RUN chmod +x /sbin/fido-mds-tool +# Extract dynamically linked dependencies +RUN < /out/sbom/\$PACKAGE.spdx.json + done +EOF + +# Extract dynamically linked dependencies +RUN <