Skip to content

Commit f5d7669

Browse files
committed
build: Export the stub's dynamic symbols when libpython is static
The manylinux base container builds CPython with --disable-shared, so on that platform triton_python_backend_stub links libpython3.x.a and the Py_* symbols live in the executable rather than a shared library. Python C extension modules are dlopen'ed and deliberately leave Py_* undefined -- every one of the 77 lib-dynload modules in the image does, math.so alone with 63 undefined symbols and no libpython in DT_NEEDED -- so they can only bind against the stub's dynamic symbol table. Without the export the stub links and starts, then fails on the first `import math`. Set ENABLE_EXPORTS, which makes CMake add the platform's flag (-Wl,--export-dynamic on Linux, per Platform/Linux.cmake), matching how CPython links its own interpreter. Gate it on the resolved library being an archive rather than on the platform. A shared libpython needs no export, so the Debian build is unaffected, and the condition stays correct if a target moves between the two linkage modes. The condition reads PYTHON_LIBRARY, not PYTHON_LIBRARIES: pybind11 arrives through FetchContent_MakeAvailable, which uses add_subdirectory, so only the cache entry find_library creates crosses back into this scope. PYTHON_LIBRARIES is a plain variable in pybind11's directory and is empty here, which would have made this a silent no-op.
1 parent f9088ff commit f5d7669

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# Copyright 2020-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# Redistribution and use in source and binary forms, with or without
44
# modification, are permitted provided that the following conditions
@@ -285,6 +285,30 @@ target_compile_options(
285285
)
286286
target_compile_definitions(triton-python-backend-stub PRIVATE TRITON_PB_STUB)
287287

288+
# When libpython is a static archive -- the manylinux base container builds
289+
# CPython with --disable-shared -- the Py_* symbols land inside the stub
290+
# executable instead of a shared library. Python C extension modules are
291+
# dlopen'ed and deliberately leave Py_* undefined, so they can only resolve
292+
# against the stub's dynamic symbol table. ENABLE_EXPORTS makes CMake add the
293+
# platform's flag for that (-Wl,--export-dynamic on Linux), the same way
294+
# CPython links its own interpreter binary.
295+
#
296+
# A shared libpython needs none of this, since modules bind to it directly.
297+
# Key off the linkage rather than the platform so this stays correct if a
298+
# target ever moves between the two.
299+
#
300+
# PYTHON_LIBRARY rather than PYTHON_LIBRARIES: pybind11 is pulled in with
301+
# FetchContent_MakeAvailable, which uses add_subdirectory, so only the cache
302+
# entry that find_library creates survives into this scope. PYTHON_LIBRARIES
303+
# is a plain variable set inside pybind11's directory and reads empty here.
304+
if(PYTHON_LIBRARY MATCHES "\\.a$")
305+
set_target_properties(
306+
triton-python-backend-stub
307+
PROPERTIES
308+
ENABLE_EXPORTS TRUE
309+
)
310+
endif()
311+
288312
# RHEL assets are not released in a container environment nor do the current
289313
# Python lib versions in the manylinux base container match those currently
290314
# available for RHEL8 package managers. Therefore, we package the correct

0 commit comments

Comments
 (0)