Skip to content
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

bring python2 support back #630

Merged
merged 8 commits into from
Nov 2, 2023
Merged
Changes from 2 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
50 changes: 35 additions & 15 deletions python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,32 @@ macro(FINDPYTHON)
ERROR_VARIABLE _PYTHON_VERSION_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE)

if(NOT "${_PYTHON_VERSION_RESULT_VARIABLE}" STREQUAL "0")
message(FATAL_ERROR "${PYTHON_EXECUTABLE} --version did not succeed.")
endif(NOT "${_PYTHON_VERSION_RESULT_VARIABLE}" STREQUAL "0")
nim65s marked this conversation as resolved.
Show resolved Hide resolved
string(REGEX REPLACE "Python " "" _PYTHON_VERSION
${_PYTHON_VERSION_OUTPUT})
string(REGEX REPLACE "\\." ";" _PYTHON_VERSION ${_PYTHON_VERSION})
list(GET _PYTHON_VERSION 0 _PYTHON_VERSION_MAJOR)

# Provide some hints according to the current PYTHON_EXECUTABLE
if(NOT DEFINED PYTHON_INCLUDE_DIR)
if(_PYTHON_VERSION_MAJOR EQUAL "2")
set(PYTHON_INCLUDE_DIR_CMD
nim65s marked this conversation as resolved.
Show resolved Hide resolved
"import distutils.sysconfig as sysconfig; print(sysconfig.get_python_inc())"
)
else()
set(PYTHON_INCLUDE_DIR_CMD
nim65s marked this conversation as resolved.
Show resolved Hide resolved
"import sysconfig; print(sysconfig.get_path('include'))")
endif()
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sysconfig; print(sysconfig.get_path('include'))"
COMMAND "${PYTHON_EXECUTABLE}" "-c" ${PYTHON_INCLUDE_DIR_CMD}
nim65s marked this conversation as resolved.
Show resolved Hide resolved
OUTPUT_VARIABLE PYTHON_INCLUDE_DIR
ERROR_QUIET)
string(STRIP "${PYTHON_INCLUDE_DIR}" PYTHON_INCLUDE_DIR)
file(TO_CMAKE_PATH "${PYTHON_INCLUDE_DIR}" PYTHON_INCLUDE_DIR)
endif()

if(NOT "${_PYTHON_VERSION_RESULT_VARIABLE}" STREQUAL "0")
message(FATAL_ERROR "${PYTHON_EXECUTABLE} --version did not succeed.")
endif(NOT "${_PYTHON_VERSION_RESULT_VARIABLE}" STREQUAL "0")
string(REGEX REPLACE "Python " "" _PYTHON_VERSION
${_PYTHON_VERSION_OUTPUT})
string(REGEX REPLACE "\\." ";" _PYTHON_VERSION ${_PYTHON_VERSION})
list(GET _PYTHON_VERSION 0 _PYTHON_VERSION_MAJOR)

# Hint for finding the right Python version
set(Python_EXECUTABLE ${PYTHON_EXECUTABLE})
set(Python${_PYTHON_VERSION_MAJOR}_EXECUTABLE ${PYTHON_EXECUTABLE})
Expand Down Expand Up @@ -215,9 +222,16 @@ macro(FINDPYTHON)
# Retrieve the corresponding value of PYTHON_INCLUDE_DIR if it is not
# defined
if(NOT DEFINED PYTHON_INCLUDE_DIR)
if(_PYTHON_VERSION_MAJOR EQUAL "2")
set(PYTHON_INCLUDE_DIR_CMD
nim65s marked this conversation as resolved.
Show resolved Hide resolved
"import distutils.sysconfig as sysconfig; print(sysconfig.get_python_inc())"
)
else()
set(PYTHON_INCLUDE_DIR_CMD
nim65s marked this conversation as resolved.
Show resolved Hide resolved
"import sysconfig; print(sysconfig.get_path('include'))")
endif()
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import sysconfig; print(sysconfig.get_path('include'))"
COMMAND "${PYTHON_EXECUTABLE}" "-c" ${PYTHON_INCLUDE_DIR_CMD}
nim65s marked this conversation as resolved.
Show resolved Hide resolved
OUTPUT_VARIABLE PYTHON_INCLUDE_DIR
ERROR_QUIET)
string(STRIP "${PYTHON_INCLUDE_DIR}" PYTHON_INCLUDE_DIR)
Expand Down Expand Up @@ -269,9 +283,15 @@ macro(FINDPYTHON)
"import sys, os; print(os.sep.join(['lib', 'python' + '.'.join(sys.version.split('.')[:2]), 'site-packages']))"
)
else(PYTHON_STANDARD_LAYOUT)
set(PYTHON_SITELIB_CMD
"import sysconfig; from pathlib import Path; print(Path(sysconfig.get_path('purelib')).relative_to(sysconfig.get_path('data')))"
)
if(_PYTHON_VERSION_MAJOR EQUAL "2")
set(PYTHON_SITELIB_CMD
nim65s marked this conversation as resolved.
Show resolved Hide resolved
"from distutils import sysconfig; print(sysconfig.get_python_lib(prefix='', plat_specific=False))"
)
else()
set(PYTHON_SITELIB_CMD
nim65s marked this conversation as resolved.
Show resolved Hide resolved
"import sysconfig; from pathlib import Path; print(Path(sysconfig.get_path('purelib')).relative_to(sysconfig.get_path('data')))"
)
endif()
endif(PYTHON_STANDARD_LAYOUT)

execute_process(
Expand Down