Skip to content

Commit 5d2abd3

Browse files
Add ldscript code for Python extensions in CMake (#6665)
* Add ldscript code for Python extensions in CMake We added ldscripts to the Makefile for Python extensions (to restrict exported symbols to just the PyInit_foo symbol), but neglected to do so for CMake. This corrects that.
1 parent 8aba364 commit 5d2abd3

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

python_bindings/correctness/ext/CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include(TargetExportScript)
2+
13
set(FEATURES_user_context user_context)
24

35
foreach (GEN IN ITEMS addconstant bit user_context)
@@ -18,4 +20,18 @@ foreach (GEN IN ITEMS addconstant bit user_context)
1820
Python3_add_library(py_${GEN} MODULE ${${GEN}_py_cpp})
1921
target_link_libraries(py_${GEN} PRIVATE ext_${GEN})
2022
set_target_properties(py_${GEN} PROPERTIES OUTPUT_NAME ${GEN}) # Python3_add_library adds target info to name.
23+
24+
# Fake up a linker script that will export *just* the PyInit entry
25+
# point we want. (If we don't do this we can have interesting failures
26+
# when loading multiple of these Python extensions in the same space.)
27+
#
28+
# TODO: How to do this for Windows as well?
29+
configure_file(ext.ldscript.apple.in "${CMAKE_CURRENT_BINARY_DIR}/${GEN}.ldscript.apple")
30+
configure_file(ext.ldscript.linux.in "${CMAKE_CURRENT_BINARY_DIR}/${GEN}.ldscript")
31+
target_export_script(
32+
py_${GEN}
33+
APPLE_LD "${CMAKE_CURRENT_BINARY_DIR}/${GEN}.ldscript.apple"
34+
GNU_LD "${CMAKE_CURRENT_BINARY_DIR}/${GEN}.ldscript"
35+
)
36+
2137
endforeach ()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_PyInit_${GEN}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
global: PyInit_${GEN};
3+
local: *;
4+
};

0 commit comments

Comments
 (0)