Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 67 additions & 25 deletions eng/common/cross/crosstool-ng-build-rootfs.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash

# Builds a distro-free glibc rootfs (sysroot) for cross-compiling with the host clang.
# Builds a distro-free glibc or musl rootfs (sysroot) for cross-compiling with the host clang.
# Nothing in the rootfs is ever executed: it only provides headers, crt objects and
# link-time libraries. The C/C++ ABI floor (glibc, libstdc++/libgcc_s) is built from
# link-time libraries. The C/C++ ABI floor (C library, libstdc++/libgcc_s) is built from
# source with crosstool-ng; the remaining dependencies are built from pinned upstream
# tarballs.
#
Expand All @@ -18,8 +18,9 @@ set -euo pipefail

usage()
{
echo "Usage: $0 <BuildArch> [--rootfsdir <directory>] [--cachedir <directory>]"
echo "Usage: $0 <BuildArch> [--libc glibc|musl] [--rootfsdir <directory>] [--cachedir <directory>]"
echo "BuildArch can be: arm, arm64, loongarch64, ppc64le, riscv64, s390x, x64"
echo "--libc - C library to build the rootfs with (default: glibc)"
echo "--rootfsdir - output directory (default: \$ROOTFS_DIR or .tools/rootfs/<BuildArch>)"
echo "--cachedir - directory to keep downloaded tarballs across runs"
exit 1
Expand All @@ -37,22 +38,28 @@ __Deps=(
"icu * 79.1 https://github.com/unicode-org/icu/releases/download/release-79.1/icu4c-79.1-sources.tgz 06fa63ecb521048f8082f12ad988f6b678d90cbd88aafce24cbc94902b191d99"
"krb5 * 1.22.3 https://kerberos.org/dist/krb5/1.22/krb5-1.22.3.tar.gz fe90af8441ad9fa9b064b8e53077cefb782a96f8a68276b031dc54af1789ff0b"
"linux * 4.19.325 https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.19.325.tar.xz 607bed7de5cda31a443df4c8a78dbe5e8a9ad31afde2a4d28fe99ab4730e8de1"
"linux *-musl 6.1.175 https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.1.175.tar.xz 37e984a9e449eb15023b3063c3fd6f64fdd1d83af5bb6a944cd7244f40beca85"
"linux loongarch64 6.12.93 https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.12.93.tar.xz 492648a87c0b69c5ac7f43be64792b9000e3439550d4e82e4a14710c49094fa3"
"linux riscv64 6.12.93 https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.12.93.tar.xz 492648a87c0b69c5ac7f43be64792b9000e3439550d4e82e4a14710c49094fa3"
"lldb * 3.9.1 https://releases.llvm.org/3.9.1/lldb-3.9.1.src.tar.xz 7e3311b2a1f80f4d3426e09f9459d079cab4d698258667e50a46dccbaaa460fc"
"lttng-ust * 2.12.10 https://lttng.org/files/lttng-ust/lttng-ust-2.12.10.tar.bz2 aa802851c6d08c653ac1dd7da8f552baaa5d87e9b03c2d6a8b7a04322e53dc47"
"musl * 1.2.3 https://musl.libc.org/releases/musl-1.2.3.tar.gz 7d5b0b6062521e4627e099e4c9dc8248d32a30285e959b7eecaa780cf8cfd4a4"
"musl loongarch64 1.2.6 https://musl.libc.org/releases/musl-1.2.6.tar.gz d585fd3b613c66151fc3249e8ed44f77020cb5e6c1e635a616d3f9f82460512a"
"musl riscv64 1.2.5 https://musl.libc.org/releases/musl-1.2.5.tar.gz a9a118bbe84d8764da0ea0d28b3ab3fae8477fc7e4085d90102b8596fc7c75e4"
"openssl * 4.1.0 https://github.com/openssl/openssl/releases/download/openssl-4.1.0/openssl-4.1.0.tar.gz 50c17baf54cb008f6ce42d50deb4b28050a1aad711636cabd4b241c7e90cc2fd"
"userspace-rcu * 0.15.7 https://lttng.org/files/urcu/userspace-rcu-0.15.7.tar.bz2 2556b83adc0f9b3ac8024e613e17d014d04c4c49110604ce55fcb14eae32edd3"
)

__CrossDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__BuildArch=
__Libc=glibc
__RootfsDir="${ROOTFS_DIR:-}"
__CacheDir=

while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help) usage ;;
--libc|-libc) shift; __Libc="$1" ;;
--rootfsdir|-rootfsdir) shift; __RootfsDir="$1" ;;
--cachedir|-cachedir) shift; __CacheDir="$1" ;;
*) [[ -z "$__BuildArch" ]] && __BuildArch="$1" || usage ;;
Expand Down Expand Up @@ -116,27 +123,40 @@ CT_ARCH_64=y'
;;
esac

