Skip to content

Commit 5e2454b

Browse files
committed
unix: properly annotate core library dependencies
Before this commit, the PYTHON.json file for Linux and macOS distributions didn't properly annotate linking dependencies for the Python core: only modules. Typically missing libraries would be linked automatically as they are present in many programs or were provided by extension modules. However, this may not always be the case. (I discovered this issue after PyOxidizer was complaining about missing pthread symbols on Ubuntu 21.10.) In this commit, we simply parse the LIBS configuration variable from the Python build and extract linker annotations from it, adding links annotations to the produced PYTHON.json.
1 parent 8eebbfc commit 5e2454b

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

cpython-unix/build.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
SUPPORT = ROOT / "cpython-unix"
4545
TARGETS_CONFIG = SUPPORT / "targets.yml"
4646

47+
LINUX_ALLOW_SYSTEM_LIBRARIES = {"c", "crypt", "dl", "m", "pthread", "rt", "util"}
48+
MACOS_ALLOW_SYSTEM_LIBRARIES = {"dl", "m", "pthread"}
49+
MACOS_ALLOW_FRAMEWORKS = {"CoreFoundation"}
50+
4751

4852
def install_sccache(build_env):
4953
"""Attempt to install sccache into the build environment.
@@ -258,7 +262,8 @@ def build_binutils(client, image, host_platform):
258262
add_env_common(env)
259263

260264
build_env.run(
261-
"build-binutils.sh", environment=env,
265+
"build-binutils.sh",
266+
environment=env,
262267
)
263268

264269
build_env.get_tools_archive(
@@ -499,6 +504,7 @@ def python_build_info(
499504
config_c_in,
500505
setup_dist,
501506
setup_local,
507+
extra_metadata,
502508
):
503509
"""Obtain build metadata for the Python distribution."""
504510

@@ -545,6 +551,42 @@ def python_build_info(
545551

546552
bi["object_file_format"] = object_file_format
547553

554+
# Add in core linking annotations.
555+
libs = extra_metadata["python_config_vars"].get("LIBS", "").split()
556+
skip = False
557+
for i, lib in enumerate(libs):
558+
if skip:
559+
skip = False
560+
continue
561+
562+
if lib.startswith("-l"):
563+
lib = lib[2:]
564+
565+
if platform == "linux64" and lib not in LINUX_ALLOW_SYSTEM_LIBRARIES:
566+
raise Exception("unexpected library in LIBS (%s): %s" % (libs, lib))
567+
elif platform == "macos" and lib not in MACOS_ALLOW_SYSTEM_LIBRARIES:
568+
raise Exception("unexpected library in LIBS (%s): %s" % (libs, lib))
569+
570+
log("adding core system link library: %s" % lib)
571+
bi["core"]["links"].append(
572+
{
573+
"name": lib,
574+
"system": True,
575+
}
576+
)
577+
elif lib == "-framework":
578+
skip = True
579+
framework = libs[i + 1]
580+
if framework not in MACOS_ALLOW_FRAMEWORKS:
581+
raise Exception(
582+
"unexpected framework in LIBS (%s): %s" % (libs, framework)
583+
)
584+
585+
log("adding core link framework: %s" % framework)
586+
bi["core"]["links"].append({"name": framework, "framework": True})
587+
else:
588+
raise Exception("unknown word in LIBS (%s): %s" % (libs, lib))
589+
548590
# Object files for the core distribution are found by walking the
549591
# build artifacts.
550592
core_objs = set()
@@ -838,6 +880,8 @@ def build_cpython(
838880
else:
839881
raise ValueError("unhandled platform: %s" % host_platform)
840882

883+
extra_metadata = json.loads(build_env.get_file("metadata.json"))
884+
841885
# Create PYTHON.json file describing this distribution.
842886
python_info = {
843887
"version": "7",
@@ -861,6 +905,7 @@ def build_cpython(
861905
config_c_in,
862906
setup_dist_content,
863907
setup_local_content,
908+
extra_metadata,
864909
),
865910
"licenses": entry["licenses"],
866911
"license_path": "licenses/LICENSE.cpython.txt",
@@ -884,8 +929,7 @@ def build_cpython(
884929
]
885930

886931
# Add metadata derived from built distribution.
887-
extra_metadata = build_env.get_file("metadata.json")
888-
python_info.update(json.loads(extra_metadata))
932+
python_info.update(extra_metadata)
889933

890934
validate_python_json(python_info)
891935

0 commit comments

Comments
 (0)