Skip to content

Refactor static into the musl build triple #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ci-targets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ linux:
minimum-python-version: "3.13"
run: true

x86_64-unknown-linux-musl:
x86_64-unknown-linux-musl-static:
arch: x86_64
libc: musl
python_versions:
Expand All @@ -262,7 +262,7 @@ linux:
- lto
run: true

x86_64_v2-unknown-linux-musl:
x86_64_v2-unknown-linux-musl-static:
arch: x86_64
arch_variant: v2
libc: musl
Expand All @@ -278,7 +278,7 @@ linux:
- lto
run: true

x86_64_v3-unknown-linux-musl:
x86_64_v3-unknown-linux-musl-static:
arch: x86_64
arch_variant: v3
libc: musl
Expand All @@ -294,7 +294,7 @@ linux:
- lto
run: true

x86_64_v4-unknown-linux-musl:
x86_64_v4-unknown-linux-musl-static:
arch: x86_64
arch_variant: v4
libc: musl
Expand Down
2 changes: 1 addition & 1 deletion cpython-unix/build-cpython.sh
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ CONFIGURE_FLAGS="
--without-ensurepip
${EXTRA_CONFIGURE_FLAGS}"

if [ "${CC}" = "musl-clang" ]; then
if [ -n "${CPYTHON_STATIC}" ]; then
CFLAGS="${CFLAGS} -static"
CPPFLAGS="${CPPFLAGS} -static"
LDFLAGS="${LDFLAGS} -static"
Expand Down
34 changes: 23 additions & 11 deletions cpython-unix/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ def python_build_info(
version,
platform,
target_triple,
musl,
lto,
extensions,
extra_metadata,
Expand All @@ -506,7 +505,7 @@ def python_build_info(
)
)

if not musl:
if not target_triple.endswith("-static"):
bi["core"]["shared_lib"] = "install/lib/libpython%s%s.so.1.0" % (
version,
binary_suffix,
Expand Down Expand Up @@ -825,6 +824,8 @@ def build_cpython(
env["CPYTHON_OPTIMIZED"] = "1"
if "lto" in parsed_build_options:
env["CPYTHON_LTO"] = "1"
if target_triple.endswith("-static"):
env["CPYTHON_STATIC"] = "1"

add_target_env(env, host_platform, target_triple, build_env)

Expand All @@ -834,19 +835,26 @@ def build_cpython(
crt_features = []

if host_platform == "linux64":
if "musl" in target_triple:
if target_triple.endswith("-static"):
crt_features.append("static")
else:
extension_module_loading.append("shared-library")
crt_features.append("glibc-dynamic")

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

crt_features.append(
"glibc-max-symbol-version:%s" % glibc_max_version.decode("ascii")
)
else:
crt_features.append("glibc-dynamic")

glibc_max_version = build_env.get_file("glibc_version.txt").strip()
if not glibc_max_version:
raise Exception("failed to retrieve glibc max symbol version")

crt_features.append(
"glibc-max-symbol-version:%s"
% glibc_max_version.decode("ascii")
)

python_symbol_visibility = "global-default"

Expand Down Expand Up @@ -874,7 +882,9 @@ def build_cpython(
"python_stdlib_test_packages": sorted(STDLIB_TEST_PACKAGES),
"python_symbol_visibility": python_symbol_visibility,
"python_extension_module_loading": extension_module_loading,
"libpython_link_mode": "static" if "musl" in target_triple else "shared",
"libpython_link_mode": (
"static" if target_triple.endswith("-static") else "shared"
),
"crt_features": crt_features,
"run_tests": "build/run_tests.py",
"build_info": python_build_info(
Expand Down Expand Up @@ -946,6 +956,7 @@ def main():
print("unable to connect to Docker: %s" % e, file=sys.stderr)
return 1

# Note these arguments must be synced with `build-main.py`
parser = argparse.ArgumentParser()
parser.add_argument(
"--host-platform", required=True, help="Platform we are building from"
Expand All @@ -962,6 +973,7 @@ def main():
default="noopt",
help="Build options to apply when compiling Python",
)

parser.add_argument(
"--toolchain",
action="store_true",
Expand Down
8 changes: 4 additions & 4 deletions cpython-unix/targets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ x86_64_v4-unknown-linux-gnu:
openssl_target: linux-x86_64
bolt_capable: true

x86_64-unknown-linux-musl:
x86_64-unknown-linux-musl-static:
host_platforms:
- linux64
pythons_supported:
Expand Down Expand Up @@ -990,7 +990,7 @@ x86_64-unknown-linux-musl:
- zlib
openssl_target: linux-x86_64

x86_64_v2-unknown-linux-musl:
x86_64_v2-unknown-linux-musl-static:
host_platforms:
- linux64
pythons_supported:
Expand Down Expand Up @@ -1034,7 +1034,7 @@ x86_64_v2-unknown-linux-musl:
- zlib
openssl_target: linux-x86_64

x86_64_v3-unknown-linux-musl:
x86_64_v3-unknown-linux-musl-static:
host_platforms:
- linux64
pythons_supported:
Expand Down Expand Up @@ -1078,7 +1078,7 @@ x86_64_v3-unknown-linux-musl:
- zlib
openssl_target: linux-x86_64

x86_64_v4-unknown-linux-musl:
x86_64_v4-unknown-linux-musl-static:
host_platforms:
- linux64
pythons_supported:
Expand Down
13 changes: 7 additions & 6 deletions pythonbuild/cpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,13 @@ def derive_setup_local(
enabled_extensions[name]["setup_line"] = name.encode("ascii")
continue

# musl is static only. Ignore build-mode override.
if "musl" in target_triple:
section = "static"
else:
section = info.get("build-mode", "static")

section = (
# If performing a static build, always use static
"static"
if target_triple.endswith("-static")
# Otherwise, use the build mode (falling back to static)
else info.get("build-mode", "static")
)
enabled_extensions[name]["build-mode"] = section

# Presumably this means the extension comes from the distribution's
Expand Down
Loading