CMake: Use Python::Module, not Python::Python, for extension linkage

CMake 3.15+ provides the former, which extension modules should link
against in order to ensure the ABI-appropriate linkage for the
platform.

For older versions of CMake, try to hack up the correct linkage for
the platform. If the platform isn't recognized, tell the user to
upgrade CMake.

This also tweaks p3pystub just a bit, since now it's actually relied
upon to resolve runtime link issues when p3dcparse pulls in direct,
as p3dcparse doesn't link against Python at all.
This commit is contained in:
Sam Edwards 2019-08-15 15:05:37 -06:00
parent 82d14d98d4
commit d008037244
5 changed files with 45 additions and 5 deletions

View File

@ -1,3 +1,3 @@
add_executable(p3dcparse dcparse.cxx)
target_link_libraries(p3dcparse p3direct)
target_link_libraries(p3dcparse p3direct p3pystub)
install(TARGETS p3dcparse EXPORT Direct COMPONENT Direct DESTINATION bin)

View File

@ -11,6 +11,7 @@ if(Python_FOUND)
set(PYTHON_FOUND ON)
set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
set(PYTHON_INCLUDE_DIRS ${Python_INCLUDE_DIRS})
set(PYTHON_LIBRARY_DIRS ${Python_LIBRARY_DIRS})
set(PYTHON_VERSION_STRING ${Python_VERSION})
else()
@ -27,11 +28,45 @@ else()
endif()
if(CMAKE_VERSION VERSION_LESS "3.15")
# CMake versions this old don't provide Python::Module, so we need to hack up
# the variables to ensure no explicit linkage against libpython occurs
if(WIN32)
# Nothing needed here; explicit linkage is appropriate
set(PYTHON_LIBRARY "${Python_LIBRARY}")
set(PYTHON_LIBRARIES ${Python_LIBRARIES})
elseif(APPLE OR UNIX)
# Just unset and let the implicit linkage take over
set(PYTHON_LIBRARY "")
set(PYTHON_LIBRARIES "")
if(APPLE)
# macOS requires this explicit flag on the linker command line to allow the
# references to the Python symbols to resolve at dynamic link time
string(APPEND CMAKE_MODULE_LINKER_FLAGS " -undefined dynamic_lookup")
# TODO: p3dcparser contains some direct Python references; get rid of
# this once that's gone
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -undefined dynamic_lookup")
endif()
else()
# On every other platform, guessing is a bad idea - insist the user upgrade
# their CMake instead.
message(WARNING "For Python support on this platform, please use CMake >= 3.15!")
set(PYTHON_FOUND OFF)
endif()
endif()
package_option(PYTHON
DEFAULT ON
"Enables support for Python. If INTERROGATE_PYTHON_INTERFACE
is also enabled, Python bindings will be generated."
IMPORTED_AS Python::Python)
IMPORTED_AS Python::Module)
# Also detect the optimal install paths:
if(HAVE_PYTHON)

View File

@ -60,7 +60,7 @@ set(INTERROGATE_PREAMBLE_PYTHON_NATIVE
composite_sources(interrogate INTERROGATE_SOURCES)
add_executable(interrogate ${INTERROGATE_HEADERS} ${INTERROGATE_SOURCES})
target_link_libraries(interrogate p3cppParser p3dtoolconfig p3pystub
target_link_libraries(interrogate p3cppParser p3dtoolconfig
PKG::OPENSSL)
# Python preamble for interrogate_module

View File

@ -1,6 +1,11 @@
set(P3PYSTUB_HEADERS pystub.h)
set(P3PYSTUB_SOURCES pystub.cxx)
add_library(p3pystub STATIC ${P3PYSTUB_HEADERS} ${P3PYSTUB_SOURCES})
add_library(p3pystub ${P3PYSTUB_HEADERS} ${P3PYSTUB_SOURCES})
target_link_libraries(p3pystub p3dtool)
install(FILES ${P3PYSTUB_HEADERS} COMPONENT CoreDevel DESTINATION include/panda3d)
install(TARGETS p3pystub
EXPORT Core COMPONENT Core
DESTINATION lib
RUNTIME DESTINATION bin
ARCHIVE COMPONENT CoreDevel)

View File

@ -5,5 +5,5 @@ if(NOT BUILD_PANDATOOL)
endif()
add_executable(pview pview.cxx)
target_link_libraries(pview p3framework p3pystub)
target_link_libraries(pview p3framework)
install(TARGETS pview EXPORT Tools COMPONENT Tools DESTINATION bin)