Last active
June 11, 2024 17:46
-
-
Save pulsejet/e6812fe1f7da700fbe5c3524bb21f9aa to your computer and use it in GitHub Desktop.
Script to create Docker image with Alpine / MUSL toolchains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script creates multiple musl toolchains for LLVM. | |
# These can be used directly to produce static Linux binaries. | |
# Pass the alpine version as a command line parameter | |
# (see https://alpinelinux.org/downloads/) | |
# | |
# Usage: bash build-alpine-toolchains.sh v3.20 | |
# | |
# For example, to compile for x86_64 | |
: ' | |
clang --target=x86_64-alpine-linux-musl \ | |
--sysroot=/usr/x86_64-alpine-linux-musl/usr \ | |
--gcc-toolchain=/usr/x86_64-alpine-linux-musl/usr \ | |
-static test.c | |
' | |
set -eu | |
ALPINE_VERSION=$1 | |
# Get apk binary (https://gitlab.alpinelinux.org/alpine/apk-tools) | |
APK=/tmp/apk.static | |
HOST_ARCH="$(uname -m)" | |
rm -f "${APK}" | |
wget -q "https://gitlab.alpinelinux.org/api/v4/projects/5/packages/generic/v2.12.14/${HOST_ARCH}/apk.static" -O "${APK}" | |
chmod +x "${APK}" | |
for ARCH in armhf armv7 aarch64 x86_64 x86; do | |
# Create blank rootfs | |
ROOTFS="/tmp/alpine-${ARCH}" | |
rm -rf "${ROOTFS}" && mkdir "${ROOTFS}" | |
# Add toolchain packages | |
/tmp/apk.static add -q --no-scripts --no-cache --allow-untrusted --initdb \ | |
-X "https://dl-cdn.alpinelinux.org/alpine/${ALPINE_VERSION}/main" \ | |
--arch "${ARCH}" --root "${ROOTFS}" \ | |
linux-headers g++ | |
# Get GNU triple for the toolchain | |
TRIPLE=$(ls "${ROOTFS}/usr/lib/gcc" | head -1) | |
# Move toolchain to target directory | |
rm -rf "/usr/${TRIPLE}" && mkdir -p "/usr/${TRIPLE}" | |
mv "${ROOTFS}/usr" "${ROOTFS}/lib" "/usr/${TRIPLE}/" | |
rm -rf "${ROOTFS}" | |
echo "Created toolchain in /usr/${TRIPLE}" | |
done | |
# Cleanup | |
rm -f "${APK}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment