Skip to content

Commit e5c53fd

Browse files
committed
Add concept of a native Dockerfile.
This allows builds for different architectures to work by using the native, base image native for the built platform. This supported both the Ubuntu and CentOS image bases, and adds a `CROSS_TARGET_TRIPLE` to ensure simplify exported the cargo-specific environment variables for the native images.
1 parent 596b223 commit e5c53fd

13 files changed

+304
-20
lines changed

.changes/982.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "internal",
3+
"description": "use generic dockerfiles for when the toolchain and image platfom match."
4+
}

docker/Dockerfile.native

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This dockerfile is used when the target matches the images platform in `build-docker-image`
2+
FROM ubuntu:20.04
3+
ARG DEBIAN_FRONTEND=noninteractive
4+
5+
COPY common.sh lib.sh /
6+
RUN /common.sh
7+
8+
COPY cmake.sh /
9+
RUN /cmake.sh
10+
11+
COPY xargo.sh /
12+
RUN /xargo.sh
13+
14+
ARG TARGETARCH
15+
ARG TARGETVARIANT
16+
ARG CROSS_TARGET_TRIPLE
17+
18+
COPY qemu.sh native-qemu.sh /
19+
RUN /native-qemu.sh
20+
21+
COPY dropbear.sh /
22+
RUN /dropbear.sh
23+
24+
COPY linux-image.sh native-linux-image.sh /
25+
RUN /native-linux-image.sh
26+
27+
COPY linux-runner native-linux-runner base-runner.sh /
28+
29+
ENV CROSS_TARGETARCH=$TARGETARCH
30+
ENV CROSS_TARGETVARIANT=$TARGETVARIANT
31+
ENV CARGO_TARGET_${CROSS_TARGET_TRIPLE}_RUNNER="/native-linux-runner"

docker/Dockerfile.native.centos

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM ubuntu:20.04
2+
ARG DEBIAN_FRONTEND=noninteractive
3+
4+
ARG TARGETARCH
5+
ARG TARGETVARIANT
6+
ARG CROSS_TARGET_TRIPLE
7+
8+
COPY lib.sh /
9+
COPY linux-image.sh native-linux-image.sh /
10+
RUN /native-linux-image.sh
11+
12+
FROM centos:7
13+
14+
COPY common.sh lib.sh /
15+
RUN /common.sh
16+
17+
COPY cmake.sh /
18+
RUN /cmake.sh
19+
20+
COPY xargo.sh /
21+
RUN /xargo.sh
22+
23+
# these need to be present in **both** FROM sections
24+
ARG TARGETARCH
25+
ARG TARGETVARIANT
26+
ARG CROSS_TARGET_TRIPLE
27+
28+
COPY qemu.sh native-qemu.sh /
29+
RUN /native-qemu.sh
30+
31+
COPY dropbear.sh /
32+
RUN /dropbear.sh
33+
34+
COPY --from=0 /qemu /qemu
35+
36+
COPY linux-runner native-linux-runner base-runner.sh /
37+
38+
ENV CROSS_TARGETARCH=$TARGETARCH
39+
ENV CROSS_TARGETVARIANT=$TARGETVARIANT
40+
ENV CARGO_TARGET_${CROSS_TARGET_TRIPLE}_RUNNER="/native-linux-runner"

docker/Dockerfile.x86_64-unknown-linux-gnu

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ RUN /cmake.sh
1010
COPY xargo.sh /
1111
RUN /xargo.sh
1212

13+
RUN apt-get update && apt-get install --assume-yes --no-install-recommends \
14+
g++-x86-64-linux-gnu \
15+
libc6-dev-amd64-cross
16+
17+
COPY deny-debian-packages.sh /
18+
RUN TARGET_ARCH=amd64 /deny-debian-packages.sh \
19+
binutils \
20+
binutils-x86-64-linux-gnu
21+
1322
COPY qemu.sh /
1423
RUN /qemu.sh x86_64 softmmu
1524

@@ -21,4 +30,11 @@ RUN /linux-image.sh x86_64
2130

