-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_sysroot.sh
More file actions
executable file
·53 lines (43 loc) · 1.64 KB
/
setup_sysroot.sh
File metadata and controls
executable file
·53 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
set -euo pipefail
# Ensure system architecture is x86_64
ARCH="$(uname -m)"
if [[ "${ARCH}" != "x86_64" ]]; then
echo "This script only supports x86_64 architecture"
exit 1
fi
SYSROOT="$HOME/linux_sysroot"
MUSL_VERSION="1.2.5"
MUSL_TAR="musl-${MUSL_VERSION}.tar.gz"
MUSL_URL="https://musl.libc.org/releases/${MUSL_TAR}"
# Create sysroot and build dirs
echo "Creating sysroot directory at ${SYSROOT}..."
mkdir -p "${SYSROOT}"
BUILD_DIR="$(mktemp -d)"
echo "Using build directory: ${BUILD_DIR}"
# Download musl tar
echo "Downloading and extracting musl ${MUSL_VERSION}..."
curl -LO "${MUSL_URL}"
tar -xf "${MUSL_TAR}" -C "${BUILD_DIR}"
pushd "${BUILD_DIR}/musl-${MUSL_VERSION}" >/dev/null
# configure for static linking
echo "Configuring musl..."
./configure --prefix=/usr --sysroot=~/linux_sysroot --disable-shared
make && make install DESTDIR=~/linux_sysroot
popd >/dev/null
# Update musl-gcc wrapper to point to the correct specs file
echo "Updating musl-gcc wrapper..."
MUSL_GCC_PATH="${SYSROOT}/usr/bin/musl-gcc"
if [ -f "${MUSL_GCC_PATH}" ]; then
"${SYSROOT}/usr/bin/musl-gcc" -dumpspecs >"${SYSROOT}/usr/lib/musl-gcc.specs"
sed -i 's|exec "${REALGCC:-gcc}" "$@" -specs "/usr/lib/musl-gcc.specs"|"${REALGCC:-gcc}" "$@" -specs "'${SYSROOT}'/usr/lib/musl-gcc.specs"|g' "${MUSL_GCC_PATH}"
else
echo "musl-gcc wrapper not found at ${MUSL_GCC_PATH}"
fi
# Clean up build directory and tarball
echo "Cleaning up..."
rm -rf "${BUILD_DIR}"
rm -f "${MUSL_TAR}"
echo "Sysroot setup complete at ${SYSROOT}."
echo "You can now statically compile C programs using musl by invoking:"
echo " ${SYSROOT}/usr/bin/musl-gcc -static -o your_program your_program.c"