Skip to content

Commit 49338b1

Browse files
authored
Improve support for linux/arm64/v8 on most Ubuntu-based images (part 2) (#1630)
This is a follow-up to #1597. Docker images for Arm64-based PCs has been added.
2 parents c7dee4d + a581024 commit 49338b1

File tree

9 files changed

+130
-21
lines changed

9 files changed

+130
-21
lines changed

.changes/1630.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "added",
3+
"description": "Add docker platform support for `linux/arm64/v8` target for many Ubuntu-based targets"
4+
}

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker/apt-cross-essential.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 narch
11+
local -a packages
12+
13+
narch="$(dpkg --print-architecture)"
14+
packages+=("libc6-dev-${TARGET_ARCH}-cross:${narch}")
15+
16+
# Install crossbuild-essential if CROSSBUILD_ESSENTIAL is set
17+
if [ -n "${CROSSBUILD_ESSENTIAL:-}" ]; then
18+
packages+=("crossbuild-essential-${TARGET_ARCH}:${narch}")
19+
fi
20+
21+
if ! command -v "${CROSS_TOOLCHAIN_PREFIX}g++" &>/dev/null; then
22+
packages+=("g++-${TARGET_TRIPLE}:${narch}")
23+
fi
24+
25+
if ! command -v "${CROSS_TOOLCHAIN_PREFIX}gfortran" &>/dev/null; then
26+
packages+=("gfortran-${TARGET_TRIPLE}:${narch}")
27+
fi
28+
29+
install_packages "${packages[@]}"
30+
31+
rm "${0}"
32+
}
33+
34+
main "${@}"

docker/cmake.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,33 @@ main() {
1313

1414
local td
1515
td="$(mktemp -d)"
16-
1716
pushd "${td}"
1817

19-
curl --retry 3 -sSfL "https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-linux-x86_64.sh" -o cmake.sh
18+
local cmake_arch
19+
local cmake_sha256
20+
21+
local narch
22+
narch="$(dpkg --print-architecture)"
23+
24+
case "${narch}" in
25+
amd64)
26+
cmake_arch="linux-x86_64"
27+
cmake_sha256="da2a9b18c3bfb136917fa1a579aa5316b01c1d6c111043d03f18877ff05bda30"
28+
;;
29+
arm64)
30+
cmake_arch="linux-aarch64"
31+
cmake_sha256="86122bdfd030208aa36705ef421a218ccec52a14368020b2d67043af5e45490b"
32+
;;
33+
*)
34+
echo "Unsupported architecture: ${narch}" 1>&2
35+
exit 1
36+
;;
37+
esac
38+
39+
curl --retry 3 -sSfL "https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-${cmake_arch}.sh" -o cmake.sh
40+
sha256sum --check <<<"${cmake_sha256} cmake.sh"
2041
sh cmake.sh --skip-license --prefix=/usr/local
42+
cmake --version
2143

2244
popd
2345

docker/common.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ set -euo pipefail
66
# shellcheck disable=SC1091
77
. lib.sh
88

9-
# For architectures except amd64 and i386, look for packages on ports.ubuntu.com instead.
9+
# For non-native architectures, look for packages on ports.ubuntu.com instead.
1010
# This is important if you enable additional architectures so you can install libraries to cross-compile against.
1111
# Look for 'dpkg --add-architecture' in the README for more details.
1212
if grep -i ubuntu /etc/os-release >/dev/null; then
13-
sed 's/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch-=amd64,i386] http:\/\/ports.ubuntu.com\/ubuntu-ports\//g' /etc/apt/sources.list > /etc/apt/sources.list.d/ports.list
14-
sed -i 's/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch=amd64,i386] http:\/\/\1.archive.ubuntu.com\/ubuntu\//g' /etc/apt/sources.list
13+
NATIVE_ARCH=$(dpkg --print-architecture)
14+
15+
if [ "$NATIVE_ARCH" = "amd64" ]; then
16+
sed 's/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch-=amd64,i386] http:\/\/ports.ubuntu.com\/ubuntu-ports\//g' /etc/apt/sources.list > /etc/apt/sources.list.d/ports.list
17+
sed -i 's/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch=amd64,i386] http:\/\/\1.archive.ubuntu.com\/ubuntu\//g' /etc/apt/sources.list
18+
else
19+
sed -i "s/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch-=${NATIVE_ARCH}] http:\/\/ports.ubuntu.com\/ubuntu-ports\//g" /etc/apt/sources.list
20+
sed -i "s/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch=${NATIVE_ARCH}] http:\/\/\1.archive.ubuntu.com\/ubuntu\//g" /etc/apt/sources.list
21+
fi
1522
fi
1623

1724
install_packages \

docker/linux-image.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ main() {
198198
dpkg --add-architecture "${arch}" || echo "foreign-architecture ${arch}" >/etc/dpkg/dpkg.cfg.d/multiarch
199199

200200
# Add Debian keys.
201-
curl --retry 3 -sSfL 'https://ftp-master.debian.org/keys/archive-key-{7.0,8,9,10,11}.asc' -O
202-
curl --retry 3 -sSfL 'https://ftp-master.debian.org/keys/archive-key-{8,9,10,11}-security.asc' -O
203-
curl --retry 3 -sSfL 'https://ftp-master.debian.org/keys/release-{7,8,9,10,11}.asc' -O
204-
curl --retry 3 -sSfL 'https://www.ports.debian.org/archive_{2020,2021,2022,2023,2024}.key' -O
201+
curl --retry 3 -sSfL 'https://ftp-master.debian.org/keys/archive-key-{7.0,8,9,10,11,12}.asc' -O
202+
curl --retry 3 -sSfL 'https://ftp-master.debian.org/keys/archive-key-{8,9,10,11,12}-security.asc' -O
203+
curl --retry 3 -sSfL 'https://ftp-master.debian.org/keys/release-{7,8,9,10,11,12}.asc' -O
204+
curl --retry 3 -sSfL 'https://www.ports.debian.org/archive_{2020,2021,2022,2023,2024,2025}.key' -O
205205

206206
for key in *.asc *.key; do
207207
apt-key add "${key}"

docker/qemu.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ build_static_libffi () {
1919
tar --strip-components=1 -xzf "v${version}.tar.gz"
2020
./configure --prefix="$td"/lib --disable-builddir --disable-shared --enable-static
2121
make "-j$(nproc)"
22-
install -m 644 ./.libs/libffi.a /usr/lib64/
22+
install -m 644 ./.libs/libffi.a /usr/local/lib/
2323

2424
popd
2525

@@ -42,7 +42,7 @@ build_static_libmount () {
4242
tar --strip-components=1 -xJf "util-linux-${version_spec}.tar.xz"
4343
./configure --disable-shared --enable-static --without-ncurses
4444
make "-j$(nproc)" mount blkid
45-
install -m 644 ./.libs/*.a /usr/lib64/
45+
install -m 644 ./.libs/*.a /usr/local/lib/
4646

4747
popd
4848

@@ -67,7 +67,7 @@ build_static_libattr() {
6767

6868
./configure
6969
make "-j$(nproc)"
70-
install -m 644 ./libattr/.libs/libattr.a /usr/lib64/
70+
install -m 644 ./libattr/.libs/libattr.a /usr/local/lib/
7171

7272
yum remove -y gettext
7373

@@ -87,7 +87,7 @@ build_static_libcap() {
8787
curl --retry 3 -sSfL "https://www.kernel.org/pub/linux/libs/security/linux-privs/libcap2/libcap-${version}.tar.xz" -O
8888
tar --strip-components=1 -xJf "libcap-${version}.tar.xz"
8989
make "-j$(nproc)"
90-
install -m 644 libcap/libcap.a /usr/lib64/
90+
install -m 644 libcap/libcap.a /usr/local/lib/
9191

9292
popd
9393

@@ -106,7 +106,7 @@ build_static_pixman() {
106106
tar --strip-components=1 -xzf "pixman-${version}.tar.gz"
107107
./configure
108108
make "-j$(nproc)"
109-
install -m 644 ./pixman/.libs/libpixman-1.a /usr/lib64/
109+
install -m 644 ./pixman/.libs/libpixman-1.a /usr/local/lib/
110110

111111
popd
112112

@@ -125,7 +125,7 @@ build_static_slirp() {
125125
tar -xzf "libslirp-v${version}.tar.gz"
126126
meson setup -Ddefault_library=static libslirp-v${version} build
127127
ninja -C build
128-
install -m 644 ./build/libslirp.a /usr/lib64/
128+
install -m 644 ./build/libslirp.a /usr/local/lib/
129129

130130
popd
131131

src/docker/image.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ impl ImagePlatform {
191191
format!("{}/{}", self.os, self.architecture)
192192
}
193193
}
194+
195+
/// Returns a string that can be used in codegen to represent this platform
196+
pub fn to_codegen_string(&self) -> Option<&'static str> {
197+
match self.target {
198+
TargetTriple::X86_64UnknownLinuxGnu => Some("ImagePlatform::X86_64_UNKNOWN_LINUX_GNU"),
199+
TargetTriple::Aarch64UnknownLinuxGnu => {
200+
Some("ImagePlatform::AARCH64_UNKNOWN_LINUX_GNU")
201+
}
202+
_ => None,
203+
}
204+
}
194205
}
195206

196207
impl Default for ImagePlatform {
@@ -221,6 +232,14 @@ impl std::str::FromStr for ImagePlatform {
221232
value::{Error as SerdeError, StrDeserializer},
222233
IntoDeserializer,
223234
};
235+
236+
// Try to match the docker platform string first
237+
match s {
238+
"linux/amd64" => return Ok(Self::X86_64_UNKNOWN_LINUX_GNU),
239+
"linux/arm64" | "linux/arm64/v8" => return Ok(Self::AARCH64_UNKNOWN_LINUX_GNU),
240+
_ => {}
241+
};
242+
224243
if let Some((platform, toolchain)) = s.split_once('=') {
225244
let image_toolchain = toolchain.into();
226245
let (os, arch, variant) = if let Some((os, rest)) = platform.split_once('/') {
@@ -505,4 +524,24 @@ pub mod tests {
505524
assert_eq!(Os::from_target(&t!("x86_64-pc-windows-msvc"))?, Os::Windows);
506525
Ok(())
507526
}
527+
528+
#[test]
529+
fn image_platform_from_docker_platform_str() -> Result<()> {
530+
assert_eq!(
531+
"linux/amd64".parse::<ImagePlatform>()?,
532+
ImagePlatform::X86_64_UNKNOWN_LINUX_GNU
533+
);
534+
535+
assert_eq!(
536+
"linux/arm64".parse::<ImagePlatform>()?,
537+
ImagePlatform::AARCH64_UNKNOWN_LINUX_GNU
538+
);
539+
540+
assert_eq!(
541+
"linux/arm64/v8".parse::<ImagePlatform>()?,
542+
ImagePlatform::AARCH64_UNKNOWN_LINUX_GNU
543+
);
544+
545+
Ok(())
546+
}
508547
}

xtask/src/codegen.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clap::Args;
2+
use cross::docker::ImagePlatform;
23
use eyre::Context;
34
use std::fmt::Write;
45

@@ -39,10 +40,12 @@ pub static PROVIDED_IMAGES: &[ProvidedImage] = &["#,
3940
.platforms()
4041
.iter()
4142
.map(|p| {
42-
format!(
43-
"ImagePlatform::{}",
44-
p.replace('-', "_").to_ascii_uppercase()
45-
)
43+
let image_platform: ImagePlatform =
44+
p.parse().expect("should be a valid platform");
45+
46+
image_platform
47+
.to_codegen_string()
48+
.expect("should be a valid platform")
4649
})
4750
.collect::<Vec<_>>()
4851
.join(", "),

0 commit comments

Comments
 (0)