case "$__Libc" in
glibc) ;;
musl)
# toolchain.cmake recognizes musl rootfs by the alpine vendor in the GCC triple.
if [[ "$__BuildArch" == arm ]]; then
__Triple=armv7-alpine-linux-musleabihf
__CtngArch+=$'\nCT_ARCH_SUFFIX="v7"'
else
__Triple="${__Triple%-linux-gnu}-alpine-linux-musl"
fi
;;
*)
usage
;;
esac

if [[ -z "$__RootfsDir" ]]; then
__RootfsDir="$__CrossDir/../../../.tools/rootfs/$__BuildArch"
fi

# Prints field <n> (3=version, 4=url, 5=sha256) of the entry for <name>, preferring the
# row for the current architecture over the '*' row.
# Prints field <n> (3=version, 4=url, 5=sha256) of the entry for <name>. The most specific
# row wins: '<arch>-<libc>', then '<arch>', then '*-<libc>', then '*'.
dep()
{
local name="$1" field="$2" row value=
for row in "${__Deps[@]}"; do
read -r -a cols <<< "$row"
if [[ "${cols[0]}" == "$name" ]]; then
if [[ "${cols[1]}" == "$__BuildArch" ]]; then
local name="$1" field="$2" row key
for key in "$__BuildArch-$__Libc" "$__BuildArch" "*-$__Libc" "*"; do
for row in "${__Deps[@]}"; do
read -r -a cols <<< "$row"
if [[ "${cols[0]}" == "$name" && "${cols[1]}" == "$key" ]]; then
echo "${cols[$((field - 1))]}"
return
elif [[ "${cols[1]}" == "*" ]]; then
value="${cols[$((field - 1))]}"
fi
fi
done
done
echo "$value"
}

__WorkDir="$(mktemp -d)"
Expand Down Expand Up @@ -192,6 +212,7 @@ deps()
read -r name _ <<< "$row"
if [[ "$seen" != *" $name "* ]]; then
seen+="$name "
[[ "$name" == glibc || "$name" == musl ]] && [[ "$name" != "$__Libc" ]] && continue
[[ -n "$(dep "$name" 3)" && "$(dep "$name" 3)" != "-" ]] && echo "$name"
fi
done
Expand All @@ -214,14 +235,23 @@ done
__CtngSrc="$(unpack ctng)"
(cd "$__CtngSrc" && ./configure --enable-local && make -j"$JOBS")

# 2. base: kernel UAPI headers, glibc and the GCC runtime (cached per version set)
__Prefix="$__CacheDir/x-tools/$__BuildArch-ctng$(dep ctng 3)-gcc$(dep gcc 3)-glibc$(dep glibc 3)-linux$(dep linux 3)"
# 2. base: kernel UAPI headers, the C library and the GCC runtime (cached per version set)
__Prefix="$__CacheDir/x-tools/$__BuildArch-ctng$(dep ctng 3)-gcc$(dep gcc 3)-$__Libc$(dep "$__Libc" 3)-linux$(dep linux 3)"
if [[ ! -f "$__Prefix/.complete" ]]; then
rm -rf "$__Prefix"
mkdir -p "$__WorkDir/ctng-build"
__CtLinux="CT_LINUX_V_$(dep linux 3 | cut -d. -f1-2 | tr . _)=y"
__CtGlibc="CT_GLIBC_V_$(dep glibc 3 | cut -d. -f1-2 | tr . _)=y"
__CtGcc="CT_GCC_V_$(dep gcc 3 | cut -d. -f1)=y"
if [[ "$__Libc" == glibc ]]; then
__CtLibc="CT_GLIBC_V_$(dep glibc 3 | cut -d. -f1-2 | tr . _)=y"
__CtLibcConfig="CT_OMIT_TARGET_VENDOR=y
# The rootfs only needs headers and libraries; don't build documentation.
CT_GLIBC_EXTRA_CONFIG_ARRAY=\"MAKEINFO=:\""
else
__CtLibc="CT_MUSL_V_$(dep musl 3 | tr . _)=y"
__CtLibcConfig="CT_LIBC_MUSL=y
CT_TARGET_VENDOR=\"alpine\""
fi
cat > "$__WorkDir/ctng-build/defconfig" <<EOF
CT_CONFIG_VERSION="4"
CT_EXPERIMENTAL=y
Expand All @@ -233,22 +263,20 @@ CT_SAVE_TARBALLS=y
CT_VERIFY_DOWNLOAD_DIGEST_SHA256=y
CT_LOG_PROGRESS_BAR=n
# CT_PREFIX_DIR_RO is not set
CT_OMIT_TARGET_VENDOR=y
$__CtngArch
CT_KERNEL_LINUX=y
$__CtLinux
CT_LINUX_VERSION="$(dep linux 3)"
$__CtGlibc
$__CtLibc
$__CtLibcConfig
$__CtGcc
CT_CC_LANG_CXX=y
# The rootfs only needs headers and libraries; don't build documentation.
CT_GLIBC_EXTRA_CONFIG_ARRAY="MAKEINFO=:"
EOF
(
cd "$__WorkDir/ctng-build"
"$__CtngSrc/ct-ng" defconfig DEFCONFIG=defconfig
# crosstool-ng silently falls back to its defaults for versions it can't select.
for __Opt in "$__CtLinux" "$__CtGlibc" "$__CtGcc"; do
for __Opt in "$__CtLinux" "$__CtLibc" "$__CtGcc"; do
if ! grep -qx "$__Opt" .config; then
echo "ERROR: crosstool-ng did not accept $__Opt for $__BuildArch." >&2
exit 1
Expand Down Expand Up @@ -279,7 +307,7 @@ find "$__Prefix/$__Triple" -path "$__Prefix/$__Triple/sysroot" -prune -o \
\( -name 'libstdc++.*' -o -name 'libsupc++.*' -o -name 'libgcc_s.*' -o -name 'libatomic.*' \) -print |
grep -v -E '\.py$|\.la$' | xargs -I{} cp -a --no-preserve=ownership {} "$__GccDir/"
cp -a --no-preserve=ownership "$__Prefix/$__Triple/include/c++/$__GccVer" "$__RootfsDir/usr/include/c++/"
# crosstool-ng leaves glibc in a nested multilib directory when it can't collapse it into lib (riscv64: lib64/lp64d);
# crosstool-ng leaves the C library in a nested multilib directory when it can't collapse it into lib (riscv64: lib64/lp64d);
# expose it at the multiarch paths toolchain.cmake searches.
if [[ ! -e "$__RootfsDir/usr/lib/libc.so" ]]; then
__OsDir="$(cd "$__RootfsDir/usr" && find lib64 -name libc.so -printf '%h\n' | head -n 1)"
Expand Down Expand Up @@ -368,6 +396,20 @@ done
rm -rf "$__RootfsDir"/{bin,sbin,etc,var,usr/bin,usr/sbin,usr/libexec,usr/share}
rm -rf "$__RootfsDir"/usr/lib*/{gconv,audit,getconf}
find "$__RootfsDir" -name '*.la' -delete
find "$__RootfsDir" -type f \( -name '*.so*' -o -name '*.a' -o -name '*.o' \) -exec llvm-strip --strip-debug {} + 2>/dev/null || true
# Linker scripts (libc.so, libm.a, ...) match these names too, and llvm-strip stops at the first file that isn't an object.
find "$__RootfsDir" -type f \( -name '*.so*' -o -name '*.a' -o -name '*.o' \) -print0 | while IFS= read -r -d '' __File; do
case "$(head -c4 "$__File")" in
$'\x7fELF'|'!<ar') llvm-strip --strip-debug "$__File" ;;
esac
done

if [[ "$__Libc" == musl ]]; then
# The runtime recognizes a musl rootfs by ID=alpine in etc/os-release (CMake, init-distro-rid.sh and NativeAOT)
# and by 'musl' in usr/bin/ldd, which on musl is the C library itself.
mkdir -p "$__RootfsDir/etc" "$__RootfsDir/usr/bin"
printf 'NAME="musl"\nID=alpine\nVERSION_ID=%s\n' "$(dep musl 3)" > "$__RootfsDir/etc/os-release"
__LibcSo="$(cd "$__RootfsDir" && find usr/lib* -name libc.so -type f -print -quit)"
ln -s "../${__LibcSo#usr/}" "$__RootfsDir/usr/bin/ldd"
fi

