Skip to content
This repository was archived by the owner on Apr 6, 2026. It is now read-only.

Commit 437d0f5

Browse files
authored
misc(cmake): rely on the more robust Python3 module to handle Python detection (#238)
This PR would make use of the Python3 cmake module in-place of Python as we are not targeting Python2
1 parent 9532ae8 commit 437d0f5

7 files changed

Lines changed: 23 additions & 23 deletions

File tree

build2cmake/src/templates/cuda/preamble.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ set(HIP_SUPPORTED_ARCHS "gfx906;gfx908;gfx90a;gfx940;gfx941;gfx942;gfx1030;gfx11
1515

1616
include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
1717

18-
if(DEFINED Python_EXECUTABLE)
18+
if(DEFINED Python3_EXECUTABLE)
1919
# Allow passing through the interpreter (e.g. from setup.py).
20-
find_package(Python COMPONENTS Development Development.SABIModule Interpreter)
21-
if (NOT Python_FOUND)
20+
find_package(Python3 COMPONENTS Development Development.SABIModule Interpreter)
21+
if (NOT Python3_FOUND)
2222
message(FATAL_ERROR "Unable to find python matching: ${EXECUTABLE}.")
2323
endif()
2424
else()
25-
find_package(Python REQUIRED COMPONENTS Development Development.SABIModule Interpreter)
25+
find_package(Python3 REQUIRED COMPONENTS Development Development.SABIModule Interpreter)
2626
endif()
2727

2828
append_cmake_prefix_path("torch" "torch.utils.cmake_prefix_path")

build2cmake/src/templates/cuda/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def build_extension(self, ext: CMakeExtension) -> None:
3939

4040
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
4141

42-
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
42+
# Set Python3_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
4343
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
4444
# from Python.
4545
cmake_args = [
4646
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
47-
f"-DPython_EXECUTABLE={sys.executable}",
47+
f"-DPython3_EXECUTABLE={sys.executable}",
4848
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
4949
]
5050
build_args = []

build2cmake/src/templates/metal/preamble.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ message(STATUS "FetchContent base directory: ${FETCHCONTENT_BASE_DIR}")
1111

1212
include(${CMAKE_CURRENT_LIST_DIR}/cmake/utils.cmake)
1313

14-
if(DEFINED Python_EXECUTABLE)
14+
if(DEFINED Python3_EXECUTABLE)
1515
# Allow passing through the interpreter (e.g. from setup.py).
16-
find_package(Python COMPONENTS Development Development.SABIModule Interpreter)
17-
if (NOT Python_FOUND)
16+
find_package(Python3 COMPONENTS Development Development.SABIModule Interpreter)
17+
if (NOT Python3_FOUND)
1818
message(FATAL_ERROR "Unable to find python matching: ${EXECUTABLE}.")
1919
endif()
2020
else()
21-
find_package(Python REQUIRED COMPONENTS Development Development.SABIModule Interpreter)
21+
find_package(Python3 REQUIRED COMPONENTS Development Development.SABIModule Interpreter)
2222
endif()
2323

2424
append_cmake_prefix_path("torch" "torch.utils.cmake_prefix_path")

build2cmake/src/templates/metal/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def build_extension(self, ext: CMakeExtension) -> None:
3939

4040
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
4141

42-
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
42+
# Set Python3_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
4343
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
4444
# from Python.
4545
cmake_args = [
4646
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
47-
f"-DPython_EXECUTABLE={sys.executable}",
47+
f"-DPython3_EXECUTABLE={sys.executable}",
4848
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
4949
]
5050
build_args = []

build2cmake/src/templates/utils.cmake

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#
88
macro (find_python_from_executable EXECUTABLE SUPPORTED_VERSIONS)
99
file(REAL_PATH ${EXECUTABLE} EXECUTABLE)
10-
set(Python_EXECUTABLE ${EXECUTABLE})
11-
find_package(Python COMPONENTS Interpreter Development.Module Development.SABIModule)
12-
if (NOT Python_FOUND)
10+
set(Python3_EXECUTABLE ${EXECUTABLE})
11+
find_package(Python3 COMPONENTS Interpreter Development.Module Development.SABIModule)
12+
if (NOT Python3_FOUND)
1313
message(FATAL_ERROR "Unable to find python matching: ${EXECUTABLE}.")
1414
endif()
15-
set(_VER "${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
15+
set(_VER "${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}")
1616
set(_SUPPORTED_VERSIONS_LIST ${SUPPORTED_VERSIONS} ${ARGN})
1717
if (NOT _VER IN_LIST _SUPPORTED_VERSIONS_LIST)
1818
message(FATAL_ERROR
@@ -30,7 +30,7 @@ endmacro()
3030
function (run_python OUT EXPR ERR_MSG)
3131
execute_process(
3232
COMMAND
33-
"${Python_EXECUTABLE}" "-c" "${EXPR}"
33+
"${Python3_EXECUTABLE}" "-c" "${EXPR}"
3434
OUTPUT_VARIABLE PYTHON_OUT
3535
RESULT_VARIABLE PYTHON_ERROR_CODE
3636
ERROR_VARIABLE PYTHON_STDERR
@@ -89,7 +89,7 @@ function (hipify_sources_target OUT_SRCS NAME ORIG_SRCS)
8989

9090
add_custom_target(
9191
hipify${NAME}
92-
COMMAND "${Python_EXECUTABLE}" ${CMAKE_SOURCE_DIR}/cmake/hipify.py -p ${CMAKE_SOURCE_DIR} -o ${CMAKE_CURRENT_BINARY_DIR} ${SRCS}
92+
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_SOURCE_DIR}/cmake/hipify.py -p ${CMAKE_SOURCE_DIR} -o ${CMAKE_CURRENT_BINARY_DIR} ${SRCS}
9393
DEPENDS ${CMAKE_SOURCE_DIR}/cmake/hipify.py ${SRCS}
9494
BYPRODUCTS ${HIP_SRCS}
9595
COMMENT "Running hipify on ${NAME} extension source files.")
@@ -505,9 +505,9 @@ function (define_gpu_extension_target GPU_MOD_NAME)
505505
endif()
506506

507507
if (GPU_USE_SABI)
508-
Python_add_library(${GPU_MOD_NAME} MODULE USE_SABI ${GPU_USE_SABI} ${GPU_WITH_SOABI} "${GPU_SOURCES}")
508+
Python3_add_library(${GPU_MOD_NAME} MODULE USE_SABI ${GPU_USE_SABI} ${GPU_WITH_SOABI} "${GPU_SOURCES}")
509509
else()
510-
Python_add_library(${GPU_MOD_NAME} MODULE ${GPU_WITH_SOABI} "${GPU_SOURCES}")
510+
Python3_add_library(${GPU_MOD_NAME} MODULE ${GPU_WITH_SOABI} "${GPU_SOURCES}")
511511
endif()
512512

513513
if (GPU_LANGUAGE STREQUAL "HIP")

build2cmake/src/templates/xpu/preamble.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ message(STATUS "FetchContent base directory: ${FETCHCONTENT_BASE_DIR}")
2020
include("cmake/utils.cmake")
2121

2222
# Find Python with all necessary components for building extensions
23-
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module Development.SABIModule)
23+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module Development.SABIModule)
2424

2525
append_cmake_prefix_path("torch" "torch.utils.cmake_prefix_path")
2626

build2cmake/src/templates/xpu/setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def build_extension(self, ext: CMakeExtension) -> None:
3939

4040
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
4141

42-
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
42+
# Set Python3_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
4343
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
4444
# from Python.
4545
cmake_args = [
4646
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
47-
f"-DPython_EXECUTABLE={sys.executable}",
47+
f"-DPython3_EXECUTABLE={sys.executable}",
4848
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
4949
]
5050
build_args = []

0 commit comments

Comments
 (0)