Skip to content

Commit 9663760

Browse files
committed
Dynamically link libc in musl distributions
1 parent aa430e2 commit 9663760

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

cpython-unix/build-cpython.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,8 @@ CONFIGURE_FLAGS="
382382
${EXTRA_CONFIGURE_FLAGS}"
383383

384384
if [ "${CC}" = "musl-clang" ]; then
385-
CFLAGS="${CFLAGS} -static"
386-
CPPFLAGS="${CPPFLAGS} -static"
387-
LDFLAGS="${LDFLAGS} -static"
388-
PYBUILD_SHARED=0
385+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --enable-shared"
386+
PYBUILD_SHARED=1
389387

390388
# In order to build the _blake2 extension module with SSE3+ instructions, we need
391389
# musl-clang to find headers that provide access to the intrinsics, as they are not
@@ -874,6 +872,7 @@ EOF
874872
${BUILD_PYTHON} ${ROOT}/generate_metadata.py ${ROOT}/metadata.json
875873
cat ${ROOT}/metadata.json
876874

875+
# TODO: Output a dynamic library version for musl
877876
if [ "${CC}" != "musl-clang" ]; then
878877
objdump -T ${LIBPYTHON_SHARED_LIBRARY} | grep GLIBC_ | awk '{print $5}' | awk -F_ '{print $2}' | sort -V | tail -n 1 > ${ROOT}/glibc_version.txt
879878
cat ${ROOT}/glibc_version.txt

cpython-unix/build-libX11.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ if [ -n "${CROSS_COMPILING}" ]; then
7575
s390x-unknown-linux-gnu)
7676
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
7777
;;
78+
x86_64-unknown-linux-musl)
79+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
80+
;;
81+
aarch64-unknown-linux-musl)
82+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
83+
;;
84+
i686-unknown-linux-musl)
85+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
86+
;;
87+
mips-unknown-linux-musl)
88+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
89+
;;
90+
mipsel-unknown-linux-musl)
91+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
92+
;;
93+
ppc64le-unknown-linux-musl)
94+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
95+
;;
96+
riscv64-unknown-linux-musl)
97+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
98+
;;
99+
s390x-unknown-linux-musl)
100+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
101+
;;
78102
*)
79103
echo "cross-compiling but malloc(0) override not set; failures possible"
80104
;;

cpython-unix/build-musl.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ index 4a6ebe4..0000000
5252
-}
5353
EOF
5454

55-
./configure \
55+
56+
CFLAGS="${CFLAGS} -fPIC" CPPFLAGS="${CPPFLAGS} -fPIC" ./configure \
5657
--prefix=/tools/host \
57-
--disable-shared
58+
--enable-shared
5859

5960
make -j `nproc`
6061
make -j `nproc` install DESTDIR=/build/out

cpython-unix/build-xextproto.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@ export PKG_CONFIG_PATH=/tools/deps/share/pkgconfig
1515
tar -xf xextproto-${XEXTPROTO_VERSION}.tar.gz
1616
pushd xextproto-${XEXTPROTO_VERSION}
1717

18+
EXTRA_CONFIGURE_FLAGS=
19+
20+
21+
if [ -n "${CROSS_COMPILING}" ]; then
22+
if echo "${TARGET_TRIPLE}" | grep -q -- "-unknown-linux-musl"; then
23+
# xextproto does not support configuration of musl targets so we pretend the target matches the
24+
# build triple and enable cross-compilation manually
25+
TARGET_TRIPLE="$(echo "${TARGET_TRIPLE}" | sed -e 's/-unknown-linux-musl/-unknown-linux-gnu/g')"
26+
EXTRA_CONFIGURE_FLAGS="cross_compiling=yes"
27+
fi
28+
fi
29+
1830
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
1931
--build=${BUILD_TRIPLE} \
2032
--host=${TARGET_TRIPLE} \
21-
--prefix=/tools/deps
33+
--prefix=/tools/deps \
34+
"${EXTRA_CONFIGURE_FLAGS}"
2235

2336
make -j `nproc`
2437
make -j `nproc` install DESTDIR=${ROOT}/out

cpython-unix/build-xproto.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@ export PKG_CONFIG_PATH=${TOOLS_PATH}/deps/share/pkgconfig
1515
tar -xf xproto-${XPROTO_VERSION}.tar.gz
1616
pushd xproto-${XPROTO_VERSION}
1717

18+
EXTRA_CONFIGURE_FLAGS=
19+
20+
21+
if [ -n "${CROSS_COMPILING}" ]; then
22+
if echo "${TARGET_TRIPLE}" | grep -q -- "-unknown-linux-musl"; then
23+
# xproto does not support configuration of musl targets so we pretend the target matches the
24+
# build triple and enable cross-compilation manually
25+
TARGET_TRIPLE="$(echo "${TARGET_TRIPLE}" | sed -e 's/-unknown-linux-musl/-unknown-linux-gnu/g')"
26+
EXTRA_CONFIGURE_FLAGS="cross_compiling=yes"
27+
fi
28+
fi
29+
1830
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
1931
--build=${BUILD_TRIPLE} \
2032
--host=${TARGET_TRIPLE} \
21-
--prefix=/tools/deps
33+
--prefix=/tools/deps \
34+
"${EXTRA_CONFIGURE_FLAGS}"
2235

2336
make -j ${NUM_CPUS}
2437
make -j ${NUM_CPUS} install DESTDIR=${ROOT}/out

cpython-unix/build.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def add_target_env(env, build_platform, target_triple, build_env):
121121
.replace("x86_64_v3-", "x86_64-")
122122
.replace("x86_64_v4-", "x86_64-")
123123
# TODO should the musl target be normalized?
124-
.replace("-unknown-linux-musl", "-unknown-linux-gnu")
124+
# .replace("-unknown-linux-musl", "-unknown-linux-gnu")
125125
)
126126

127127
# This will make x86_64_v2, etc count as cross-compiling. This is
@@ -506,6 +506,7 @@ def python_build_info(
506506
)
507507
)
508508

509+
# TODO: Update this for non-static musl
509510
if not musl:
510511
bi["core"]["shared_lib"] = "install/lib/libpython%s%s.so.1.0" % (
511512
version,
@@ -834,6 +835,7 @@ def build_cpython(
834835
crt_features = []
835836

836837
if host_platform == "linux64":
838+
# TODO: Update the musl target triple to reflect it is dynamic
837839
if "musl" in target_triple:
838840
crt_features.append("static")
839841
else:
@@ -874,6 +876,7 @@ def build_cpython(
874876
"python_stdlib_test_packages": sorted(STDLIB_TEST_PACKAGES),
875877
"python_symbol_visibility": python_symbol_visibility,
876878
"python_extension_module_loading": extension_module_loading,
879+
# TODO: Update this for dynamic musl
877880
"libpython_link_mode": "static" if "musl" in target_triple else "shared",
878881
"crt_features": crt_features,
879882
"run_tests": "build/run_tests.py",

0 commit comments

Comments
 (0)