diff --git a/cmake/python.cmake b/cmake/python.cmake index 203ee32044..caf1eb1d6c 100644 --- a/cmake/python.cmake +++ b/cmake/python.cmake @@ -2,8 +2,8 @@ if(NOT BUILD_PYTHON) return() endif() -# Use latest UseSWIG module -cmake_minimum_required(VERSION 3.14) +# Use latest UseSWIG module (3.14) and Python3 module (3.18) +cmake_minimum_required(VERSION 3.18) if(NOT TARGET ortools::ortools) message(FATAL_ERROR "Python: missing ortools TARGET") @@ -22,18 +22,15 @@ if(UNIX AND NOT APPLE) list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64") endif() -# Find Python -find_package(Python REQUIRED COMPONENTS Interpreter Development) - -if(Python_VERSION VERSION_GREATER_EQUAL 3) - list(APPEND CMAKE_SWIG_FLAGS "-py3" "-DPY3") -endif() +# Find Python 3 +find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module) +list(APPEND CMAKE_SWIG_FLAGS "-py3" "-DPY3") # Find if python module MODULE_NAME is available, -# if not install it to the Python user install directory. +# if not install it to the Python 3 user install directory. function(search_python_module MODULE_NAME) execute_process( - COMMAND ${Python_EXECUTABLE} -c "import ${MODULE_NAME}; print(${MODULE_NAME}.__version__)" + COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}; print(${MODULE_NAME}.__version__)" RESULT_VARIABLE _RESULT OUTPUT_VARIABLE MODULE_VERSION ERROR_QUIET @@ -44,7 +41,7 @@ function(search_python_module MODULE_NAME) else() message(WARNING "Can't find python module \"${MODULE_NAME}\", install it using pip...") execute_process( - COMMAND ${Python_EXECUTABLE} -m pip install --user ${MODULE_NAME} + COMMAND ${Python3_EXECUTABLE} -m pip install --user ${MODULE_NAME} OUTPUT_STRIP_TRAILING_WHITESPACE ) endif() @@ -53,7 +50,7 @@ endfunction() # Find if python builtin module MODULE_NAME is available. function(search_python_internal_module MODULE_NAME) execute_process( - COMMAND ${Python_EXECUTABLE} -c "import ${MODULE_NAME}" + COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}" RESULT_VARIABLE _RESULT OUTPUT_VARIABLE MODULE_VERSION ERROR_QUIET @@ -92,7 +89,7 @@ foreach(PROTO_FILE IN LISTS proto_py_files) "--mypy_out=${PROJECT_BINARY_DIR}/python" ${PROTO_FILE} DEPENDS ${PROTO_FILE} ${PROTOC_PRG} - COMMENT "Generate Python protocol buffer for ${PROTO_FILE}" + COMMENT "Generate Python 3 protocol buffer for ${PROTO_FILE}" VERBATIM) list(APPEND PROTO_PYS ${PROTO_PY}) endforeach() @@ -172,8 +169,8 @@ add_custom_target(python_package ALL COMMAND ${CMAKE_COMMAND} -E copy $ ${PROJECT_NAME}/sat COMMAND ${CMAKE_COMMAND} -E copy $ ${PROJECT_NAME}/data COMMAND ${CMAKE_COMMAND} -E copy $ ${PROJECT_NAME}/util - #COMMAND ${Python_EXECUTABLE} setup.py bdist_egg bdist_wheel - COMMAND ${Python_EXECUTABLE} setup.py bdist_wheel + #COMMAND ${Python3_EXECUTABLE} setup.py bdist_egg bdist_wheel + COMMAND ${Python3_EXECUTABLE} setup.py bdist_wheel BYPRODUCTS python/${PROJECT_NAME} python/build @@ -196,25 +193,25 @@ if(BUILD_TESTING) search_python_module(absl-py) search_python_internal_module(venv) # Testing using a vitual environment - set(VENV_EXECUTABLE ${Python_EXECUTABLE} -m venv) + set(VENV_EXECUTABLE ${Python3_EXECUTABLE} -m venv) set(VENV_DIR ${PROJECT_BINARY_DIR}/python/venv) if(WIN32) - set(VENV_Python_EXECUTABLE "${VENV_DIR}\\Scripts\\python.exe") + set(VENV_Python3_EXECUTABLE "${VENV_DIR}\\Scripts\\python.exe") else() - set(VENV_Python_EXECUTABLE ${VENV_DIR}/bin/python) + set(VENV_Python3_EXECUTABLE ${VENV_DIR}/bin/python) endif() # make a virtualenv to install our python package in it add_custom_command(TARGET python_package POST_BUILD COMMAND ${VENV_EXECUTABLE} ${VENV_DIR} # Must not call it in a folder containing the setup.py otherwise pip call it # (i.e. "python setup.py bdist") while we want to consume the wheel package - COMMAND ${VENV_Python_EXECUTABLE} -m pip install --find-links=${CMAKE_CURRENT_BINARY_DIR}/python/dist ${PROJECT_NAME} + COMMAND ${VENV_Python3_EXECUTABLE} -m pip install --find-links=${CMAKE_CURRENT_BINARY_DIR}/python/dist ${PROJECT_NAME} COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/test.py.in ${VENV_DIR}/test.py BYPRODUCTS ${VENV_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} VERBATIM) # run the tests within the virtualenv add_test(NAME pytest_venv - COMMAND ${VENV_Python_EXECUTABLE} ${VENV_DIR}/test.py) + COMMAND ${VENV_Python3_EXECUTABLE} ${VENV_DIR}/test.py) endif() # add_python_sample() @@ -232,7 +229,7 @@ function(add_python_sample FILE_NAME) if(BUILD_TESTING) add_test( NAME python_${COMPONENT_NAME}_${SAMPLE_NAME} - COMMAND ${VENV_Python_EXECUTABLE} ${FILE_NAME} + COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME} WORKING_DIRECTORY ${VENV_DIR}) endif() endfunction() @@ -251,7 +248,7 @@ function(add_python_example FILE_NAME) if(BUILD_TESTING) add_test( NAME python_${COMPONENT_NAME}_${EXAMPLE_NAME} - COMMAND ${VENV_Python_EXECUTABLE} ${FILE_NAME} + COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME} WORKING_DIRECTORY ${VENV_DIR}) endif() endfunction() @@ -270,7 +267,7 @@ function(add_python_test FILE_NAME) if(BUILD_TESTING) add_test( NAME python_${COMPONENT_NAME}_${TEST_NAME} - COMMAND ${VENV_Python_EXECUTABLE} ${FILE_NAME} + COMMAND ${VENV_Python3_EXECUTABLE} ${FILE_NAME} WORKING_DIRECTORY ${VENV_DIR}) endif() endfunction() diff --git a/ortools/algorithms/python/CMakeLists.txt b/ortools/algorithms/python/CMakeLists.txt index e050fae001..b41a64c50c 100644 --- a/ortools/algorithms/python/CMakeLists.txt +++ b/ortools/algorithms/python/CMakeLists.txt @@ -8,11 +8,9 @@ swig_add_library(pywrapknapsack_solver OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/algorithms SOURCES knapsack_solver.i) -target_include_directories(pywrapknapsack_solver PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(pywrapknapsack_solver PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET pywrapknapsack_solver PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(pywrapknapsack_solver PUBLIC "PY3") -endif() +target_compile_definitions(pywrapknapsack_solver PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -32,5 +30,5 @@ target_link_libraries(pywrapknapsack_solver PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(pywrapknapsack_solver PRIVATE ${Python_LIBRARIES}) + target_link_libraries(pywrapknapsack_solver PRIVATE ${Python3_LIBRARIES}) endif() diff --git a/ortools/constraint_solver/python/CMakeLists.txt b/ortools/constraint_solver/python/CMakeLists.txt index f6fa345837..262052b4b7 100644 --- a/ortools/constraint_solver/python/CMakeLists.txt +++ b/ortools/constraint_solver/python/CMakeLists.txt @@ -9,11 +9,9 @@ swig_add_library(pywrapcp OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/constraint_solver SOURCES routing.i) -target_include_directories(pywrapcp PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(pywrapcp PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET pywrapcp PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(pywrapcp PUBLIC "PY3") -endif() +target_compile_definitions(pywrapcp PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -33,5 +31,5 @@ target_link_libraries(pywrapcp PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(pywrapcp PRIVATE ${Python_LIBRARIES}) + target_link_libraries(pywrapcp PRIVATE ${Python3_LIBRARIES}) endif() diff --git a/ortools/data/python/CMakeLists.txt b/ortools/data/python/CMakeLists.txt index 29037ee4d6..d603d95ac2 100644 --- a/ortools/data/python/CMakeLists.txt +++ b/ortools/data/python/CMakeLists.txt @@ -8,11 +8,9 @@ swig_add_library(pywraprcpsp OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/data SOURCES rcpsp.i) -target_include_directories(pywraprcpsp PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(pywraprcpsp PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET pywraprcpsp PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(pywraprcpsp PUBLIC "PY3") -endif() +target_compile_definitions(pywraprcpsp PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -32,5 +30,5 @@ target_link_libraries(pywraprcpsp PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(pywraprcpsp PRIVATE ${Python_LIBRARIES}) + target_link_libraries(pywraprcpsp PRIVATE ${Python3_LIBRARIES}) endif() diff --git a/ortools/graph/python/CMakeLists.txt b/ortools/graph/python/CMakeLists.txt index af07b1ea32..a623384640 100644 --- a/ortools/graph/python/CMakeLists.txt +++ b/ortools/graph/python/CMakeLists.txt @@ -8,11 +8,9 @@ swig_add_library(pywrapgraph OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/graph SOURCES graph.i) -target_include_directories(pywrapgraph PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(pywrapgraph PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET pywrapgraph PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(pywrapgraph PUBLIC "PY3") -endif() +target_compile_definitions(pywrapgraph PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -32,5 +30,5 @@ target_link_libraries(pywrapgraph PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(pywrapgraph PRIVATE ${Python_LIBRARIES}) + target_link_libraries(pywrapgraph PRIVATE ${Python3_LIBRARIES}) endif() diff --git a/ortools/init/python/CMakeLists.txt b/ortools/init/python/CMakeLists.txt index 993d64f7c9..7b9a0c4243 100644 --- a/ortools/init/python/CMakeLists.txt +++ b/ortools/init/python/CMakeLists.txt @@ -8,11 +8,9 @@ swig_add_library(pywrapinit OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/init SOURCES init.i) -target_include_directories(pywrapinit PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(pywrapinit PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET pywrapinit PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(pywrapinit PUBLIC "PY3") -endif() +target_compile_definitions(pywrapinit PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -32,5 +30,5 @@ target_link_libraries(pywrapinit PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(pywrapinit PRIVATE ${Python_LIBRARIES}) + target_link_libraries(pywrapinit PRIVATE ${Python3_LIBRARIES}) endif() diff --git a/ortools/linear_solver/python/CMakeLists.txt b/ortools/linear_solver/python/CMakeLists.txt index c24762a525..dade8da188 100644 --- a/ortools/linear_solver/python/CMakeLists.txt +++ b/ortools/linear_solver/python/CMakeLists.txt @@ -8,11 +8,9 @@ swig_add_library(pywraplp OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/linear_solver SOURCES linear_solver.i) -target_include_directories(pywraplp PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(pywraplp PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET pywraplp PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(pywraplp PUBLIC "PY3") -endif() +target_compile_definitions(pywraplp PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -32,5 +30,5 @@ target_link_libraries(pywraplp PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(pywraplp PRIVATE ${Python_LIBRARIES}) + target_link_libraries(pywraplp PRIVATE ${Python3_LIBRARIES}) endif() diff --git a/ortools/sat/python/CMakeLists.txt b/ortools/sat/python/CMakeLists.txt index 1a24324cfd..c76518578e 100644 --- a/ortools/sat/python/CMakeLists.txt +++ b/ortools/sat/python/CMakeLists.txt @@ -8,11 +8,9 @@ swig_add_library(pywrapsat OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/sat SOURCES sat.i) -target_include_directories(pywrapsat PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(pywrapsat PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET pywrapsat PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(pywrapsat PUBLIC "PY3") -endif() +target_compile_definitions(pywrapsat PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -32,5 +30,5 @@ target_link_libraries(pywrapsat PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(pywrapsat PRIVATE ${Python_LIBRARIES}) + target_link_libraries(pywrapsat PRIVATE ${Python3_LIBRARIES}) endif() diff --git a/ortools/util/python/CMakeLists.txt b/ortools/util/python/CMakeLists.txt index e3838924aa..2cfea9bb9f 100644 --- a/ortools/util/python/CMakeLists.txt +++ b/ortools/util/python/CMakeLists.txt @@ -8,11 +8,9 @@ swig_add_library(sorted_interval_list OUTPUT_DIR ${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}/util SOURCES sorted_interval_list.i) -target_include_directories(sorted_interval_list PRIVATE ${Python_INCLUDE_DIRS}) +target_include_directories(sorted_interval_list PRIVATE ${Python3_INCLUDE_DIRS}) set_property(TARGET sorted_interval_list PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) -if(Python_VERSION VERSION_GREATER_EQUAL 3) - target_compile_definitions(sorted_interval_list PUBLIC "PY3") -endif() +target_compile_definitions(sorted_interval_list PUBLIC "PY3") # note: macOS is APPLE and also UNIX ! if(APPLE) @@ -32,6 +30,6 @@ target_link_libraries(sorted_interval_list PRIVATE ortools::ortools) # i.e. we can't use: $<$:${PYTHON_LIBRARIES}> # see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries if(MSVC) - target_link_libraries(sorted_interval_list PRIVATE ${Python_LIBRARIES}) + target_link_libraries(sorted_interval_list PRIVATE ${Python3_LIBRARIES}) endif()