echo "rootfs for $__BuildArch ($__Triple, glibc $(dep glibc 3), gcc $__GccVer, linux $(dep linux 3)): $__RootfsDir ($(du -sh "$__RootfsDir" | cut -f1))"
echo "rootfs for $__BuildArch ($__Triple, $__Libc $(dep "$__Libc" 3), gcc $__GccVer, linux $(dep linux 3)): $__RootfsDir ($(du -sh "$__RootfsDir" | cut -f1))"
2 changes: 1 addition & 1 deletion src/azurelinux/4.0/net12.0/cross/amd64-musl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG ROOTFS_DIR=/crossrootfs/x64
FROM mcr.microsoft.com/dotnet-buildtools/prereqs:azurelinux-4.0-net12.0-crossdeps-builder-amd64 AS builder
ARG ROOTFS_DIR

RUN SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt /scripts/eng/common/cross/build-rootfs.sh x64 alpine3.17 --skipunmount --skipemulation
RUN /scripts/eng/common/cross/crosstool-ng-build-rootfs.sh x64 --libc musl

RUN TARGET_TRIPLE="x86_64-alpine-linux-musl" && \
GCC_VER=$(basename "$(find "$ROOTFS_DIR/usr/include/c++/" -mindepth 1 -maxdepth 1 -type d | sort -V | head -n1)") && \
Expand Down
2 changes: 1 addition & 1 deletion src/azurelinux/4.0/net12.0/cross/arm-musl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG ROOTFS_DIR=/crossrootfs/arm
FROM mcr.microsoft.com/dotnet-buildtools/prereqs:azurelinux-4.0-net12.0-crossdeps-builder-amd64 AS builder
ARG ROOTFS_DIR

RUN SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt /scripts/eng/common/cross/build-rootfs.sh arm alpine3.17 --skipunmount --skipemulation
RUN /scripts/eng/common/cross/crosstool-ng-build-rootfs.sh arm --libc musl

RUN TARGET_TRIPLE="armv7-alpine-linux-musleabihf" && \
GCC_VER=$(basename "$(find "$ROOTFS_DIR/usr/include/c++/" -mindepth 1 -maxdepth 1 -type d | sort -V | head -n1)") && \
Expand Down
2 changes: 1 addition & 1 deletion src/azurelinux/4.0/net12.0/cross/arm64-musl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG ROOTFS_DIR=/crossrootfs/arm64
FROM mcr.microsoft.com/dotnet-buildtools/prereqs:azurelinux-4.0-net12.0-crossdeps-builder-amd64 AS builder
ARG ROOTFS_DIR

RUN SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt /scripts/eng/common/cross/build-rootfs.sh arm64 alpine3.17 --skipunmount --skipemulation
RUN /scripts/eng/common/cross/crosstool-ng-build-rootfs.sh arm64 --libc musl

RUN TARGET_TRIPLE="aarch64-alpine-linux-musl" && \
GCC_VER=$(basename "$(find "$ROOTFS_DIR/usr/include/c++/" -mindepth 1 -maxdepth 1 -type d | sort -V | head -n1)") && \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG ROOTFS_DIR=/crossrootfs/loongarch64
FROM mcr.microsoft.com/dotnet-buildtools/prereqs:azurelinux-4.0-net12.0-crossdeps-builder-amd64 AS builder
ARG ROOTFS_DIR

RUN SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt /scripts/eng/common/cross/build-rootfs.sh loongarch64 alpineedge --skipunmount --skipemulation
RUN /scripts/eng/common/cross/crosstool-ng-build-rootfs.sh loongarch64 --libc musl

RUN TARGET_TRIPLE="loongarch64-alpine-linux-musl" && \
GCC_VER=$(basename "$(find "$ROOTFS_DIR/usr/include/c++/" -mindepth 1 -maxdepth 1 -type d | sort -V | head -n1)") && \
Expand Down
2 changes: 1 addition & 1 deletion src/azurelinux/4.0/net12.0/cross/riscv64-musl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG ROOTFS_DIR=/crossrootfs/riscv64
FROM mcr.microsoft.com/dotnet-buildtools/prereqs:azurelinux-4.0-net12.0-crossdeps-builder-amd64 AS builder
ARG ROOTFS_DIR

RUN SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt /scripts/eng/common/cross/build-rootfs.sh riscv64 alpine3.22 --skipunmount --skipemulation
RUN /scripts/eng/common/cross/crosstool-ng-build-rootfs.sh riscv64 --libc musl

RUN TARGET_TRIPLE="riscv64-alpine-linux-musl" && \
GCC_VER=$(basename "$(find "$ROOTFS_DIR/usr/include/c++/" -mindepth 1 -maxdepth 1 -type d | sort -V | head -n1)") && \
Expand Down
Loading