Skip to content

Commit aad77be

Browse files
SwooshyCuebalanking
authored andcommitted
[#147] Force build-time linker to link libpython
1 parent 26c9792 commit aad77be

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
cmake_minimum_required(VERSION 3.12 FATAL_ERROR) #FindPython
1+
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
2+
# 3.12: FindPython
3+
# 3.13: target_link_options
24

35
find_package(IRODS REQUIRED)
46
set(IRODS_PLUGIN_REVISION "1")
@@ -41,10 +43,10 @@ find_package(OpenSSL REQUIRED COMPONENTS Crypto SSL)
4143
find_package(nlohmann_json "3.6.1" REQUIRED)
4244
find_package(Python "${IRODS_PYTHON_VERSION}" REQUIRED COMPONENTS Development)
4345

46+
get_target_property(__PYTHON_IMPORTED_LOCATION Python::Python IMPORTED_LOCATION)
4447
if (NOT IRODS_PYTHON_SONAME)
4548
get_target_property(IRODS_PYTHON_SONAME Python::Python IMPORTED_SONAME)
4649
if (NOT IRODS_PYTHON_SONAME)
47-
get_target_property(__PYTHON_IMPORTED_LOCATION Python::Python IMPORTED_LOCATION)
4850
if (__PYTHON_IMPORTED_LOCATION)
4951
if (IS_SYMLINK "${__PYTHON_IMPORTED_LOCATION}")
5052
file(READ_SYMLINK "${__PYTHON_IMPORTED_LOCATION}" __PYTHON_LIBRARY_PATH)
@@ -203,9 +205,40 @@ target_link_libraries(
203205
irods_common
204206
irods_server
205207
nlohmann_json::nlohmann_json
206-
Python::Python
207208
)
208209

210+
# Since we are embedding Python rather than just creating extension modules, we must link to
211+
# libpython. However, the build-time linker will silently drop libpython unless we pass
212+
# --no-as-needed just before libpython. Furthermore, Python::Python might specify multiple
213+
# libraries, and other libraries may be passed after libpython. To mitigate this, we toggle off
214+
# --as-needed just for Python::Python's IMPORTED_LOCATION. If we can't fetch IMPORTED_LOCATION, we
215+
# just toggle it for Python::Python.
216+
if (__PYTHON_IMPORTED_LOCATION)
217+
target_link_libraries(
218+
${PLUGIN}
219+
PRIVATE
220+
Python::Python
221+
-Wl,--no-as-needed
222+
"${__PYTHON_IMPORTED_LOCATION}"
223+
-Wl,--as-needed
224+
)
225+
else()
226+
target_link_libraries(
227+
${PLUGIN}
228+
PRIVATE
229+
-Wl,--no-as-needed
230+
Python::Python
231+
-Wl,--as-needed
232+
)
233+
endif()
234+
235+
# Disable lazy-loading
236+
target_link_options(
237+
${PLUGIN}
238+
PRIVATE
239+
-Wl,-z,now
240+
)
241+
209242
target_compile_definitions(
210243
${PLUGIN}
211244
PRIVATE

0 commit comments

Comments
 (0)