Skip to content

Commit a873560

Browse files
author
Vincent Moens
committed
[Setup] statically link _C extension against the Python library
ghstack-source-id: 26f374e Pull-Request-resolved: #1304 (cherry picked from commit af17524)
1 parent e84d44f commit a873560

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

.github/scripts/version_script.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export MACOSX_DEPLOYMENT_TARGET=15.0
77

88
${CONDA_RUN} pip install --upgrade pip
99

10-
${CONDA_RUN} conda install conda-forge::rust -y
1110
# for orjson
1211
export UNSAFE_PYO3_BUILD_FREE_THREADED=1
1312

.github/scripts/win-pre-script.bat

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ if "%CONDA_RUN%"=="" (
77

88
:: Run the pip install command
99
%CONDA_RUN% conda install conda-forge::pybind11 -y
10-
%CONDA_RUN% conda install conda-forge::rust -y
1110

1211
:: Check if the installation was successful
1312
if errorlevel 1 (

tensordict/csrc/CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ else()
3636
)
3737
endif()
3838

39+
find_package(Python COMPONENTS Development.Static)
40+
target_link_libraries(_C ${Python_STATIC_LIBRARIES})
41+
3942
target_include_directories(_C PRIVATE ${PROJECT_SOURCE_DIR})
4043

41-
if(APPLE OR WIN32) # Check if the target OS is OSX/macOS
42-
target_link_libraries(_C PRIVATE pybind11::module)
43-
else()
44-
target_link_libraries(_C PRIVATE Python3::Python pybind11::module)
45-
endif()
44+
#if(APPLE OR WIN32) # Check if the target OS is OSX/macOS
45+
target_link_libraries(_C PRIVATE pybind11::module)
46+
#else()
47+
# target_link_libraries(_C PRIVATE Python3::Python pybind11::module)
48+
#endif()
4649

4750
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
4851
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fsanitize=address")

test/smoke_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
5+
import subprocess
6+
import sys
7+
from pathlib import Path
8+
9+
_IS_LINUX = sys.platform.startswith("linux")
510

611

712
def test_imports_deps():
@@ -23,6 +28,32 @@ def test_imports():
2328
print("version", tensordict.__version__) # noqa
2429

2530

31+
def test_static_linking():
32+
if not _IS_LINUX:
33+
return
34+
# Locate _C.so
35+
try:
36+
import tensordict._C
37+
except ImportError as e:
38+
raise RuntimeError(f"Failed to import tensordict._C: {e}")
39+
# Get the path to _C.so
40+
_C_path = Path(tensordict._C.__file__)
41+
if not _C_path.exists():
42+
raise RuntimeError(f"_C.so not found at {_C_path}")
43+
# Run ldd on _C.so
44+
try:
45+
output = subprocess.check_output(["ldd", str(_C_path)]).decode("utf-8")
46+
except subprocess.CalledProcessError as e:
47+
raise RuntimeError(f"Failed to run ldd on {_C_path}: {e}")
48+
# Check if libpython is dynamically linked
49+
for line in output.splitlines():
50+
if "libpython" in line and "=>" in line and "not found" not in line:
51+
raise RuntimeError(
52+
f"tensordict/_C.so is dynamically linked against {line.strip()}"
53+
)
54+
print("Test passed: tensordict/_C.so does not show dynamic linkage to libpython.") # noqa
55+
56+
2657
if __name__ == "__main__":
2758
test_imports_deps()
2859
test_imports()

0 commit comments

Comments
 (0)