Skip to content

Commit aa35d0d

Browse files
[PATCH] Improve musl build logic
Changes in file dockerfile: * tweak LDFLAGS * tweak to use libc++ instead of libstdc++ * other related tweaks
1 parent e97fdda commit aa35d0d

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

dockerfile

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ FROM --platform="linux/${TARGETARCH}" alpine:latest AS musl-builder
99
# shellcheck disable=SC2154
1010
ARG MUSL_VER=${MUSL_VER:-"1.2.5"}
1111
ENV MUSL_VER=${MUSL_VER}
12-
ENV MUSL_PREFIX="/usr/local/musl-llvm-staging"
12+
ENV MUSL_PREFIX="/usr"
13+
ENV MUSL_SYSROOT="/usr/local/musl-llvm-staging"
1314

1415
RUN set -eux \
1516
&& apk add --no-cache \
1617
cmd:bsdtar \
1718
clang \
1819
llvm \
1920
cmd:llvm-ar \
21+
libc++ \
22+
libc++-dev \
23+
compiler-rt \
24+
llvm-runtimes \
2025
lld \
2126
make \
2227
binutils \
@@ -50,24 +55,24 @@ WORKDIR /build/musl
5055

5156
# Configure, build, and install musl with shared enabled (default) using LLVM tools
5257
RUN mkdir -p ${MUSL_PREFIX} && \
53-
./configure --prefix=${MUSL_PREFIX} && \
54-
make CC=clang CFLAGS="${CFLAGS} -fno-math-errno -fPIC -fno-common" AR=llvm-ar LDFLAGS="${LDFLAGS}" -j"$(nproc)" && \
55-
make install
58+
./configure --prefix=${MUSL_PREFIX} --target=${TARGETARCH}-generic-linux-musl && \
59+
make CC=clang CFLAGS="${CFLAGS} -stdlib=libc++ -rtlib=compiler-rt -fno-math-errno -fPIC -fno-common" AR=llvm-ar LDFLAGS="-fmerge-constants ${LDFLAGS}" -j"$(nproc)" && \
60+
DESTDIR=${MUSL_SYSROOT} make install
5661

5762
# Ensure we have the dynamic loader and libs present (example paths)
58-
RUN ls -l ${MUSL_PREFIX}/lib || true \
59-
&& file ${MUSL_PREFIX}/lib/* || true
63+
RUN ls -l ${MUSL_SYSROOT}/${MUSL_PREFIX}/lib || true \
64+
&& file ${MUSL_SYSROOT}/${MUSL_PREFIX}/lib/* || true
6065

6166
# Strip unneeded symbols from shared objects to save space (optional)
6267
RUN set -eux \
6368
&& if command -v llvm-strip >/dev/null 2>&1; then \
64-
find ${MUSL_PREFIX}/lib -type f -name "*.so*" -exec llvm-strip --strip-unneeded {} + || true; \
69+
find ${MUSL_SYSROOT}/${MUSL_PREFIX}/lib -type f -name "*.so*" -exec llvm-strip --strip-unneeded {} + || true; \
6570
else \
66-
find ${MUSL_PREFIX}/lib -type f -name "*.so*" -exec strip --strip-unneeded {} + || true; \
71+
find ${MUSL_SYSROOT}/${MUSL_PREFIX}/lib -type f -name "*.so*" -exec strip --strip-unneeded {} + || true; \
6772
fi
6873

69-
RUN touch -d ${MITL_DATE_EPOCH} ${MUSL_PREFIX}/lib/* || true \
70-
&& touch -d ${MITL_DATE_EPOCH} ${MUSL_PREFIX}/include/* || true
74+
RUN touch -d ${MITL_DATE_EPOCH} ${MUSL_SYSROOT}/${MUSL_PREFIX}/lib/* || true \
75+
&& touch -d ${MITL_DATE_EPOCH} ${MUSL_SYSROOT}/${MUSL_PREFIX}/include/* || true
7176

7277

7378
# Stage 2: Build toybox based filesystem
@@ -97,7 +102,7 @@ ENV LINUX=/usr/include/linux
97102
# shellcheck disable=SC2154
98103
ARG MUSL_VER=${MUSL_VER:-"1.2.5"}
99104
ENV MUSL_VER=${MUSL_VER}
100-
ENV MUSL_PREFIX="/usr/local/musl-llvm-staging"
105+
ENV MUSL_PREFIX="/usr/local/musl-llvm-staging/usr/"
101106

102107
# Install necessary packages
103108
# llvm - LLVM-apache-2
@@ -181,7 +186,7 @@ RUN if [ -f .config ]; then \
181186
RUN rm -rf generated flags.* || true && make oldconfig || true
182187

183188
# build with clang and lld
184-
RUN make V=1 CC=clang CFLAGS="-fno-math-errno -fstrict-aliasing -fPIC -fno-common" AR=llvm-ar LINUX="${LINUX}" LDFLAGS="${LDFLAGS}" toybox root && \
189+
RUN make V=1 CC=clang CFLAGS="-fno-math-errno -fstrict-aliasing -fPIC -fno-common" AR=llvm-ar LINUX="${LINUX}" LDFLAGS="-fmerge-constants ${LDFLAGS}" toybox root && \
185190
mkdir -p /output/usr/bin /output/etc /output/lib && \
186191
make install PREFIX=/usr DESTDIR=/output
187192

0 commit comments

Comments
 (0)