2231
COPY linux-runner base-runner.sh /
2332

24-
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="/linux-runner x86_64"
33+
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc \
34+
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="/linux-runner x86_64" \
35+
CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc \
36+
CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++ \
37+
BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_linux_gnu="--sysroot=/usr/x86_64-linux-gnu" \
38+
QEMU_LD_PREFIX=/usr/x86_64-linux-gnu \
39+
RUST_TEST_THREADS=1 \
40+
PKG_CONFIG_PATH="/usr/lib/x86_64-linux-gnu/pkgconfig/:${PKG_CONFIG_PATH}"

docker/lib.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,72 @@ download_gcc() {
8686

8787
download_mirrors "gcc/gcc-${version}" "${filename}" "${GNU_MIRRORS[@]}"
8888
}
89+
90+
docker_to_qemu_arch() {
91+
local arch="${1}"
92+
case "${arch}" in
93+
arm64)
94+
echo "aarch64"
95+
;;
96+
386)
97+
echo "i386"
98+
;;
99+
amd64)
100+
echo "x86_64"
101+
;;
102+
arm|ppc64le|riscv64|s390x)
103+
echo "${arch}"
104+
;;
105+
*)
106+
echo "Unknown Docker image architecture, got \"${arch}\"." >&2
107+
exit 1
108+
;;
109+
esac
110+
}
111+
112+
docker_to_linux_arch() {
113+
# variant may not be provided
114+
local oldstate
115+
oldstate="$(set +o)"
116+
set +u
117+
118+
local arch="${1}"
119+
local variant="${2}"
120+
case "${arch}" in
121+
arm64)
122+
echo "aarch64"
123+
;;
124+
386)
125+
echo "i686"
126+
;;
127+
amd64)
128+
echo "x86_64"
129+
;;
130+
ppc64le)
131+
echo "powerpc64le"
132+
;;
133+
arm)
134+
case "${variant}" in
135+
v6)
136+
echo "arm"
137+
;;
138+
""|v7)
139+
echo "armv7"
140+
;;
141+
*)
142+
echo "Unknown Docker image variant, got \"${variant}\"." >&2
143+
exit 1
144+
;;
145+
esac
146+
;;
147+
riscv64|s390x)
148+
echo "${arch}"
149+
;;
150+
*)
151+
echo "Unknown Docker image architecture, got \"${arch}\"." >&2
152+
exit 1
153+
;;
154+
esac
155+
156+
eval "${oldstate}"
157+
}

docker/linux-image.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ main() {
171171
sharutils \
172172
gnupg
173173

174-
# amd64 has conflicting versions of the packages installed, so
174+
# conflicting versions of some packages will be installed already for the host platform,
175175
# we need to remove the system installs later. since apt relies
176176
# on these packages, we need to download them and reinstall
177177
# using dpkg later, since we cannot redownload via apt.
178+
local dpkg_arch
179+
dpkg_arch=$(dpkg --print-architecture)
178180
local libgcc_packages=("${libgcc}:${arch}" "libstdc++6:${arch}")
179-
if [[ "${arch}" == "amd64" ]]; then
181+
if [[ "${arch}" == "${dpkg_arch}" ]]; then
180182
local libgcc_root=/qemu/libgcc
181183
mkdir -p "${libgcc_root}"
182184
pushd "${libgcc_root}"
@@ -186,6 +188,7 @@ main() {
186188

187189
# Download packages
188190
mv /etc/apt/sources.list /etc/apt/sources.list.bak
191+
mv /etc/apt/sources.list.d /etc/apt/sources.list.d.bak
189192
echo -e "${debsource}" > /etc/apt/sources.list
190193

191194
# Old ubuntu does not support --add-architecture, so we directly change multiarch file
@@ -240,10 +243,10 @@ main() {
240243
ncurses-base"${ncurses}" \
241244
"zlib1g:${arch}"
242245

243-
if [[ "${arch}" != "amd64" ]]; then
246+
if [[ "${arch}" != "${dpkg_arch}" ]]; then
244247
apt-get -d --no-install-recommends download "${libgcc_packages[@]}"
245248
else
246-
# amd64 has conflicting versions of the packages installed
249+
# host arch has conflicting versions of the packages installed
247250
# this prevents us from downloading them, so we need to
248251
# simply grab the last version from the debian sources.
249252
# we're search for a paragraph with:
@@ -380,7 +383,7 @@ EOF
380383
find . | cpio --create --format='newc' --quiet | gzip > ../initrd.gz
381384
cd -
382385

383-
if [[ "${arch}" == "amd64" ]]; then
386+
if [[ "${arch}" == "${dpkg_arch}" ]]; then
384387
# need to reinstall these packages, since basic utilities rely on them.
385388
pushd "${libgcc_root}"
386389
dpkg -i --force-depends "${libgcc_root}"/*.deb
@@ -391,15 +394,16 @@ EOF
391394
# Clean up
392395
rm -rf "/qemu/${root}" "/qemu/${arch}"
393396
mv -f /etc/apt/sources.list.bak /etc/apt/sources.list
397+
mv -f /etc/apt/sources.list.d.bak /etc/apt/sources.list.d
394398
if [ -f /etc/dpkg/dpkg.cfg.d/multiarch.bak ]; then
395399
mv /etc/dpkg/dpkg.cfg.d/multiarch.bak /etc/dpkg/dpkg.cfg.d/multiarch
396400
fi
397-
# can fail if arch is used (amd64 and/or i386)
401+
# can fail if arch is used (image arch, such as amd64 and/or i386)
398402
dpkg --remove-architecture "${arch}" || true
399403
apt-get update
400404

401405
# need to reinstall the removed libgcc packages, which are required for apt
402-
if [[ "${arch}" == "amd64" ]]; then
406+
if [[ "${arch}" == "${dpkg_arch}" ]]; then
403407
apt-get install --no-install-recommends --assume-yes "${packages[@]}"
404408
fi
405409

docker/linux-runner

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fi
1616
arch="${1}"
1717
shift
1818

19-
if [ "${CROSS_RUNNER}" = "" ]; then
19+
if [[ -z "${CROSS_RUNNER}" ]]; then
2020
if is_native_binary "${arch}"; then
2121
CROSS_RUNNER=native
2222
else

docker/native-linux-image.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
set -eo pipefail
5+
6+
# shellcheck disable=SC1091
7+
. lib.sh
8+
9+
main() {
10+
local arch
11+
arch=$(docker_to_linux_arch "${TARGETARCH}" "${TARGETVARIANT}")
12+
/linux-image.sh "${arch}"
13+
rm "${0}"
14+
}
15+
16+
main "${@}"

docker/native-linux-runner

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
# shellcheck disable=SC1091
6+
. /lib.sh
7+
8+
main() {
9+
local arch
10+
arch=$(docker_to_linux_arch "${CROSS_TARGETARCH}" "${CROSS_TARGETVARIANT}")
11+
12+
if [[ -z "${CROSS_RUNNER}" ]]; then
13+
export CROSS_RUNNER=native
14+
fi
15+
16+
exec /linux-runner "${arch}" "${@}"
17+
}
18+
19+
main "${@}"

docker/native-qemu.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
set -euo pipefail
5+
6+
# shellcheck disable=SC1091
7+
. lib.sh
8+
9+
main() {
10+
local arch
11+
arch=$(docker_to_qemu_arch "${TARGETARCH}")
12+
/qemu.sh "${arch}" softmmu
13+
rm "${0}"
14+
}
15+
16+
main "${@}"

docker/qemu-runner

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fi
1616
arch="${1}"
1717
shift
1818

19-
if [ "${CROSS_RUNNER}" = "" ]; then
19+
if [[ -z "${CROSS_RUNNER}" ]]; then
2020
if is_native_binary "${arch}"; then
2121
CROSS_RUNNER=native
2222
else

0 commit comments

Comments
 (0)