From 78aecc266be62dc59cda92075643e39812d6684c Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Mon, 30 Oct 2023 16:45:24 +0100 Subject: [PATCH] python: bring python 2 support back It was removed in #624, but ROS-CI still needs it. --- python.cmake | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/python.cmake b/python.cmake index cc043ddf7..66fe299fa 100644 --- a/python.cmake +++ b/python.cmake @@ -144,9 +144,16 @@ macro(FINDPYTHON) # 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 + "import distutils.sysconfig as sysconfig; print(sysconfig.get_python_inc())" + ) + else() + set(PYTHON_INCLUDE_DIR_CMD + "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} OUTPUT_VARIABLE PYTHON_INCLUDE_DIR ERROR_QUIET) string(STRIP "${PYTHON_INCLUDE_DIR}" PYTHON_INCLUDE_DIR) @@ -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 + "import distutils.sysconfig as sysconfig; print(sysconfig.get_python_inc())" + ) + else() + set(PYTHON_INCLUDE_DIR_CMD + "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} OUTPUT_VARIABLE PYTHON_INCLUDE_DIR ERROR_QUIET) string(STRIP "${PYTHON_INCLUDE_DIR}" PYTHON_INCLUDE_DIR) @@ -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 + "from distutils import sysconfig; print(sysconfig.get_python_lib(prefix='', plat_specific=False))" + ) + else() + set(PYTHON_SITELIB_CMD + "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(