From d008037244601f795a73c7202b1e85808ef0def5 Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Thu, 15 Aug 2019 15:05:37 -0600 Subject: [PATCH] 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. --- direct/src/dcparse/CMakeLists.txt | 2 +- dtool/Package.cmake | 37 +++++++++++++++++++++++++++- dtool/src/interrogate/CMakeLists.txt | 2 +- dtool/src/pystub/CMakeLists.txt | 7 +++++- panda/src/testbed/CMakeLists.txt | 2 +- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/direct/src/dcparse/CMakeLists.txt b/direct/src/dcparse/CMakeLists.txt index 747f5896b2..c84e8ecd15 100644 --- a/direct/src/dcparse/CMakeLists.txt +++ b/direct/src/dcparse/CMakeLists.txt @@ -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) diff --git a/dtool/Package.cmake b/dtool/Package.cmake index 51e227649d..dca10e6ccc 100644 --- a/dtool/Package.cmake +++ b/dtool/Package.cmake @@ -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) diff --git a/dtool/src/interrogate/CMakeLists.txt b/dtool/src/interrogate/CMakeLists.txt index 2ddc367aa7..86431b2bfa 100644 --- a/dtool/src/interrogate/CMakeLists.txt +++ b/dtool/src/interrogate/CMakeLists.txt @@ -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 diff --git a/dtool/src/pystub/CMakeLists.txt b/dtool/src/pystub/CMakeLists.txt index 70b7b85898..b4f6f5a4a2 100644 --- a/dtool/src/pystub/CMakeLists.txt +++ b/dtool/src/pystub/CMakeLists.txt @@ -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) diff --git a/panda/src/testbed/CMakeLists.txt b/panda/src/testbed/CMakeLists.txt index e44ce3a2a5..8170c13d55 100644 --- a/panda/src/testbed/CMakeLists.txt +++ b/panda/src/testbed/CMakeLists.txt @@ -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)