Skip to content

Commit f41e684

Browse files
committed
Refactor static into the musl build triple
1 parent 2a9167e commit f41e684

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

ci-targets.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ linux:
247247
minimum-python-version: "3.13"
248248
run: true
249249

250-
x86_64-unknown-linux-musl:
250+
x86_64-unknown-linux-musl-static:
251251
arch: x86_64
252252
libc: musl
253253
python_versions:
@@ -262,7 +262,7 @@ linux:
262262
- lto
263263
run: true
264264

265-
x86_64_v2-unknown-linux-musl:
265+
x86_64_v2-unknown-linux-musl-static:
266266
arch: x86_64
267267
arch_variant: v2
268268
libc: musl
@@ -278,7 +278,7 @@ linux:
278278
- lto
279279
run: true
280280

281-
x86_64_v3-unknown-linux-musl:
281+
x86_64_v3-unknown-linux-musl-static:
282282
arch: x86_64
283283
arch_variant: v3
284284
libc: musl
@@ -294,7 +294,7 @@ linux:
294294
- lto
295295
run: true
296296

297-
x86_64_v4-unknown-linux-musl:
297+
x86_64_v4-unknown-linux-musl-static:
298298
arch: x86_64
299299
arch_variant: v4
300300
libc: musl

cpython-unix/build-cpython.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ CONFIGURE_FLAGS="
381381
--without-ensurepip
382382
${EXTRA_CONFIGURE_FLAGS}"
383383

384-
if [ "${CC}" = "musl-clang" ]; then
384+
if [ -n "${CPYTHON_STATIC}" ]; then
385385
CFLAGS="${CFLAGS} -static"
386386
CPPFLAGS="${CPPFLAGS} -static"
387387
LDFLAGS="${LDFLAGS} -static"

cpython-unix/build.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ def python_build_info(
486486
version,
487487
platform,
488488
target_triple,
489-
musl,
490489
lto,
491490
extensions,
492491
extra_metadata,
@@ -506,7 +505,7 @@ def python_build_info(
506505
)
507506
)
508507

509-
if not musl:
508+
if not target_triple.endswith("-static"):
510509
bi["core"]["shared_lib"] = "install/lib/libpython%s%s.so.1.0" % (
511510
version,
512511
binary_suffix,
@@ -825,6 +824,8 @@ def build_cpython(
825824
env["CPYTHON_OPTIMIZED"] = "1"
826825
if "lto" in parsed_build_options:
827826
env["CPYTHON_LTO"] = "1"
827+
if target_triple.endswith("-static"):
828+
env["CPYTHON_STATIC"] = "1"
828829

829830
add_target_env(env, host_platform, target_triple, build_env)
830831

@@ -834,19 +835,26 @@ def build_cpython(
834835
crt_features = []
835836

836837
if host_platform == "linux64":
837-
if "musl" in target_triple:
838+
if target_triple.endswith("-static"):
838839
crt_features.append("static")
839840
else:
840841
extension_module_loading.append("shared-library")
841-
crt_features.append("glibc-dynamic")
842842

843-
glibc_max_version = build_env.get_file("glibc_version.txt").strip()
844-
if not glibc_max_version:
845-
raise Exception("failed to retrieve glibc max symbol version")
843+
if "musl" in target_triple:
844+
crt_features.append("musl-dynamic")
845+
# TODO: Determine the dynamic musl libc version
846846

847-
crt_features.append(
848-
"glibc-max-symbol-version:%s" % glibc_max_version.decode("ascii")
849-
)
847+
else:
848+
crt_features.append("glibc-dynamic")
849+
850+
glibc_max_version = build_env.get_file("glibc_version.txt").strip()
851+
if not glibc_max_version:
852+
raise Exception("failed to retrieve glibc max symbol version")
853+
854+
crt_features.append(
855+
"glibc-max-symbol-version:%s"
856+
% glibc_max_version.decode("ascii")
857+
)
850858

851859
python_symbol_visibility = "global-default"
852860

@@ -874,7 +882,9 @@ def build_cpython(
874882
"python_stdlib_test_packages": sorted(STDLIB_TEST_PACKAGES),
875883
"python_symbol_visibility": python_symbol_visibility,
876884
"python_extension_module_loading": extension_module_loading,
877-
"libpython_link_mode": "static" if "musl" in target_triple else "shared",
885+
"libpython_link_mode": (
886+
"static" if target_triple.endswith("-static") else "shared"
887+
),
878888
"crt_features": crt_features,
879889
"run_tests": "build/run_tests.py",
880890
"build_info": python_build_info(
@@ -946,6 +956,7 @@ def main():
946956
print("unable to connect to Docker: %s" % e, file=sys.stderr)
947957
return 1
948958

959+
# Note these arguments must be synced with `build-main.py`
949960
parser = argparse.ArgumentParser()
950961
parser.add_argument(
951962
"--host-platform", required=True, help="Platform we are building from"
@@ -962,6 +973,7 @@ def main():
962973
default="noopt",
963974
help="Build options to apply when compiling Python",
964975
)
976+
965977
parser.add_argument(
966978
"--toolchain",
967979
action="store_true",

pythonbuild/cpython.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,13 @@ def derive_setup_local(
466466
enabled_extensions[name]["setup_line"] = name.encode("ascii")
467467
continue
468468

469-
# musl is static only. Ignore build-mode override.
470-
if "musl" in target_triple:
471-
section = "static"
472-
else:
473-
section = info.get("build-mode", "static")
474-
469+
section = (
470+
# If performing a static build, always use static
471+
"static"
472+
if target_triple.endswith("-static")
473+
# Otherwise, use the build mode (falling back to static)
474+
else info.get("build-mode", "static")
475+
)
475476
enabled_extensions[name]["build-mode"] = section
476477

477478
# Presumably this means the extension comes from the distribution's

0 commit comments

Comments
 (0)