Skip to content

fix: use FindPython's suffix computation if possible #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions cmake/nanobind-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,57 @@ if (NOT TARGET Python::Module)
endif()

# Determine the right suffix for ordinary and stable ABI extensions.
# Python_SOSABI is guaranteed to be available in CMake 3.26+, and it may
# also be available as part of backported FindPython in scikit-build-core
if (DEFINED Python_SOSABI)
if (WIN32)
set(NB_SUFFIX_EXT ".pyd")
else()
set(NB_SUFFIX_EXT "${CMAKE_SHARED_MODULE_SUFFIX}")
endif()

set(NB_SUFFIX ".${Python_SOABI}${NB_SUFFIX_EXT}")
# We always need to know the extension
if(WIN32)
set(NB_SUFFIX_EXT ".pyd")
else()
set(NB_SUFFIX_EXT "${CMAKE_SHARED_MODULE_SUFFIX}")
endif()

# This was added in CMake 3.17+, also available earlier in scikit-build-core.
# PyPy sets an invalid SOABI (platform missing), causing older FindPythons to
# report an incorrect value. Only use it if it looks correct (X-X-X form).
if(DEFINED Python_SOABI AND "${Python_SOABI}" MATCHES ".+-.+-.+")
set(NB_SUFFIX ".${Python_SOABI}${NB_SUFFIX_EXT}")
endif()

if (Python_SOSABI STREQUAL "")
# Python_SOSABI is guaranteed to be available in CMake 3.26+, and it may
# also be available as part of backported FindPython in scikit-build-core.
if(DEFINED Python_SOSABI)
if(Python_SOSABI STREQUAL "")
set(NB_SUFFIX_S "${NB_SUFFIX_EXT}")
else()
set(NB_SUFFIX_S ".${Python_SOSABI}${NB_SUFFIX_EXT}")
endif()
else()
# Query Python directly to get the right suffix
endif()

# If either suffix is missing, call Python to compute it
if(NOT DEFINED NB_SUFFIX OR NOT DEFINED NB_SUFFIX_S)
# Query Python directly to get the right suffix.
execute_process(
COMMAND "${Python_EXECUTABLE}" "-c"
"import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"
RESULT_VARIABLE NB_SUFFIX_RET
OUTPUT_VARIABLE NB_SUFFIX
OUTPUT_VARIABLE EXT_SUFFIX
OUTPUT_STRIP_TRAILING_WHITESPACE)

if (NB_SUFFIX_RET AND NOT NB_SUFFIX_RET EQUAL 0)
if(NB_SUFFIX_RET AND NOT NB_SUFFIX_RET EQUAL 0)
message(FATAL_ERROR "nanobind: Python sysconfig query to "
"find 'EXT_SUFFIX' property failed!")
endif()

get_filename_component(NB_SUFFIX_EXT "${NB_SUFFIX}" LAST_EXT)
if (WIN32)
set(NB_SUFFIX_S "${NB_SUFFIX_EXT}")
else()
set(NB_SUFFIX_S ".abi3${NB_SUFFIX_EXT}")
if(NOT DEFINED NB_SUFFIX)
set(NB_SUFFIX "${EXT_SUFFIX}")
endif()

if(NOT DEFINED NB_SUFFIX_S)
get_filename_component(NB_SUFFIX_EXT "${EXT_SUFFIX}" LAST_EXT)
if(WIN32)
set(NB_SUFFIX_S "${NB_SUFFIX_EXT}")
else()
set(NB_SUFFIX_S ".abi3${NB_SUFFIX_EXT}")
endif()
endif()
endif()

Expand Down