Skip to content

Commit

Permalink
Merge pull request #43 from tonistiigi/alpine-build
Browse files Browse the repository at this point in the history
alpine based building via xx
  • Loading branch information
tonistiigi authored Aug 17, 2021
2 parents e7205b9 + e38dff0 commit 99c76af
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 209 deletions.
2 changes: 2 additions & 0 deletions .github/buildkit.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[worker.oci]
max-parallelism = 4
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ jobs:
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
config: .github/buildkit.toml
-
name: Test
if: matrix.target == 'buildkit'
Expand Down
87 changes: 27 additions & 60 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,85 +1,52 @@
# syntax=docker/dockerfile:1.2

ARG ALPINE_BASE=alpine:3.14

ARG QEMU_VERSION
ARG QEMU_REPO=https://github.com/qemu/qemu

FROM --platform=$BUILDPLATFORM debian:buster AS src
RUN apt-get update && apt-get install -y git
# xx is a helper for cross-compilation
FROM --platform=$BUILDPLATFORM tonistiigi/xx@sha256:56b19a5fb89b99195ec494d59ad34370d14540858c1f56c560ec1e7f2d1c177f AS xx

FROM --platform=$BUILDPLATFORM ${ALPINE_BASE} AS src
RUN apk add --no-cache git patch
ARG QEMU_VERSION
ARG QEMU_REPO
WORKDIR /src
RUN git clone $QEMU_REPO && \
git clone --depth 1 -b 3.14-stable https://github.com/alpinelinux/aports.git && \
cd qemu && \
git checkout $QEMU_VERSION && \
for f in ../aports/community/qemu/*.patch; do patch -p1 < $f; done && \
scripts/git-submodule.sh update \
ui/keycodemapdb \
tests/fp/berkeley-testfloat-3 \
tests/fp/berkeley-softfloat-3 \
dtc slirp

FROM --platform=$BUILDPLATFORM debian:buster AS qemu

RUN apt-get update && \
apt-get install -y \
dpkg-dev \
git \
ninja-build \
pkg-config \
python3 \
python3-setuptools

WORKDIR /qemu

ARG TARGETPLATFORM

ENV PATH=/qemu/install-scripts:$PATH
RUN --mount=target=./install-scripts,src=scripts \
TARGETPLATFORM=${TARGETPLATFORM} cross.sh install gcc libglib2.0-dev | sh

FROM qemu AS base-amd64
FROM qemu AS base-arm64
FROM qemu AS base-ppc64le
FROM qemu AS base-s390x
FROM qemu AS base-armv7
FROM qemu AS base-armv6
FROM qemu AS base-386
FROM qemu AS base-mips64le

FROM tonistiigi/debian:riscv AS riscv-libglibc
RUN apt-get update && apt-get install -y libglib2.0-dev

RUN for f in $(dpkg-query -L zlib1g-dev libglib2.0-dev libpcre3-dev libglib2.0-0 libpcre3); do [ ! -d $f ] && echo $f; done > /tmp/list
RUN mkdir -p /out && tar cvf /out/libglibc.tar -T /tmp/list

FROM tonistiigi/xx:riscv-toolchain AS base-riscv64
RUN apt-get update && \
apt-get install -y \
dpkg-dev \
git \
ninja-build \
pkg-config \
python3 \
python3-setuptools
FROM --platform=$BUILDPLATFORM ${ALPINE_BASE} AS base
RUN apk add --no-cache git clang lld python3 llvm make ninja pkgconfig glib-dev gcc musl-dev perl bash
COPY --from=xx / /
ENV PATH=/qemu/install-scripts:$PATH
WORKDIR /qemu

RUN --mount=from=riscv-libglibc,target=/riscv-libglibc,src=out \
mkdir -p /tmp/out && tar xvf /riscv-libglibc/libglibc.tar -C /tmp/out && \
cp -a /tmp/out/usr/include/* /usr/riscv64-linux-gnu/include/ && \
cp -a /tmp/out/usr/lib/riscv64-linux-gnu/* /usr/riscv64-linux-gnu/lib/ && \
cp -a /tmp/out/usr/lib/* /usr/riscv64-linux-gnu/lib/ && \
ln -s /usr/riscv64-linux-gnu /usr/riscv64-buildroot-linux-gnu
ENV CROSS_PREFIX=riscv64-buildroot-linux-gnu
ARG TARGETPLATFORM
RUN xx-apk add musl-dev gcc glib-dev glib-static linux-headers zlib-static
RUN set -e; \
[ "$(xx-info arch)" = "ppc64le" ] && XX_CC_PREFER_LINKER=ld xx-clang --setup-target-triple; \
[ "$(xx-info arch)" = "386" ] && XX_CC_PREFER_LINKER=ld xx-clang --setup-target-triple; \
true

FROM base-$TARGETARCH$TARGETVARIANT AS base

FROM base AS build
ARG TARGETPLATFORM
ARG QEMU_VERSION
ARG QEMU_VERSION QEMU_TARGETS
ENV AR=llvm-ar STRIP=llvm-strip
RUN --mount=target=.,from=src,src=/src/qemu,rw --mount=target=./install-scripts,src=scripts \
TARGETPLATFORM=${TARGETPLATFORM} configure_qemu.sh && \
make -j "$(getconf _NPROCESSORS_ONLN)" && \
make install
make install && \
cd /usr/bin && for f in $(ls qemu-*); do xx-verify $f; done

ARG BINARY_PREFIX
RUN cd /usr/bin; [ -z "$BINARY_PREFIX" ] || for f in $(ls qemu-*); do ln -s $f $BINARY_PREFIX$f; done
Expand All @@ -88,18 +55,18 @@ FROM build AS build-archive
RUN cd /usr/bin && mkdir -p /archive && \
tar czvfh "/archive/${BINARY_PREFIX}qemu_${QEMU_VERSION}_$(echo $TARGETPLATFORM | sed 's/\//-/g').tar.gz" ${BINARY_PREFIX}qemu*

FROM --platform=$BUILDPLATFORM tonistiigi/xx:golang@sha256:6f7d999551dd471b58f70716754290495690efa8421e0a1fcf18eb11d0c0a537 AS xgo
FROM --platform=$BUILDPLATFORM golang:1.16-alpine AS binfmt
COPY --from=xgo / /
COPY --from=xx / /
ENV CGO_ENABLED=0
ARG TARGETPLATFORM
ARG QEMU_VERSION
WORKDIR /src
RUN apk add --no-cache git
RUN --mount=target=. \
TARGETPLATFORM=$TARGETPLATFORM go build \
TARGETPLATFORM=$TARGETPLATFORM xx-go build \
-ldflags "-X main.revision=$(git rev-parse --short HEAD) -X main.qemuVersion=${QEMU_VERSION}" \
-o /go/bin/binfmt ./cmd/binfmt
-o /go/bin/binfmt ./cmd/binfmt && \
xx-verify /go/bin/binfmt

FROM scratch AS binaries
ARG BINARY_PREFIX
Expand All @@ -110,7 +77,7 @@ COPY --from=build-archive /archive/* /

FROM --platform=$BUILDPLATFORM tonistiigi/bats-assert AS assert

FROM golang:alpine AS buildkit-test
FROM golang:1.16-alpine AS buildkit-test
RUN apk add --no-cache bash bats
WORKDIR /work
COPY --from=assert . .
Expand Down
1 change: 0 additions & 1 deletion docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ target "all-arch" {
"linux/s390x",
"linux/riscv64",
"linux/386",
"linux/mips64le"
]
}

Expand Down
30 changes: 19 additions & 11 deletions scripts/configure_qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
set -e

: ${QEMU_TARGETS=}
: ${FLAG_CROSS_PREFIX=}

arch="$(cross.sh arch)"

if [ "$arch" != "x86_64" ]; then
arch="$(xx-info arch)"

if [ -z "$QEMU_TARGETS" ]; then
if [ "$arch" != "amd64" ]; then
QEMU_TARGETS="$QEMU_TARGETS x86_64-linux-user"
fi
if [ "$arch" != "aarch64" ]; then
if [ "$arch" != "arm64" ]; then
QEMU_TARGETS="$QEMU_TARGETS aarch64-linux-user"
fi
if [ "$arch" != "armv7l" ] && [ "$arch" != "armv6l" ] ; then
if [ "$arch" != "arm" ]; then
QEMU_TARGETS="$QEMU_TARGETS arm-linux-user"
fi
if [ "$arch" != "riscv64" ]; then
Expand All @@ -22,10 +23,10 @@ fi
if [ "$arch" != "ppc64le" ]; then
QEMU_TARGETS="$QEMU_TARGETS ppc64le-linux-user"
fi
if [ "$arch" != "s390x" ] && [ "$arch" != "riscv64" ] ; then
if [ "$arch" != "s390x" ]; then
QEMU_TARGETS="$QEMU_TARGETS s390x-linux-user"
fi
if [ "$arch" != "i386" ] ; then
if [ "$arch" != "386" ] ; then
QEMU_TARGETS="$QEMU_TARGETS i386-linux-user"
fi
if [ "$arch" != "mips64le" ] ; then
Expand All @@ -34,9 +35,6 @@ fi
if [ "$arch" != "mips64" ] ; then
QEMU_TARGETS="$QEMU_TARGETS mips64-linux-user"
fi

if cross.sh is_cross; then
FLAG_CROSS_PREFIX="--cross-prefix=$(cross.sh cross-prefix)-"
fi

set -x
Expand Down Expand Up @@ -67,4 +65,14 @@ set -x
--disable-sdl \
--disable-spice \
--disable-tools \
--disable-vte $FLAG_CROSS_PREFIX --target-list="$QEMU_TARGETS"
--disable-vte \
--disable-werror \
--disable-debug-info \
--disable-glusterfs \
--cross-prefix=$(xx-info)- \
--host-cc=$(xx-clang --print-target-triple)-clang \
--host=$(xx-clang --print-target-triple) \
--build=$(TARGETPLATFORM= TARGETPAIR= xx-clang --print-target-triple) \
--cc=$(xx-clang --print-target-triple)-clang \
--extra-ldflags=-latomic \
--target-list="$QEMU_TARGETS"
131 changes: 0 additions & 131 deletions scripts/cross.sh

This file was deleted.

6 changes: 0 additions & 6 deletions scripts/riscv64-buildroot-linux-gnu-pkg-config

This file was deleted.

0 comments on commit 99c76af

Please sign in to comment.